SQL Server Table Action Statement (original)

Source: Internet
Author: User

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

--Delete Table
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

--Renaming
EXEC sp_rename N ' [dbo]. [Test]. [Asdad] ', N ' id11 ', ' COLUMN '
GO

--Is empty
ALTER TABLE [dbo]. [Test] ALTER COLUMN [id11] int not NULL
GO

--PRIMARY key
ALTER TABLE [dbo]. [Test] ADD CONSTRAINT [PK__TEST__3213E83F753864A1] PRIMARY KEY ([Id11])
GO

ALTER TABLE test DROP constraint Id11

--Single primary key change double primary key
ALTER TABLE [dbo]. [Test] DROP CONSTRAINT [PK__TEST__3213E83F753864A1]
GO

ALTER TABLE [dbo]. [Test] ADD CONSTRAINT [PK__TEST__3213E83F753864A1] PRIMARY KEY ([Id11], [Asas])
GO

--Field type
ALTER TABLE [dbo]. [Test] ALTER COLUMN [Asas] tinyint
GO


--Index
CREATE INDEX [QW] on [dbo]. [Test]
([Id11] ASC)
GO

DROP INDEX [QW] on [dbo]. [Test]
GO

--FOREIGN key
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

--Unique key
ALTER TABLE [dbo]. [Test] ADD CONSTRAINT [Wsas] UNIQUE ([Id11])
GO

--Check
ALTER TABLE [dbo]. [Test] ADD CONSTRAINT [We] CHECK (We)
GO


--Trigger Comment
IF (SELECT COUNT (*) from Fn_listextendedproperty (' Ms_description ',
' SCHEMA ', N ' dbo ',
' TABLE ', N ' test ',
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--equals demodtcms. Dt_express
Go
--The primary key name of the query table
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

--The default value name of the query table
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 joins sysobjects D on a.id=d.id and d.xtype= ' U ' and d.name = ' GGG Ggggggg ' 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 Table Action Statement (original)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.