CREATE TABLE IF NOT EXISTS scores(sid int NOT NULL PRIMARY KEY ,course_name varchar(64) NOT NULL,
score int,
CONSTRAINT FOR_s_sid FOREIGN KEY (sid) REFERENCES students(sid),
CONSTRAINT for_s_course FOREIGN KEY (course_name) REFERENCES course(course_name)
);
报错:1 Failed to add the foreign key constraint. Missing index for constraint 'for_s_course' in the referenced table 'course' SQL.sql 486 12
解决办法:
添加指定表的索引属性
例如:
CREATE TABLE IF NOT EXISTS scores(sid int NOT NULL PRIMARY KEY ,course_name varchar(64) NOT NULL,
score int,
CONSTRAINT FOR_s_sid FOREIGN KEY (sid) REFERENCES students(sid),
CONSTRAINT for_s_course FOREIGN KEY (course_name) REFERENCES course(course_name)
);
ALTER TABLE course ADD INDEX in_coursename(course_name);
/*为某张表添加外键时,那么关联的表的字段必须有索引属性*/