Oracle、MySQL、SQL Server自增變量的方法
SQL Server自增變量和Oracle等數(shù)據(jù)庫中的實(shí)現(xiàn)方法都不盡相同,下面就為您詳細(xì)介紹三種常用數(shù)據(jù)庫自增變量的方法,希望對您能有所幫助。
1、MySQL的自增變量是比較好記的,使用AUTO_INCREMENT關(guān)鍵字,如果知道英文的就容易記憶了,如下創(chuàng)建一個(gè)帶有自增變理的表:
create table test(id int AUTO_INCREMENT
primary key not null,name varchar(50));
注釋:此處的id一定要申明為主鍵,否則會(huì)報(bào)錯(cuò)。
2、SQl Server使用identity關(guān)鍵字實(shí)現(xiàn)SQL Server自增變量,可以很容易指定從什么數(shù)開始,增幅是多少,SQL Server自增變量的方法如下:
create table test(id int identity(100,10)
primary key not null,name varchar(50));
3、Oracle不能夠在創(chuàng)建表的時(shí)候指定自動(dòng)關(guān)鍵字,它需要重新創(chuàng)建sequence,然后以"創(chuàng)建鍵。nextval"來引用:
create table test(id int primary key
not null,name varchar(50));
create sequence test_id(***是表名+序列號標(biāo)記)
increment by 1 start with 1 maxvalue 9999;
引用如下:
insert into test(test_id.nextval,'www');
【編輯推薦】