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


--建主键
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]

你可能感兴趣的:(sqlserver)