当前位置:网站首页>TSQL–标示列、GUID 、序列
TSQL–标示列、GUID 、序列
2022-07-05 10:13:00 【全栈程序员站长】
–1. IDENTIY 列不能为空,不能设默认值,创建后不能使用ALTER TABLE TableName ALTER COLUMN修改,每张表只能有一个自增列 –2. 查看当前值:SELECT IDENT_CURRENT(‘TableName’), — 查看增量值:SELECT IDENT_INCR(‘TableName’) — 查看原始种子值:SELECT IDENT_SEED(‘TableName’),起始值, TRUNCATE TABLE 后的初始值。 –3. 允许 显式 插入自增列:SET IDENTITY_INSERT TableName ON; 设置为ON后,允许当前回话对自增列插入时指定值,该设置只影响当前回话,并且同一回话中只允许同时修改一张表的IDENTITY_INSERT 属性,对其他表再次设置时会提示:”表 ‘XXX1’ 的 IDENTITY_INSERT 已经为 ON。无法对表 ‘XXX2’ 执行 SET 操作。“,在对自增列显式插入值后,会检查或修改自增列的当前值为整表中最大值。 –4. IDENT_CURRENT 不受作用域和会话的限制,而受限于指定的表。 SCOPE_IDENTITY 和 @@IDENTITY 返回在当前会话中的任何表内所生成的最后一个标识值。但是,SCOPE_IDENTITY 只返回插入到当前作用域中的值;@@IDENTITY 不受限于特定的作用域。@@IDENTITY能获取到由当前语句引发的触发器,内置存储过程等倒置的自增值。 –如对表T1插入引发触发器对表T2也进行插入,@@IDENTITY得到T2的自增值,而SCOPE_IDENTITY获取当前作用域T1的自增值。
–4. 如果要求值在所有表中唯一,可以使用UNIQUEIDENTIFIER,使用NEWID()来获得唯一值。 –5. 为避免GUID作为聚簇索引照成的页面碎片和页拆分,可以使用NEWSEQUENTIALID()来获取自增的GUID: — CREATE TABLE myTable (ColumnA uniqueidentifier DEFAULT NEWSEQUENTIALID()) — 如果考虑安全性和保密性,建议不适用NEWSEQUENTIALID() –6. NEWSEQUENTIALID()只能针对表使用DEFAULT方式来实现。使用 NEWSEQUENTIALID() 生成的每个 GUID 在该计算机上都是唯一的。仅当源计算机具有网卡时,使用 NEWSEQUENTIALID() 生成的 –GUID 在多台计算机上才是唯一的 –7. 序列,2012新特性,数据库级别对象,每次获取后自增。 –============================================= –创建序列 CREATE SEQUENCE [schema_name . ] sequence_name [ AS [ built_in_integer_type | user-defined_integer_type ] ] [ START WITH <constant> ] [ INCREMENT BY <constant> ] [ { MINVALUE [ <constant> ] } | { NO MINVALUE } ] [ { MAXVALUE [ <constant> ] } | { NO MAXVALUE } ] [ CYCLE | { NO CYCLE } ] [ { CACHE [ <constant> ] } | { NO CACHE } ] [ ; ] –============================================= –获取序列 NEXT VALUE FOR [ database_name . ] [ schema_name . ] sequence_name [ OVER (<over_order_by_clause>) ] –============================================= –获取范围的序列值 sp_sequence_get_range [ @sequence_name = ] N'<sequence>’ , [ @range_size = ] range_size , [ @range_first_value = ] range_first_value OUTPUT [, [ @range_last_value = ] range_last_value OUTPUT ] [, [ @range_cycle_count = ] range_cycle_count OUTPUT ] [, [ @sequence_increment = ] sequence_increment OUTPUT ] [, [ @sequence_min_value = ] sequence_min_value OUTPUT ] [, [ @sequence_max_value = ] sequence_max_value OUTPUT ] [ ; ]
–============================================= –创建初始值为1递增1的序列 CREATE SEQUENCE dbo.mySquence AS INT INCREMENT BY 1 START WITH 1
–============================================= –获取下个值 SELECT NEXT VALUE FOR dbo.mySquence –============================================= –使用表排序后获取自增序列 SELECT NEXT VALUE FOR dbo.mySquence OVER (ORDER BY C2) AS RID, C2 FROM [dbo].[TB4] –============================================= –可以在列的DEFAULT中使用序列 ALTER TABLE Test.MyTable ADD DEFAULT N’AdvWorks_’ + CAST(NEXT VALUE FOR Test.CounterSeq AS NVARCHAR(20)) FOR IDColumn;
–============================================ –获取4个连续的递增序列,并返回最小值 DECLARE @range_first_value sql_variant , @range_first_value_output sql_variant ;
EXEC sp_sequence_get_range @sequence_name = N’dbo.mySquence’ , @range_size = 4 , @range_first_value = @range_first_value_output OUTPUT ;
SELECT @range_first_value_output AS FirstNumber ;
–===================================== –查看当前数据库中的自增列 SELECT * FROM sys.identity_columns
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/109933.html原文链接:https://javaforall.cn
边栏推荐
- flink cdc不能监听mysql日志,大家遇到过这个问题吧?
- @JsonAdapter注解使用
- @Serializedname annotation use
- [observation] with the rise of the "independent station" model of cross-border e-commerce, how to seize the next dividend explosion era?
- 【观察】跨境电商“独立站”模式崛起,如何抓住下一个红利爆发时代?
- WorkManager学习一
- Detailed explanation of the use of staticlayout
- How can PostgreSQL CDC set a separate incremental mode, debezium snapshot. mo
- 微信小程序触底加载与下拉刷新的实现
- Fluent generates icon prompt logo widget
猜你喜欢
Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease
学习笔记6--卫星定位技术(上)
How does redis implement multiple zones?
[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
Have you learned to make money in Dingding, enterprise micro and Feishu?
驱动制造业产业升级新思路的领域知识网络,什么来头?
Learning note 4 -- Key Technologies of high-precision map (Part 2)
How to plan the career of a programmer?
pytorch输出tensor张量时有省略号的解决方案(将tensor完整输出)
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
随机推荐
Glide Mastery
程序员搞开源,读什么书最合适?
Learning Note 6 - satellite positioning technology (Part 1)
[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
Design and Simulation of fuzzy PID control system for liquid level of double tank (matlab/simulink)
“军备竞赛”时期的对比学习
Secteur non technique, comment participer à devops?
Who is the "conscience" domestic brand?
Constraintlayout officially provides rounded imagefilterview
The Alipay in place function can't be found, and the Alipay in place function is offline
TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
TypeError: Cannot read properties of undefined (reading ‘cancelToken‘)
WorkManager學習一
如何写出高质量的代码?
Swift uses userdefaults and codable to save an array of class objects or structure instances
【Vite】1371- 手把手开发 Vite 插件
《微信小程序-基础篇》小程序中的事件与冒泡
uniapp + uniCloud+unipay 实现微信小程序支付功能
Completion report of communication software development and Application
Applet image height adaptation and setting text line height