国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

解決由于操作不當出現的ERROR 1005錯誤

2024-07-21 02:44:24
字體:
來源:轉載
供稿:網友

在使用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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 钟祥市| 文成县| 读书| 盐津县| 榕江县| 视频| 白山市| 会理县| 中阳县| 诸暨市| 临颍县| 平安县| 璧山县| 两当县| 文水县| 宣化县| 左云县| 沙田区| 巩义市| 徐州市| 依兰县| 大厂| 烟台市| 渭源县| 沂源县| 涟源市| 密山市| 庐江县| 长宁县| 安义县| 青岛市| 宿迁市| 田阳县| 湟源县| 德江县| 梅州市| 赞皇县| 宁津县| 马龙县| 屏南县| 嘉兴市|