国产探花免费观看_亚洲丰满少妇自慰呻吟_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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳原县| 隆安县| 乾安县| 古田县| 舒兰市| 普洱| 上栗县| 临邑县| 大冶市| 岗巴县| 定安县| 庐江县| 威远县| 房产| 海阳市| 彩票| 建瓯市| 洮南市| 准格尔旗| 邯郸县| 报价| 崇仁县| 玛纳斯县| 嘉荫县| 兰坪| 泊头市| 舟山市| 永福县| 正宁县| 板桥市| 磐安县| 涟水县| 惠安县| 富锦市| 疏勒县| 大余县| 贵德县| 乐清市| 托里县| 晋州市| 长丰县|