MySql 主键自动增长.docx_第1页
MySql 主键自动增长.docx_第2页
MySql 主键自动增长.docx_第3页
MySql 主键自动增长.docx_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

MySql 主键自动增长 Mysql,SqlServer,Oracle主键自动增长设置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包含两个参数,第一个参数表示起始值,第二个参数表示增量。 以前经常会碰到这样的问题,当我们删除了一条自增长列为1的记录以后,再次插入的记录自增长列是2了。我们想在插入一条自增长列为1的记录是做不到的。今天跟同事讨论的时候发现可以通过设置SET IDENTITY_INSERT 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_INSERTSET 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_customerscreate or replacetrigger trg_customers before insert on customers for each row begin select C

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论