2 qq 22447717 qq_22447717 于 2016.03.09 19:13 提问

数据库foreign key error 1215
 create table department
(dept_name varchar(20) not null primary key, 
building varchar(20),
budget numeric(12,2)); 


create table instructor
( ID varchar(5),
  name varchar(20),
  dept_name varchar(20),
  salary numeric(12,1)
);


create table course
( course_id varchar(8),
  title     varchar(50),
  dept_name varchar(20) references department,
  credits   numeric(2,0),
  primary key (course_id)
  #foreign key constraints can be here too :  
  #foreign key (dept_name) references department
);


/*alter table course 
  change column course_id 
    course_id varchar(8) unique;
*/

create table section
( course_id     varchar(8),
  sec_id        varchar(8),
  semster       varchar(6),
  year          numeric(4,0),
  building      varchar(15),
  room_number   varchar(7),
  time_sloth_id varchar(4),
  primary key(course_id,semster,year),
  foreign key (course_id) references course
);

create table teach
( ID          varchar(5),
  course_id   varchar(8),
  semster     varchar(6),
  year        numeric(4,0),
  primary key (ID,course_id,semster, year),
  foreign key (ID) references instructor,
  foreign key (course_id, semster, year) references section
);

这段代码运行在MySql上总是在create table section的时候报error 1215,不能添加外键......求问为什么啊......

1个回答

cxsmarkchan
cxsmarkchan   2016.03.09 19:40
已采纳

试试foreign key (course_id) references course(course_id)

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!