1、把主键定义为自动增长标识符类型

MySql

在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值。例如:

create table customers(id int auto_increment primary key not null, name varchar(15));
insert into customers(name) values("name1"),("name2");
select id from customers;

以上sql语句先创建了customers表,然后插入两条记录,在插入时仅仅设定了name字段的值。最后查询表中id字段,查询结果为:

由此可见,一旦把id设为auto_increment类型,mysql数据库会自动按递增的方式为主键赋值。

Sql Server

在MS SQLServer中,如果把表的主键设为identity类型,数据库就会自动为主键赋值。例如:

create table customers(id int identity(1,1) primary key not null, name varchar(15));
insert into customers(name) values('name1'),('name2');
select id from customers;

注意:在sqlserver中字符串用单引号扩起来,而在mysql中可以使用双引号。

查询结果和mysql的一样。

由此可见,一旦把id设为identity类型,MS SQLServer数据库会自动按递增的方式为主键赋值。identity包含两个参数,第一个参数表示起始值,第二个参数表示增量。

PS:2012-7-13

以前经常会碰到这样的问题,当我们删除了一条自增长列为1的记录以后,再次插入的记录自增长列是2了。我们想在插入一条自增长列为1的记录是做不到的。今天跟同事讨论的时候发现可以通过设置SET IDENTITY_INSERT <table_name> ON;来取消自增长,等我们插入完数据以后在关闭这个功能。实验如下:

use TESTDB2
--step1:创建表
create table customers(
id int identity primary key not null,
name varchar(15)
); --step2:执行插入操作
insert into customers(id,name) values(1,'name1');
--报错:An explicit value for the identity column in table 'customers' can only be specified when a column list is used and IDENTITY_INSERT is ON. --step3:放开主键列的自增长
SET IDENTITY_INSERT customers ON; --step4:插入两条记录,主键分别为1和3。插入成功
insert into customers(id,name) values(1,'name1');
insert into customers(id,name) values(3,'name1'); --step5:再次插入一个主键为2的记录。插入成功
insert into customers(id,name) values(2,'name1'); --step6:插入重复主键,
--报错:Violation of PRIMARY KEY constraint 'PK__customer__3213E83F00551192'. Cannot insert duplicate key in object 'dbo.customers'.
insert into customers(id,name) values(3,'name1'); --step7:关闭IDENTITY_INSERT
SET IDENTITY_INSERT customers OFF;

2、从序列中获取自动增长的标识符

Oracle

在Oracle中,可以为每张表的主键创建一个单独的序列,然后从这个序列中获取自动增加的标识符,把它赋值给主键。例如一下语句创建了一个名为customer_id_seq的序列,这个序列的起始值为1,增量为2。

create sequence customer_id_seq increment by 2 start with 1

一旦定义了customer_id_seq序列,就可以访问序列的curval和nextval属性。

  • curval:返回序列的当前值
  • nextval:先增加序列的值,然后返回序列值

以下sql语句先创建了customers表,然后插入两条记录,在插入时设定了id和name字段的值,其中id字段的值来自于customer_id_seq序列。最后查询customers表中的id字段。

create table customers(id int primary key not null, name varchar(15));
insert into customers values(customer_id_seq.nextval, 'name1');
insert into customers values(customer_id_seq.nextval, 'name2');
select id from customers;

如果在oracle中执行以上语句,查询结果为:

通过触发器自动添加id字段

从上述插入语句可以发现,如果每次都要插入customer_id_seq.nextval的值会非常累赘与麻烦,因此可以考虑使用触发器来完成这一步工作。

创建触发器trg_customers

create or replace
trigger trg_customers before insert on customers for each row
begin
select CUSTOMER_ID_SEQ.nextval into :new.id from dual;
end;

插入一条记录

insert into customers(name) values('test'); 

这是我们会发现这一条记录被插入到数据库中,并且id还是自增长的。

Mysql,SqlServer,Oracle主键自动增长的设置的更多相关文章

  1. Mysql,SqlServer,Oracle主键自动增长的设置

    在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值.例如: CREATE TABLE google(id INT AUTO_INCREMENT PRIMARY ...

  2. oracle 主键自动增长

    oracle 主键自动增长 2009-12-11 16:07:00|  分类: 数据库资料|字号 订阅     这几天搞Oracle,想让表的主键实现自动增长,查网络实现如下: create tabl ...

  3. MySQL 和 Oracle 主键自增长

    1.MySQL 1)建表 auto_increment:每插入一条数据,客户表(customers)的主键id就自动增1,如下所示 create table customers -- 创建客户表 ( ...

  4. PowerDesigner 15设置mysql主键自动增长及基数

    PowerDesigner 15设置mysql主键自动增长及基数 1.双击标示图,打开table properties->columns,  如图点击图标Customize Columns an ...

  5. MySQL与Oracle主键Query性能测试结果

    测试结果总结如下: 1. 按主键读:SQL形式:SELECT * FROM table WHERE id=?. 1.1. 主键为数字.如果所有ID均不存在,纯比较SQL解析能力.MySQL解析SQL的 ...

  6. oracle添加数据时主键自动增长

    CREATE TABLE STUDENT( --创建学生表  ID NUMBER(10) PRIMARY KEY,   --主键ID  SNAME VARCHAR2(20), ); 此时给学生表添加数 ...

  7. mysql——主键自动增长&唯一索引

    首先说一下主键和唯一索引的区别 主键:一个数据库的一张表有且仅有一个主键,而且主键不能重复 唯一索引:一个数据库的一张表上唯一索引可以有多个,只是所在唯一索引上的值不能重复,这一点和主键一样 下面我们 ...

  8. Mysql:设置主键自动增长起始值

    比较郁闷昨天在家使用‘alter table `tablename` AUTO_INCREMENT=10000;’怎么也不起效,但是今天下班时间公司一同事尝试了一下就可以了.搞不明白自己当时是怎么操作 ...

  9. oracle主键自增长

    这几天搞Oracle,想让表的主键实现自动增长,查网络实现如下: create table simon_example ( id number(4) not null primary key, nam ...

随机推荐

  1. 【Asp.Net MVC--资料汇总】杂七杂八

    Html.RenderPartial与Html.RenderAction的区别 http://blog.sina.com.cn/s/blog_8278b1800100zkn0.html ASP.NET ...

  2. Consumer closed input channel or an error occurred. events=0x8 channel is unrecoverably broken and will be disposed(待解决)

    跟文件读取有关?关闭文件读取试试. 有可能是读取文件 出现报错

  3. c# 与 Unity3d 中的序列化

    圣典中对于Unity3D的序列化介绍很容易和C#的序列化介绍搞混,做个笔记,方便以后查找. 很多资料算是拾人牙慧. 一.Serializable 序列化 Inherits from Attribute ...

  4. hdu1022 Train Problem I

    http://acm.hdu.edu.cn/showproblem.php?pid=1022 #include<iostream> #include<stdio.h> #inc ...

  5. 欧拉工程第71题:Ordered fractions

    题目链接:https://projecteuler.net/problem=71 If n<d and HCF(n,d)=1, it is called a reduced proper fra ...

  6. Minimum_Window_Substring两种方法求解

    题目描述: Given a string S and a string T, find the minimum window in S which will contain all the chara ...

  7. Ubuntu下编译运行Kamailio

    kamailio----配置没有成功,这个文档过几天删除,因为这个项目的文档非常少,而且qq群里的人也不活跃,现在正在研究Freeswitch,如果能够满足,就不研究这个了,这篇文档会删除. Kama ...

  8. 256. Paint House

    题目: There are a row of n houses, each house can be painted with one of the three colors: red, blue o ...

  9. 76. Minimum Window Substring

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  10. NSSize

    #import <Foundation/Foundation.h>   int main(int argc, const char * argv[]) {    @autoreleasep ...