gpt4 book ai didi

mysql - 学习<<数据库系统概念>>6ed出现ERROR 1005 (HY000) : Can't create table,

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:20 24 4
gpt4 key购买 nike

create table classroom
(building varchar(15),
room_number varchar(7),
capacity numeric(4,0),
primary key (building, room_number)
);

create table department
(dept_name varchar(20),
building varchar(15),
budget numeric(12,2) check (budget > 0),
primary key (dept_name)
);

前两个create语句是对的。但是在

create table course
(course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0) check (credits > 0),
primary key (course_id),
foreign key (dept_name) references department
on delete set null
);

出现ERROR 1005 (HY000): Can't create table ...我不知道如何解决它。显然,它的外键数据类型是一致的。

最佳答案

要获取有关错误的详细信息,请运行 SHOW ENGINE INNODB STATUS\G 并查找 LATEST FOREIGN KEY ERROR。它应该是这样的:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
2013-12-04 09:50:19 139428000 Error in foreign key constraint of table test/course:
foreign key (dept_name) references department
on delete set null
):
Syntax error close to:

on delete set null
)

解决方法是指定父表的列名,像这样:

create table course
(course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0) check (credits > 0),
primary key (course_id),
foreign key (dept_name) references department (dept_name)
on delete set null
);

附言您应该知道 CREATE TABLE 语句中的 CHECK 约束实际上没有做任何事情。如 the manual 中所述:

The CHECK clause is parsed but ignored by all storage engines.

关于mysql - 学习<<数据库系统概念>>6ed出现ERROR 1005 (HY000) : Can't create table,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20377979/

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