SQL Server 表可以有两个标识列吗?
我需要将一列作为主键,另一列作为自动递增订单号字段。 这可能吗?
编辑:我想我只会使用复合数作为订单号。 不管怎么说,多谢拉。
I need to have one column as the primary key and another to auto increment an order number field. Is this possible?
EDIT: I think I'll just use a composite number as the order number. Thanks anyways.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
返回
所以,不,你不能有两个标识列。 您当然可以使主键不自动递增(身份)。
编辑: msdn:CREATE TABLE (Transact-SQL) 和 创建表 (SQL Server 2000):
returns
So, no, you can't have two identity columns. You can of course make the primary key not auto increment (identity).
Edit: msdn:CREATE TABLE (Transact-SQL) and CREATE TABLE (SQL Server 2000):
则可以使用具有默认值的第二列的序列
如果您使用 SQL Server 2012 http://technet.microsoft, 。 com/en-us/library/ff878058.aspx
You can use Sequence for second column with default value IF you use SQL Server 2012
http://technet.microsoft.com/en-us/library/ff878058.aspx
添加一个标识列,然后添加一个计算列,其公式是标识列的名称
现在两者将同时递增
Add one identity column and then add a computed column whose formula is the name of the identity column
Now both will increment at the same time
不,不可能有多个身份列。
企业管理器甚至不允许您设置> 1 列作为标识。 当第二列被设置为标识时
还要注意,@@identity 返回打开连接的最后一个标识值,如果一个表可能有多个标识列,则该值将毫无意义。
No it is not possible to have more than one identity column.
The Enterprise Manager does not even allow you to set > 1 column as identity. When a second column is made identity
Also note that @@identity returns the last identity value for the open connection which would be meaningless if more than one identity column was possible for a table.
两个标识列是不可能的,但如果您接受使用唯一标识符列,那么此代码也可以完成相同的工作。 您还需要一个额外的列 - 名称列 - 用于插入值。
用法示例:
Ps: NewID() 函数创建 type 的唯一值唯一标识符。
Two identity column is not possible but if you accept to use a unique identifier column then this code does the same job as well. And also you need an extra column - Name column- for inserting values.
Example usage:
Ps: NewID() function creates a unique value of type uniqueidentifier.
主键不需要是标识列。
您不能有两个 Identity 列。
你可以通过触发器得到接近你想要的东西......
The primary key doesn't need to be an identity column.
You can't have two Identity columns.
You could get something close to what you want with a trigger...
在 sql server 中,不可能有多个列作为标识。
in sql server it's not possible to have more than one column as identity.
我刚刚创建了一个代码,允许您在同一个表上插入两个身份。 让我与您分享,以防有帮助:
谢谢,
I've just created a code that will allow you inserting two identities on the same table. let me share it with you in case it helps:
Thanks,
在大多数关系数据库(例如 SQL Server、Oracle 或 MySQL)中,一张表通常只能有一个标识列。 标识列是在向表中添加新行时自动生成唯一值的列。
表中包含多个标识列会导致关于哪一列作为主键产生歧义。
但是,您可以通过使用其他方法来实现类似的功能,例如:
In most relational databases like SQL Server, Oracle, or MySQL, a table can typically have only one identity column. An identity column is a column that automatically generates unique values when new rows are added to the table.
Having multiple identity columns within a table would create ambiguity regarding which column serves as the primary key.
However, you can achieve similar functionality by using other approaches, such as:
解决方法是创建一个递增计数器的 INSERT 触发器。
所以我有一张表,它有一个标识 col :applicationstatusid。 它也是主键。
我想自动增加另一个列:applicationnumber
所以这是我写的触发器。
A workaround would be to create an INSERT Trigger that increments a counter.
So I have a table that has one identity col : applicationstatusid. its also the primary key.
I want to auto increment another col: applicationnumber
So this is the trigger I write.