SqlServer 使用 MySql 创建表,如 auto_increment 主键
我想在 SqlServer 中创建一个表,该表将在插入时添加自动递增的主键。 这应该是一个类似于 MySql auto_increment 功能的自动增量 id。 (如下)
create table foo
(
user_id int not null auto_increment,
name varchar(50)
)
有没有一种方法可以在不创建插入触发器的情况下执行此操作?
I want to make a table in SqlServer that will add, on insert, a auto incremented primary key. This should be an autoincremented id similar to MySql auto_increment functionality. (Below)
create table foo
(
user_id int not null auto_increment,
name varchar(50)
)
Is there a way of doing this with out creating an insert trigger?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
像这样
Like this
OP 请求一个自动递增的主键。 IDENTITY 关键字本身并不使列成为主键。
OP requested an auto incremented primary key. The IDENTITY keyword does not, by itself, make a column be the primary key.
他们已经回答了您的问题,但我想为刚开始使用身份列的人添加一点建议。 有时,您必须返回刚刚插入的标识的值,以便可以插入到相关表中。 许多来源会告诉您使用@@identity 来获取此值。 如果您想保持数据完整性,在任何情况下都不应使用@@identity。 如果添加其中一个触发器以插入到另一个表,它将给出在触发器中创建的标识。 由于您无法保证@@identity 的值始终正确,因此最好不要使用@@identity。 请使用scope_identity() 来获取该值。 我知道这有点偏离主题,但它对于您理解如何在 SQL Server 中使用身份非常重要。 相信我,您不想解决向其提供错误身份值的相关记录的问题。 在被发现之前,这种情况可能会悄悄地发生几个月的问题,并且之后几乎不可能修复数据。
They have answered your question but I want to add one bit of advice for someone new to using identity columns. There are times when you have to return the value of the identity just inserted so that you can insert into a related table. Many sources will tell you to use @@identity to get this value. Under no circumstances should you ever use @@identity if you want to mantain data integrity. It will give the identity created in a trigger if one of them is added to insert to another table. Since you cannot guarantee the value of @@identity will always be correct, it is best to never use @@identity. Use scope_identity() to get this value instead. I know this is slightly off topic, but it is important to your understanding of how to use identity with SQL Server. And trust me, you did not want to be fixing a problem of the related records having the wrong identity value fed to them. This is something that can quietly go wrong for months before it is dicovered and is almost impossible to fix the data afterward.
正如其他人提到的:将 IDENTITY 属性添加到列中,并使其成为主键。
然而,MSSQL 的 IDENTITY 和 MySQL 的 AUTO_INCRMENT 之间存在差异:
约束(通常以
主键)被定义为
AUTO_INCRMENT 列。
MSSQL 没有这样的要求。
MSSQL 阻止您手动将值插入 IDENTITY
柱子; 如果需要,您可以覆盖
通过发出“SET
IDENTITY_INSERT 表名 ON"
插入之前的命令。
MSSQL 拒绝更新 AUTO_INCRMENT 列中的值
身份列。
As others have mentioned: add the IDENTITY attribute to the column, and make it a primary key.
There are, however, differences between MSSQL's IDENTITY and MySQL's AUTO_INCREMENT:
constraint (often in the form of a
primary key) be defined for the
AUTO_INCREMENT column.
MSSQL doesn't have such a requirement.
MSSQL prevents you from manually inserting a value into an IDENTITY
column; if needed, you can override
this by issuing a "SET
IDENTITY_INSERT tablename ON"
command before the insert.
MSSQL refuses to update values in an
IDENTITY column.
只需将该字段设置为身份字段即可。
Just set the field as an identity field.
声明该字段为identity
declare the field to be identity
如上所述,使用 IDENTITY 字段。
As advised above, use an IDENTITY field.
正如其他人所说,只需设置“身份”选项即可。
As others have said, just set the Identity option.