Oracle入门基础(九)一一创建表和管理表
练习:查询每一年入职人数及总人数
SQL> select count(*) Total,
2 sum(decode(to_char(hiredate,'yyyy'),'1980',1,0)) "1980",
3 sum(decode(to_char(hiredate,'yyyy'),'1981',1,0)) "1981",
4 sum(decode(to_char(hiredate,'yyyy'),'1982',1,0)) "1982",
5 sum(decode(to_char(hiredate,'yyyy'),'1987',1,0)) "1987"
6 from emp;
TOTAL 1980 1981 1982 1987
---------- ---------- ---------- ---------- ----------
14 1 10 1 2
SQL> --创建表
SQL> create table test1
2 (tid number,tname varchar2(20));
SQL> --rowid 行地址
SQL> select rowid,empno,ename,sal from emp;
ROWID EMPNO ENAME SAL
------------------ ---------- ---------- ----------
AAAMfPAAEAAAAAgAAA 7369 SMITH 800
AAAMfPAAEAAAAAgAAB 7499 ALLEN 1600
SQL> select * from emp where rowid='AAAMfPAAEAAAAAgAAJ';
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
已选择 1 行。
SQL> --创建表:保存20号部门的员工
SQL> create table emp20 as select * from emp where deptno=20;
SQL> --创建表:员工号 姓名 月薪 年薪 部门名称
SQL> 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> --修改表:增加新列,修改列,删除列,重命名列,重命名表
SQL> --增加新列
SQL> alter table test1 add photo blob;
SQL> --修改列
SQL> alter table test1 modify tname varchar2(40);
SQL> --删除列
SQL> alter table test1 drop column photo;
SQL> --重命名列
SQL> alter table test1 rename column tname to username;
SQL> --重命名表
SQL> rename test1 to test2;
SQL> --删除表
SQL> drop table TESTDELETE;
SQL> --查看回收站
SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
TESTDELETE BIN$z6+GnIyhQtaIrbPpN1H99A==$0 TABLE 2016-10-06:15:24:14
SQL> --清空回收站
SQL> purge recyclebin;
回收站已清空。
SQL> select * from TESTSAVEPOINT;
TID TNAME
---------- --------------------
1 Tom
2 Mary
已选择 2 行。
SQL> drop table TESTSAVEPOINT;
表已删除。
SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
TESTSAVEPOINT BIN$384BF4yOT+aAlXzC7eLPIA==$0 TABLE 2016-10-06:15:28:07
SQL> select * from TESTSAVEPOINT;
select * from TESTSAVEPOINT
*
第 1 行出现错误:
ORA-00942: 表或视图不存在
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT TABLE
BIN$384BF4yOT+aAlXzC7eLPIA==$0 TABLE
SQL> select * from "BIN$384BF4yOT+aAlXzC7eLPIA==$0";
TID TNAME
---------- --------------------
1 Tom
2 Mary
已选择 2 行。
SQL> --注意:管理员没有回收站
SQL> show user
USER 为 "SCOTT"
SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
TESTSAVEPOINT BIN$384BF4yOT+aAlXzC7eLPIA==$0 TABLE 2016-10-06:15:28:07
SQL> --闪回删除 ---> 回收站
SQL> flashback table TESTSAVEPOINT to before drop;
闪回完成。
SQL> show recyclebin;
SQL> select * from TESTSAVEPOINT;
TID TNAME
---------- --------------------
1 Tom
2 Mary
已选择 2 行。
SQL> create table test3
2 (tid number,
3 tname varchar2(20),
4 gender varchar2(2) check (gender in ('男','女')),
5 sal number check (sal > 0)
6 );
表已创建。
SQL> insert into test3 values(1,'Tom','男',2000);
已创建 1 行。
SQL> insert into test3 values(2,'Mike','啊',2000);
insert into test3 values(2,'Mike','啊',2000)
*
第 1 行出现错误:
ORA-02290: 违反检查约束条件 (SCOTT.SYS_C005393)
SQL> create table student
2 (
3 sid number constraint student_pk primary key,
4 sname varchar2(20) constraint student_name_notnull not null,
5 gender varchar2(2) constraint student_gender check (gender in ('男','女')),
6 email varchar2(40) constraint student_email_unique unique
7 constraint student_email_notnull not null,
8 deptno number constraint student_fk references dept(deptno) on delete set null
9 );
SQL> insert into student values(1,'Tom','男','tom@126.com',10);
已创建 1 行。
SQL> insert into student values(2,'Mike','男','tom@126.com',10);
insert into student values(2,'Mike','男','tom@126.com',10)
*
第 1 行出现错误:
ORA-00001: 违反唯一约束条件 (SCOTT.STUDENT_EMAIL_UNIQUE)
生命很短,请让生活更精彩一些!