很全的SQL中文解釋代碼第1/2頁
更新時間:2008年04月29日 22:06:10 作者:
學(xué)習(xí)sql的朋友可以參考下,中文版sql命令
SQL語句大全
--語 句 功 能
--數(shù)據(jù)操作
SELECT --從數(shù)據(jù)庫表中檢索數(shù)據(jù)行和列
INSERT --向數(shù)據(jù)庫表添加新數(shù)據(jù)行
DELETE --從數(shù)據(jù)庫表中刪除數(shù)據(jù)行
UPDATE --更新數(shù)據(jù)庫表中的數(shù)據(jù)
--數(shù)據(jù)定義
CREATE TABLE --創(chuàng)建一個數(shù)據(jù)庫表
DROP TABLE --從數(shù)據(jù)庫中刪除表
ALTER TABLE --修改數(shù)據(jù)庫表結(jié)構(gòu)
CREATE VIEW --創(chuàng)建一個視圖
DROP VIEW --從數(shù)據(jù)庫中刪除視圖
CREATE INDEX --為數(shù)據(jù)庫表創(chuàng)建一個索引
DROP INDEX --從數(shù)據(jù)庫中刪除索引
CREATE PROCEDURE --創(chuàng)建一個存儲過程
DROP PROCEDURE --從數(shù)據(jù)庫中刪除存儲過程
CREATE TRIGGER --創(chuàng)建一個觸發(fā)器
DROP TRIGGER --從數(shù)據(jù)庫中刪除觸發(fā)器
CREATE SCHEMA --向數(shù)據(jù)庫添加一個新模式
DROP SCHEMA --從數(shù)據(jù)庫中刪除一個模式
CREATE DOMAIN --創(chuàng)建一個數(shù)據(jù)值域
ALTER DOMAIN --改變域定義
DROP DOMAIN --從數(shù)據(jù)庫中刪除一個域
--數(shù)據(jù)控制
GRANT --授予用戶訪問權(quán)限
DENY --拒絕用戶訪問
REVOKE --解除用戶訪問權(quán)限
--事務(wù)控制
COMMIT --結(jié)束當(dāng)前事務(wù)
ROLLBACK --中止當(dāng)前事務(wù)
SET TRANSACTION --定義當(dāng)前事務(wù)數(shù)據(jù)訪問特征
--程序化SQL
DECLARE --為查詢設(shè)定游標(biāo)
EXPLAN --為查詢描述數(shù)據(jù)訪問計劃
OPEN --檢索查詢結(jié)果打開一個游標(biāo)
FETCH --檢索一行查詢結(jié)果
CLOSE --關(guān)閉游標(biāo)
PREPARE --為動態(tài)執(zhí)行準(zhǔn)備SQL 語句
EXECUTE --動態(tài)地執(zhí)行SQL 語句
DESCRIBE --描述準(zhǔn)備好的查詢
---局部變量
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 秒后才執(zhí)行SELECT 語句
waitfor delay '01:02:03'
select * from employee
--例 等到晚上11 點零8 分后才執(zhí)行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關(guān)鍵字的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只返回一個行的值,
--------- 否則應(yīng)在外層where子句中用一個in限定符
select distinct column_name form table_name ---------
distinct指定檢索獨有的列值,不重復(fù)
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合并查詢結(jié)果集,all-保留重復(fù)行
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*** --- 修改數(shù)據(jù)庫表結(jié)構(gòu)
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 ......... ----- 實現(xiàn)刪除列的方法(創(chuàng)建新表)
alter table table_name drop constraint Stockname_default ----
刪除Stockname的default約束
***function(/*常用函數(shù)*/)***
----統(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)
--STDEV()
--STDEV()函數(shù)返回表達(dá)式中所有數(shù)據(jù)的標(biāo)準(zhǔn)差
--STDEVP()
--STDEVP()函數(shù)返回總體標(biāo)準(zhǔn)差
--VAR()
--VAR()函數(shù)返回表達(dá)式中所有值的統(tǒng)計變異數(shù)
--VARP()
--VARP()函數(shù)返回總體變異數(shù)
--語 句 功 能
--數(shù)據(jù)操作
SELECT --從數(shù)據(jù)庫表中檢索數(shù)據(jù)行和列
INSERT --向數(shù)據(jù)庫表添加新數(shù)據(jù)行
DELETE --從數(shù)據(jù)庫表中刪除數(shù)據(jù)行
UPDATE --更新數(shù)據(jù)庫表中的數(shù)據(jù)
--數(shù)據(jù)定義
CREATE TABLE --創(chuàng)建一個數(shù)據(jù)庫表
DROP TABLE --從數(shù)據(jù)庫中刪除表
ALTER TABLE --修改數(shù)據(jù)庫表結(jié)構(gòu)
CREATE VIEW --創(chuàng)建一個視圖
DROP VIEW --從數(shù)據(jù)庫中刪除視圖
CREATE INDEX --為數(shù)據(jù)庫表創(chuàng)建一個索引
DROP INDEX --從數(shù)據(jù)庫中刪除索引
CREATE PROCEDURE --創(chuàng)建一個存儲過程
DROP PROCEDURE --從數(shù)據(jù)庫中刪除存儲過程
CREATE TRIGGER --創(chuàng)建一個觸發(fā)器
DROP TRIGGER --從數(shù)據(jù)庫中刪除觸發(fā)器
CREATE SCHEMA --向數(shù)據(jù)庫添加一個新模式
DROP SCHEMA --從數(shù)據(jù)庫中刪除一個模式
CREATE DOMAIN --創(chuàng)建一個數(shù)據(jù)值域
ALTER DOMAIN --改變域定義
DROP DOMAIN --從數(shù)據(jù)庫中刪除一個域
--數(shù)據(jù)控制
GRANT --授予用戶訪問權(quán)限
DENY --拒絕用戶訪問
REVOKE --解除用戶訪問權(quán)限
--事務(wù)控制
COMMIT --結(jié)束當(dāng)前事務(wù)
ROLLBACK --中止當(dāng)前事務(wù)
SET TRANSACTION --定義當(dāng)前事務(wù)數(shù)據(jù)訪問特征
--程序化SQL
DECLARE --為查詢設(shè)定游標(biāo)
EXPLAN --為查詢描述數(shù)據(jù)訪問計劃
OPEN --檢索查詢結(jié)果打開一個游標(biāo)
FETCH --檢索一行查詢結(jié)果
CLOSE --關(guān)閉游標(biāo)
PREPARE --為動態(tài)執(zhí)行準(zhǔn)備SQL 語句
EXECUTE --動態(tài)地執(zhí)行SQL 語句
DESCRIBE --描述準(zhǔn)備好的查詢
---局部變量
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 秒后才執(zhí)行SELECT 語句
waitfor delay '01:02:03'
select * from employee
--例 等到晚上11 點零8 分后才執(zhí)行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關(guān)鍵字的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只返回一個行的值,
--------- 否則應(yīng)在外層where子句中用一個in限定符
select distinct column_name form table_name ---------
distinct指定檢索獨有的列值,不重復(fù)
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合并查詢結(jié)果集,all-保留重復(fù)行
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*** --- 修改數(shù)據(jù)庫表結(jié)構(gòu)
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 ......... ----- 實現(xiàn)刪除列的方法(創(chuàng)建新表)
alter table table_name drop constraint Stockname_default ----
刪除Stockname的default約束
***function(/*常用函數(shù)*/)***
----統(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)
--STDEV()
--STDEV()函數(shù)返回表達(dá)式中所有數(shù)據(jù)的標(biāo)準(zhǔn)差
--STDEVP()
--STDEVP()函數(shù)返回總體標(biāo)準(zhǔn)差
--VAR()
--VAR()函數(shù)返回表達(dá)式中所有值的統(tǒng)計變異數(shù)
--VARP()
--VARP()函數(shù)返回總體變異數(shù)
相關(guān)文章
SQL數(shù)據(jù)庫與oracle數(shù)據(jù)庫鏡像有什么不同對比
數(shù)據(jù)庫鏡像是將數(shù)據(jù)庫事務(wù)處理從一個數(shù)據(jù)庫移動到不同環(huán)境中的另一個數(shù)據(jù)庫中。鏡像的拷貝是一個備用的拷貝,不能直接訪問,它只用在錯誤恢復(fù)的情況下。2010-03-03sql2005可實時監(jiān)測數(shù)據(jù)庫版本控制SQL的觸發(fā)器
用于sql2005實時監(jiān)測數(shù)據(jù)庫版本控制SQL的觸發(fā)器2008-10-10DBeaver操作所有數(shù)據(jù)庫管理工具使用詳解
這篇文章主要為大家介紹了一款操作所有數(shù)據(jù)庫工具DBeaver使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07使用DataGrip創(chuàng)建數(shù)據(jù)庫并讀取sql文件圖文教程
這篇文章主要給大家介紹了關(guān)于使用DataGrip創(chuàng)建數(shù)據(jù)庫并讀取sql文件的相關(guān)資料,DataGrip是一款數(shù)據(jù)庫管理客戶端工具,方便連接到數(shù)據(jù)庫服務(wù)器,執(zhí)行sql、創(chuàng)建表、創(chuàng)建索引以及導(dǎo)出數(shù)據(jù)等,需要的朋友可以參考下2023-11-11數(shù)據(jù)庫查詢中遭遇特殊字符導(dǎo)致問題的解決方法
數(shù)據(jù)庫查詢中遭遇特殊字符導(dǎo)致問題的解決方法,我們提供的是asp的,但其它的數(shù)據(jù)庫與語言下的解決方法也大同小異。2007-12-12SQL利用Function創(chuàng)建長整形的唯一ID示例代碼
這篇文章主要給大家介紹了關(guān)于SQL利用Function創(chuàng)建長整形的唯一ID的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07聊聊Navicat統(tǒng)計的行數(shù)竟然和表實際行數(shù)不一致的問題
Navicat作為數(shù)據(jù)庫管理工具,在業(yè)界廣受歡迎,這篇文章主要介紹了Navicat統(tǒng)計的行數(shù)竟然和表實際行數(shù)不一致的問題,需要的朋友可以參考下2021-12-12