文章主要描述的是SQL Server数据库中使用率比较高的identity列,我们大家都知道在SQL Server数据库中,经常会对Identity标识列进行使用,在实际操作中这种自增长的字段操作起来的确是比较方便。但它有时还会带来一些麻烦。
示例一 :当表中被删除了某些数据的时候,自增长列的编号就不再是一个连线的数列。这种时候我们可以用以下方案来解决。
允许将显式值插入表的标识列中,当设置为ON时,这时可能在INSERT操作时手工指定插入到标识列中的编号,同时必须在操作完成后,将IDENTITY_INSERT还原成OFF,否则下次插入的时候必须指定编号,那不然就无法完成INSERT操作。

示例二:当表中的记录被全部删除,但此时标识列的值越来越大的时候,如果不加以重置,它还会无休止的增长。这个时候我们就要用到:
将把指定表的种子值强制重设为1。然而,你可能不想将种子重设为1,在这种情况下,你可以用你想用的种子值替代第三个参数。有时候你可能想知道当前的种子,而不是想重设种子,这时你就要用到NORESEED,而不用再去顾忌第三个参数。
【编辑推荐】
如何使用Microsoft SQL Server Management Studio来写SQL?
首先你需要打开它吧?它是SQL的DBMS图形化操作系统。 如果是想用SQL语言来操作的话呢,那就在建立好连接的基础上,然后点击窗体上面的“新建查询”按钮,然后在窗体右边会出现一块编辑区,你在编辑区域里面书写相应的SQL语句就可以。 下面是我写的一个例子,你可以参考下: 现在是建立数据库的SQL语句: use master create, size=5mb,filegrowth=15%, filename=E:\B\ ) log on (,size=1mb,maxsize=6mb,filename=E:\B\stuDB_ ) use stuDB go 下面呢,是在新建好的数据库中新建数据表: create table stuInfo ( stuName varchar(32) not null, stuNo varchar(16) not null primary key , stuSex varchar(8) not null default(男), stuAge int not null, stuSeat int not null identity(1,1), stuAddress ntext default(地址不详) ) 以下是为新建好的数据表添加约束: alter table stuInfo add constraint ck_stuNo check (stuNo like s253xx) alter table stuInfo add constraint ck_stuAge check (stuAge>=15 and stuAge<=50) 下面是给数据表添加数据: insert into stuInfo(stuName,stuNo,stuSex,stuAge,stuAddress) values(张秋丽,s,default,18,北京海淀) insert into stuInfo(stuName,stuNo,stuSex,stuAge,stuAddress) values(李斯文,s,女,22,河南洛阳) insert into stuInfo(stuName,stuNo,stuSex,stuAge,stuAddress) values(李文才,s,default,31,) insert into stuInfo(stuName,stuNo,stuSex,stuAge,stuAddress) values(欧阳骏雄,s,default,28,新疆克拉玛依) 下面是查询表中的所有数据 select *from stuInfo select stuName as 姓名,stuNo as 学号,stuSex as 性别,stuAge as 年龄,stuSeat as 座号,stuAddress as 家庭住址 from stuInfo 同样,也是新建数据表的语句块: create table stuMarks ( examNo varchar(32) not null primary key, stuNo varchar(16) not null, writtenExam int default(0), labExam int default(0) ) 添加约束: alter table stuMarks add constraint ck_examNo check (examNo like Exxxx) alter table stuMarks add constraint fk_stuNo foreign key (stuNo) references stuInfo(stuNo) alter table stuMarks add constraint ck_writtenExam check (writtenExam>0 and writtenExam<=100) alter table stuMarks add constraint ck_labExam check (labExam>0 and labExam<=100) 添加数据到数据表: insert into stuMarks(examNo,stuNo,writtenExam,labExam) values(E, s,80,58) insert into stuMarks(examNo,stuNo,writtenExam,labExam) values(E, s,50,default) insert into stuMarks(examNo,stuNo,writtenExam,labExam) values(E, s,97,82) 下面就是一些简单的查询了: select examNo as 考号,stuNo as 学号,writtenExam as 笔试成绩,labExam as 试验成绩 from stuMarks select stuName as 姓名,stuNo as 学号,stuSex as 性别,stuAge as 年龄,stuSeat as 座号,stuAddress as 家庭住址 from stuInfo select *from stuInfo where stuSex=男 order by stuSeat desc select * from stuMarks where writtenExam>75 and writtenExam<=100 order by stuNo select* from stuMarks where writtenExam <>0 and labExam <>0 select stuName,writtenExam,labExam into lingshi from stuInfo,stuMarks 以下就是SQL的聚合函数部分了: select avg(writtenExam) as 笔试平均成绩 from stuMarks select avg(labExam) as 机试平均成绩 from stuMarks select count(*) as 考试人数 from stuMarks where writtenExam>60 select count(*) as 没有通过考试的人数 from stuMarks where writtenExam<60
SQL中identity后括号中的值是什么意思?
identity(m,n),表示的是初始值,n表示的是每次自动增加的值。 如果m和n的值都没有指定,默认为(1,1)。 要么同时指定m和n的值,要么m和n都不指定,不能只写其中一个值,不然会出错。 一、不指定m和n的值时,于 SQL Server 的语法举例:结果展示,按照默认(1,1)开始排列:二、指定m和n的值结果展示,按照发开需求排列:扩展资料:向identity字段插入数据。 【语法】set identity_insert 表名 on;insert into 表名(列名1,列名2,列名3,列名4) values (数据1,数据2,数据3,数据4);set identity_insert 表名 off;【实例代码】结果展示:注意:插入数据时必须得指定identity修饰的字段的名字。
sql常用语句写法
1、说明:创建数据库
CREATE DATABASE database-name
2、说明:删除数据库
drop database dbname
3、说明:备份sql server
--- 创建 备份数据的 device
USE masterEXEC sp_addumpdevice ’disk’, ’testBack’, ’c:mssql7backupMyNwind_’
--- 开始 备份
BACKUP DATABASE pubs TO testBack
4、说明:创建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根据已有的表创建新表:
A:create table tab_new like tab_old (使用旧表创建新表)B:create table tab_new as select col1,col2… from tab_old definition only
5、说明:
删除新表:drop table tabname
6、说明:
增加一个列:Alter table tabname add column col type
注:列增加后将不能删除。 DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。
7、说明:
添加主键:Alter table tabname add primary key(col)
说明:
删除主键:Alter table tabname drop primary key(col)
8、说明:
创建索引:create [unique] index idxname on tabname(col….)
删除索引:drop index idxname
注:索引是不可更改的,想更改必须删除重新建。
9、说明:
创建视图:create view viewname as select statement
删除视图:drop view viewname
10、说明:几个简单的基本的sql语句
选择:select * from table1 where 范围
插入:insert into table1(field1,field2) values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!
排序:select * from table1 order by field1,field2 [desc]
总数:select count * as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1
发表评论