oracle笔记四(常用操作)
事务处理
显示结束事务:commit/rollback(rollback默认回滚到事务最初端)
隐式提交事务:DDL/DCL
保存点: savepoint(提交以后保存点释放)
Savepoint A;
Rollback to A;
用户字典:user_objects;(用户名下的对象)
查看用户定义的表:
Select object_name from user_objects where object_type=’TABLE’;
Select table_name from user_tables;(用户所有表集)
查看用户名下的对象种类:
Select distinct object_type from user_objects;
用户名下的对象 user_objects;
用户可以访问的对象 all_objects
数据库所有的对象(DBA身份才可访问)dba_objects;
修改表结构:
加一新列:
Alter table emp1 add (empno varchar2(6) default ‘00001’);
修改原有列:
Alter table emp1 modify (empno varchar2(7));
删除列:
Alter table emp1 drop column empno [cascade];
或:
Alter table emp1 set unused column job;
Alter table emp1 drop unused column;
重命名:
Rename emp1 to emp_c;
清空表:(DDL操作,不可回滚)
Truncate table emp_c;
表的注释:
Comment on table emp_c is ‘my emp info’;
查看表注释:
Select * from user_tab_comments where table_name=’EMP_C’(表名必须大写);
约束:
1. not null (只能加在列级,所以在建表之后增加只能使用modify)
2. unique
3. primary key
4. foreign key on delete cascade/on delete set null
5. check
查询约束条件的名字;
Select constraint_name from user_constraints where table_name=’EMP1’;
Select constraint_name,column_name from user_cons_columns where table_name=’EMPLOYEE’;
失效约束与激活约束:
Alter table employee disable constraint sys_c005173;
Alter table employee enable constraint sys_c005173;
查询用户名下的视图:
Select view_name,text from user_views;
Select object_name from user_objects where object_type=’VIEW’;
复杂视图不支持进行DML操作;
尽量不要通过对简单视图进行DML操作;
简单视图不能阻止这种行为;
使用特定语法阻止这种行为(with read only)
临时视图:
获取到薪水最高的无名员工信息:
Select rownum num,empno,ename,sal,job
From (select * from emp where sal is not null order by sal desc)
Where rownum<=5;
创建序列:
Create sequence myseq;(默认的起始值和步进均为1);
Create sequence myseq start with 100 increment by 10 maxvalue 9999 nocache nocycle
获取下一个序列值:
Select myseq.nextval from dual;
获取序列当前值:
Select myseq.currval from dual;
通过数据字典查找序列:
Select sequence_name from user_sequences;
Select object_name from user_objects where object_type=’SEQUENCE’;
序列应用:(创建主键)
Insert into dept values(seq.nextval,’product’,’beijing’);
索引:(加快查询速度,但不利于DML操作过频繁的表)
Create index emp_ename_idx
On emp(ename);
查询索引:
User_indexes;
User_objects;
User_ind_columns;
同义词:
Create synonym d_emp from emp;
Create public synonym sy_e from [scott.]emp;(不加public 只能本人使用,加了public 其他用户也可使用)
Drop synonym d_emp;