--部门表 --DROP TABLE DEPT; CREATE TABLE DEPT ( DEPTNO NUMBER(2) CONSTRAINT PK_DEPT PRIMARY KEY, --部门编号 主键 DNAME VARCHAR2(14) , --部门名称 LOC VARCHAR2(13) --位置 ); --员工表 --DROP TABLE EMP; CREATE TABLE EMP ( EMPNO NUMBER(4) CONSTRAINT PK_EMP PRIMARY KEY,--员工编号 ENAME VARCHAR2(10),--员工姓名 JOB VARCHAR2(9),--工作职位 MGR NUMBER(4),--上级领导 HIREDATE DATE,--入职时间 SAL NUMBER(7,2),--薪水 COMM NUMBER(7,2),--津贴 DEPTNO NUMBER(2) CONSTRAINT FK_DEPTNO REFERENCES DEPT --员工编号 ); --------------------添加约束--------------------------------- --ALTER TABLE DEPT ADD CONSTRAINT PK_DeptNo PRIMARY KEY (DEPTNO); --ALTER TABLE EMP ADD CONSTRAINT FK_DeptNo FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO); INSERT INTO DEPT VALUES(10,'ACCOUNTING','NEW YORK'); INSERT INTO DEPT VALUES(20,'RESEARCH','DALLAS'); INSERT INTO DEPT VALUES(30,'SALES','CHICAGO'); INSERT INTO DEPT VALUES(40,'OPERATIONS','BOSTON'); INSERT INTO EMP VALUES(7369,'SMITH','CLERK',7902,to_date('17-12-1980','dd-mm-yyyy'),800,NULL,20); INSERT INTO EMP VALUES(7499,'ALLEN','SALESMAN',7698,to_date('20-2-1981','dd-mm-yyyy'),1600,300,30); INSERT INTO EMP VALUES(7521,'WARD','SALESMAN',7698,to_date('22-2-1981','dd-mm-yyyy'),1250,500,30); INSERT INTO EMP VALUES(7566,'JONES','MANAGER',7839,to_date('2-4-1981','dd-mm-yyyy'),2975,NULL,20); INSERT INTO EMP VALUES(7654,'MARTIN','SALESMAN',7698,to_date('28-9-1981','dd-mm-yyyy'),1250,1400,30); INSERT INTO EMP VALUES(7698,'BLAKE','MANAGER',7839,to_date('1-5-1981','dd-mm-yyyy'),2850,NULL,30); INSERT INTO EMP VALUES(7782,'CLARK','MANAGER',7839,to_date('9-6-1981','dd-mm-yyyy'),2450,NULL,10); INSERT INTO EMP VALUES(7788,'SCOTT','ANALYST',7566,to_date('13-10-1987','dd-mm-yyyy'),3000,NULL,20); INSERT INTO EMP VALUES(7839,'KING','PRESIDENT',NULL,to_date('17-11-1981','dd-mm-yyyy'),5000,NULL,10); INSERT INTO EMP VALUES(7844,'TURNER','SALESMAN',7698,to_date('8-9-1981','dd-mm-yyyy'),1500,0,30); INSERT INTO EMP VALUES(7876,'ADAMS','CLERK',7788,to_date('13-7-1987','dd-mm-yyyy'),1100,NULL,20); INSERT INTO EMP VALUES(7900,'JAMES','CLERK',7698,to_date('3-12-1981','dd-mm-yyyy'),950,NULL,30); INSERT INTO EMP VALUES(7902,'FORD','ANALYST',7566,to_date('3-12-1981','dd-mm-yyyy'),3000,NULL,20); INSERT INTO EMP VALUES(7934,'MILLER','CLERK',7782,to_date('23-1-1982','dd-mm-yyyy'),1300,NULL,10); --薪水记录信息表 --DROP TABLE SALGRADE; CREATE TABLE SALGRADE ( GRADE NUMBER, --级别 LOSAL NUMBER, --最低薪水 HISAL NUMBER --最高薪水 ); INSERT INTO SALGRADE VALUES (1,700,1200); INSERT INTO SALGRADE VALUES (2,1201,1400); INSERT INTO SALGRADE VALUES (3,1401,2000); INSERT INTO SALGRADE VALUES (4,2001,3000); INSERT INTO SALGRADE VALUES (5,3001,9999); COMMIT; --红利表 --DROP TABLE BONUS; CREATE TABLE BONUS ( ENAME VARCHAR2(10), JOB VARCHAR2(9), SAL NUMBER, COMM NUMBER ); --班级表 create table t_classes( classes_id number(3), classes_name varchar2(30) not null, constraint pk_classes_id primary key(classes_id) ); --学生表 create table t_student( student_id number(10), student_name varchar2(50) not null, sex char(2) not null, birthday date not null, email varchar2(30) unique, classes_id number(3) not null, constraint pk_student_id primary key(student_id), constraint fk_classes_id foreign key(classes_id) references t_classes(classes_id) ); --创建序列号 create sequence seq_stuId start with 1 increment by 1; --查询序列号 select * from user_sequences; --对学生表添加一个字段 alter table t_student add(contact_tel varchar2(40)); --修改表的字段的长度 alter table t_student modify(student_name varchar2(32)); --删除字段 alter table t_student drop (contact_tel); --索引 --索引的目的是提高查询数据的速度.索引一本书的目录一样,索引的建立原则,比较少的 --DML(insert、update、delete),经常出现在where 语句中的字段 ---创建视图 create view v_dept_emp as select d.dname,d.loc,e.ename,e.job from dept d,emp e where d.deptno=e.deptno; create table t_log ( log_id number(10) primary key, log_time date ); create sequence seq_log_id start with 1 increment by 1; --建立触发器 create or replace trigger tri_test after insert on emp --每次对emp表插入一条就更新这个t_log表 begin insert into t_log(log_id, log_time) values(seq_log_id.nextval, sysdate); end; insert into emp (empno, ename) values(9998, 'ls'); /* *根据岗位加工资,如果是MANAGER 增加20%的工资,如果是 *SALESMAN 增加10%的工资,其他的增加5%的工资 */ create or replace procedure proc_sal is cursor c is select * from emp for update; begin for v_emp in c loop if (v_emp.job = 'MANAGER') then update emp set sal = sal + sal*0.2 where current of c; elsif (v_emp.job = 'SALESMAN') then update emp set sal = sal + sal*0.1 where current of c; else update emp set sal = sal + sal*0.05 where current of c; end if; end loop; commit; end --执行存储过程 exec proc_sal; --查询当前用户有哪些操作对象 select object_name from user_objects; ---查看当前用户建立了哪些约束 select constraint_name from user_constraints; --查看当前用户拥有哪些表 select * from tab; --select table_name from user_tables; ---分页 select * from ( select rownum r, t.* from ( select * from emp ) t where rownum <=9 ) where r > 6; /*一个完整的select 语句格式如下 select 字段 from 表名 where …… group by …… having …… order by …… 以上语句的执行顺序 1. 首先执行where 语句过滤原始数据 2. 执行group by 进行分组 3. 执行having 对分组数据进行操作 4. 执行select 选出数据 5. 执行order by 排序 */ --查询出员工的名称长度为5的--length select d.dname from dept d where length(d.dname)=5; --查询所有以M开头名字的员工 select e.ename from emp e where substr(e.ename,1,1)=upper('m'); select d.deptno,e.deptno from emp e,dept d where d.deptno=e.deptno; --条件连接 select d.deptno,e.deptno from emp e left join dept d on d.deptno=e.deptno; --左连接 select d.deptno,e.deptno from emp e right join dept d on d.deptno=e.deptno; --右连接 select d.deptno,e.deptno from emp e inner join dept d on d.deptno=e.deptno; --内连接 --order by select d.deptno,e.deptno from dept d ,emp e where d.deptno=e.deptno order by d.deptno asc ;--asc升序 select d.deptno,e.deptno from dept d ,emp e where d.deptno=e.deptno order by d.deptno desc ;--desc 降序 select d.deptno from dept d ,emp e where d.deptno=e.deptno group by d.deptno order by d.deptno desc ; select e.deptno from dept d ,emp e where d.deptno=e.deptno group by e.deptno order by e.deptno desc ; select e.deptno from dept d ,emp e where d.deptno=e.deptno group by e.deptno having avg(e.sal)>200 order by e.deptno desc ; select e.job from emp e group by e.job having avg(e.sal)>400 ; select e.ename from emp e group by e.ename having avg(e.sal)>400 ; --对那个列分组就查询那个列出来 select to_char(hiredate,'fmyyyy-mm-dd') from emp; --1981-2-20格式化 select to_char(hiredate,'yyyy-mm-dd') from emp; --1981-02-20 delete --删除所有记录,表结构还在,写日志,可以恢复的,速度慢 |savepoint p;--在删除数据之前创建一个点p,用于回滚你删除的数据 |delete from 表名; --在删除这个表时,commit命令又无法回滚了 |rollback to p; --当你回滚这个点的时候就报你savepoint 'P' never established; drop --删除表的结构和数据 |drop table 表名 truncate --删除表中的所有记录,表结构还在,不写日志,无法找回删除的记录,速度快。 |truncate table 表名 SET TIMING ON; --打开显示操作时间的开关,在下面显示查询时间。 select * from t_xiaoxi where rowid in ( select rid from ( select rownum rn, rid from( select rowid rid, cid from t_xiaoxi order by cid desc ) where rownum<10000 ) where rn>9980 ) order by cid desc;