错误代码1215 MySQL
下面是我的模式,除了评估表,我得到一个错误1215,所有的东西都运行。不能添加外键约束。 提前致谢。 我还引用了其他表的主键作为评估表中的外键。
create database if not exists tcsdb;
use tcsdb;
create table if not exists Person (
PersonID int not null auto_increment,
ID varchar(10) not null unique,
FirstName varchar(45) not null,
LastName varchar(45) not null,
UserName varchar(45) not null unique,
Password varchar(45) not null,
Email varchar(45) not null,
ContactNo varchar(45) not null,
primary key(PersonID)
);
create table if not exists Role (
RoleID int not null auto_increment,
RoleTitle varchar(45),
RoleDescription varchar(45),
primary key(RoleID)
);
create table if not exists Semester (
SemesterID int not null auto_increment,
SemesterPeriod varchar(45),
SemesterYear date,
primary key(SemesterID)
);
create table if not exists Unit (
UnitID int not null auto_increment,
SemesterID int,
UnitCode varchar(10) not null unique,
UnitName varchar(45) not null unique,
UnitDescription varchar(45) not null unique,
foreign key(SemesterID) references Semester(SemesterID) on update cascade on delete restrict,
primary key(UnitID, SemesterID)
);
create table if not exists Team (
TeamID int not null auto_increment,
TeamCode varchar(45) not null unique,
TeamName varchar(45) not null,
primary key(TeamID)
);
create table if not exists Project (
ProjectID int not null auto_increment,
ProjectCode varchar(45) not null unique,
ProjectDescription varchar(45) not null,
primary key(ProjectID)
);
create table if not exists AssessmentType (
AssessmentTypeID int not null auto_increment,
AssessmentType varchar(45) not null unique,
primary key(AssessmentTypeID)
);
create table if not exists Assessment (
AssessmentID int not null auto_increment,
PersonID int,
UnitID int,
SemesterID int,
TeamID int ,
ProjectID int ,
AssessmentComments varchar(45) not null,
AssessmentDueDate date,
AssessmentDateSubmitted date,
AssessmentTypeID int,
foreign key(PersonID) references PersonUnit(PersonID) on update cascade on delete restrict,
foreign key(UnitID) references PersonUnit(UnitID) on update cascade on delete restrict,
foreign key(SemesterID) references PersonUnit(SemesterID) on update cascade on delete restrict,
foreign key(TeamID) references Team(TeamID) on update cascade on delete restrict,
foreign key(ProjectID) references Project(ProjectID)on update cascade on delete restrict,
foreign key(AssessmentTypeID) references AssessmentType(AssessmentTypeID) on update cascade on delete restrict,
primary key(AssessmentID, PersonID, UnitID, SemesterID, TeamID, ProjectID, AssessmentTypeID)
);
create table if not exists PersonRole (
PersonID int,
RoleID int,
primary key(PersonID, RoleID),
foreign key(PersonID) references Person(PersonID) on update cascade on delete restrict,
foreign key(RoleID) references Role(RoleID) on update cascade on delete restrict
);
create table if not exists PersonUnit (
PersonID int,
UnitID int,
SemesterID int,
foreign key(PersonID) references Person(PersonID) on update cascade on delete restrict,
foreign key(UnitID, SemesterID) references Unit(UnitID, SemesterID) on update cascade on delete restrict,
primary key(PersonID, UnitID, SemesterID)
);
下面是我得到的SemesterID,TeamID,ProjectID和AssessmentTypeID的错误信息:
无法在引用表中找到引用列作为第一列显示的索引,或者表中的列类型和引用的表格与约束不匹配。 请注意,ENUM和SET的内部存储类型在使用> = InnoDB-4.1.12创建的表中进行了更改,旧表中的这些列无法在新表中被这些列引用。
任何帮助表示赞赏。
当你使用组合主键(即多列键)对外键进行外键操作时,也应该使用外键组合。
create table if not exists Assessment (
AssessmentID int not null auto_increment,
PersonID int,
UnitID int,
SemesterID int,
TeamID int ,
ProjectID int ,
AssessmentComments varchar(45) not null,
AssessmentDueDate date,
AssessmentDateSubmitted date,
AssessmentTypeID int,
foreign key(PersonID,UnitID,SemesterID) references PersonUnit(PersonID,UnitID,SemesterID) on update cascade on delete restrict,
restrict,
foreign key(TeamID) references Team(TeamID) on update cascade on delete restrict,
foreign key(ProjectID) references Project(ProjectID)on update cascade on delete restrict,
foreign key(AssessmentTypeID) references AssessmentType(AssessmentTypeID) on update cascade on delete restrict,
primary key(AssessmentID, PersonID, UnitID, SemesterID, TeamID, ProjectID, AssessmentTypeID)
);
我在这里回答了一个非常类似的问题:SQL无法创建表(errno:150)
链接地址: http://www.djcxy.com/p/86277.html