常見SQL語句 建立表 新增主鍵 新增列

2022-02-03 19:31:51 字數 2236 閱讀 8595

新建表:

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 [表名]

插入資料:

insert into [表名] (欄位1,欄位2) values (100,\'51windows.net\')

刪除資料:

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

更新資料:

update [表名] set [欄位1] = 200,[欄位2] = \'51windows.net\' where [欄位三] = \'haiwa\'

新增字段:

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 ([約束字段] <= \'2000-1-1\')

刪除約束:

alter table [表名] drop constraint 約束名

新建預設值

alter table [表名] add constraint 預設值名 default \'51windows.net\' for [欄位名]

刪除預設值

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

刪除sql server 中的日誌,減小資料庫檔案大小

dump transaction 資料庫名 with no_log

backup log 資料庫名 with no_log

dbcc shrinkdatabase(資料庫名)

exec sp_dboption \'資料庫名\', \'autoshrink\', \'true\'

\\\'新增字段通用函式

sub addcolumn(tablename,columnname,columntype)

conn.execute(\"alter table \"&tablename&\" add \"&columnname&\" \"&columntype&\"\")

end sub

\\\'更改字段通用函式

sub modcolumn(tablename,columnname,columntype)

conn.execute(\"alter table \"&tablename&\" alter column \"&columnname&\" \"&columntype&\"\")

end sub

\\\'檢查表是否存在

sql=\"select count(*) as dida from sysobjects where id = object_id(n\'[所有者].[表名]\') and objectproperty(id, n\'isusertable\') = 1\"

set rs=conn.execute(sql)

response.write rs(\"dida\")\'返回乙個數值,0代表沒有,1代表存在

判斷表的存在:

select * from sysobjects where id = object_id(n\'[dbo].[tablename]\') and objectproperty(id, n\'isusertable\') = 1

某個表的結構

select * from syscolumns where id = object_id(n\'[dbo].[你的表名]\') and objectproperty(id, n\'isusertable\') = 1

常見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,...

常見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,...

sql語句建立主鍵

對於有資料的表,在企業管理器裡面建立索引時,容易超時,使用sql語句只要在,工具 選項設定超時值為0,就不會超時。sql語句建立有聯合主鍵的表 create table tabcheck check id int not null,check no int not null,startdate da...