gpt4 book ai didi

Mysql工作台错误1215 : cannot add foreign key to constraint

转载 作者:行者123 更新时间:2023-11-29 20:12:07 26 4
gpt4 key购买 nike

所以我尝试向表添加外键,但不断收到此错误:

错误代码:1215。无法添加外键约束

代码如下

create table Students

(
ID char(10) primary key not null,
StudentsName varchar(30),
Address varchar(40),
StudentsTelephone varchar(20),
constraint M1 check(len(NIM)=10),
constraint M2 check(NIM like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
)

create table Lecturers

(
CodeLecturer char(5) primary key not null,
LecturersName varchar(30),
Salary numeric,
Address varchar(40),
LecturerTelephone varchar(20),
constraint D1 check(len(CodeLecturer)=5),
constraint D2 check(CodeLecturer like 'D[0-9][0-9][0-9][0-9]')

)

这是外键代码

create table TrHeaderKampus

(
TransactionCOde char(5) primary key not null,
ID char(10) not null,
CodeLecturer char(5) not null,
DateandTime datetime,
constraint H1 check(len(TransactioniCode)=5),
constraint H2 check(TransactionCode like 'TR[0-9][0-9][0-9]'),
foreign key(ID)REFERENCES Students ON UPDATE CASCADE ON DELETE CASCADE,
foreign key(CodeLecturer)REFERENCES Lecturers ON UPDATE CASCADE ON DELETE CASCADE
)

最佳答案

作为 MySQL 文档 using foreign keys说明:您不仅需要提供表,还需要提供外键引用的字段。

[CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name, ...)
REFERENCES tbl_name (index_col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]

将字段名称添加到它们引用的外键约束中。

关于Mysql工作台错误1215 : cannot add foreign key to constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40018724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com