当前位置:网站首页>TSQL – identity column, guid, sequence
TSQL – identity column, guid, sequence
2022-07-05 11:09:00 【Full stack programmer webmaster】
–1. IDENTIY Column cannot be empty , Default value cannot be set , Cannot use after creation ALTER TABLE TableName ALTER COLUMN modify , Each table can only have one auto increment column –2. Check the current value :SELECT IDENT_CURRENT(‘TableName’), — View incremental values :SELECT IDENT_INCR(‘TableName’) — View the original seed value :SELECT IDENT_SEED(‘TableName’), Starting value , TRUNCATE TABLE Initial value after . –3. allow Explicit Insert auto increment column :SET IDENTITY_INSERT TableName ON; Set to ON after , Allow the current callback to specify a value when inserting the auto increment column , This setting only affects the current reply , And only one table can be modified at the same time in the same reply IDENTITY_INSERT attribute , When you set other tables again, you will be prompted :” surface ‘XXX1’ Of IDENTITY_INSERT Have been to ON. Can't check the table ‘XXX2’ perform SET operation .“, After explicitly inserting values for auto incrementing columns , The current value of the auto increment column will be checked or modified to be the maximum value in the whole table . –4. IDENT_CURRENT Not limited by scope and session , And limited to the specified table . SCOPE_IDENTITY and @@IDENTITY Returns the last identity value generated in any table in the current session . however ,SCOPE_IDENTITY Returns only the values inserted into the current scope ;@@IDENTITY Not limited to a specific scope [email protected]@IDENTITY You can get the trigger triggered by the current statement , Built in stored procedures and other inverted self increment . – As for the watch T1 Insert trigger pair table T2 Also insert ,@@IDENTITY obtain T2 Since the appreciation , and SCOPE_IDENTITY Get the current scope T1 Since the appreciation .
–4. If the required value is unique in all tables , have access to UNIQUEIDENTIFIER, Use NEWID() To get the unique value . –5. To avoid GUID Page fragments and page splits as clustered indexes , have access to NEWSEQUENTIALID() To get self increasing GUID: — CREATE TABLE myTable (ColumnA uniqueidentifier DEFAULT NEWSEQUENTIALID()) — If you consider security and confidentiality , Recommendation not applicable NEWSEQUENTIALID() –6. NEWSEQUENTIALID() Only for tables DEFAULT To achieve . Use NEWSEQUENTIALID() Each of the generated GUID It is unique on this computer . Only if the source computer has a network card , Use NEWSEQUENTIALID() Generated –GUID It is the only one on multiple computers –7. Sequence ,2012 New characteristics , Database level objects , Increase automatically after each acquisition . –============================================= – Create sequence 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 } ] [ ; ] –============================================= – Get sequence NEXT VALUE FOR [ database_name . ] [ schema_name . ] sequence_name [ OVER (<over_order_by_clause>) ] –============================================= – Get the sequence value of the range 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 ] [ ; ]
–============================================= – Create an initial value of 1 Increasing 1 Sequence CREATE SEQUENCE dbo.mySquence AS INT INCREMENT BY 1 START WITH 1
–============================================= – Get next value SELECT NEXT VALUE FOR dbo.mySquence –============================================= – Use the table to sort and get the auto increment sequence SELECT NEXT VALUE FOR dbo.mySquence OVER (ORDER BY C2) AS RID, C2 FROM [dbo].[TB4] –============================================= – It can be listed in DEFAULT Use sequences in ALTER TABLE Test.MyTable ADD DEFAULT N’AdvWorks_’ + CAST(NEXT VALUE FOR Test.CounterSeq AS NVARCHAR(20)) FOR IDColumn;
–============================================ – obtain 4 A continuous increasing sequence , And return the minimum 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 ;
–===================================== – View the auto increment column in the current database SELECT * FROM sys.identity_columns
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/109933.html Link to the original text :https://javaforall.cn
边栏推荐
- Wechat nucleic acid detection appointment applet system graduation design completion (6) opening defense ppt
- uniapp
- 基于OpenHarmony的智能金属探测器
- Buried point 111
- The first product of Sepp power battery was officially launched
- 磨礪·聚變|知道創宇移動端官網煥新上線,開啟數字安全之旅!
- 中职组网络安全C模块全漏洞脚本讲解包含4个漏洞的脚本
- 小红书自研KV存储架构如何实现万亿量级存储与跨云多活
- Broyage · fusion | savoir que le site officiel de chuangyu mobile end est en ligne et commencer le voyage de sécurité numérique!
- PWA (Progressive Web App)
猜你喜欢

Wechat nucleic acid detection appointment applet system graduation design completion (8) graduation design thesis template

Go-3-第一个Go程序

Ddrx addressing principle

【广告系统】增量训练 & 特征准入/特征淘汰

Wechat nucleic acid detection appointment applet system graduation design completion (7) Interim inspection report

关于vray5.2怎么关闭日志窗口

R3Live系列学习(四)R2Live源码阅读(2)

Detailed explanation of MATLAB cov function

在C# 中实现上升沿,并模仿PLC环境验证 If 语句使用上升沿和不使用上升沿的不同

第五届 Polkadot Hackathon 创业大赛全程回顾,获胜项目揭秘!
随机推荐
Use bat command to launch common browsers with one click
【广告系统】Parameter Server分布式训练
基于昇腾AI丨以萨技术推出视频图像全目标结构化解决方案,达到业界领先水平
Based on shengteng AI Aibi intelligence, we launched a digital solution for bank outlets to achieve full digital coverage of information from headquarters to outlets
go语言学习笔记-初识Go语言
Basic part - basic project analysis
跨页面通讯
中职组网络安全2021年江苏省省赛题目5套题目环境+解析全有需要的私信我
Sqlserver regularly backup database and regularly kill database deadlock solution
关于vray 5.2的使用(自研笔记)
Wechat nucleic acid detection appointment applet system graduation design completion (6) opening defense ppt
Basic testing process of CSDN Software Testing Introduction
如何让全彩LED显示屏更加节能环保
In the last process before the use of the risk control model, 80% of children's shoes are trampled here
LSTM applied to MNIST dataset classification (compared with CNN)
Advanced scaffold development
Implement the rising edge in C #, and simulate the PLC environment to verify the difference between if statement using the rising edge and not using the rising edge
Array
购买小间距LED显示屏的三个建议
Data type