SQL92标准(一)

2015-02-02 22:54:19 · 作者: · 浏览: 8

————数据操作

select——从数据库表中检索数据行和列

按日期条件查询时:select * from table where T < "12/8/2014"

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 domin ——改变域定义

drop domin ——从数据库中删除一个域

————数据控制

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'--打印字符串

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

while @y<3

begin

select @c = 100*@x+@y

print @c

select @y=@y+1

end

select @x=@x+1

select @y=1

end

————Waitfor

waitfor delay '01:02:03' --等待1小时2分零3秒后才执行select语句

select * from employee

waitfor time '23:08:00' --等到晚上11点零8分后才执行select语句

select * from employee

—————Select

select * from stock_information where stockid=str(nid)

stockname = 'str_name'

stockname like '% find this %'

stockname like '[a-zA-Z]%' --[]指定值得范围

stockname like '[^F-M]' --^排除指定范围

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)

--子查询除非能确保内层select只返回一个行的值,否则应在外层where子句中用一个in限定符

select distinct column_name from 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表示

select stockname from table1 union[all] --union合并查询结果集,all保留重复行

————Insert

insert into table_name(Stock_name,stock_number) value("xxx","xxxx")

value(select Stockname,Stocknumber from Stock_table2)

————Update

update table_name set Stockname = "xxx"

where Stokid =3

Stockname=default

Stockname=null

Stocknumber = Stockname +4

————Delete

delete from table_name where Stockid=3

truncate table_name ----删除表中所有行,依然保持表的完整性

drop table table_name ----完全删除表

————alter

alter table database.owner.table_name add column_name char(2) null...

sp_help table_name--显示表已有特征

————常用函数

————统计函数

avg --求平均值

count --统计数目

max --求最大值

min --求最小值

sum --求和--avg

--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 = (sele