MySQL“ERROR 1005 (HY000): Can't create table 'foo.#sql-12c_4' (errno: 150)"

MySQL quot;ERROR 1005 (HY000): Can#39;t create table #39;foo.#sql-12c_4#39; (errno: 150)quot;(MySQL“ERROR 1005 (HY000): Cant create table foo.#sql-12c_4 (errno: 150))
本文介绍了MySQL“ERROR 1005 (HY000): Can't create table 'foo.#sql-12c_4' (errno: 150)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在数据库 foo 中创建一些表,但每次我最终得到关于外键的 errno 150.首先,这是我创建表格的代码:

I was working on creating some tables in database foo, but every time I end up with errno 150 regarding the foreign key. Firstly, here's my code for creating tables:

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);

这些是我得到的错误:

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)

我运行了 SHOW ENGINE INNODB STATUS,它给出了更详细的错误描述:

I ran SHOW ENGINE INNODB STATUS which gives a more detailed error description:

------------------------
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.
------------

我在 StackOverflow 和其他地方在线搜索 - 在这里发现了一篇有用的博客文章,其中包含有关如何解决此错误的指示 - 但我不知道出了什么问题.任何帮助将不胜感激!

I searched around on StackOverflow and elsewhere online - came across a helpful blog post here with pointers on how to resolve this error - but I can't figure out what's going wrong. Any help would be appreciated!

推荐答案

你应该让 car_id 成为汽车的主键.

You should make car_id a primary key in cars.

这篇关于MySQL“ERROR 1005 (HY000): Can't create table 'foo.#sql-12c_4' (errno: 150)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)