查看指定表结构
exec sp_help Reports
修改表名
exec sp_rename 'Reports','Reports2'
删除数据表
不能删除有外键约束的表。
drop table Reports
表字段
alter table Reports add NewColumn nchar(5) null --新增字段
alter table Reports alter column NewColumn nvarchar(10) --修改字段属性
exec sp_rename 'Reports.NewColumn','OldColumn'--修改字段名
alter table Reports drop column NewColumn --删除列
字段约束
alter table Reports add constraint Name_UQ unique(Name) --新增唯一约束(此非索引)
alter table Reports drop constraint Name_UQ --删除此约束
字段索引
MSSQL默认主键是聚集索引。一个表只能有一个聚集索引(Clustered Index)。
create index NameIndex on Reports(Name) --新增普通索引(非聚集索引)
create unique index Name_UQ on Reports(Name) --新增唯一索引(非聚集索引)
exec sp_helpindex Reports --查看表的索引
drop index Reports.NameIndex --删除索引
create nonclustered index NameFileIndex on Categories(CategoryName,PictureFile) --创建非聚集索引(组合索引)
本文详细介绍了使用SQL进行表结构管理的方法,包括查看、修改、删除表及字段,添加约束和索引,以及如何处理外键约束等问题。适用于数据库管理员和开发人员快速掌握表结构的维护技巧。
545





