从SQL Server 2012开始有了Sequence,简单用列如下: CREATESEQUENCETestSeqSTARTWITH1INCREMENTBY1;SELECTNEXTVALUEFORTestSeqASNextValue; 在这之前,表中生成序列号大多都是借助IDENTITY列属性,当然也有一些时候,是在自定义表中,自己维护序列号。 一.
从SQL Server 2012开始有了Sequence,简单用列如下:
CREATE SEQUENCE TestSeq START WITH 1 INCREMENT BY 1; SELECT NEXT VALUE FOR TestSeq AS NextValue;
在这之前,表中生成序列号大多都是借助IDENTITY列属性,当然也有一些时候,是在自定义表中,自己维护序列号。
一. 创建IDENTITY列
if OBJECT_ID('test','U') is not null drop table test GO create table test(id int identity, c1 char(1)) insert test values('a'); insert test values('b'); select * from test
1. 没有指定IDENTITY(seed ,increment),默认就是 IDENTITY(1, 1),效果同如下语句
create table test(id int identity(1,1), c1 char(1))
2. 通过函数或者系统视图,都可以查看是否为IDENTITY列
SELECT COLUMNPROPERTY(OBJECT_ID('test'),'id','IsIdentity') AS is_identity select object_name(object_id) as table_name, is_identity,* from sys.columns where object_id=object_id('test') --and is_identity=1
3. 重置IDENTITY列的初始值,通常在数据删除/归档后进行
DELETE test DBCC CHECKIDENT('test', RESEED, 1) DBCC CHECKIDENT('test', NORESEED) --TRUNCATE表后会自动重置IDENTITY列 TRUNCATE TABLE test DBCC CHECKIDENT('test', NORESEED)
二. 获取IDENTITY列值
插入了数据,有时还需要获取刚才生成的序列值另作他用,返回给前端也好,或者插入其他将来需要关联的表。
记得曾经有个面试题:假设当前表IDENTITY列最大值为N,在存储过程中,对这个表插入1行数据,获取到的IDENTITY列值有时小于或者大于N+1,可能是什么原因?
获取IDENTITY列值有三种方式:
(1) IDENT_CURRENT( 'table_name' ) 返回为任何会话和任何作用域中的特定表最后生成的标识值。
(2) @@IDENTITY 返回为当前会话的所有作用域中的任何表最后生成的标识值。
(3) SCOPE_IDENTITY() 返回为当前会话和当前作用域中的任何表最后生成的标识值。
IDENT_CURRENT( 'table_name' ) 针对特定表,是全局的。@@IDENTITY和SCOPE_IDENTITY()针对所有表,区别在于作用域,也就是上下文:
(1) 如果当前INSERT语句上有函数,触发器等(不同作用域的)对象返回的IDENTITY值,那么@@IDENTITY会取所有表上的最后1个,而不是当前表上的;
(2) SCOPE_IDENTITY()会取当前作用域所有表上最后1个IDENTITY值,被调用的函数,触发器已经超出了作用域/上下文。所以在使用INSERT后,接着使用SCOPE_IDENTITY()获取IDENTITY列值,就不会有问题了:
insert test values('z'); select SCOPE_IDENTITY() as curr_value
一个GO语句/批处理,也是一个上下文的分界点,但是SQL语句是顺序执行的,所以一个会话里,只要在INSERT之后用SCOPE_IDENTITY()来获取IDENTITY值是没问题的。
三. 修改IDENTITY列值/属性
1. 对已存在的列增加/删除IDENTITY属性
if OBJECT_ID('t_id') is not null drop table t_id GO create table t_id(id int,c1 char(1)) insert into t_id select 1,'a' union all select 2,'b' alter table t_id alter column id int identity(1,2) /* Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'identity'. */
直接修改列属性会报错,IDENTITY属性只能伴随着列增加/删除。
(1) 利用中间表
在SSMS界面上设计表(SSMS/Tables/Design),可以直接增加/删除列上的IDENTITY属性,如果生成脚本看看的话(右击编辑框/工具栏/菜单栏),可以发现SSMS是利用了中间表,并非在原表直接修改属性。
表上有约束,索引等对象时,脚本会更加繁杂些。示例如下图:
如果出现如下错误:
Saving changes is not permitted. The changes that you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to be re-created.
是因为SSMS里有个选项没设置,SQL Server认为有删除/重建表的脚本不安全,所以默认关闭了,需要手动开启一下,去掉那个勾:
对表上已存在列添加IDENTITY属性,生成的脚本如下:
BEGIN TRANSACTION SET QUOTED_IDENTIFIER ON SET ARITHABORT ON SET NUMERIC_ROUNDABORT OFF SET CONCAT_NULL_YIELDS_NULL ON SET ANSI_NULLS ON SET ANSI_PADDING ON SET ANSI_WARNINGS ON COMMIT BEGIN TRANSACTION GO CREATE TABLE dbo.Tmp_t_id ( id int NOT NULL IDENTITY (1, 1), c1 char(1) NULL ) ON [PRIMARY] GO ALTER TABLE dbo.Tmp_t_id SET (LOCK_ESCALATION = TABLE) GO SET IDENTITY_INSERT dbo.Tmp_t_id ON GO IF EXISTS(SELECT * FROM dbo.t_id) EXEC('INSERT INTO dbo.Tmp_t_id (id, c1) SELECT id, c1 FROM dbo.t_id WITH (HOLDLOCK TABLOCKX)') GO SET IDENTITY_INSERT dbo.Tmp_t_id OFF GO DROP TABLE dbo.t_id GO EXECUTE sp_rename N'dbo.Tmp_t_id', N't_id', 'OBJECT' GO COMMIT
对表上已存在列删除IDENTITY属性,生成的脚本如下:
BEGIN TRANSACTION SET QUOTED_IDENTIFIER ON SET ARITHABORT ON SET NUMERIC_ROUNDABORT OFF SET CONCAT_NULL_YIELDS_NULL ON SET ANSI_NULLS ON SET ANSI_PADDING ON SET ANSI_WARNINGS ON COMMIT BEGIN TRANSACTION GO CREATE TABLE dbo.Tmp_t_id ( id int NOT NULL, c1 char(1) NULL ) ON [PRIMARY] GO ALTER TABLE dbo.Tmp_t_id SET (LOCK_ESCALATION = TABLE) GO IF EXISTS(SELECT * FROM dbo.t_id) EXEC('INSERT INTO dbo.Tmp_t_id (id, c1) SELECT id, c1 FROM dbo.t_id WITH (HOLDLOCK TABLOCKX)') GO DROP TABLE dbo.t_id GO EXECUTE sp_rename N'dbo.Tmp_t_id', N't_id', 'OBJECT' GO COMMIT
(2) 利用中间列
对表上已存在列删除IDENTITY属性
if OBJECT_ID('t_id') is not null drop table t_id GO create table t_id(id int identity(1,1),c1 char(1)) insert into t_id select 'a' union all select 'b' select * from t_id SELECT COLUMNPROPERTY(OBJECT_ID('t_id'),'id','IsIdentity') --在表上新增一个列,把IDENTITY列值复制过去 alter table t_id add id_new int GO update t_id set id_new = id --删除原来的列,并重命名新增列 alter table t_id drop column id exec sp_rename 't_id.id_new','id' select * from t_id SELECT COLUMNPROPERTY(OBJECT_ID('t_id'),'id','IsIdentity')
对表上已存在列添加IDENTITY属性,用中间列的方式不太可行,因为IDENTITY列不接受UPDATE,新增的IDENTITY列无法直接复制原id的值,还得借助中间表,但如果不需要原来id的值,那么可以:
if OBJECT_ID('t_id') is not null drop table t_id GO create table t_id(id int,c1 char(1)) insert into t_id select 1,'a' union all select 3,'b' select * from t_id SELECT COLUMNPROPERTY(OBJECT_ID('t_id'),'id','IsIdentity') --在表上新增一个IDENTITY列,不复制原来的ID值 alter table t_id add id_new int identity(1,1) not null --删除原来的列,并重命名新增列 alter table t_id drop column id exec sp_rename 't_id.id_new','id' select * from t_id SELECT COLUMNPROPERTY(OBJECT_ID('t_id'),'id','IsIdentity')
2. 在IDENTITY列上做增删改操作(DML)
(1) 删除操作没有问题,直接DELETE即可
delete test where id = 2
(2) 如果要显式INSERT某个值,需要开启IDENTITY_INSERT这个SESSION级的选项
set IDENTITY_INSERT test on; insert test(id,c1) values(3,'c'); set IDENTITY_INSERT test off; select * from test
(3) 如果要UPDATE IDENTITY列值,无论是否开启IDENTITY_INSERT这个选项都无法更新
set IDENTITY_INSERT test on; update test set id = 10 where id = 1 set IDENTITY_INSERT test off; /* Msg 8102, Level 16, State 1, Line 1 Cannot update identity column 'id'. */
非要修改的话,就得借助中间表,在不含IDENTITY属性的中间表里做完UPDATE,然后再把数据导回来。中间表可参考上面的脚本。
3. IDENTITY列属性复制
(1) 直接从单表SELECT INTO table_name,原表其他约束,索引等等都不会被复制,但是IDENTITY属性会被复制。
select * into test2 from test select * from test2 select columnproperty(OBJECT_ID('test'),'id','IsIdentity') select columnproperty(OBJECT_ID('test2'),'id','IsIdentity')
(2) 如果有IDENTITY属性的表和其他表JOIN,那么IDENTITY属性不会被复制。
select a.* into test3 from test a inner join sys.objects b on a.id = b.object_id select * from test3 select columnproperty(OBJECT_ID('test3'),'id','IsIdentity')
假如复制表时,不想要IDENTITY属性,正好可以利用一下这个特点,如下:
select a.* into test4 from test a inner join sys.objects b on 1=2
(3) 如果用SELECT INTO table_name导数据时,FROM子句有多表关联,且想要保留IDENTITY属性,这时可以用INSERT,并考虑使用TABLOCK提示
if OBJECT_ID('test5','U') is not null drop table test5 GO create table test5(id int identity, c1 char(1)) select * from test5 GO set IDENTITY_INSERT test5 on; insert into test5 WITH(TABLOCK) (id,c1) select a.* from test a inner join test2 b on a.id = b.id set IDENTITY_INSERT test5 off; select * from test5 select columnproperty(OBJECT_ID('test5'),'id','IsIdentity')
这里使用了WITH(TABLOCK)选项,在SIMPLE或者BULK_LOGGED恢复模式下,SELECT…INTO table_name和INSERT INTO table_name WITH(TABLOCK)都能最小化日志。
4. 借助SWITCH来处理IDENTITY属性,推荐
同样也是利用中间表,上面的几个列子都使用了INSERT,这里使用SWITCH,不再有数据倒来倒去的开销,需要SQL Server 2008及以上版本,能比较有效地同时解决上面的3个问题:
(1) 不能直接对表上现有列增加/删除IDENTITY属性;
(2) 不能直接更新IDENTITY列;
(3) 复制表时,有选择的复制IDENTITY列属性(多表关联,对关联后的表做SWITCH以实现);
CREATE TABLE Temp1 ( ID INT IDENTITY(1,1) PRIMARY KEY, X VARCHAR(10) ) INSERT INTO Temp1 OUTPUT INSERTED.* SELECT 'Foo' UNION ALL SELECT 'Bar' UNION ALL SELECT 'Baz' CREATE TABLE Temp2 ( ID INT PRIMARY KEY, X VARCHAR(10) ) ALTER TABLE Temp1 SWITCH TO Temp2; SELECT COLUMNPROPERTY(OBJECT_ID('Temp1'),'id','IsIdentity') SELECT COLUMNPROPERTY(OBJECT_ID('Temp2'),'id','IsIdentity') INSERT INTO Temp2 OUTPUT INSERTED.* SELECT 10,'Foo' UNION ALL SELECT 20,'Bar' UNION ALL SELECT 5, 'Baz' UPDATE Temp2 SET ID = ID + 1; ALTER TABLE Temp2 SWITCH TO Temp1; SELECT * FROM Temp2 SELECT * FROM Temp1
另外,从SQL Server 2012开始,如果开发时使用了SEQUENCE,这些IDENTITY列的限制就都不会存在了。
四. IDENTITY函数
这是一个函数,使用时和IDENTITY属性的格式很相似,不过两者没什么关系,纯粹因为名字相同,顺便提一下。
select IDENTITY(int,1,1) as id into #t from sysobjects select cast(IDENTITY(int,1,1) as varchar(1000)) as id into #t2 from sysobjects -- can not use expression with identity function directly
IDENTITY函数限制比较多,只能用在SELECT INTO语句里,不能结合表达式使用,而且有了ROW_NUMBER(),IDENTITY函数就更显得不好用了。

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools