Mysql 错误1005(HY000):Can';t创建表';测试模式。#sql-44c#U 2a和#x27;(错误编号:150)

Mysql 错误1005(HY000):Can';t创建表';测试模式。#sql-44c#U 2a和#x27;(错误编号:150),mysql,database,foreign-keys,constraints,Mysql,Database,Foreign Keys,Constraints,因此,我用更具可读性的代码重新创建了这个问题。我在mysql提示符中运行此sql文件: CREATE SCHEMA IF NOT EXISTS test_schema DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ; USE test_schema ; -- ----------------------------------------------------- -- Table test_schema.table_one -- -

因此,我用更具可读性的代码重新创建了这个问题。我在mysql提示符中运行此sql文件:

CREATE SCHEMA IF NOT EXISTS test_schema 
DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;

USE test_schema ;

-- -----------------------------------------------------
-- Table test_schema.table_one
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS table_one (
    table_one_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    table_one_name VARCHAR(45) NOT NULL,
    PRIMARY KEY (table_one_id, table_one_name)
)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table test_schema.table_two
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS table_two (
    table_two_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    table_two_name VARCHAR(45) NOT NULL,
    table_one_name VARCHAR(45) NOT NULL,
    PRIMARY KEY (table_two_id)
)
ENGINE = InnoDB;

ALTER TABLE table_two ADD FOREIGN KEY (table_one_name) REFERENCES table_one(table_one_name);
我从提示符中得到的输出是:

mysql> source test-database.sql;
Query OK, 1 row affected (0.00 sec)

Database changed
Query OK, 0 rows affected (0.07 sec)

Query OK, 0 rows affected (0.08 sec)

ERROR 1005 (HY000): Can't create table 'test_schema.#sql-44c_2a' (errno: 150)
如果我运行“显示引擎INNODB状态;”我得到以下细节

------------------------
LATEST FOREIGN KEY ERROR
------------------------
150603  9:22:25 Error in foreign key constraint of table test_schema/#sql-44c_2a:
FOREIGN KEY (table_one_name) REFERENCES table_one(table_one_name):
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.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
我搜索过这个问题,几乎每个人都说它是关于:
1) 没有相同的类型
-它们都是“VARCHAR(45)NOT NULL”(尝试让表2中的一个为NULL,但没有改变错误消息)
2) 外键不是主键
-table_one_name与table_one_id一起是table_one中的主键
3) 确保Charset和Collate选项在表级别相同
-这是什么意思?我想是的,因为我没有改变它们

请帮忙
//Finbel

要添加外键,必须对这两个字段编制索引

试试这个:

-- -----------------------------------------------------
-- Table test_schema.table_one
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS table_one (
    table_one_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    table_one_name VARCHAR(45) NOT NULL,
    PRIMARY KEY (table_one_id, table_one_name),
    KEY `one_name` (`table_one_name`)
)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table test_schema.table_two
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS table_two (
    table_two_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    table_two_name VARCHAR(45) NOT NULL,
    table_one_name VARCHAR(45) NOT NULL,
    PRIMARY KEY (table_two_id),
    KEY `two_name` (`table_two_name`)
)
ENGINE = InnoDB;

ALTER TABLE table_two  ADD FOREIGN KEY (table_one_name) REFERENCES table_one(table_one_name);

谢谢关于我在哪里可以阅读索引,你有什么建议吗?出于某种原因,他们跳过了我的数据库课程中的整个部分。MySql文档将是开始的地方,从那里开始,关于这个问题的其他帖子,在表中使用索引非常重要,索引有不同的类型,但对我来说,规则是,对于搜索的每个字段都必须有一个索引