SQL指令碼修改表結構

2022-10-10 23:18:18 字數 1562 閱讀 6904

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 [表名] add [欄位名] nvarchar (50) null,[欄位名] nvarchar (50) null

如:alter table daylog add aaa char(10), abb char(10), acc char(10), ade char(10)

刪除字段:

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

修改字段:

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

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

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

新建約束:

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

刪除約束:

alter table [表名] drop constraint 約束名

如:if  exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[fk_buiaddprotype_buildfunction]') and type = 'f')

alter table [dbo].[buiaddprotype] drop constraint [fk_buiaddprotype_buildfunction]

go新建預設值

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

刪除預設值

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

SQL指令碼修改表結構

sql指令碼修改表結構 新建表 create table 表名 自動編號字段 int identity 1,1 primary key 欄位1 nvarchar 50 default 預設值 null 欄位2 ntext null 欄位3 datetime,欄位4 money null 欄位5 in...

SQL 修改表結構

資料庫修改表結構sql 修改表結構包括 增加字段 刪除字段 增加約束 刪除約束 修改預設值 修改字段資料型別 重新命名字段 重新命名表。所有這些動作都是用 alter table 命令執行的。1 增加字段 alter table products add description text 你也可以同...

sql修改表結構

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