gpt4 book ai didi

mysql - 外键格式不正确 :MySQL

转载 作者:行者123 更新时间:2023-11-29 01:35:12 25 4
gpt4 key购买 nike

我的脚本

create table student
(
stud_id int(9) unsigned not null,
stud_name varchar(30),
stud_phone int(10),
stud_dob date,
stud_city varchar(15),
stud_address varchar(50),
stud_postcode int(5),
primary key(stud_id)
);

create table subject
(
sub_code varchar(9) not null,
sub_title varchar(30),
primary key(sub_code)
);

create table grade
(
stud_id int(9) unsigned not null,
sub_code varchar(9) not null,
sem int(1) not null,
year int(4) not null,
comment varchar(50),
primary key(stud_id,sub_code,sem,year),
foreign key(stud_id) references student,
foreign key(sub_code) references subject
);

我不确定为什么它没有引用,我是 SQL 的新手。列数据类型相同,排序规则均为 latin1,签名定义相同,到底哪里错了?感谢您的帮助。

最佳答案

您还需要指定引用其他表中的哪一列,因此外键声明需要例如foreign key(stud_id) references student(stud_id)

例如:

create table subject
(
sub_code varchar(9) not null,
sub_title varchar(30),
primary key(sub_code)
);

create table grade
(
stud_id int(9) unsigned not null,
sub_code varchar(9) not null,
sem int(1) not null,
year int(4) not null,
comment varchar(50),
primary key(stud_id,sub_code,sem,year),
foreign key(sub_code) references subject(sub_code)
);

关于mysql - 外键格式不正确 :MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52665368/

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