博客总结十三——ORACLE

1、select distinct ..from ..where..order by..
2、decode()
select e.ename,e.sal,d.dname,
decode(s.grade,1,'fifth',2,'forth',3,'third',4,'seconde',5,'first'),m.ename,m.sal,

decode(ms.grade,1,'fifth',2,'forth',3,'third',4,'seconde',5,'first')

from emp e,dept d,salgrade s,emp m,salgrade ms

where e.deptno=d.deptno AND e.sal BETWEEN s.losal AND 
s.hisal AND e.mgr=m.empno AND m.sal BETWEEN ms.losal AND ms.hisal;
3、select e.empno,e.ename,d.deptno,d.dname,d.loc
from emp e,dept d
where e.deptno(+)=d.deptno;
(+)在=的左边为右连接,表示如果d中有空项,则也会显示
(+)在=的右边为左连接,表示如果e中有空项,则也会显示
4、
SELECT * FROM emp CROSS JOIN dept;--交叉连接,产生笛卡尔积
SELECT * FROM emp NATURAL JOIN dept;--自然连接,自动进行关联字段的匹配
SELECT * FROM emp e JOIN dept d USING(deptno) WHERE 
deptno=30;--直接关联的操作列
SELECT * FROM emp e JOIN dept d ON(e.deptno=d.deptno) 
WHERE deptno=30;--ON,用户自定义连接的条件
SELECT e.ename,d.deptno,d.dname,d.loc
FROM emp e RIGHT OUTER JOIN dept d
ON(e.deptno=d.deptno);--右连接
5、
SELECT COUNT(empno) FROM emp;
SELECT MIN(sal) FROM emp;
SELECT MAX(sal) FROM emp;--SUM(sal)\AVG(sal)
6、
SELECT deptno,COUNT(deptno) FROM emp GROUP BY deptno;
SELECT deptno,AVG(sal) FROM emp GROUP BY deptno;

SELECT d.dname,COUNT(e.empno) FROM dept d,emp e WHERE 

d.deptno=e.deptno GROUP BY d.dname;

SELECT deptno,AVG(sal) FROM emp WHERE GROUP BY deptno HAVING AVG(sal)>200
SELECT ename,SUM(sal),su FROM emp WHERE job<>'SALESMAN' GROUP BY job HAVING SUM(sal)>5000 ORDER BY su;
SELECT MAX(AVG(sal)) FROM emp GROUP BY deptno;
7、
SELECT * FROM emp WHERE sal>(SELECT sal FROM emp WHERE empno=7654);
SELECT * FROM emp WHERE sal>(SELECT sal FROM emp WHERE empno=7654) AND job=(SELECT job FROM emp WHERE empno=7788);
SELECT * FROM emp WHERE sal=(SELECT MIN(sal) FROM emp);
SELECT d.dname,ed.c,ed.a,e.ename FROM dept d,(SELECT deptno,COUNT(empno) c,AVG(sal) a,MIN(sal) min FROM emp GROUP BY deptno) ed,emp e WHERE d.deptno=ed.deptno AND e.sal=ed.min;
SELECT * FROM emp WHERE sal IN(SELECT MIN(sal) FROM emp GROUP BY deptno);
SELECT * FROM emp WHERE sal=ANY(SELECT MIN(sal) FROM emp GROUP BY deptno);
SELECT * FROM emp WHERE sal>ANY(SELECT MIN(sal) FROM emp GROUP BY deptno);
8、
CREATE TABLE copy AS SELECT * FROM emp;
INSERT INTO copy(empno,ename,job,mgr,hiredate,sal,comm,deptno) VALUES(7899,'FENG','CLEAN','7369','14-2月-1995',9000,300,40);
TO_DATE('2009-01-16','yyyy-mm-dd');
UPDATE copy SET comm=1000;
UPDATE copy SET sal=5000 WHERE empno=7899;
UPDATE copy SET mgr=null,comm=null WHERE empno IN(7369,8899,7788);
DELETE FROM copy WHERE empno=7899;
DELETE FROM copy;--删除整张表的内容
9、
CREATE TABLE emp10 AS SELECT * FROM emp WHERE deptno=10;
需要进行事务提交,不然其他窗口无法查询
10、
VARCHAR\VARCHAR2() 范围:0-255
NUMBER(n) n长度的数字
NUMBER(m,n) m长度,n为小数部分的长度
DATE 时间格式存放,TO_DATE()
CLOB 大对象,大文本数据,存放4G
BLOB 大对象,二进制数据,存放4G
11、
CREATE TABLE person(pid VARCHAR2(18),name VARCHAR2(200),age NUMBER(3),birthday DATE,sex VARCHAR2(2) DEFAULT '男');

DROP TABLE person;

ALTER TABLE person ADD(address VARCHAR2(200) DEFAULT '暂无地址');

ALTER TABLE person MODIFY(name VARCHAR2(20) DEFAULT '无名氏');
RENAME person TO tperson;
TRUNCATE TABLE tperson;
12、
主键约束:
pid VARCHAR2(18)PRIMARY KEY
非空约束:
name VARCHAR2(200) NOT NULL
唯一约束:
name VARCHAR2(200) UNIQUE NOT NULL
检查约束:
age NUMBER(3) NOT NULL CHECK(age BETWEEN 0 AND 150)
主外键约束:
CONSTRAINT person_book_pid_fk FOREIGN KEY(pid) REFERENCES person(pid)
使用person的pid作为book的主外键
先删子表,再删父表
13、
ROWNUM:伪列
SELECT ROWNUM,empno,ename,job,sal,hiredate FROM emp WHERE ROWNUM<=5;--显示前五行
14、
视图:一个视图实际上就是封装了一条复杂的查询语句
CREATE VIEW empv20 AS SELECT empno,ename,job,hiredate FROM emp WHERE deptno=20;
SELECT * FROM empv20;
DROP VIEW empv20;
UPDATE empv20 SET deptno=30 WHERE empno=7369;
CREATE VIEW...WITH CHECK OPTION;
CREATE VIEW...WITH READ ONLY;
15、
CREATE SEQUENCE myseq;
myseq.nextVal:取得序列的下一个内容
myseq.currVal:取得序列的当前内容
DROP SEQUENCE myseq;
CREATE SEQUENCE myseq INCREMENT BY 2 START WITH 10;
CREATE SEQUENCE myseq MAXVALUE 10 INCREMENT BY 2 START WITH 1
16、
exp:数据库备份
imp:数据库恢复

17、范式

18、索引

UNIQUE:唯一索引:不允许两行具有相同的索引值
主键索引:要求主键中的每一个值是唯一的,创建主键自动创建主键索引
CLUSTERED:聚集索引:表中各行的物理顺序与键值的逻辑顺序相同
NONCLUSTERED:非聚集索引:表中各行的物理顺序与键值的逻辑顺序不匹配

CREATE UNIQUE COUSTERED INDEX myclumn_cindex ON mytable(mycolumn)

CREATE INDEX name_index ON username(firstname,lastname)

DROP INDEX mytable.mycolumn_index

你可能感兴趣的:(2014)