帮助中心 >  技术知识库 >  数据库 >  相关技术支持 >  sqlserver,建主键,索引,唯一键语句

sqlserver,建主键,索引,唯一键语句

2017-07-29 17:55:28 8450

--建主键
ALTER TABLE [dbo].[t_test] ADD  CONSTRAINT [PK_t_test] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]

--删主键
IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[t_test]') AND name = N'PK_t_test')
ALTER TABLE [dbo].[t_test] DROP CONSTRAINT [PK_t_test]

--建索引
CREATE NONCLUSTERED INDEX [t_test_index] ON [dbo].[t_test] 
(
    [v1] ASC,
    [v2] ASC,
    [v3] ASC,
    [v4] ASC
)WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]

--删除索引
DROP INDEX [t_test_index] ON [dbo].[t_test] WITH ( ONLINE = OFF )

--建唯一键
ALTER TABLE [dbo].[t_test] ADD  CONSTRAINT [t_test_unique] UNIQUE NONCLUSTERED 
(
    [v1] ASC,
    [v2] ASC,
    [v3] ASC
)WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]

--删除唯一键
IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[t_test]') AND name = N't_test_unique')
ALTER TABLE [dbo].[t_test] DROP CONSTRAINT [t_test_unique]

提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: