標準SQL語句總結,標準SQL語言基本上適用于下面所列出的數(shù)據(jù)庫軟件
-----------------------------------------------------------------------------
數(shù)據(jù)庫軟件清單
A商業(yè)數(shù)據(jù)庫軟件如下
1.微軟的MS SQL Server和Access
2.IBM的DB2,informax
3.Sybase的大型數(shù)據(jù)庫ASE,中小型數(shù)據(jù)庫ASA
4.甲骨文公司的Oracle8.0,oracle9i系列
5.Borland公司的InterBaseB多種開源免費數(shù)據(jù)庫
Mysql,PostgreSQL,SQLite、SimpleSQL、Berkely DB、Minosse、Firebird
(Mysql,PostgreSQL是目前使用最廣泛)
-----------------------------------------------------------------------------
最精簡短小的SQL語句
SQL分類:
DDL—數(shù)據(jù)定義語言(CREATE,ALTER,DROP,DECLARE)
DML—數(shù)據(jù)操縱語言(SELECT,DELETE,UPDATE,INSERT)
DCL—數(shù)據(jù)控制語言(GRANT,REVOKE,COMMIT,ROLLBACK)
1、說明:創(chuàng)建數(shù)據(jù)庫CREATE DATABASE database-name
2、說明:刪除數(shù)據(jù)庫drop database dbname
3、說明:
備份sql server
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
開始備份
BACKUP DATABASE pubs TO testBack
4、說明:創(chuàng)建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根據(jù)已有的表創(chuàng)建新表:
A:create table tab_new like tab_old (使用舊表創(chuàng)建新表)
B:create table tab_new as select col1,col2… from tab_old definition only
5、說明:刪除新表drop table tabname
6、說明:增加一個列Alter table tabname add column col type
7、說明
添加主鍵: Alter table tabname add primary key(col)
刪除主鍵: Alter table tabname drop primary key(col)
8、
創(chuàng)建索引:create [unique] index idxname on tabname(col….)
刪除索引:drop index idxname
9、說明:
創(chuàng)建視圖:create view viewname as select statement
刪除視圖:drop view viewname
10、說明:幾個簡單的基本的sql語句
選擇: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的語法很精妙,查資料!
排序:select * from table1 order by field1,field2 [desc]
總數(shù):select count as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最?。簊elect min(field1) as minvalue from table1
-----------------------------------------------------------------------------
--數(shù)據(jù)常用操作
SELECT --從數(shù)據(jù)庫表中檢索數(shù)據(jù)
INSERT --向數(shù)據(jù)庫表添加新數(shù)據(jù)
UPDATE --更新數(shù)據(jù)庫表中的數(shù)據(jù)
DELETE --從數(shù)據(jù)庫表中刪除數(shù)據(jù)
-----------------------------------------------------------------------------
--數(shù)據(jù)庫對象操作語句
CREATE TABLE --創(chuàng)建一個數(shù)據(jù)庫表
ALTER TABLE --修改數(shù)據(jù)庫表結構
DROP TABLE --從數(shù)據(jù)庫中刪除表
CREATE VIEW --創(chuàng)建一個視圖
ALTER VIEW --修改一個視圖
DROP VIEW --刪除一個視圖
CREATE INDEX --為表創(chuàng)建一個索引
DROP INDEX --從表中刪除索引
CREATE PROCEDURE--創(chuàng)建一個存儲過程
DROP PROCEDURE --刪除存儲過程
CREATE TRIGGER --創(chuàng)建一個觸發(fā)器
DROP TRIGGER --從刪除觸發(fā)器
-----------------------------------------------------------------------------
--數(shù)據(jù)權限控制
GRANT --授予用戶訪問權限
DENY --拒絕用戶訪問
REVOKE --解除用戶訪問權限
-----------------------------------------------------------------------------
--事務控制
COMMIT --結束當前事務
ROLLBACK --中止當前事務
SET TRANSACTION --定義當前事務數(shù)據(jù)訪問特征
-----------------------------------------------------------------------------
--數(shù)據(jù)庫函數(shù),過程,觸發(fā)器腳本的SQL
DECLARE --為查詢設定游標
EXPLAN --為查詢描述數(shù)據(jù)訪問計劃
OPEN --檢索查詢結果打開一個游標
FETCH --檢索一行查詢結果
CLOSE --關閉游標
PREPARE --為動態(tài)執(zhí)行準備SQL 語句
EXECUTE --動態(tài)地執(zhí)行SQL 語句
DESCRIBE --描述準備好的查詢
-----------------------------------------------------------------------------
SELECT --從數(shù)據(jù)庫表中檢索數(shù)據(jù)
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 stock*** = '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)
--------- 子查詢
--------- 除非能確保內(nèi)層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 --向數(shù)據(jù)庫表添加新數(shù)據(jù)
insert into table_name (Stock_name,Stock_number) value ("xxx","xxxx")
value (select Stockname , Stocknumber from Stock_table2)---value為select語句
-----------------------------------------------------------------------------
UPDATE --更新數(shù)據(jù)庫表中的數(shù)據(jù)
update table_name set
Stockname = "xxx" [where Stockid = 3]
Stockname = default
Stockname = null
Stocknumber = Stockname + 4
where ***
-----------------------------------------------------------------------------
DELETE --從數(shù)據(jù)庫表中刪除數(shù)據(jù)
delete from table_name where Stockid = 3
truncate table_name ----------- 刪除表中所有行,仍保持表的完整性
drop table table_name --------------- 完全刪除表
-----------------------------------------------------------------------------
標準SQL統(tǒng)計函數(shù)
AVG --求平均值
COUNT --統(tǒng)計數(shù)目
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)
-----------------------------------------------------------------------------
標準SQL字符串函數(shù)
ASCII() --函數(shù)返回字符表達式最左端字符的ASCII 碼值
CHAR() --函數(shù)用于將ASCII 碼轉換為字符--如果沒有輸入0 ~ 255 之間的ASCII 碼值CHAR 函數(shù)會返回一個NULL
LOWER() --函數(shù)把字符串全部轉換為小寫
UPPER() --函數(shù)把字符串全部轉換為大寫
STR() --函數(shù)把數(shù)值型數(shù)據(jù)轉換為字符型數(shù)據(jù)
LTRIM() --函數(shù)把字符串頭部的空格去掉
RTRIM() --函數(shù)把字符串尾部的空格去掉
LEFT(),RIGHT(),SUBSTRING() --函數(shù)返回部分字符串
CHARINDEX(),PATINDEX() --函數(shù)返回字符串中某個指定的子串出現(xiàn)的開始位置
REPLICATE() --函數(shù)返回一個重復character_expression 指定次數(shù)的字符串
select replicate('abc', 3) replicate( 'abc', -2)運行結果如下abcabcabc NULL
REVERSE() --函數(shù)將指定的字符串的字符排列順序顛倒
REPLACE() --函數(shù)返回被替換了指定子串的字符串
select replace('abc123g', '123', 'def')運行結果如下abcdefg
SPACE() --函數(shù)返回一個有指定長度的空白字符串
STUFF() --函數(shù)用另一子串替換字符串指定位置長度的子串
-----------------------------------------------------------------------------
標準SQL語法
局部變量和局變量
---局部變量 (以@開頭)
格式:declare @變量名 類型
--set @id = '10010001'
select @id = '10010001'
---全局變量 (必須以@@開頭)
格式:declare @@變量名 類型
代碼: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
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 秒后才執(zhí)行SELECT 語句
waitfor delay ’01:02:03’
select * from employee
--例 等到晚上11 點零8 分后才執(zhí)行SELECT 語句
waitfor time ’23:08:00’
select * from employee