Ank*_*jee 45 mysql foreign-keys mysql-error-1005
我正在努力在数据库中创建一些表foo,但每次我最终都得到关于外键的errno 150.首先,这是我创建表的代码:
CREATE TABLE Clients
(
client_id CHAR(10) NOT NULL ,
client_name CHAR(50) NOT NULL ,
provisional_license_num CHAR(50) NOT NULL ,
client_address CHAR(50) NULL ,
client_city CHAR(50) NULL ,
client_county CHAR(50) NULL ,
client_zip CHAR(10) NULL ,
client_phone INT NULL ,
client_email CHAR(255) NULL ,
client_dob DATETIME NULL ,
test_attempts INT NULL
);
CREATE TABLE Applications
(
application_id CHAR(10) NOT NULL ,
office_id INT NOT NULL ,
client_id CHAR(10) NOT NULL ,
instructor_id CHAR(10) NOT NULL ,
car_id CHAR(10) NOT NULL ,
application_date DATETIME NULL
);
CREATE TABLE Instructors
(
instructor_id CHAR(10) NOT NULL ,
office_id INT NOT NULL ,
instructor_name CHAR(50) NOT NULL ,
instructor_address CHAR(50) NULL ,
instructor_city CHAR(50) NULL ,
instructor_county CHAR(50) NULL ,
instructor_zip CHAR(10) NULL ,
instructor_phone INT NULL ,
instructor_email CHAR(255) NULL ,
instructor_dob DATETIME NULL ,
lessons_given INT NULL
);
CREATE TABLE Cars
(
car_id CHAR(10) NOT NULL ,
office_id INT NOT NULL ,
engine_serial_num CHAR(10) NULL ,
registration_num CHAR(10) NULL ,
car_make CHAR(50) NULL ,
car_model CHAR(50) NULL
);
CREATE TABLE Offices
(
office_id INT NOT NULL ,
office_address CHAR(50) NULL ,
office_city CHAR(50) NULL ,
office_County CHAR(50) NULL ,
office_zip CHAR(10) NULL ,
office_phone INT NULL ,
office_email CHAR(255) NULL
);
CREATE TABLE Lessons
(
lesson_num INT NOT NULL ,
client_id CHAR(10) NOT NULL ,
date DATETIME NOT NULL ,
time DATETIME NOT NULL ,
milegage_used DECIMAL(5, 2) NULL ,
progress CHAR(50) NULL
);
CREATE TABLE DrivingTests
(
test_num INT NOT NULL ,
client_id CHAR(10) NOT NULL ,
test_date DATETIME NOT NULL ,
seat_num INT NOT NULL ,
score INT NULL ,
test_notes CHAR(255) NULL
);
ALTER TABLE Clients ADD PRIMARY KEY (client_id);
ALTER TABLE Applications ADD PRIMARY KEY (application_id);
ALTER TABLE Instructors ADD PRIMARY KEY (instructor_id);
ALTER TABLE Offices ADD PRIMARY KEY (office_id);
ALTER TABLE Lessons ADD PRIMARY KEY (lesson_num);
ALTER TABLE DrivingTests ADD PRIMARY KEY (test_num);
ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Offices FOREIGN KEY (office_id) REFERENCES Offices (office_id);
ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Clients FOREIGN KEY (client_id) REFERENCES Clients (client_id);
ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Instructors FOREIGN KEY (instructor_id) REFERENCES Instructors (instructor_id);
ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Cars FOREIGN KEY (car_id) REFERENCES Cars (car_id);
ALTER TABLE Lessons ADD CONSTRAINT FK_Lessons_Clients FOREIGN KEY (client_id) REFERENCES Clients (client_id);
ALTER TABLE Cars ADD CONSTRAINT FK_Cars_Offices FOREIGN KEY (office_id) REFERENCES Offices (office_id);
ALTER TABLE Clients ADD CONSTRAINT FK_DrivingTests_Clients FOREIGN KEY (client_id) REFERENCES Clients (client_id);
Run Code Online (Sandbox Code Playgroud)
这些是我得到的错误:
mysql> ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Cars FOREIGN KEY
(car_id) REFERENCES Cars (car_id);
ERROR 1005 (HY000): Can't create table 'foo.#sql-12c_4' (errno: 150)
Run Code Online (Sandbox Code Playgroud)
我跑了SHOW ENGINE INNODB STATUS,它提供了更详细的错误描述:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
100509 20:59:49 Error in foreign key constraint of table foo/#sql-12c_4:
FOREIGN KEY (car_id) REFERENCES Cars (car_id):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
------------
Run Code Online (Sandbox Code Playgroud)
我在StackOverflow和其他地方搜索过 - 在这里发现了一篇有用的博客文章,其中有关于如何解决此错误的指示 - 但我无法弄清楚出了什么问题.任何帮助,将不胜感激!
小智 31
注意:我遇到了同样的问题,这是因为引用的字段在2个不同的表中使用了不同的排序规则(它们具有完全相同的类型).
确保所有引用的字段具有相同的类型和相同的排序规则!
小智 14
检查BOTH表是否具有相同的ENGINE.例如,如果你有:
CREATE Table FOO ();
Run Code Online (Sandbox Code Playgroud)
和:
CREATE Table BAR () ENGINE=INNODB;
Run Code Online (Sandbox Code Playgroud)
如果您尝试从表BAR到表FOO创建约束,它将无法在某些MySQL版本上运行.
通过以下方式修复此问题:
CREATE Table FOO () ENGINE=INNODB;
Run Code Online (Sandbox Code Playgroud)
Eri*_* L. 11
微妙,但这个错误让我失望,因为我忘了将smallint列声明为unsigned以匹配引用的现有表,即"smallint unsigned".有一个无符号和一个未签名导致MySQL阻止在新表上创建外键.
id smallint(3) not null
Run Code Online (Sandbox Code Playgroud)
为了外键,不匹配,
id smallint(3) unsigned not null
Run Code Online (Sandbox Code Playgroud)
当我尝试以下时,我得到了这个毫无价值且无法提供信息的错误:
ALTER TABLE `comments` ADD CONSTRAINT FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE;
Run Code Online (Sandbox Code Playgroud)
我的问题出在我的评论表中,user_id被定义为:
`user_id` int(10) unsigned NOT NULL
Run Code Online (Sandbox Code Playgroud)
所以...在我的情况下,问题是NOT NULL和ON DELETE SET NULL之间的冲突.
小智 5
两个表都需要具有相同的字符集.
例如
CREATE TABLE1 (
FIELD1 VARCHAR(100) NOT NULL PRIMARY KEY,
FIELD2 VARCHAR(100) NOT NULL
)ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_bin;
Run Code Online (Sandbox Code Playgroud)
至
CREATE TABLE2 (
Field3 varchar(64) NOT NULL PRIMARY KEY,
Field4 varchar(64) NOT NULL,
CONSTRAINT FORIGEN KEY (Field3) REFERENCES TABLE1(FIELD1)
) ENGINE=InnoDB;
Run Code Online (Sandbox Code Playgroud)
会失败,因为他们有不同的字符集.这是另一个微妙的失败,其中mysql返回相同的错误.
| 归档时间: |
|
| 查看次数: |
101120 次 |
| 最近记录: |