創(chuàng)建數(shù)據(jù)庫(kù)
創(chuàng)建之前判斷該數(shù)據(jù)庫(kù)是否存在
if exists (select * from sysdatabases where name=databasename) drop database databasename
go
create database database-name
刪除數(shù)據(jù)庫(kù)
drop database dbname
備份sql server
--- 創(chuàng)建備份數(shù)據(jù)的 device
use master
exec sp_addumpdevice disk, testback,
c:\mssql7backup\mynwind_1.dat
--- 開(kāi)始備份
backup database pubs to testback
創(chuàng)建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根據(jù)已有的表創(chuàng)建新表:
a:go
use 原數(shù)據(jù)庫(kù)名
go
select * into 目的數(shù)據(jù)庫(kù)名.dbo.目的表名 from 原表名(使用舊表創(chuàng)建新表)
b:create table tab_new as select col1,col2? from tab_old definition only
創(chuàng)建序列
create sequence simon_sequence
minvalue 1 -- 最小值
maxvalue 999999999999999999999999999 -- 最大值
start with 1 -- 開(kāi)始值
increment by 1 -- 每次加幾
cache 20;
刪除新表
drop table tabname
增加一個(gè)列
alter table tabname add colname coltype
刪除一個(gè)列
alter table tabname drop column colname
添加主鍵
alter table tabname add primary key(col)
說(shuō)明:刪除主鍵:alter table tabname drop primary key(col)? 創(chuàng)建索引
create [unique] index idxname on tabname(col?。)
刪除索引:drop index idxname on tabname
注:索引是不可更改的,想更改必須刪除重新建。
創(chuàng)建視圖
create view viewname as select statement
刪除視圖:drop view viewname
簡(jiǎn)單基本的sql語(yǔ)句
(1) 數(shù)據(jù)記錄篩選:
sql=select * from 數(shù)據(jù)表 where 字段名=字段值 order by 字段名
[desc]
sql=select * from 數(shù)據(jù)表 where 字段名 like %字段值% order by 字段名 [desc]
sql=select top 10 * from 數(shù)據(jù)表 where 字段名 order by 字段名
[desc]
sql=select * from 數(shù)據(jù)表 where 字段名 in (值1,值2,值3)
sql=select * from 數(shù)據(jù)表 where 字段名 between 值1 and 值2
(2) 更新數(shù)據(jù)記錄:
sql=update 數(shù)據(jù)表 set 字段名=字段值 where 條件表達(dá)式
sql=update 數(shù)據(jù)表 set 字段1=值1,字段2=值2 ?? 字段n=值n where 條件表達(dá)式
(3) 刪除數(shù)據(jù)記錄:
sql=delete from 數(shù)據(jù)表 where 條件表達(dá)式
sql=delete from 數(shù)據(jù)表 (將數(shù)據(jù)表所有記錄刪除)
(4) 添加數(shù)據(jù)記錄:
sql=insert into 數(shù)據(jù)表 (字段1,字段2,字段3 ?) values (值1,值2,值3 ?)
sql=insert into 目標(biāo)數(shù)據(jù)表 select * from 源數(shù)據(jù)表 (把源數(shù)據(jù)表的記錄添加到目標(biāo)數(shù)據(jù)表)
(5) 數(shù)據(jù)記錄統(tǒng)計(jì)函數(shù):
avg(字段名) 得出一個(gè)表格欄平均值
count(*;字段名) 對(duì)數(shù)據(jù)行數(shù)的統(tǒng)計(jì)或?qū)δ骋粰谟兄档臄?shù)據(jù)行數(shù)統(tǒng)計(jì) max(字段名) 取得一個(gè)表格欄最大的值
min(字段名) 取得一個(gè)表格欄最小的值
sum(字段名) 把數(shù)據(jù)欄的值相加
引用以上函數(shù)的方法:
sql=select sum(字段名) as 別名 from 數(shù)據(jù)表 where 條件表達(dá)式 set rs=conn.excute(sql)
用 rs(別名) 獲取統(tǒng)計(jì)的值,其它函數(shù)運(yùn)用同上。
查詢?nèi)コ貜?fù)值:select distinct * from table1
(5) 數(shù)據(jù)表的建立和刪除:
create table 數(shù)據(jù)表名稱(字段1 類型1(長(zhǎng)度),字段2 類型2(長(zhǎng)度) ?? )
幾個(gè)高級(jí)查詢運(yùn)算詞
a:union 運(yùn)算符
union 運(yùn)算符通過(guò)組合其他兩個(gè)結(jié)果表(例如table1 和table2)并消去表中任何重復(fù)行而派生出一個(gè)結(jié)果表。當(dāng) all 隨union 一起使用時(shí)(即union all),不消除重復(fù)行。兩種情況下,派生表的每一行不是來(lái)自table1 就是來(lái)自table2。
b: except 運(yùn)算符
except 運(yùn)算符通過(guò)包括所有在table1 中但不在table2 中的行并消除所有重復(fù)行而派生出一個(gè)結(jié)果表。當(dāng)all 隨except 一起使用時(shí)(except all),不消除重復(fù)行。
c:intersect 運(yùn)算符
intersect 運(yùn)算符通過(guò)只包括table1 和table2 中都有的行并消除所有重復(fù)行而派生出一個(gè)結(jié)果表。當(dāng)all 隨intersect 一起使用時(shí)
(intersect all),不消除重復(fù)行。
注:使用運(yùn)算詞的幾個(gè)查詢結(jié)果行必須是一致的。
使用外連接
a、left outer join:
左外連接(左連接):結(jié)果集既包括連接表的匹配行,也包括左連接表的所有行。
sql: select a.a, a.b, a.c, b.c, b.d, b.f from a left outer join b on a.a = b.c
b:right outer join:
右外連接(右連接):結(jié)果集既包括連接表的匹配連接行,也包括右連接表的所有行。
c:full outer join:
全外連接:不僅包括符號(hào)連接表的匹配行,還包括兩個(gè)連接表中的所有記錄。
編輯本段判斷對(duì)象是否存在 判斷數(shù)據(jù)庫(kù)是否存在
if exists (select* from sysdatabases wherename= 數(shù)據(jù)庫(kù)名) dropdatabase[數(shù)據(jù)庫(kù)名]
判斷表是否存在
if not exists (select * from sysobjects where [name] = 表名 and xtype=u)
begin
--這里創(chuàng)建表
end
判斷存儲(chǔ)過(guò)程是否存在
if exists (select* from sysobjects whereid = object_id(n[存儲(chǔ)過(guò)程名]) and objectproperty(id, nisprocedure) = 1) dropprocedure[存儲(chǔ)過(guò)程名]
判斷臨時(shí)表是否存在
if object_id(tempdb..#臨時(shí)表名) isnot null
droptable#臨時(shí)表名
判斷視圖是否存在
--sql server 2000
if exists (select* from sysviews whereobject_id = [dbo].[視圖名]
--sql server 2005
if exists (select* from sys.views whereobject_id = [dbo].[視圖名]
判斷函數(shù)是否存在
if exists (select* from dbo.sysobjects whereid =
object_id(n[dbo].[函數(shù)名]) and xtype in (nfn, nif, ntf)) dropfunction[dbo].[函數(shù)名]
獲取創(chuàng)建信息
select[name],[id],crdate from sysobjects where xtype=u /*
xtype 的表示參數(shù)類型,通常包括如下這些 c = check約束 d = 默認(rèn)值或default約束 f = foreignkey約束 l = 日志 fn = 標(biāo)量函數(shù) if = 內(nèi)嵌表函數(shù) p = 存儲(chǔ)過(guò)程 pk = primarykey約束(類型是k) rf = 復(fù)制篩選存儲(chǔ)過(guò)程 s = 系統(tǒng)表 tf = 表函數(shù) tr = 觸發(fā)器u = 用戶表 uq = unique約束(類型是k) v = 視圖 x = 擴(kuò)展存儲(chǔ)過(guò)程 */
判斷列是否存在
if exists(select* from syscolumns whereid=object_id(表名) and name=列名)
altertable表名dropcolumn列名