在Mysql,SqlServer及Oracle中设置主键自动增长

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

在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

1

2

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

在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;

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

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

在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.curval, "name1"),(customer_id_seq.nextval, "name2");

select id from customers;

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

id

1

3

本文出自 “技术成就梦想” 博客,请务必保留此出处http://ixdba.blog.51cto.com/2895551/526419

时间: 2024-08-18 05:09:21

在Mysql,SqlServer及Oracle中设置主键自动增长的相关文章

MySQL 处理插入过程中的主键唯一键重复值的解决方法_Mysql

本篇文章主要介绍在插入数据到表中遇到键重复避免插入重复值的处理方法,主要涉及到IGNORE,ON DUPLICATE KEY UPDATE,REPLACE:接下来就分别看看这三种方式的处理办法. IGNORE 使用ignore当插入的值遇到主键(PRIMARY KEY)或者唯一键(UNIQUE KEY)重复时自动忽略重复的记录行,不影响后面的记录行的插入, 创建测试表 CREATE TABLE Tignore (ID INT NOT NULL PRIMARY KEY , NAME1 INT )d

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

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 fr

SQL Server设置主键自增长列(使用sql语句实现)_MsSql

1.新建一数据表,里面有字段id,将id设为为主键 复制代码 代码如下: create table tb(id int,constraint pkid primary key (id)) create table tb(id int primary key ) 2.新建一数据表,里面有字段id,将id设为主键且自动编号 复制代码 代码如下: create table tb(id int identity(1,1),constraint pkid primary key (id)) create

SQL Server设置主键自增长列(使用sql语句实现)

1.新建一数据表,里面有字段id,将id设为为主键 复制代码 代码如下: create table tb(id int,constraint pkid primary key (id)) create table tb(id int primary key ) 2.新建一数据表,里面有字段id,将id设为主键且自动编号 复制代码 代码如下: create table tb(id int identity(1,1),constraint pkid primary key (id)) create

mysql下创建字段并设置主键的php代码_php技巧

复制代码 代码如下: mysql_select_db("hills_database_test",$dbcon); $alterpkadd="alter table hills_testcreatetable add id int(32) not null auto_increment primary key;"; mysql_query($alterpkadd,$dbcon); mysql_close($dbcon); 嘿嘿..好容易哟..一句话搞定

hibernate annotation 怎么申明主键自动增长?sqlserver数据库

问题描述 如题 解决方案 解决方案二:<idname="id"type="long"><columnname="id"></column><generatorclass="increment"></generator></id>解决方案三:如果是采用注释方式呢?怎么注释?解决方案四:pirvateLongid;@Id@GeneratedValue(stra

如何使Oracle触发器主键自动增长

1,创建sequence: create sequence SEQ_SM_USER minvalue 1 maxvalue 999999999999999999999999999 start with 1 increment by 1 cache 20; 2,创建触发器: create or replace trigger tg_sm_user before insert on sm_user referencing old as oldval new as newval for each ro

sqlite developer 怎么设置主键Id自增长呢

问题描述 sqlite developer 怎么设置主键Id自增长呢 刚接触sqlite数据库,然后想建表操作,用的是sqlite developer,但不知该如何设置主键自增长呢 解决方案 创建表的时候给该键加上 PRIMARY KEY AUTOINCREMENT ?

MySQL中的主键以及设置其自增的用法教程_Mysql

1.声明主键的方法:您可以在创建表的时候就为表加上主键,如: CREATE TABLE tbl_name ([字段描述省略...], PRIMARY KEY(index_col_name)); 也可以更新表结构时为表加上主键,如: ALTER TABLE tbl_name ADD PRIMARY KEY (index_col_name,-); /* 创建一个qq表,将qq_id设为主键,且没有对其进行NOT NULl约束 */ create table qq( qq_id int(10), ni