SQL Server 修改表結構(轉載)

SQL Server 修改表結構

本文連接: https://blog.csdn.net/petezh/article/details/81744374

查看指定表結構

exec sp_help Reports

修改表名

exec sp_rename 'Reports','Reports2'

刪除數據表

不能刪除有外鍵約束的表。sql

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 server可能會彈出對話框:bash

不容許保存更改。您所作的更改要求刪除並從新建立如下表。您對沒法從新建立的表進行了更改或者啓用了「阻止保存要求從新建立表的更改」選項。markdown

解決方案:菜單欄->工具->選項->設計器->表設計器和數據庫設計器,右側面板,取消勾選「阻止保存要求從新建立表的更改」。數據庫設計

相關文章
相關標籤/搜索