oracle知识点小结3(子查询)
oracle知识点小结3(子查询)
一.子查询的使用
1.insert
用子查询在insert中进行插入数据时,要注意不要加上Vaules关键字。
SQL> create table copy_dep as select * from departments; Table created. SQL> truncate table copy_dep; Table truncated. SQL> insert into copy_dep 2 select * from departments; 27 rows created.
2.create
用子查询来创建表时,在原表中只有not null会被传递到新表,其它约束不会传递。
在用子查询创建表和视图时,将表达式加上别名是必须的;
SQL> create table dept 2 as 3 select employee_id,last_name,salary*12,hire_date 4 from employees where department_id=80; select employee_id,last_name,salary*12,hire_date
*
ERROR at line 3:
ORA-00998: must name this expression with a column alias
修改,加上别名
SQL> create table dept(emp_id,name,salary,hire_date) 2 as select employee_id,last_name,salary*12,hire_date 3 from employees where department_id=80; Table created.
或者
SQL> create table dept 2 as 3 select employee_id,last_name,salary*12 ANNSAL,hire_date 4 from employees where department_id=80; Table created.
3.update
用子查询update时,可以从其它表中获得数据。
SQL> select * from copy_dep; DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID ----------------------- ------------------------------ ----------------- ------------------ 10 Administration 200 1700 20 Marketing 201 1800 30 Purchasing 114 1700 40 Human Resources 203 2400 50 Shipping 121 1500 60 IT 103 1400 SQL> update copy_dep set department_name=(select department_name from departments where department_id=10), 2 location_id=(select location_id from departments where department_id=10) 3 where department_id=50; 1 row updated. SQL> select * from copy_dep; DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID ------------- ------------------------------ ---------- ----------- 10 Administration 200 1700 20 Marketing 201 1800 30 Purchasing 114 1700 40 Human Resources 203 2400 50 Administration 121 1700 60 IT 103 1400
4.read-only表
表改为read-only可以阻止对表的增删改,但不被阻止删除表。
SQL> alter table dept read only; Table altered. SQL> delete from dept where employee_id=170; delete from dept where employee_id=170 * ERROR at line 1: ORA-12081: update operation not allowed on table "HR"."DEPT" SQL> alter table dept read write; Table altered. SQL> delete from dept where employee_id=170; 1 row deleted. SQL> alter table dept read only; Table altered. SQL> drop table dept; Table dropped.
5.View
简单视图:只要一张表,不使用函数;复杂视图:一张或者多张表,使用函数。
一般来说可以对简单视图执行DML,但并非总是如此,如果视图不包括具有NOT NULL的强制列,那么对视图进行insert操作就不会成功,复杂视图不能执行DML。
创建个视图
SQL> create view empvu 2 as select employee_id,last_name,salary from employees where department_id=80; View created. SQL> desc empvu Name Null? Type ----------------------------------------- -------- ---------------------------- EMPLOYEE_ID NOT NULL NUMBER(6) LAST_NAME NOT NULL VARCHAR2(25) SALARY NUMBER(8,2) SQL> select * from empvu; EMPLOYEE_ID LAST_NAME SALARY ----------- ------- ------------------ ---------- 167 Banda 6200 168 Ozer 11500 169 Bloom 10000 170 Fox 9600 171 Smith 7400 172 Bates 7300
把视图中employee_id=169的last_name=Bloom修改为last_name=’Blooms’。
SQL> update empvu set last_name='Blooms' 2 where employee_id=169; 1 row updated. SQL> commit; Commit complete.
现在查看一下基表有没有发生修改,
SQL> select employee_id,last_name,salary from employees where employee_id=169; EMPLOYEE_ID LAST_NAME SALARY ----------- --------- ---------------- ---------- 169 Blooms 10000
基表也修改了。
对视图的修改成功,也是对基表的修改。
对于在创建视图时不加上with check option。
SQL> create table emp as select * from employees; Table created. SQL> create or replace view empvu1 2 as select * from emp where department_id=20; View created. SQL> select employee_id,last_name,department_id from empvu1; EMPLOYEE_ID LAST_NAME DEPARTMENT_ID ----------- ------------ ------------- ------------- 201 Hartstein 20 202 Fay 20 SQL> update empvu1 set department_id=10 where employee_id=202; 1 row updated. SQL> select employee_id,last_name,department_id from empvu1; EMPLOYEE_ID LAST_NAME DEPARTMENT_ID ----------- ------------------------- ------------- 201 Hartstein 20
可以将where子句中 department_id=20修改为department_id=10,从而导致了这行的消失。
用with check option来创建视图,可以防止导致行从视图中消失的DML操作。
SQL> create or replace view empvu1 2 as select * from emp where department_id=20 with check option; View created. SQL> select employee_id,last_name,department_id from empvu1; EMPLOYEE_ID LAST_NAME DEPARTMENT_ID ----------- - ---------------------- -- ------------- 201 Hartstein 20 SQL> update empvu1 set department_id=10 where e
- 更多Oracle疑问解答:
- 运行exp备份oracle数据库提示oracle-12154错误
- 有没有,生产Oracle Rman 备份脚本的工具啊!
- 初学orcle,希望有大大帮忙解说一下详细步骤,从登录oracle到创建表的过程
- oracle语句问题:一张user表,三个字段,id,name,time,插入记录比如:张三2007,李四2008,张三2011
- 如何写一个ORACLE触发器同步两个表中的数据?
- oracle 如何查看一个服务器上有多少个数据库.
- oracle 创建包的时候错误 求解
- oracle 重复列的问题
- oracle 中如何查处2星期前的数据
- 请教oracle数据库安装中的问题
- 请问谁能提供给我标准的oracle ERP的数据库表结构并详细说明各表主要的作用?
- 安装oracle遇到的问题 invalid entry CRC (expected 0x3e12e795 but got 0x9db0e9fd)
- 我的是ORACLE 10G,在RMAN中如何按指定的时间恢复数据文件啊?
- oracle为什么没有自动增长列
- oracle快捷键都有哪些啊?