易截截图软件、单文件、免安装、纯绿色、仅160KB

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


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://blog.csdn.net/andyelvis/archive/2008/05/14/2446865.aspx


相关文档:

oracle distinct用法

distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用 它来返回不重复记录的条数,而不是用它来返回不重记录的所有值。其原因是distinct只有用二重循环查询来解决,而这样对于一个数据量非常大的站来说,无疑是会直接影响到效率的。
下面先来看看例子:
table表
字段1     字段2
&nbs ......

oracle优化2

--定义数据库连接分发器
alter system set dispatchers='(PROTOCOL=TCP)(SERVICE=imapdb)(DISPATCHERS=5)(PROTOCOL=IPC)(DISPATCHERS=5)' scope=spfile;
--定义共享服务数
alter system set shared_servers=20 scope=spfile;
--定义共享服务的session数
alter system set shared_server_sessions=1024 scope=spfile;
......

利用oracle快照dblink解决数据库表同步问题

本实例已完全通过测试,单向,双向同步都可使用.
--名词说明:源——被同步的数据库
            目的——要同步到的数据库
前6步必须执行,第6以后是一些辅助信息.
--1、在目的数据库上,创建dblink
drop public database link dblink ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号