1、說(shuō)明:創(chuàng)建數(shù)據(jù)庫(kù)
create datebase database-name
2、說(shuō)明:刪除數(shù)據(jù)庫(kù)
drop database dbname
3、說(shuō)明:備份sqlserver
--- 創(chuàng)建 備份數(shù)據(jù)的 device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
--- 開(kāi)始 備份
BACKUP DATABASE pubs TO testBack
3-1、對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作:
分離數(shù)據(jù)庫(kù): sp_detach_db;
附加數(shù)據(jù)庫(kù):sp_attach_db 后接表明,附加需要完整的路徑名
3-2.如何修改數(shù)據(jù)庫(kù)的名稱:
sp_renamedb 'old_name', 'new_name'
4、說(shuō)明:創(chuàng)建新表(SQL下要去掉[])
create table tabname(col1type1 [not null] [primary key],col2 type2 [not null],..)
根據(jù)已有的表創(chuàng)建新表:
SQL:
select * into 新表 from 表 –全復(fù)制含數(shù)據(jù)
select * into 新表 from 表 where 1=2 --只要表結(jié)構(gòu)
A:create table tab_new like tab_old (使用舊表創(chuàng)建新表)
B:createtable tab_new as select col1,col2… from tab_old definition only
5、說(shuō)明:刪除新表
drop table tabname
6、說(shuō)明:增加一個(gè)列
Alter table tabname add字段名char(10) identity (1,1) –-從1自增1
注:列增加后將不能刪除。DB2中列加上后數(shù)據(jù)類型也不能改變,唯一能改變的是增加varchar類型的長(zhǎng)度。
7、說(shuō)明:添加主鍵: Alter table tabname add primary key(col)
說(shuō)明:刪除主鍵:
sp_helpconstraint table_name –得到約束名
Alter table tabname drop primary key(上面得到的約束名)
8、說(shuō)明:創(chuàng)建索引:create[unique] index idxname on tabname(col….)
刪除索引:drop index idxname
注:索引是不可更改的,想更改必須刪除重新建。
9、說(shuō)明:創(chuàng)建視圖:create view viewname as select statement
刪除視圖:drop view viewname
10、說(shuō)明:幾個(gè)簡(jiǎn)單的基本的sql語(yǔ)句
選擇:select * from table1 where 范圍
插入:insert into table1(field1,field2)values(value1,value2)
刪除:delete from table1 where 范圍
更新:update table1 set field1=value1 where 范圍
查找:select * from table1 where field1 like ’%value1%’---like的語(yǔ)法很精妙,查資料!
select * from table1 where field1 isno null
排序:select * from table1 order by field1,field2desc
總數(shù):select count as totalcount from table1
SQL: select count (value) as Tname from table_namewhere name=’x’
求和:select sum(field1) as sumvalue from table1
SQL: select sum(value) from table1
平均:select avg(field1) as avgvalue from table1
SQL: select avg(value) from table1
最大:select max(field1) as maxvalue from table1
SQL: select max(value) from table1
最小:select min(field1) as minvalue from table1
SQL: select min(value) from table1
11、說(shuō)明:幾個(gè)高級(jí)查詢運(yùn)算詞
A: union 運(yùn)算符(去掉A與B中的重復(fù)行)
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。
例:
select username from table1
union
select username fromtable2
(僅返回table1與table2中共有的數(shù)據(jù))
select username from table1
union
select username from table2
(返回table1與table2中所有數(shù)據(jù),有重復(fù)的)
B:except 運(yùn)算符 (查A表,不顯示B表中有的)
EXCEPT 運(yùn)算符通過(guò)包括所有在 TABLE1 中但不在TABLE2 中的行并消除所有重復(fù)行而派生出一個(gè)結(jié)果表。當(dāng) ALL 隨EXCEPT 一起使用時(shí) (EXCEPT ALL),不消除重復(fù)行。
例:
select username from table1
except
select username from table2
(以table2為條件,返回tabel1并且不在table2中的)
C:intersect 運(yùn)算符(返回A與B中都有的項(xiàng))
INTERSECT 運(yùn)算符通過(guò)只包括 TABLE1 和 TABLE2 中都有的行并消除所有重復(fù)行而派生出一個(gè)結(jié)果表。當(dāng) ALL 隨INTERSECT 一起使用時(shí)(INTERSECT ALL),不消除重復(fù)行。
例:
select username from table1
intersect
select username from table2
(返回tabel1與table2共有的)
注:使用運(yùn)算詞的幾個(gè)查詢結(jié)果行必須是一致的。
12、說(shuō)明:使用外連接
A、left (outer)join:
左外連接(左連接):結(jié)果集幾包括連接表的匹配行,也包括左連接表的所有行。
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
B:right (outer)join:
右外連接(右連接):結(jié)果集既包括連接表的匹配連接行,也包括右連接表的所有行。
C:full/cross (outer) join:
全外連接:不僅包括符號(hào)連接表的匹配行,還包括兩個(gè)連接表中的所有記錄。
12、分組:Groupby:
一張表,一旦分組 完成后,查詢后只能得到組相關(guān)的信息。
組相關(guān)的信息:(統(tǒng)計(jì)信息) count,sum,max,min,avg 分組的標(biāo)準(zhǔn))
在SQLServer中分組時(shí):不能以text,ntext,image類型的字段作為分組依據(jù)
在selecte統(tǒng)計(jì)函數(shù)中的字段,不能和普通的字段放在一起;