【MySQL】ERROR 1005 (HY000): Can't create table' (errno: 150)
建立含有外來鍵的表的時候遇到如下錯誤
ERROR 1005 (HY000): Can't create table 'eidsvr.oplog' (errno: 150)
test@3302 12:02:22>CREATE TABLE biz(
-> id INTEGER NOT NULL AUTO_INCREMENT,
-> buid VARCHAR(255),
-> appkey VARCHAR(255),
-> createtime DATETIME,
-> name VARCHAR(255),
-> seccode TEXT(2048),
-> externel1 INTEGER,
-> externel2 VARCHAR(255),
-> externel3 VARCHAR(255),
-> PRIMARY KEY (id)
-> ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)
test@3302 12:03:13>CREATE TABLE oplog (
-> id INTEGER NOT NULL AUTO_INCREMENT,
-> eid VARCHAR(128),
-> optime DATETIME,
-> kp VARCHAR(128),
-> optype INTEGER,
-> appkey VARCHAR(255),
-> buid VARCHAR(255),
-> description VARCHAR(255),
-> externel1 INTEGER,
-> externel2 VARCHAR(255),
-> externel3 VARCHAR(255),
-> PRIMARY KEY (id),
-> FOREIGN KEY(appkey) REFERENCES biz(appkey)
-> ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8 DEFAULT CHARSET=utf8;
ERROR 1005 (HY000): Can't create table 'eidsvr.oplog' (errno: 150)
以下情況會導致上述問題
1、外來鍵欄位與要做外來鍵校驗的欄位型別不匹配
2、MySQL支援外來鍵約束,並提供與其它DB相同的功能,但表型別必須為 InnoDB,非InnoDB 儲存引擎會導致報錯。
3、建外來鍵的表的那個列沒有index。
針對本例,在父表上的appkey 上新增索引 解決該問題!
eidsvr@3302 12:10:09>alter table biz add index idx_b_appkey(appkey);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
eidsvr@3302 12:14:06>CREATE TABLE oplog (
-> id INTEGER NOT NULL AUTO_INCREMENT,
-> eid VARCHAR(128),
-> optime DATETIME,
-> kp VARCHAR(128),
-> optype INTEGER,
-> appkey VARCHAR(255),
-> buid VARCHAR(255),
-> description VARCHAR(255),
-> externel1 INTEGER,
-> externel2 VARCHAR(255),
-> externel3 VARCHAR(255),
-> PRIMARY KEY (id),
-> FOREIGN KEY(appkey) REFERENCES biz(appkey)
-> ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.00 sec)
MySQL官方提供的問題原因:
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.
不能在“被reference的表”即父表裡找到包含“被reference欄位”的索引,或者是兩個關聯欄位型別不匹配!
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-743759/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ERROR 1005 (HY000): Can't create table 'starive.SC' (errno: 150)"Error
- ERROR 1005 (HY000): Can't create table'matrix.system_log' (errno: 150)Error
- ERROR 1135 (HY000): Can't create a new thread (errno 11)Errorthread
- MySQL案例-不同尋常的[ERROR]Can't create a new thread (errno 11)MySqlErrorthread
- ERROR 1135 (HY000): Can't create a new threadErrorthread
- [ERROR] Can't open the mysql.plugin tableErrorMySqlPlugin
- mysql [ERROR] Can't create IP socket: Permission deniedMySqlError
- MySQL報錯Table 'plugin' is read only [ERROR] Can't open the mysql.plugin table.MySqlPluginError
- MYSQL ERROR 2003 (HY000) CanMySqlError
- MySQL報錯'ERROR 2002 (HY000): Can't connect to local MySQL server through'MySqlErrorServer
- ERROR 2002(HY000):Can't connect to local MySQL socket '/tmp/mysql.sock'ErrorMySql
- ERROR 2002 (HY000): Can't connect server socket /tmp/mysql.sockErrorServerMySql
- MySQL "ERROR 1010(HY000):Error dropping database(canMySqlErrorDatabase
- Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist解決辦法ErrorMySql
- MySQL 5.7向表匯入資料包錯"ERROR 13 (HY000): Can't get stat of"MySqlError
- ERROR 2003 (HY000)Can't connect to MySQL server on '192.168.8.247' (113)ErrorMySqlServer
- ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)ErrorMySqlServerlocalhost
- ERROR 1786 (HY000): Statement violates GTID consistency: CREATE TABLE ... SELECTError
- [ERROR] mysqld: Can‘t open shared library ‘/usr/local/mysql/lib/plugin/validate_password.so‘ (errno:ErrorMySqlPlugin
- mysql中You can’t specify target table for update in FROM clMySql
- Mysql報錯Fatal error:Can't open and lock privilege tablesMySqlError
- mysql can't connect error about privilege----not root userMySqlError
- 解決Error (1133): Can’t find any matching row in the user tableError
- MySQL ERROR 1031 (HY000) at line 33: Table storage engine forMySqlError
- create table of mysql databaseMySqlDatabase
- MySQL 建立外來鍵報錯Can't write; duplicate key in tableMySql
- MySQL 5.5 原始碼安裝報錯"[ERROR] Can't start server"MySql原始碼ErrorServer
- ERROR 2002 (HY000): Can't connect server socket /var/run/mysqld/mysqld.sockErrorServerMySql
- MYSQL報1022錯誤:Can't write;duplicate key in table '.....'MySql
- ERROR 1114 (HY000) The table '' is fullError
- can't create PID file問題處理
- linux下連線mysql報錯ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysqLinuxMySqlErrorServer
- MySQL建立使用者報錯 ERROR 1396 (HY000): Operation CREATE USER failed for 'afei'@'%'MySqlErrorAI
- ERROR 1548 (HY000): Cannot load from mysql.proc. The table is probably corruptedErrorMySql
- mysqld: Can‘t create directory ‘E:\Software\mysql-5.7.24-winx64\data\‘ (Errcode: 2 - NoMySql
- T-SQL的CREATE TABLE語法(下)SQL
- T-SQL的CREATE TABLE語法(上)SQL
- ERROR in Entry module not found Error Can't resolve 'babel' in ' UseErrorBabel