`
ldzyz007
  • 浏览: 700153 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

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

阅读更多
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 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;

SET IDENTITY_INSERT customers ON;

insert into customers(id,name) values(1,'name1');

SET IDENTITY_INSERT customers OFF;

alter table 表名 drop constraint 主键名
alter table 表名 add  constraint 主键名 primary key (column1,column2,....,column)

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;

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

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');


分享到:
评论
1 楼 勇往直前wwt 2014-08-28  
这样是自动增长,但每次id还得插入,如何只插入其他字段而让id自动增长呢

相关推荐

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

    1、把主键定义为自动增长标识符类型 在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值。例如: create table customers(id int auto_increment primary key notnull, name varchar(15))...

    深入Mysql,SqlServer,Oracle主键自动增长的设置详解

    1、把主键定义为自动增长标识符类型MySql在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值。例如: 代码如下:create table customers(id int auto_increment primary key not null, name...

    精通hibernate:对象持久化技术孙卫琴第二版part2

    6.1.1 把主键定义为自动增长标识符类型 123 6.1.2 从序列(Sequence)中获取自动增长的标识符 124 6.2 Java语言按内存地址区分不同的对象 125 6.3 Hibernate用对象标识符(OID)来区分对象 126 6.4 Hibernate的...

    精通Hibernate:对象持久化技术第二版part3

    6.1.1 把主键定义为自动增长标识符类型 123 6.1.2 从序列(Sequence)中获取自动增长的标识符 124 6.2 Java语言按内存地址区分不同的对象 125 6.3 Hibernate用对象标识符(OID)来区分对象 126 6.4 Hibernate的...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part4

     6.1.1 把主键定义为自动增长标识符类型  6.1.2 从序列(Sequence)中获取自动增长的标识符 6.2 Java语言按内存地址区分不同的对象 6.3 Hibernate用对象标识符(OID)来区分对象 6.4 Hibernate的内置标识符生成器...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

     6.1.1 把主键定义为自动增长标识符类型  6.1.2 从序列(Sequence)中获取自动增长的标识符 6.2 Java语言按内存地址区分不同的对象 6.3 Hibernate用对象标识符(OID)来区分对象 6.4 Hibernate的内置标识符生成器...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part3

     6.1.1 把主键定义为自动增长标识符类型  6.1.2 从序列(Sequence)中获取自动增长的标识符 6.2 Java语言按内存地址区分不同的对象 6.3 Hibernate用对象标识符(OID)来区分对象 6.4 Hibernate的内置标识符生成器...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part1.rar

     6.1.1 把主键定义为自动增长标识符类型  6.1.2 从序列(Sequence)中获取自动增长的标识符 6.2 Java语言按内存地址区分不同的对象 6.3 Hibernate用对象标识符(OID)来区分对象 6.4 Hibernate的内置标识符生成器...

Global site tag (gtag.js) - Google Analytics