sql server 修改表結構

2022-01-15 05:39:14 字數 1735 閱讀 6260

--修改資料庫名稱、表名稱、欄位名

--修改資料庫名 

sp_renamedb 'olddbname','newdbname'

--修改表名

sp_rename 'oldtablename','newtablename'

--修改欄位名 引數簡單易懂:oldcolumnname:oldcolumnname 舊表名:如果多表字段重複的話可用table.oldcolumnname 指定

sp_rename 'oldcolumnname','newcolumnname','column'

sql指令碼修改表結構

新建表:

create table [表名]

([自動編號字段] int identity (1,1) primary key ,

[欄位1] nvarchar(50) default '預設值' null ,

[欄位2] ntext null ,

[欄位3] datetime,

[欄位4] money null ,

[欄位5] int default 0,

[欄位6] decimal (12,4) default 0,

[欄位7] image null ,

)刪除表:

drop table [表名]

truncate table [表名] --不寫log 而且自增字段復位從1開始

插入資料:

insert into [表名] (欄位1,欄位2) values (1,'abc')

刪除資料:

delete from [表名] where [欄位名]>100

更新資料:

update [表名] set [欄位1] = 1,[欄位2] = 'abc' where [欄位三] = 'what'

新增字段:

alter table [表名] add [欄位名] nvarchar (50) null

刪除字段:

alter table [表名] drop column [欄位名]

修改字段:

alter table [表名] alter column [欄位名] nvarchar (50) null

重新命名表:(access 重新命名表,請參考文章:在access資料庫中重新命名表)

sp_rename '表名', '新錶名', 'object'

新建約束:

alter table [表名] add constraint 約束名 check ([約束字段] <= '2010-12-1')

add constraint pk primary key (id) --主鍵約束

add constraint pk primary key (id)alter table products add constraint some_name unique (product_no); --唯一值約束

alter table products add foreign key (product_group_id) references product_groups;  --外來鍵約束

刪除約束:

alter table [表名] drop constraint 約束名

新建預設值

alter table [表名] add constraint 預設值名 default 'abc' for [欄位名]

刪除預設值

alter table [表名] drop constraint 預設值名

SQL Server 修改表結構

檢視指定表結構exec sp help reports修改表名exec sp rename reports reports2 刪除資料表 不能刪除有外來鍵約束的表。drop table reports表字段alter table reports add newcolumn nchar 5 null ...

SQL Server 2005 修改表結構

為表新增具有預設值的一列 if not exists select from syscolumns where name website and objectproperty id,isusertable 1 and object name id tbbrowser begin alter tabl...

sql server 修改表結構語法大全

1.增加字段 alter table docdsp add dspcode char 200 2.刪除字段 alter table table name drop column column name 3.修改字段型別 alter table table name alter column colu...