数据库_基础代码<sql>

来源:互联网 发布:经常疲劳知乎 编辑:程序博客网 时间:2024/06/08 01:36

以table1 <含字段:id,name> 为例:

--是否存在

              if exists(select 1 from sysobjects where id = object_id('table1'))

--修改表名

              alter table1  rename to table2

--添加列

             alter table table1 add Status int default 1

--添加列的约束

           ALTER TABLE tablename ADD CONSTRAINT DF_tablename_列名 DEFAULT(0) FOR 列名

--修改列

             alter table table1 alter column name varchar(50)

--修改列名

           alter table table1  column name  to  name1

--删除列

             alert table table1 drop column name

--删除关系

           alert table table1 drop constraint  GuanXiMing

存储过程

---是否存在

if exists(select 1 from sysobjects where id=object_id(proc1) and xtype='p')
drop procedure proc1

--创建

create procedure proc1

as

decare....

触发器

if exists(select * from dbo.sysobjects where id =object_id('trig1)and objectProperty(id,N'IsTrigger')=1)
drop trigger   trig1

--创建

Create Trigger UpdateTrigger_table1
on T_CorrespondentNode
for UPDATE
as
....

 

原创粉丝点击