--語 句 功 能
--資料操作
SELECT --從資料庫表中檢索資料行和列
INSERT --向資料庫表添加新資料行
DELETE --從資料庫表中刪除資料行
UPDATE --更新資料庫表中的資料
--資料定義
CREATE TABLE --建立一個資料庫表
DROP TABLE --從資料庫中刪除表
ALTER TABLE --修改資料庫表結構
CREATE VIEW --建立一個視圖
DROP VIEW --從資料庫中刪除視圖
CREATE INDEX --為資料庫表建立一個索引
DROP INDEX --從資料庫中刪除索引
CREATE PROCEDURE --建立一個預存程序
DROP PROCEDURE --從資料庫中刪除預存程序
CREATE TRIGGER --建立一個觸發器
DROP TRIGGER --從資料庫中刪除觸發器
CREATE SCHEMA --向資料庫添加一個新模式
DROP SCHEMA --從資料庫中刪除一個模式
CREATE DOMAIN --建立一個資料範圍
ALTER DOMAIN --改變域定義
DROP DOMAIN --從資料庫中刪除一個域
--資料控制
GRANT --授予使用者存取權限
DENY --拒絕使用者訪問
REVOKE --解除使用者存取權限
--事務控制
COMMIT --結束當前事務
ROLLBACK --中止當前事務
SET TRANSACTION --定義當前交易資料訪問特徵
--程式化SQL
DECLARE --為查詢設定遊標
EXPLAN --為查詢描述資料訪問計劃
OPEN --檢索查詢結果開啟一個遊標
FETCH --檢索一行查詢結果
CLOSE --關閉遊標
PREPARE --為動態執行準備SQL 陳述式
EXECUTE --動態地執行SQL 陳述式
DESCRIBE --描述準備好的查詢
---局部變數
declare @id char(10)
--set @id = '10010001'
select @id = '10010001'
---全域變數
---必須以@@開頭
--IF ELSE
declare @x int @y int @z int
select @x = 1 @y = 2 @z=3
if @x > @y
print 'x > y' --列印字串'x > y'
else if @y > @z
print 'y > z'
else print 'z > y'
--CASE
use pangu
update employee
set e_wage =
case
when job_level = ’1’ then e_wage*1.08
when job_level = ’2’ then e_wage*1.07
when job_level = ’3’ then e_wage*1.06
else e_wage*1.05
end
--WHILE CONTINUE BREAK
declare @x int @y int @c int
select @x = 1 @y=1
while @x < 3
begin
print @x --列印變數x 的值
while @y < 3
begin
select @c = 100*@x + @y
print @c --列印變數c 的值
select @y = @y + 1
end
select @x = @x + 1
select @y = 1
end
--WAITFOR
--例 等待1 小時2 分零3 秒後才執行SELECT 語句
waitfor delay ’01:02:03’
select * from employee
--例 等到晚上11 點零8 分後才執行SELECT 語句
waitfor time ’23:08:00’
select * from employee
***SELECT***
select *(列名) from table_name(表名) where column_name operator value
ex:(宿主)
select * from stock_information where stockid = str(nid)
stockname = 'str_name'
stockname like '% find this %'
stockname like '[a-zA-Z]%' --------- ([]指定值的範圍)
stockname like '[^F-M]%' --------- (^排除指定範圍)
--------- 只能在使用like關鍵字的where子句中使用萬用字元)
or stockpath = 'stock_path'
or stocknumber < 1000
and stockindex = 24
not stocksex = 'man'
stocknumber between 20 and 100
stocknumber in(10,20,30)
order by stockid desc(asc) --------- 排序,desc-降序,asc-升序
order by 1,2 --------- by列號
stockname = (select stockname from stock_information where stockid = 4)
--------- 子查詢
--------- 除非能確保內層select只返回一個行的值,
--------- 否則應在外層where子句中用一個in限定符
select distinct column_name form table_name --------- distinct指定檢索專屬的列值,不重複
select stocknumber ,"stocknumber + 10" = stocknumber + 10 from table_name
select stockname , "stocknumber" = count(*) from table_name group by stockname
--------- group by 將表按行分組,指定列中有相同的值
having count(*) = 2 --------- having選定指定的組
select *
from table1, table2
where table1.id *= table2.id -------- 左外部串連,table1中有的而table2中沒有得以null表示
table1.id =* table2.id -------- 右外部串連
select stockname from table1
union [all] ----- union合并查詢結果集,all-保留重複行
select stockname from table2
***insert***
insert into table_name (Stock_name,Stock_number) value ("xxx","xxxx")
value (select Stockname , Stocknumber from Stock_table2)---value為select語句
***update***
update table_name set Stockname = "xxx" [where Stockid = 3]
Stockname = default
Stockname = null
Stocknumber = Stockname + 4
***delete***
delete from table_name where Stockid = 3
truncate table_name ----------- 刪除表中所有行,仍保持表的完整性
drop table table_name --------------- 完全刪除表
***alter table*** --- 修改資料庫表結構
alter table database.owner.table_name add column_name char(2) null .....
sp_help table_name ---- 顯示表已有特徵
create table table_name (name char(20), age smallint, lname varchar(30))
insert into table_name select ......... ----- 實現刪除列的方法(建立新表)
alter table table_name drop constraint Stockname_default ---- 刪除Stockname的default約束
***function(/*常用函數*/)***
----統計函數----
AVG --求平均值
COUNT --統計數目
MAX --求最大值
MIN --求最小值
SUM --求和
--AVG
use pangu
select avg(e_wage) as dept_avgWage
from employee
group by dept_id
--MAX
--求工資最高的員工姓名
use pangu
select e_name
from employee
where e_wage =
(select max(e_wage)
from employee)
--STDEV()
--STDEV()函數返回運算式中所有資料的標準差
--STDEVP()
--STDEVP()函數返回總體標準差
--VAR()
--VAR()函數返回運算式中所有值的統計變異數
--VARP()
--VARP()函數返回總體變異數