`
豆慧
  • 浏览: 7278 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

马士兵老师oracle视频学习笔记(四)

阅读更多
创建视图:
create view min_avg_sal as select min(avgsal) minavgsal from (select deptno,avg(sal) avgsal from (select * from emp where empno in (select mgr from emp)) t group by t.deptno);

--求比普通员工的最高薪水还要高的经理人名称
select ename from emp where empno in(select mgr from emp where mgr is not null) and sal >
(select max(sal) from emp where empno not in(select mgr from emp where mgr is not null))

--求薪水最高的前5名雇员
select ename,sal,rownum rn from (select ename,sal from emp  order by sal desc) where rownum < 6;

--求薪水最高的第6到10名雇员(重点掌握)
select ename,sal from (select ename,sal,rownum rn from (select ename,sal from emp  order by sal desc)) where rn between 6 and 10;

--面试题:比较效率
select * from emp where deptno = 10 and ename like '%A%'; 效率高
select * from emp where ename like '%A%' and deptno = 10;

创建新用户:conn system/system as sysdba;
drop user douzi cascade; --(没有这个用户名的话不用删除)
backup scott; --备份scott用户的所有资料
exp; --导出信息 在哪个文件夹下操作就导出到哪个文件夹下
scott/scott --导出scott用户的信息 默认为导出用户所有信息
create user douzi identified by douzi default tablespace users quota 10M on users; --identified (被认同 赋值密码) --quota (配额 分配空间)
grant create session,create table,create view to douzi; --将登陆,建表,

建视图的权限给tmp用户
import 导入数据
tmp/tmp

下面选择默认就行。但随后还会输入一次用户名 则输入scott/scott
备份表的信息:create table emp2 as select * from emp;

事务:transaction 开始于一条dml语句 ,用rollback或commit结束。碰见ddl(如create语句)或dcl(r如grant语句)语句也会结束。

当用exit正常退出时transaction自动提交,当直接关闭等非正常关闭时transaction自动回滚。
建表:create table student (
id number(6),
name varchar2(20) constraint stu_name_nn not null,
sex number(1),
age number(3),
createdate date,
grade number(2) default 1,
class number(4),
email varchar2(50) unique
);
unique(唯一性约束)

alter常用语句:
alter table student add (addr varchar2(100));
alter table student modify (addr varchar2(150));
alter table student drop (addr);

索引:  create index idx_student_email on student(email);
--给学生表的email建立索引。如果括号里是多个字段的话就为这几个字段的组合建索引。
drop index idx_student_email;
select index_name from user_indexes; --查看所有索引

序列: create sequence seq;
select seq.nextval from dual;
数据库设计三范式: 原则是不存在冗余数据。
第一范式的两个条件:要有主键,列不可分
第二范式:如果是双主键的话,里面的字段不能出现部分依赖。
第三范式:不能出现传递依赖.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics