Oracle SQL学习入门
【 目录 】
一、语法基础
1、数据类型
2、操作符
(1)数学运算符
(2)比较运算符
(3)SQL运算符
3、函数
(1)字符函数
(2)数学函数
(3)转换函数
常见日期格式
yyyy/mm/dd hh24:mi:ss day 2017/03/31 17:45:45 星期五
(4)聚合函数
二、数据表
1、创建表
(1)通过定义列创建表
Create table 表名 ( 变量名 变量类型 其他 , 变量名 变量类型 其他 )
例如:
Create table sample ( name nvarchar2(100) not null , --不能为空 age number , sex nvarchar2(10) , birth date default sysdate --默认值为系统日期 );
(2)通过选择集创建表
Create table 表名 as select 语句
例如:
Create table sample as select * from A where A.name is not null
2、删除表
3、修改表
(1)增加字段
alter table 表名 add(字段名 字段类型)
例如:
Alter table sample add (birth date)
(2)删除字段
alter table 表名 drop column 字段名
例如:
Alter table sample add column birth
(3)修改字段类型
alter table 表名 modify 字段名 字段类型
例如:
Alter table sample modify birth number(8)
(4)重命名表
alter table 表名 rename to 新表名
例如:
Alter table sample rename to sample2
4、表查询
(1)单表查询
查询全部
Select * from sample
查询某几列
Select t.name,t.value from sample t
查询某一列的去重值
Select distinct t.name from sample t
查询前100条
Select * from sample where rownum < = 100
数据排序
Select * from sample t order by t.value (asc/desc)
(2)分组查询
分组求行数
Select count(t.category) from sample t group by t.category
分组求和
Select sum(t.value) over(partition by t.category) from sample t
分组求均值
Select sum(t.value)/ count(t.category) from sample t group by t.category
多条件分组
Select count(t.category) from sample t group by t.category,t.regionname
(3)自定义查询
自定义查询结果
Select case t.sex when 1 then‘男’when 2 then‘女’ end from sample t
自定义排序
Select t.category from sample t order by case when t.category=‘常熟’then 0 when t.category=‘昆山’ then 1 end asc
(4)多表连接
5、表更新
(1)insert
插入指定字段
Insert into B(name,value) select A.name, 100 as value from A
(2)update
更新指定字段
update B set B.value= (select A.value from A where B.name in A.name)
出现问题后
update B set B.value= (select A.value from A where B.name in A.name and rownum=1) where exists (select 1 from A where B.name in A,name and rownum=1)
三、视图
创建视图 create or update view 视图名 as select语句 查询视图 select * from 视图名
四、存储过程
1、基本结构
CREATE OR REPLACE PROCEDURE 存储过程名称 ( 参数1 IN 类型, 参数2 IN 类型 ) IS 变量1 INTEGER :=0; 变量2 DATE; BEGIN 存储体 END 存储过程名称
2、条件语句
IF something THEN BEGIN do something END; END IF;
3、循环语句
(1)for循环
for i in 1 .. 100 loop end loop;
(2)while循环
i :=1; while i<100 loop end loop;
(3)loop循环
i :=1; loop if i>100 then exit; end if; end loop;
4、游标
定义游标:cursor 游标名 is 选择集; 使用游标: for i in 游标名 loop end loop;
5、集合
(1)数组
Tips:最大元素个数为10个
declare type type1 is varray(10) of varchar2(30); v1 type1:=type1('1','2','3'); begin for i in 1..v1.count loop dbms_output_line(v1(i)); end loop; end;
(2)嵌套表
Tips:必须进行初始化
declare type type1 is table of varchar2(30); v1 type1:=type1(); v2 type1:=type1(‘男’,’女’); begin for i in 1..v2.count loop v1(i):=i; dbms_output.put_line(v1(i)); dbms_output.put_line(v2(i)); end loop; end;
6、SQL执行体
declare type type1 is table of varchar2(10); v1 type1 := type1('常熟', '昆山', '张家港'); sql varchar2(1000); begin for i in 1 .. v1.count loop sql:= 'insert into test(id,name,value) select '''||i||''' as id, '''||v1(i)||''' as name,t.value as value from sample t'; execute immediate sql; commit; end loop; end;
7、异常处理
Tips:放于执行体之后,end之前
exception when others then err:=sqlerrm; dbms_output.put_line(err); rollback;
五、触发器
create or replace trigger 触发器名 before / after insert or delete or update on 表名 begin if(to_char(sysdate,'DY')='星期日') then raise_application_error(-20600,'不能在周末修改表A'); end if; end;