博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
oracle课堂随笔--第十四天
阅读量:5140 次
发布时间:2019-06-13

本文共 6299 字,大约阅读时间需要 20 分钟。

集合操作

select employee_id, job_id from employees

union all          不去除  保留重复值

select employee_id, job_id from job_history;

 

select employee_id, job_id from employees

Union                去除重复值

select employee_id, job_id from job_history;

 

select employee_id, job_id from employees

Intersect   相交

select employee_id, job_id from job_history;

 

select employee_id from employees

Minus    

select employee_id from job_history;

 

select employee_id, job_id, salary from employees

union all

select employee_id, job_id, null from job_history;

 

select employee_id, job_id, to_char(salary) from employees

union all

select employee_id, job_id, 'no salary' from job_history;

上下相对应

 

集合排序:

select employee_id, job_id, salary from employees

union all

select employee_id, job_id, null from job_history

order by salary;

 

select employee_id, job_id, null from job_history

union all

select employee_id, job_id, salary from employees

order by 3;

DML

Create table t1 as select *from employees

Drop  table tl;

 

insert:            插入                                字符串

SQL> create table t1(x int, y char(1), z date);

SQL> insert into t1(x, y, z) values (1, 'a', sysdate);

                                          数值                                  

SQL> insert into t1(x, z, y) values (2, sysdate+1, 'b');

SQL> insert into t1(x, y, z) values (1, null, sysdate);

SQL> insert into t1(x, z) values (2, sysdate+1);

SQL> insert into t1 values (1, null, sysdate);

                                                 所有值都得写上

SQL> create table my_emp as select * from employees;

SQL> create table my_emp as select last_name, salary from employees where department_id=50;

SQL> create table avg_sal as select department_id, avg(salary) avg_sal from employees group by department_id;

SQL> create table my_emp as select * from employees where 1=0;

SQL> insert into my_emp select * from employees;

 

update:更新  

SQL> update my_emp set salary=salary*1.1;

SQL> update my_emp set salary=salary*1.1 where department_id=50;

SQL> update my_emp set salary=salary*1.1, commission_pct=0.5 where employee_id=197;

 

delete:

SQL> delete from my_emp where employee_id=197;

SQL> delete from my_emp where department_id=50;

SQL> delete from my_emp;

 

子查询:

SQL> create table my_emp as select * from employees;

SQL> alter table my_emp add(department_name varchar2(30));

SQL> update my_emp outer set department_name=(select department_name from departments where department_id=outer.department_id);

 

update (select t1.department_name as aname,t2.department_name bname from my_emp t1 ,departments t2 where t1.department_id=t2.department_id) set aname=bname;

 

 在new_dept表中删除没有员工的部门

SQL> create table my_dept as select * from departments;

delete from my_dept outer

where not exists

(select 1 from my_emp

where department_id=outer.department_id);

Commit 结束

 

delete和truncate:

delete truncate

语句类型 dml ddl

undo数据 产生大量undo数据 不产生undo数据

空间管理        不释放 释放

语法 where 删除全部数据

 

DDL

 

 

字符串:

SQL> create table t1(x char(10), y varchar2(10));

SQL> insert into t1 values('x', 'y');

SQL> select dump(x), dump(y) from t1;

数值:

SQL> create table t1(x number(5,2), y number(5));

SQL> insert into t1 values (123.45, 12345);

SQL> insert into t1 values (12.345, 12345);

SQL> insert into t1 values (12.345, 123.45);

SQL> select * from t1;

SQL> insert into t1 values (12.345, 112345);

日期时间:

SQL> create table t1(a date, b timestamp, c timestamp with time zone, d timestamp with local time zone);

SQL> insert into t1 values (sysdate, systimestamp, systimestamp, systimestamp);

SQL> alter session set time_zone='+9:00';

SQL> select * from t1;

修改表结构:

SQL> alter table t1 add(e char(10));

SQL> alter table t1 drop(e);

SQL> alter table t1 modify(d not null);

 

约束条件:

字段(列):not null, check(salary>0)

行与行:primary key, unique

表与表之间:foreign key

create table dept (

    deptno int constraint dept_deptno_pk primary key,

    dname varchar2(20) constraint dept_dname_nn not null);

 

create table emp (

    empno int constraint emp_empno_pk primary key,

    ename varchar2(20) constraint emp_ename_nn not null,

    email varchar2(50) constraint emp_email_uq unique,

    salary int constraint emp_salary_ck check(salary>0),

    deptno int constraint emp_deptno_fk references dept(deptno))

 

SQL> select constraint_name, constraint_type from user_constraints where table_name in('DEPT', 'EMP');

 

SQL> insert into emp values (100, 'abc', 'abc@123.com', 10000, 10);

insert into emp values (100, 'abc', 'abc@123.com', 10000, 10)

*

ERROR at line 1:

ORA-02291: integrity constraint (HR.EMP_DEPTNO_FK) violated - parent key not

found

 

 

SQL> insert into dept values (10, 'sales');

 

1 row created.

 

SQL> insert into dept values (10, 'market');

insert into dept values (10, 'market')

*

ERROR at line 1:

ORA-00001: unique constraint (HR.DEPT_DEPTNO_PK) violated

 

 

SQL> insert into dept values (20, 'market');

 

1 row created.

 

SQL> commit;

 

Commit complete.

 

SQL> insert into emp values (101, 'def', 'def@123.com', 10000, 20);

 

create table emp (

    empno int constraint emp_empno_pk primary key,

    ename varchar2(20) constraint emp_ename_nn not null,

    email varchar2(50) constraint emp_email_uq unique,

    salary int constraint emp_salary_ck check(salary>0),

deptno int constraint emp_deptno_fk references dept(deptno) on delete set null)或者on delete cascade

 

Create table tl(x int constraint tl_x_pk primary key);

Alter  table tl (add y int);

Alter table tl add(z int ,a int);

Alter table tl frop (z ,a);

Alter table  tl modify(y char(1));

Alter table  tl modify(y default ‘a’)

Alter table tl  disable novalidate constraint tl_x_pk;

Insert into tl values(1,’a’);

Insert into tl values(1,’b’);

Alter table tl enable validate constraint tl_x_pk;

Delete tl;

Alter table tl enable validate constraint tl_x_pk;

Drop table tl;

 

 

instead of trigger视图触发器  替代触发器

 

序列:

SQL> create sequence test_seq increment by 1 start with 1 maxvalue 1000 nocycle cache 20;

SQL> create table t1(x int primary key, y int);

SQL> insert into t1 values (test_seq.nextval, 11); 反复执行

SQL> select * from t1;

 

 

Select test_seq.currva  from dual   每用一次 序列增长一次

 

 

 

索引:

主键和唯一性约束自动创建索引:

SQL> select constraint_name, constraint_type from user_constraints where table_name='EMPLOYEES';

SQL> select index_name, index_type  uniqueness; from user_indexes where table_name='EMPLOYEES';

SQL> set autot on

SQL> select last_name from employees where employee_id=100; 走索引

SQL> select email from employees; 走索引

SQL> select last_name from employees where salary=2100; 全表扫描

SQL> create index emp_salary_ix on employees(salary);

SQL> select last_name from employees where salary=2100; 走索引

SQL> set autot off

转载于:https://www.cnblogs.com/Matilda/p/7281224.html

你可能感兴趣的文章
在Winform,Silvelight,WPF等程序中访问Asp.net MVC web api
查看>>
HDU 1561 The more, The Better (有依赖背包 || 树形DP)
查看>>
SQL Server数据库表重置自增主键号
查看>>
字典方法(3)
查看>>
猜大小押宝(c语言简单控制台程序)
查看>>
centos7安装docker
查看>>
idea debug调试步骤
查看>>
爬虫必备:Python 执行 JS 代码 —— PyExecJS、PyV8、Js2Py
查看>>
【译】将字符转换为双精度浮点型
查看>>
推荐给4.3.3越狱用户的安全漏洞修复工
查看>>
用HTML的select+option标签实现下拉框
查看>>
[改善Java代码]asList方法产生的List对象不可更改
查看>>
ACM题目————Find them, Catch them
查看>>
LeetCode Weekly Contest 119
查看>>
Angular2-之开发环境搭建/调试环境配置
查看>>
DDA, Bresenham line's algorithm and Voxel Traversal used in the Grid-Accelerator in PBRT
查看>>
执行p4 submit后,perl文件丢了可执行属性
查看>>
数字电路中表示频率误差的ppm是什么意思?
查看>>
upf学习
查看>>
第五篇:CUDA 并行程序中的同步
查看>>