程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> MYSQL中ERROR 1005信息處理

MYSQL中ERROR 1005信息處理

編輯:關於MYSQL數據庫

在使用MYsql的時候,在操作不當時,很容易出現 ERROR 1005 (HY000): Can't create table 這類錯誤。很多站長朋友可能需要排查很久才會找到問題的原因其實很簡單,希望這篇文章可以對站長朋友以及MySQL初學者一點幫助。

MySQL官方提供的問題原因:

在信息中有一組【LATEST FOREIGN KEY ERROR】會有最近錯誤的詳細描述和解決辦法。

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字段”的索引,或者是兩個關聯字段類型不匹配)


以下介紹兩個示例:

示例一:

create table booktype
(
   btid   int(5) unsigned zerofill auto_increment not null primary key,
   btname varchar(100) not null unique,
   btnote text
);
create table books
(
   bid int(5) unsigned zerofill auto_increment not null primary key,
   bname char(30) not null,
   isbn char(50) not null,
   author char(30) not null,
   press   text,
   summary text,
   bcount int not null default 0,
   btid   int,
   foreign key(btid) references booktype(btid)
);

出現的報錯:

ERROR 1005 (HY000): Can't create table '.\bookdata\books.frm' (errno: 150)

主要問題以及解決辦法是:

foreign key(btid) references booktype(btid) 中books表的 btid   是int和booktype表中的btid設置的關聯字段類型不匹配,books表中btid改正成:btid   int(5) unsigned zerofill ,就不會報錯了,創建表和修改表地時候常常一步小小就忘記了這個.

 

示例二:

MySQL裡創建外鍵時(Alter table xxx add constraint fk_xxx foreign key),提示錯誤,但只提示很簡單的信息:ERROR 1005 (HY000): Can't create table '.\env_mon\#sql-698_6.frm' (errno: 150)。根本起不到解決問題的作用。

drop table if exists products;
create table products(
 id  int  not null auto_increment,
 title  varchar(100) not null,
 description text  not null,
 image_url varchar(200) not null,
 price  decimal(10,2) not null,
 date_available  datetime not null,
 primary key(id)
)type=innodb;
drop table if exists line_items;
create table line_items(
 id  int  not null auto_increment,
 product_id int  not null,
 quantity int  not null default 0,
 unit_price decimal(10,2) not null,
 constraint fk_items_product foreign key (product_id) references producets(id),
index(product_id)
 primary key(id)
)type=innodb;

出現的報錯:

ERROR 1005: Can't create table

主要問題以及解決辦法是:

1,MySQL支持外鍵約束,並提供與其它DB相同的功能,但表類型必須為 InnoDB
2、建外鍵的表的那個列要加上index

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved