sql中的有一个小问题 - .NET技术 / C#
我在数据库中设置了一个ID编号,我以为他可以自动编号,我在程序中执行时,让他插入一调记录,则ID自动加1,让他删一条语句,譬如开始有九条记录,我把第四条删掉,使他后面的,第5,6,7,8,9一次变为4,5,6,7,8,但不知在程序中如何修改,难道说一定要一个一个的用update吗
自动编号的字段你想修改还不行。
你为何要修改?
自动编号ID不能修改吧。
自动编号的话可以设施列的属性,但是不会自动下移
不能修改,主键本来就是区分唯一性的,只要能区分就行了啊!
自动增长列不能修改。
SQL code:
'设置非递增编号字段,然后为此表创建触发器来重新按顺序编号!'
use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
id int
)
go
insert into tb select 2
union all select 4
union all select 6
union all select 8
union all select 10
go
select id=(select count(1)+1 from tb where id<t.id),* from tb t
/*
id id
---------------
1 2
2 4
3 6
4 8
5 10
*/
select * from tb
/*
id
--------
2
4
6
8
10
*/
if object_id('test.dbo.t') is not null drop table t
select idd=identity(int,1,1),id into t from tb
update tb set id=t.idd
from t,tb where t.id=tb.id
select * from tb
/*
id
----
1
2
3
4
5
*/
自定义函数 把你的主键字段默认值 搞成函数 就可以了
主键id只要能标识唯一性就行了,你要那么好看干嘛,可以用存储过程和触发器来替代自增主键
{
相关问答:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jas ......
可能因为工作的原因 接触数据库这块比较少,之前都是做程序这块,数据库这块都有专门的人来做 分工都很明细 所以对数据库这一块完全不了解。前段时间 去面试了几家公司 几乎都是在数据库这块挂掉的 连个简单的SQ ......
使用ACCESS最大的隐患就是不安全。今天对ACCESS数据库设置了一个密码,必须使用密码才能打开,但是在程序中却无法连接数据库了。大家知道使用用户名和密码,如何连接ACCESS数据库?貌似ACCESS的用户名还不知道?只知 ......
我觉得mysql和sqlserver有共同的地方:
有个问题是关于表的锁问题:
进程A 进程B
select * from user where id in lock share mode(共享锁)
&nb ......