标签:
Column 两个特殊的属性 $ROWGUID,$IDENTITY, $ROWGUID用于引用被属性 RowGUIDCol 标识的UniqueIdentifier类型的 column,$IDENTITY 用于引用被属性Identity 标识的整数类型(int,bigint,tinyint,smallint,decimal(p,0),numeric(p,0))类型的 column。
在每个table中,只能有个一Column被标识为RowGUIDCol,只能有一个Column被标识为Identity.
一,示例
CREATE TABLE dbo.myTable_RowGUIDCol ( ColumnA uniqueidentifier ROWGUIDCOL not null constraint DF__myTable_RowGUIDCol_ColumnA DEFAULT NewID(), ColumnB int identity, columnC varchar(10) ) go select $ROWGUID,$IDENTITY from dbo.myTable_RowGUIDCol
查看查询的结果
二,属性IDENTITY
Indicates that the new column is an identity column. When a new row is added to the table, the Database Engine provides a unique, incremental value for the column. Identity columns are typically used with PRIMARY KEY constraints to serve as the unique row identifier for the table. The IDENTITY property can be assigned to tinyint, smallint, int, bigint, decimal(p,0), or numeric(p,0) columns. Only one identity column can be created per table. Bound defaults and DEFAULT constraints cannot be used with an identity column. Both the seed and increment or neither must be specified. If neither is specified, the default is (1,1).
seed
Is the value used for the very first row loaded into the table.
increment
Is the incremental value added to the identity value of the previous row loaded.
NOT FOR REPLICATION
In the CREATE TABLE statement, the NOT FOR REPLICATION clause can be specified for the IDENTITY property, FOREIGN KEY constraints, and CHECK constraints. If this clause is specified for the IDENTITY property, values are not incremented in identity columns when replication agents perform inserts. If this clause is specified for a constraint, the constraint is not enforced when replication agents perform insert, update, or delete operations.
三,属性 ROWGUIDCOL
Indicates that the new column is a row GUID column. Only one uniqueidentifier column per table can be designated as the ROWGUIDCOL column. Applying the ROWGUIDCOL property enables the column to be referenced using $ROWGUID. The ROWGUIDCOL property can be assigned only to a uniqueidentifier column. User-defined data type columns cannot be designated with ROWGUIDCOL.
The ROWGUIDCOL property does not enforce uniqueness of the values stored in the column. ROWGUIDCOL also does not automatically generate values for new rows inserted into the table. To generate unique values for each column, either use the NEWID or NEWSEQUENTIALID function on INSERT statements or use these functions as the default for the column.
四,向Identity column插入显式值
Allows explicit values to be inserted into the identity column of a table.
SET IDENTITY_INSERT schema_name.table_name ON | OFF
At any time, only one table in a session can have the IDENTITY_INSERT property set to ON. If a table already has this property set to ON, and a SET IDENTITY_INSERT ON statement is issued for another table, SQL Server returns an error message that states SET IDENTITY_INSERT is already ON and reports the table it is set ON for.
If the value inserted is larger than the current identity value for the table, SQL Server automatically uses the new inserted value as the current identity value.
The setting of SET IDENTITY_INSERT is set at execute or run time and not at parse time.
参考文档:
https://msdn.microsoft.com/en-us/library/ms186775(v=sql.110).aspx
Column属性:RowGUIDCol 和 Identity
标签:
原文地址:http://www.cnblogs.com/ljhdo/p/5133262.html