1、原理实现
待补充。
2、设置主键自动增加
MS SQLServer
在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包含两个参数,第一个参数表示起始值,第二个参数表示增量。
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数据库会自动按递增的方式为主键赋值。
图形化展现方式:

在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_customers
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');
这是我们会发现这一条记录被插入到数据库中,并且id还是自增长的。

- /**
- * 自增主键主键插入值后获取自增ID
- * @param sql
- * @return
- */
- public int insertIntoDB(String sql){
- Connection conn = null;
- Statement state = null;
- ResultSet rs = null;
- int key = -1;
- try{
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jx3", "root", "root");
- state = conn.createStatement();
- state.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
- rs = state.getGeneratedKeys();
- if(rs.next()){
- key = rs.getInt(1);
- }
- return key;
- }catch (SQLException e) {
- e.printStackTrace();
- return key;
- }finally{
- try{
- if(rs != null){
- rs.close();
- rs = null;
- }
- if(state != null){
- state.close();
- state = null;
- }
- if(conn != null){
- conn.close();
- conn = null;
- }
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
4、问题来了,加入主为1的数据删除后,如何进行插入主键为1的数据呢?
删除数据,重新添加带有显示值的数据
|
1
|
delete
[xxx].[dbo].[t_test]
where
id = 1
插入带有显示值的数据: |
|
1
|
insert
into
[guagua_new_event_system_test].[dbo].[t_test]
values
(1,
'xiaoming'
)
|
消息 8101,级别 16,状态 1,第 1 行
仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'xxx.dbo.t_test'中的标识列指定显式值。
重新设置INDENTITY_INSERT为ON时,重新重加,sql 语句为:
|
1
|
SET
IDENTITY_INSERT [xxx].[dbo].[t_test]
ON
</span>
|
|
1
|
insert
into
[xxx].[dbo].[t_test]
values
(1,
'xiaoming'
)</span>
|
仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'xxx.dbo.t_test'中的标识列指定显式值。
明明已经设置了INDENTITY_INSERT为ON,但是为什么还是没有添加进去,看了SQL Server 2008的帮助文档,才明白需要制定一一对应的列名在显示插入的时候。所以,正确的Sql 语句为:
|
1
2
3
|
SET
IDENTITY_INSERT [xxx].[dbo].[t_test]
ON
insert
into
[xxx].[dbo].[t_test](id ,
name
)
values
(1,
'xiaoming'
)
SET
IDENTITY_INSERT [xxx].[dbo].[t_test]
OFF
</span>
|
只是在显示插入值的时候的时候需要制定列名,同时打开允许显示插入的INDENTITY_INSERT,才能够插入!
主键归零; http://www.open-open.com/lib/view/open1407922298176.html
http://www.cnblogs.com/xwdreamer/archive/2012/06/08/2542277.html
http://blog.youkuaiyun.com/zhuyucheng123/article/details/17502007
http://www.2cto.com/database/201406/310640.html
SQL Server 2008 自动主键与触发器应用解析
本文介绍了如何在SQL Server 2008中设置主键自动增加,对比了MySQL和Oracle的自动增量方式,并详细讲解了使用触发器自动添加id字段的方法。此外,还探讨了Java获取数据库自增主键后的ID以及如何处理显示插入标识列的问题。
7114

被折叠的 条评论
为什么被折叠?



