MySQL必知必會 標識列

2021-08-15 01:33:21 字數 1908 閱讀 3289

#標識列

/*又稱為自增長列

含義:可以不用手動的插入值,系統提供預設的序列值

特點:1、標識列必須和主鍵搭配嗎?不一定,但要求是乙個key(也可以是unique)

2、乙個表可以有幾個標識列?至多乙個!

3、標識列的型別只能是數值型

4、標識列可以通過 set auto_increment_increment=3;設定步長

可以通過 手動插入值,設定起始值

*/#一、建立表時設定標識列

drop table if exists tab_identity;

create table tab_identity(

id int primary key auto_increment,

name float unique,

seat int

);drop table if exists tab_identity;

create table tab_identity(

id int ,

name float unique auto_increment,

seat int

);truncate table tab_identity;

# 要麼 帶著所有的字段,插入時,自增長列對應插入null

insert into tab_identity(id,name) values(null,'john');

#要麼 不帶自增長列,插入時只插別的字段

insert into tab_identity(name) values('lucy');

select * from tab_identity;

# 自增長列缺省從1開始遞增,步長為1

show variables like '%auto_increment%';

# auto_increment_increment:步長

# auto_increment_offset:起始位置(mysql中不能人為設定)

set auto_increment_increment=3; # 設定步長

#如果想自己設定起始位置:10

#先執行下一句通過 手動插入值,設定起始值

insert into tab_identity(id,name) values(10,'john');

insert into tab_identity(name) values('lucy');

#二、修改表時設定標識列

drop table if exists test;

create table test(

id int primary key,

name varchar(20)

);alter table test modify column id int auto_increment;

insert into test values (null,'aaa');

select * from test;

truncate table test; #清空表,注意與drop table if exists test; 的區別,乙個是清空,另乙個刪除

show variables like '%auto_increment%';

set auto_increment_increment = 2;

insert into test values(5,'haha');

insert into test values(null,'haha');#執行多次該句

select * from test; # 結果是 id = 5,7,9....

desc test;

#二、修改表時刪除標識列

alter table test modify column id int ;

desc test;

mysql必知必會 mysql必知必會(四)

十四 理解子查詢 1 通過子查詢過濾 這本書在所有的章節都關連到了資料庫表,訂單資料是儲存在兩個表中,orders表儲存著 訂單號碼 顧客id和訂單日期。個人的訂單列表關連著orderitems表,訂單表沒有儲存顧客資訊,它只是儲存著顧客id,這實際的顧客資訊是儲存在customers表中。現在假設...

mysql的必知必會 mysql 必知必會 筆記

好久沒有寫了。1 show columns from table 等同於describe table顯示的是表的結構。而select from table 則顯示的是整個表中插入的資料。2 select distinct c1,c2 from table除非列不相同,否則所有行將被檢索出來,即不能對...

mysql必知必會

一周了,總想寫點什麼,有的時候進步真的很難在一周顯示出來,週三的時候,我跟我的領導說,好快啊,又週三了,不知不覺,他說是啊,現在對於他來說,有時候他過一天可能跟我過一周的感覺差不多,每天都在忙,時間過的特別快,也沒有感覺做出來點什麼,當然實際肯定是怎麼做了一些東西的,是否我以後也會如此呢?說說技術把...