mysql自动编号步进值_MySQL-自动编号
為什么要設置自動編號:
如上圖所示,類別編號如果不設置自動編號的話,則需要人為的進行編輯和插入。
設置表的屬性值自動增加:
語法規則:
列名?數據類型? auto_increment
注:
auto_increment約束的字段可以是任何整數類型(tinyint,smallint,int等)
例子:
創建自動編號表:
create table bookcategory(
category_id int primary key auto_increment,
category varchar(20) not null unique,
parent_id int not null
);
自增列的初始值默認為1,每添加1條記錄,自動增長1。
在建表時可用’auto_increment=n‘選項來指定一個自增的初始值。
向表中插入記錄:
insert into bookcategory (category,parent_id) values('計算機',0);
這條語句中沒有給category_id指定值,我們看一下查詢表記錄的結果:
表中的category_id自動變成了1.
在創建表時設置自動編號的初始值:
create table bookcategory_tmp(
category_id int primary key auto_increment,
category varchar(20) not null unique,
parent_id int not null
)? auto_increment=5;
向表中插入記錄:
insert into bookcategory_tmp (category,parent_id)values('醫學’,0);
查看表記錄時,category_id的值變成了5,往后每增加1條,編號自動+1:
如何為已存在的表增加自動編號列:
如果在創建表時沒有為列指定自動編號,那么可以通過以下命令來修改:
create table bookcategory_tmp(
category_id int primary key,
category varchar(20) not null unique,
parent_id int not null
);
alter table bookcategory_tmp modify category? int? auto_increment;
修改自增列的起始值:
create table bookcategory(
category_id int primary key auto_increment,
category varchar(20) not null unique,
parent_id int not null
);
alter table bookcategory auto_increment=X;
修改后的auto_increment列起始值從X開始。
去掉自增列:
alter table bookcategory modify? category_id? int;
如果要添加自增列的表與其他表存在關聯關系,那么首先要去掉關聯關系以后,才可以修改表的自動編號列。
刪除關聯關系:
alter table?主表名 drop foreign key外鍵名;
然后再修改表的自動編號列:
alter table bookcategorymodify category_idint auto_increment;
然后再恢復兩表之間的關聯關系:
alter table bookinfo
add constraint? fk_bcid
foreign key(book_category_id)
inferences? bookcategory(category_id);
未完待續~
了解更多內容,請關注:
總結
以上是生活随笔為你收集整理的mysql自动编号步进值_MySQL-自动编号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 宣酱七星多少钱一瓶?
- 下一篇: Twipstopixels java_1