MSSQL自增列重複與不連續
--> Title : 自增列重複與不連續
--> Author : wufeng4552
--> Date : 2009-11-13 08:31:12
--自增列通常在以下幾種情況而導致不連續
if object_id('tb')is not null drop table tb
go
create table tb(ID int identity(0,1),name varchar(10)unique)
--1插入失敗自動回滾
insert tb([name]) select 'A'
insert tb([name]) select 'A'
/*
錯誤原因
訊息2627,層級14,狀態1,行4
違反UNIQUE KEY 條件約束'UQ__tb__6B79F03D'。無法在物件'dbo.tb' 中插入重複的索引鍵。
陳述式已經結束。
*/
insert tb([name]) select 'B'
select * from tb
-->查詢結果
/*
(1 個資料列受到影響)
ID name
----------- ----------
0 A
2 B
(2 個資料列受到影響)
*/
if object_id('tb')is not null drop table tb
go
create table tb(ID int identity(0,1),name varchar(10)unique)
--2手動回滾
insert tb([name]) select 'B'
begin tran
insert tb([name]) select 'A'
rollback tran
insert tb([name]) select 'C'
select * from tb
-->查詢結果
/*
ID name
----------- ----------
0 B
2 C
(2 個資料列受到影響)
*/
if object_id('tb')is not null drop table tb
go
create table tb(ID int identity(0,1),name varchar(10)unique)
--3重置種子
insert tb([name]) select 'B'
dbcc checkident(tb,reseed,2)
insert tb([name]) select 'C'
insert tb([name]) select 'D'
select * from tb
-->查詢結果
/*
ID name
-----------
相关文档:
今天在聊天系统中需要系统执行一个多层嵌套查询。
一开始语句如下总出现错误:原来是在]='123') 后我多加了一个 as tb1
改为如下后,正确运行。
select * from ( select top(10) * from ( select top(100) * from (select [chatcontent].[senderid],[chatcontent].[id] ,[chatcontent].[toid] ,[chatc ......
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。
2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:
select id from t where num is null
可以在num上设置默认值0,确保表中num列没有null值,然后这样查询:
select id ......
数据类型
类型
描述
bit
整型
bit 数据类型是整型,其值只能是0、1或空值。这种数据类型用于存储只有两种可能值的数据 ,如Yes 或No、True 或Fa lse 、On 或Off
int
整型
int 数据类型可以存储从- 231( ......
select语句前加:
declare @d datetime
set @d=getdate()
并在select语句后加:
select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())
转自:动态网制作指南 www.knowsky.com
这是简易的查看执行时间的方法。
===========================================(一下内容转自:CSDN)
MSSQL Server中通过查 ......
最近有个小东西要查看mssql数据库是用php实现的,以前我用php5.2时感觉挺简单的所以想php5.3也应该很简单的
为什么要用php5.3呢因为我想用sqlite3.0的啊,因为php5.2的不支持sqlite3.0的啊,所以我特意去下了5.3了下载回来了才发现5.3里没有mssql的dll扩展了,郁闷啊,不管这么多先用起那sqlite3.0再说了
sqlite3.0的部分 ......