MySQL error: Missing index for constraint

后端 未结 2 820
孤街浪徒
孤街浪徒 2021-01-13 12:35

I am creating 2 tables in my database:

DROP TABLE IF EXISTS `med_pharmacy`;
CREATE TABLE IF NOT EXISTS `med_pharmacy` (
  `med_pharmacy_id` int(11) NOT NULL          


        
相关标签:
2条回答
  • 2021-01-13 12:53

    The column referenced in a foreign key must be indexed. You need to add an index on medication.med_id. In fact, this should probably be the primary key of the table.

    ALTER TABLE medication ADD PRIMARY KEY (med_id);
    
    0 讨论(0)
  • 2021-01-13 12:55

    if you are giving

    ADD CONSTRAINT `fk_med_pharmacy_medication1` 
    FOREIGN KEY (`med_id`) 
    

    A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table.

    so med_id should have primary key in medication or reference the columns of a UNIQUE constraint

    0 讨论(0)
提交回复
热议问题