主键约束
这两个创建表片段之间有区别吗?一种包含 CONSTRAINT 关键字,另一种则不包含。
CREATE TABLE [dbo].[Person](
[ID] [bigint] NOT NULL,
[Name] [varchar](255) NOT NULL,
PRIMARY KEY CLUSTERED ([ID] ASC))
CREATE TABLE [dbo].[Person](
[ID] [bigint] NOT NULL,
[Name] [varchar](255) NOT NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ([ID] ASC))
我有一个数据库,其中的表以两种方式定义,我想知道是否应该对此做点什么。
Is there a difference between these two create table snippets? One includes CONSTRAINT keyword, other does not.
CREATE TABLE [dbo].[Person](
[ID] [bigint] NOT NULL,
[Name] [varchar](255) NOT NULL,
PRIMARY KEY CLUSTERED ([ID] ASC))
CREATE TABLE [dbo].[Person](
[ID] [bigint] NOT NULL,
[Name] [varchar](255) NOT NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ([ID] ASC))
I have a database with tables defined in both ways, I'm wondering if I should do something about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了约束的命名之外,没有任何区别。如果您不指定,SQL Server 将为您创建一个,但约束的名称不容易识别。
我更愿意为所有数据库对象提供良好的命名约定,而不是依赖于 SQL 引擎生成的名称。
There is no difference apart from the naming of the constraint. If you don't specify one, SQL Server will create it one for you but the name of the constraint is NOT easily recognizable.
I would prefer to give all my database objects good naming conventions instead of relying on the SQL Engine generated names.
CONSTRAINT [PK_Person]
部分是可选的。您可以在此处了解更多相关信息;来自 MSDN 页面:The
CONSTRAINT [PK_Person]
part is optional. You can read more about it here; from the MSDN page: