Oracle笔记

连接管理员账户,

本机中的sqlplus 可以在操作系统命令行里执行 sqlplus / as sysdba

1.当前用户

show user;

2.sql语句

DML语句(数据操作语言)Insert、Update、 Delete、Merge 

DDL语句(数据定义语言)Create、Alter、 Drop、Truncate 

DCL语句(数据控制语言)Grant、Revoke 

事务控制语句Commit 、Rollback、Savepoint 

 

3.count

如果数据库表的没有数据,count(*)返回的不是null,而是0 

 

4.case 语句

demo

Select avg(sal),avg(nvl(comm,0)),
(case 
when avg(nvl(comm,0))>300 then '奖金不错' 
when avg(nvl(comm,0))<100 then '奖金较差' 
else '奖金一般' 
end) 
as 奖金状况
from emp 
group by job 
order by job desc,avg(sal) desc;

结果

  AVG(SAL) AVG(NVL(COMM,0)) 奖金状况
---------- ---------------- ----
      1400              550 奖金不错
      5000                0 奖金较差
2758.33333                0 奖金较差
    1037.5                0 奖金较差
      3000                0 奖金较差

5.mysql没有这几个功能

INTERSECT  交集

MINUS 返回差异的记录 

6.topN,这个跟mysql有点不一样

select * from (select * from emp order by sal desc)
where rownum <= 5 order by sal desc;

7.分页,第一种比第二种要好一些

select * from (select rownum no,e.* from (select * from emp order by sal desc) e where rownum<=5 ) where no>=3;
select * from (select rownum no,e.* from (select * from emp order by sal desc) e) where no>=3 and no<=5;

列出员工表中每个部门的员工数(员工数必须大于3),和部门名称 

select d.* ,ed.cou from dept d,(select deptno,count(*) cou from emp group by deptno having count(*)>3) ed where d.deptno=ed.deptno; 

 

8.Oracle主要数据类型 

Char,nchar,varchar2,nvarchar2,number(),date,blob(binary二进制流的大对象),clob(文件大对象) 

 

 

1、 由于char是以固定长度的,所以它的速度会比varchar2快得多!但程序处理起来要麻烦一点,要用trim之类的函数把两边的空格去掉 

2Varchar2一般适用于英文和数字,Nvarchar2适用中文和其他字符,其中N表示Unicode常量,可以解决多语言字符集之间的转换问题 

3、 Number(4,2) 指的是整数占2位,小数占2位(99.994可以。99.995不行,因为是四舍五入) 

4、 Number默认为38 

 

9.清空表中数据 

Truncate table student;

10.条件约束

age NUMBER CHECK(age BETWEEN 0 AND 150)

外键

pid NUMBER REFERENCES person(pid) ON DELETE CASCADE;

CONSTRAINT book_pid_fk FOREIGN KEY(pid) REFERENCES person(pid)

ALTER TABLE book ADD CONSTRAINT person_book_pid_fk FOREIGN KEY (pid) REFERENCES person(pid) ON DELETE CASCADE ; 

14.7、删除约束: 

ALTER TABLE book DROP CONSTRAINT person_book_pid_fk ; 

alter table student drop unique(tel); 

14.8、 启用约束 

ALTER TABLE book enable CONSTRAINT person_book_pid_fk ; 

14.9、 禁用约束 

ALTER TABLE book disable CONSTRAINT person_book_pid_fk ; 

11、索引

select * from user_indexes 查询现有的索引 

select * from user_ind_columns 可获知索引建立在那些字段上 

12、创建数据库,比较麻烦,参考这个网址,需要sys权限,不过注意的是,第一步,创建两个文件时,必须把使用的所有文件夹都创建好

http://www.cnblogs.com/0201zcr/p/4669548.html

13、主键自增

建立表

建立序列

建立触发器

原文地址:https://www.cnblogs.com/lakeslove/p/7248076.html