[英]MySql Error Code: 1005 Can't create table errno: 150
MySql workbench reports that my syntax is error free. MySql工作台报告我的语法没有错误。 I can't figure out what's wrong with my database.
我无法弄清楚我的数据库有什么问题。 Any ideas?
有任何想法吗?
CREATE TABLE `users` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`login` VARCHAR(35) NOT NULL UNIQUE,
`pass` VARCHAR(35) NOT NULL,
`fname` VARCHAR(35),
`lname` VARCHAR(35),
`gender` VARCHAR(1),
`phone` VARCHAR(12),
`appointments` INT,
`groups` INT(1) NOT NULL,
FOREIGN KEY (`groups`) references `groups`(`gnumber`)
);
CREATE TABLE `groups` (
`gname` VARCHAR(25) NOT NULL,
`gnumber` INT(1) NOT NULL
);
INSERT INTO `groups`(`gname`, `gnumber`) values ('user', 0);
INSERT INTO `groups`(`gname`, `gnumber`) values ('admin', 1);
INSERT INTO `users`(`login`, `pass`, `groups`) values ('admin', 'secret', 1);
Error Code 150
means: 错误代码
150
表示:
If the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed.
如果错误消息引用错误150,则表创建失败,因为未正确形成外键约束。
To fix this, simply create the groups
Table before the users
Table. 要解决此问题,只需在
users
表之前创建groups
表。
Edit: You'll also need to make gnumber
a key in the groups
table for this to work. 编辑:您还需要将
gnumber
设置为groups
表中的一个键,以使其正常工作。
CREATE TABLE `groups` (
`gname` varchar(25) NOT NULL,
`gnumber` int(1) NOT NULL,
PRIMARY KEY (`gnumber`)
);
I don't know whether you want it to be the primary key, you'll have to play around with the available settings but this works for me locally insofar that I can create the tables and execute the inserts. 我不知道你是否希望它成为主键,你必须使用可用的设置,但这对我来说在本地起作用,因为我可以创建表并执行插入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.