SQL server 表操作语句(原创)

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

 

转载于:https://www.cnblogs.com/guanshan/p/guan20141119-1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值