SQL 是关系型数据库的操做指令,是一种约束,但不强制,相似于 W3C,所以这意味着:不一样的数据库产品(如 Oracle 和 MySQL)内部可能会有一些细微的区别。sql
在 SQL 中,将数据类型分红了三大类,分别为:数值型、字符串型和日期时间型。数据库
Oracle经常使用命令的使用:(以scott用户的默认建立的表作测试)编程
命令图示:session
基本查询:数据结构
/**EMP雇员--DEPT部门--SALGEADE销售分数**/
select *from emp;
select * from dept;
select * from salgrade;
/**分组和聚合函数一块儿用**/
select distinct count(*) as empnum from emp group by deptno order by empnum desc;
/**别名查询(若是有特殊字符用双引号)和去重查询**/
select empno " 雇员 编号 ", ename " 雇员 名称 " from emp;
select distinct deptno from emp;
/**对结果作运算**/
select empno,ename,sal+999 from emp;
select * from emp where not JOB = 'CLERK';
select * from emp where sal in (800,1000,1200,1400,1600,1800);
select * from emp where comm is not null;
select * from emp where comm is null;
select * from emp where sal between 1000 and 2900;
/**模糊查询**/
select * from emp where ename like '%T_';
/**带有特殊符号的查询使用转义字符,关键字指出转义符号**/
select * from emp where ename like '%\%R%' escape '\';
/**空值处理、空值参与的运算都会致使结果为空**/
select (sal+nvl(comm,0))*12 年薪 from emp where empno = 7369;
select * from emp order by comm desc nulls first;/**空值排在前面**/
select * from emp order by comm desc nulls last;/**空值排在后面**/
app
/**经常使用函数--dual 虚表, 意义:补全语法**/ide
replace,substr,length,||,to_char,to_date,round,trunc,mod,decode,case when,nvl,Coalesce,count,max,min,avg,sum函数
/**字符串的操做**/select*fromempwherehiredate>to_date('1981/1/1','yyyy.mm.dd')and hiredate<to_date('1981/12/31','yyyy.mm.dd')oop
/**数据处理**/--union all测试
--row是一个伪列,rowid是数据存储的物理地址
select rownum,rowid,ename from emp;
--找出工资表中工资最高的前三名
select rownum,ename,sal from emp order by sal desc;
--要先按照降序排序后再使用伪列生成的序号进行显示
select rownum,ename,sal from (select ename,sal from emp order by sal desc) where rownum<=3;
select rownum,t.* from (select ename,sal from emp order by sal desc) t where rownum<=3;
/**Oracle的分页补充:
查询员工表中的第5条记录到第10条记录.
<=5 and >=10
*/
--rownum--每当按照条件查询出一条记录,则当前rownum+1生成对应的序号加到当前记录上,可是若是当前没有查询出知足条件的结果时,rownum固然不会自动增加了.rownum从1开始
select * from ( select rownum rn,e.* from emp e where rownum<=10) t where t.rn>=5;
/**select * from emp union all select * from emp;
--工资大于1500,或者是20部门下的员工select * from emp where sal>1500 or deptno = 20;
select * from emp where sal>1500控制语句:
--建立表空间
create tablespace yang
datafile 'c:\yang.dbf'
size 50M
autoextend on
next 10M ;
--给用户设置建立的表空间
alter user yanghao identified by yanghao default tablespace yang;
--查看用户的表空间
select username, default_tablespace from user_users;
--建立一张用户帐号表
create table counts (id number(10) primary key,username varchar2(20),password varchar2(20),description clob);
--删除表
drop table counts;
--插入数据
insert into counts values(1,'1','hahaha1','good1 ideas');
insert into counts values(2,'2','hahaha2','good2 ideas');
insert into counts values(3,'3','hahaha3','good3 ideas');
insert into counts values(4,'4','hahaha4','good4 ideas');
insert into counts values(5,'5','hahaha5','good5 ideas');
insert into counts values(6,'6','hahaha6','good6 ideas');
--查看表数据
select * from counts;
--建立当前操做的一个保存点(方便回滚到固定的保存状态)
savepoint a_point;
rollback to a_point;
commit;
--建立一个counts表的试图,只显示当前1~3标号的用户
create or replace view count_view as
select * from counts where id<=3 with read only;
--查看当前的视图
select * from count_view;
--事务
--4个特性(ACID)
--A: 原子性, 不可再分割
--C: 一致性,要么成功,要么失败
--I: 隔离性 (脏读,不可重复读(update),虚/幻读(insert,delete))
--D: 持久化性, 事务一旦提交,就会保存到磁盘中
--MySql支持的事务级别有哪些?
-可重复读(Repeatable Read,默认),提交读(Read Committed),序列化(Serializable), 未提交读(Read Uncommmited)
--Oracle支持的事务级别
-- Read Committed(默认), Serializable, read only
--序列数据库提供一种数值类型,且按必定规则自动增加的序号 场景:须要自动增加主键,或须要自动增加的序号存储结构、数据库编程:
/**pl/sql编程**/
declare
i number(10):=incre_key.nextVal;
name counts.username%type;
psd counts.password%type;
begin
name:='test';
psd:='test'
insert into counts values(i,name,psd,'test');
commit;
dbms_output.put_line('name: '||test||'psd: 'psd);
end;
/**游标的使用**/
--使用游标输出emp表中的员工编号和姓名
declare
cursor pc is select * from scott.emp;--游标(返回整个emp表的数据)
pemp scott.emp%rowtype;
begin
open pc;--开启游标
loop
fetch pc --调用一次游标
into pemp; --把游标当前行的结果存到变量pemp中
dbms_output.put_line(pemp.empno||' '||pemp.ename);
exit when pc%notfound;
end loop;
close pc;--关闭游标
end;
/**例外/异常**/ declare pnum number; pnum2 varchar(10); begin pnum:=1/0; exception when zero_divide then dbms_output.put_line('除零异常'); when value_error then dbms_output.put_line('数值转换异常'); when others then dbms_output.put_line('其余异常'); end;