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 9i的环境下开发了occi的应用,结果放到10g(10.2.0.3)服务器上运行不了(只装了windows 版的10g服务端),只好把所有依赖的9i的dll拷过去,结果连接occi报错:
Error while trying to retrieve text for error ORA-32101
到http://www.oracle.com/technology/global/cn/software/tech/oci/instantclient/index.ht ......
登录oracle数据库时常用的操作命令整理
1、su – oracle 不是必需,适合于没有DBA密码时使用,可以不用密码来进入sqlplus界面。
2、sqlplus /nolog 或sqlplus system/manager 或./sqlplus system/manager@ora9i
;
3、SQL>connect / as sysdba ;(as sysoper)或
connect internal/oracle AS SYSDBA ;(scott/ti ......
SQL Server里的 ISNULL 与 Oracle 中的 NULLIF不同:
SQL Server 中有两个参数,语法:
ISNULL(check_expression, replacement_value)
check_expression 与 replacement_value 数据类型必须一致
如果 check_expression 为 NULL,则返回 replacement_v ......
很多朋友在Java开发中,使用Oracle数据库的时候,经常会碰到有ORA-01000: maximum open cursors exceeded.的错误。
实际上,这个错误的原因,主要还是代码问题引起的。
ora-01000: maximum open cursors exceeded.
表示已经达到一个进程打开的最大游标数。
这样的错误很容易出现在Java代码中的主要原因是:Java代码在执 ......
作者: 肖建彬
| 可以转载, 转载时务必以超链接形式标明文章原始出处
和作者信息及版权声明
网址:http://www.xiaojb.com/archives/it/mysqlroot.shtml
Method 1:
在/usr/local/mysql/bin/下:
./mysqladmin -u root password ‘new_password’
一般安装时用此方法设置。
Method 2:
在mysql状 ......