SQL server 表動作陳述式(原創)

來源:互聯網
上載者:User

標籤:des   io   ar   sp   for   on   art   cti   bs   

CREATE TABLE [dbo].[test] (
[id11] int NOT NULL ,
[as] varchar(1) COLLATE Chinese_PRC_CI_AS NULL ,
[asd] varchar(1) COLLATE Chinese_PRC_CI_AS NULL ,
CONSTRAINT [PK__test__3213E83F753864A1] PRIMARY KEY ([id11])
)
ON [PRIMARY]
GO

--刪除表
IF EXISTS(select * from sysobjects where id=object_id(‘[test]‘) and objectproperty(id,‘IsTable‘)=1)
drop TABLE test1111


ALTER TABLE [dbo].[test] DROP CONSTRAINT [PK__test__3213E83F753864A1]
GO

ALTER TABLE [dbo].[test] ALTER COLUMN [asdad] varchar(1) COLLATE Chinese_PRC_CI_AS NOT NULL
GO

--更名
EXEC sp_rename N‘[dbo].[test].[asdad]‘, N‘id11‘, ‘COLUMN‘
GO

--是否空
ALTER TABLE [dbo].[test] ALTER COLUMN [id11] int NOT NULL
GO

--主鍵
ALTER TABLE [dbo].[test] ADD CONSTRAINT [PK__test__3213E83F753864A1] PRIMARY KEY ([id11])
GO

alter table test drop constraint id11

--單主鍵改雙主鍵
ALTER TABLE [dbo].[test] DROP CONSTRAINT [PK__test__3213E83F753864A1]
GO

ALTER TABLE [dbo].[test] ADD CONSTRAINT [PK__test__3213E83F753864A1] PRIMARY KEY ([id11], [asas])
GO

--欄位類型
ALTER TABLE [dbo].[test] ALTER COLUMN [asas] tinyint
GO


--索引
CREATE INDEX [qw] ON [dbo].[test]
([id11] ASC)
GO

DROP INDEX [qw] ON [dbo].[test]
GO

--外鍵
ALTER TABLE [dbo].[test] ADD CONSTRAINT [we] FOREIGN KEY ([id11]) REFERENCES [dbo].[dt_article] ([id]) ON DELETE NO ACTION
GO

ALTER TABLE [dbo].[test] DROP CONSTRAINT [we]
GO

CREATE TRIGGER [dbo].[we]
ON [dbo].[test]
AFTER
AS
BEGIN
-- type your statements here
END
GO

--唯一鍵
ALTER TABLE [dbo].[test] ADD CONSTRAINT [wsas] UNIQUE ([id11])
GO

--檢查
ALTER TABLE [dbo].[test] ADD CONSTRAINT [we] CHECK (we)
GO


--觸發器 注釋
IF ((SELECT COUNT(*) from fn_listextendedproperty(‘MS_Description‘,
‘SCHEMA‘, N‘dbo‘,
‘TABLE‘, N‘test‘,
NULL, NULL)) > 0)
EXEC sp_updateextendedproperty @name = N‘MS_Description‘, @value = N‘asasa‘
, @level0type = ‘SCHEMA‘, @level0name = N‘dbo‘
, @level1type = ‘TABLE‘, @level1name = N‘test‘
ELSE
EXEC sp_addextendedproperty @name = N‘MS_Description‘, @value = N‘asasa‘
, @level0type = ‘SCHEMA‘, @level0name = N‘dbo‘
, @level1type = ‘TABLE‘, @level1name = N‘test‘
GO


use demodtcms --等同 demodtcms..dt_express
go
--查詢表的主鍵名
DECLARE @PKName VARCHAR(100)

select @PKName=name from sysobjects where parent_obj in (
select id from sysobjects where name=‘gggggggggg‘)
and xtype=‘pk‘

PRINT @PKName

if(@PKName <> ‘‘)
EXEC(‘ALTER TABLE [dbo].[gggggggggg] DROP CONSTRAINT [‘+ @PKName +‘]‘)
GO

--查詢表的預設值名
DECLARE @DFName VARCHAR(100)

select @DFName=name from sysobjects where parent_obj in (
select id from sysobjects where name=‘gggggggggg‘)
and xtype=‘D‘and id in(SELECT e.id from syscolumns a inner join sysobjects d on a.id=d.id and d.xtype=‘U‘ and d.name =‘gggggggggg‘ left join syscomments e on a.cdefault=e.id WHERE a.name=‘asda11‘)
PRINT @DFName
if(@DFName <> ‘‘)
EXEC(‘ALTER TABLE [dbo].[gggggggggg] DROP CONSTRAINT [‘+ @DFName +‘]‘)
GO

 

SQL server 表動作陳述式(原創)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.