欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

SQLSERVER实现更改表名,更改列名,更改约束代码

程序员文章站 2024-02-24 16:01:10
废话不多说了,具体详情如下所示: 1.修改表名 格式:sp_rename tablename,newtablename sp_rename table...

废话不多说了,具体详情如下所示:

1.修改表名

格式:sp_rename tablename,newtablename

sp_rename tablename,newtablename 

2.修改字段名

格式:sp_rename 'tablename.colname',newcolname,'column'

sp_rename 'tablename.colname',newcolname,'column' 

3.添加字段

格式:alter table table_name add new_column data_type [interality_codition]

示例1

alter table student add nationality varchar(20)

--示例2 添加int类型的列,默认值为 0

alter table student add studentname int default 0 --示例3 添加int类型的列,默认值为0,主键 
alter table student add studentid int primary key default 0 --示例4 判断student中是否存在name字段且删除字段 
if exists(select * from syscolumns where id=object_id('student') and name='name') begin 
alter table student drop column name 
end 

4.更改字段

格式:alter table table_name alter column column_name

alter table student alter column name varchar(200) 

5.删除字段

格式:alter table table_name drop column column_name

alter table student drop column nationality; 

6.查看字段约束

格式: select * from information_schema.constraint_column_usage where table_name = table_name

select table_name,column_name,constraint_name from information_schema.constraint_column_usage
where table_name = 'student' 

7.查看字段缺省约束表达式 (即默认值等)

格式:select * from information_schema.columns where table_name = table_name

select table_name, column_name, column_default from information_schema.columns
where table_name='student' 

8.查看字段缺省约束名

格式:select name from sysobjects where object_id(table_name)=parent_obj and xtype='d'

select name from sysobjects
where object_id('表?名?')=parent_obj and xtype='d' 

9.删除字段约束

格式:alter table tablename drop constraint constraintname

alter table student drop constraint pk__student__2f36bc5b772b9a0b 

10.添加字段约束

格式:alter table tablename add constraint constraintname primary key (column_name)

--示例1

alter table stuinfo add constraint pk_stuno primary key (stuno) --示例2 添加主键约束(primary key)


-- 存在主键约束pk_stuno,则删除 
if exists(select * from sysobjects where name='pk_stuno' and xtype='pk')
alter table stuinfo
drop constraint pk_stuno
go -- 重新添加主键约束pk_stuno 
alter table stuinfo add constraint pk_stuno primary key (stuno)
go --示例3 添加 唯一uq约束(unique constraint)
-- 存在唯一约束uq_stuno,则删除 
if exists(select * from sysobjects where name='uq_stuid' and xtype='uq')
alter table stuinfo
drop constraint uq_stuid
go 
-- 重新添加唯一约束uq_stuid 
alter table stuinfo add constraint uq_stuid unique (stuid) --示例4 添加默认df约束(default constraint)
-- 存在默认约束uq_stuno,则删除 
if exists(select * from sysobjects where name='df_stuaddress' and xtype='d')
alter table stuinfo drop constraint df_stuaddress
go -- 重新添加默认约束df_stuaddress 
alter table stuinfo add constraint df_stuaddress default ('地址不详') for stuaddress --示例5 检查ck约束(check constraint)
-- 存在检查约束uq_stuno,则删除 
if exists(select * from sysobjects where name='ck_stuage' and xtype='c')
alter table stuinfo drop cons

以上所述是小编给大家介绍的sqlserver实现更改表名,更改列名,更改约束代码,希望对大家有所帮助!