返回顶部
扩大
缩小

Heaton

oracle命令2

使用DDL创建和管理表

DBA角色:拥有全部特权,是系统最高权限,只有DBA才可以创建数据库结构,并且系统权限也需要DBA授出,且DBA用户可以操作全体用户的任意基表,包括删除

grant dba to user;   --user为用户表名

进入system用户下给用户赋予DBA权限,否则无法正常登陆

 

--创建表

create table test1 (tid number,tname varchar2(20));

 

--rowid 行地址

select rowid,empno,ename,sal from emp;

通过rowid可以查询到数据

select * from emp where rowid='AAAMfPAAEAAAAAgAAJ';

如果只想要表结构,那么只需要在子查询后加上一个where 1=2(假条件)

--子查询创建表:保存20号部门的员工

create table emp20 as select * from emp where deptno=20;

--创建表:员工号 姓名  月薪 年薪 部门名称

create table empinfo as select e.empno,e.ename,e.sal,e.sal*12 annsal,d.dname from emp e,dept d where e.deptno=d.deptno;

 

 --修改表:增加新列,修改列,删除列,重命名列,重命名表

 SQL> desc test1

--增加新列

 alter table test1 add photo blob;

--修改列

 alter table test1 modify tname varchar2(40);

--删除列

 alter table test1 drop column photo;

--重命名列

alter table test1 rename column tname to username;

--重命名表

rename test1 to test2;

--删除表

 

 drop table TESTDELETE;

--查看回收站

show recyclebin;

 --清空回收站

purge recyclebin;

 

---删除表情况2

--注意:管理员没有回收站

--闪回删除 ---> 回收站

flashback table emp20 to before drop;

oracle 10g有6种 11g有7种闪回  : 闪回删除/闪回表/闪回查询/闪回版本查询/闪回事务查询/闪回数据库/闪回数据归档(11g才有)

约束:

在数据库开发中,约束是必不可少的,使用约束可以更好的保证数据的完整性。在Oracle数据库中,约束的类型包括:

主键约束:Primary Key

非空约束:Not null

唯一约束:Unique

外键约束:Foreign Key

检查性约束:Check

  create table student

  (

   sid number constraint student_pk primary key,

  sname varchar2(20) constraint student_name_notnull not null,

  gender varchar2(2) constraint student_gender check (gender in ('男','女')),

  email varchar2(40) constraint student_email_unique unique

            constraint student_email_notnull not null,

  deptno number constraint student_fk references dept(deptno) on delete set null

  );

外键约束:

FOREING KEY:在字表中,定义一个表记的约束

REFERENCES:指定表和父表中的列

ON DELETE CASCADE:当删除父表时,级联删除字表记录

ON DELETE SET NULL:将字表的相关依赖记录的外键值置为null

 

 

其他数据库对象

什么是视图:视图就是封装了一条复杂查询的语句。

      视图是一个虚表。最大的优点就是简化复杂查询。

create or replace view empinfoview as 

  select e.empno,e.ename,e.sal,e.sal*12 annsal,d.dname

  from emp e,dept d

    where e.deptno=d.deptno

  with read only;

 

 

注意:不建议通过视图对表进行修改

删除视图:drop view view名;

 

 

 

什么叫序列:主要是用于提供主键值,以供主键增长,存在内存中,提升访问效率。

 

create sequence myseq;

 

 

 

 

 

 什么是索引:索引是用于加速数据存取的数据对象。合理的使用索引可以大大降低  i/o  次数,从而提高数据访问性能。(如:书的目录)

 

--SQL的执行计划

 explain plan for select * from emp where deptno=10;

 select * from table(dbms_xplan.display);

--创建目录(索引)

 create index myindex on emp(deptno);

explain plan for select * from emp where deptno=10;

通过执行计划的CPU可以看出有索引的更好;

 

 

 

 

select count(*) from hr.employees;

--为hr.employees起别名  ---> 同义词

 

create synonym hremp for hr.employees;

select count(*) from hremp;

 

 

posted on 2017-12-20 21:48  咘雷扎克  阅读(229)  评论(0编辑  收藏  举报

导航