SQL AUTO INCREMENT 字段
1、用于 SQL Server 的语法
下列 SQL 语句把 "Persons" 表中的 "P_Id" 列定义为 auto-increment 主键:
CREATE TABLE Persons
(
P_Id int PRIMARY KEY IDENTITY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
MS SQL 使用 IDENTITY 关键字来执行 auto-increment 任务。
默认地,IDENTITY 的开始值是 1,每条新记录递增 1。
要规定 "P_Id" 列以 20 起始且递增 10,请把 identity 改为 IDENTITY(20,10)
要在 "Persons" 表中插入新记录,我们不必为 "P_Id" 列规定值(会自动添加一个唯一的值):
INSERT INTO Persons (FirstName,LastName)
VALUES ('Bill','Gates')
上面的 SQL 语句会在 "Persons" 表中插入一条新记录。"P_Id" 会被赋予一个唯一的值。"FirstName" 会被设置为 "Bill","LastName" 列会被设置为 "Gates"。
2、
用于 Oracle 的语法
在 Oracle 中,代码稍微复杂一点。
您必须通过 sequence 对创建 auto-increment 字段(该对象生成数字序列)。
请使用下面的 CREATE SEQUENCE 语法:
CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10
上面的代码创建名为 seq_person 的序列对象,它以 1 起始且以 1 递增。该对象缓存 10 个值以提高性能。CACHE 选项规定了为了提高访问速度要存储多少个序列值。
要在 "Persons" 表中插入新记录,我们必须使用 nextval 函数(该函数从 seq_person 序列中取回下一个值):
INSERT INTO Persons (P_Id,FirstName,LastName)
VALUES (seq_person.nextval,'Lars','Monsen')
在这里P_Id的值就会实现从1开始自增
上面的 SQL 语句会在 "Persons" 表中插入一条新记录。"P_Id" 的赋值是来自 seq_person 序列的下一个数字。"FirstName" 会被设置为 "Bill","LastName" 列会被设置为 "Gates"。
<wbr>小注:
<div><span style="font-family:Verdana,Arial,宋体; line-height:18px; background-color:rgb(249,249,249)"><span style="font-size:14px">1、sequence与并不针对某个表。</span></span></div>
<div>
<div>
<span style="font-family:tahoma,helvetica,arial; font-size:14px; color:#454545; line-height:21px"><span style="white-space:pre">2、</span>INCREMENT BY 1 -- 每次增加1</span><br style="color:rgb(69,69,69); line-height:21px; font-family:tahoma,helvetica,arial; font-size:14px"><span style="font-family:tahoma,helvetica,arial; font-size:14px; color:#454545">START WITH 1 -- 从1开始计数</span><br style="color:rgb(69,69,69); line-height:21px; font-family:tahoma,helvetica,arial; font-size:14px"><span style="font-family:tahoma,helvetica,arial; font-size:14px; color:#454545; line-height:24px"></span><span style="font-family:tahoma,helvetica,arial; font-size:14px; color:#454545; line-height:21px">NOMAXVALUE -- 不设置最大值</span><br style="color:rgb(69,69,69); line-height:21px; font-family:tahoma,helvetica,arial; font-size:14px"><span style="font-family:tahoma,helvetica,arial; font-size:14px; color:#454545; line-height:24px"></span><span style="font-family:tahoma,helvetica,arial; font-size:14px; color:#454545; line-height:21px">NOCYCLE -- 一直累加,不循环</span><br style="color:rgb(69,69,69); line-height:21px; font-family:tahoma,helvetica,arial; font-size:14px"><span style="font-family:tahoma,helvetica,arial; font-size:14px; color:#454545; line-height:24px"></span><span style="font-family:tahoma,helvetica,arial; font-size:14px; color:#454545; line-height:21px">NOCACHE -- 不建缓冲区</span>
</div>
<div>
<span style="font-family:tahoma,helvetica,arial; font-size:14px; color:#454545; line-height:21px">3、一旦定义了</span><span style="font-family:'Courier New',Courier,monospace; line-height:normal; background-color:rgb(245,245,245)"><span style="font-size:14px">seq_person</span></span><span style="font-family:tahoma,helvetica,arial; font-size:14px; color:#454545; line-height:21px">,你就可以用CURRVAL,NEXTVAL</span><br><span style="font-family:tahoma,helvetica,arial; font-size:14px; color:#454545; line-height:21px"><span style="white-space:pre"></span>CURRVAL=返回sequence的当前值</span><br style="color:rgb(69,69,69); line-height:21px; font-family:tahoma,helvetica,arial; font-size:14px"><span style="font-family:tahoma,helvetica,arial; font-size:14px; color:#454545; line-height:21px"><span style="white-space:pre"></span>NEXTVAL=增加sequence的值,然后返回sequence值</span>
</div>
<div><span style="font-family:Verdana,Arial,宋体; line-height:18px; background-color:rgb(249,249,249)"><span style="font-size:14px"><br></span></span></div>
<div><span style="font-family:Verdana,Arial,宋体; line-height:18px; background-color:rgb(249,249,249)"><span style="font-size:14px"><br></span></span></div>
<div><span style="font-family:Verdana,Arial,宋体; line-height:18px; background-color:rgb(249,249,249)"><span style="font-size:14px"><br></span></span></div>
</div>
</wbr>
页:
[1]