常用SQL操作語句筆記

2021-06-08 15:27:41 字數 2271 閱讀 8455

新建表: 

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語句常用操作

一切的一切,都是源於對知識的渴望 最近有用到postgre這個資料庫,但是以前聽都沒有聽過,不斷的向別人請教,總算是現在能暫時操作這個資料庫了,之前有學過sql語句,但是不用了之後就感覺已經忘得差不多了,所以趕緊多看了些資料,在w3school上又重新學了下,溫故而知新啊!下面我就記錄下,我常用的一...

筆記 常用SQL語句

建立表 create table ifnot exists userinfo tag integer primary keynot null unique userid text not null unique password text not null name text 插入資料 insert...

MySQL 常用sql操作語句

獲取資料庫裡所有表 select table name,table rows from information schema.tables where table schema 資料庫名 獲取表裡所有字段 select column name from information schema.colu...