首页 > 代码库 > oracle inner join | outer join | full join | add constraint | drop constraint | disable constraint | enable constraint
oracle inner join | outer join | full join | add constraint | drop constraint | disable constraint | enable constraint
--建表语句
create table SCOTT.DEPT
(
deptno NUMBER(2) not null,
dname VARCHAR2(15),
loc VARCHAR2(13)
)
tablespace USERS
;
comment on table dept
is ‘部门表‘;
comment on column deptno
is ‘部门编号‘;
comment on column loc
is ‘部门所在地‘;
--add primary key constraint
alter table dept
add constraint pk_dept primary key (deptno);
create table SCOTT.EMP
(
empno NUMBER(4) not null,
ename VARCHAR2(10),
age NUMBER(3),
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2)
)
tablespace USERS
;
create table SCOTT.EMP
(
empno NUMBER(4) primary key not null, --add primary key constraint, not null constraint
ename VARCHAR2(10) not null unique, --add unique constraint
age NUMBER(3) not null check(between 0 and 150), --add check constraint
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2)
constraint fk_emp_deptno foreign key (deptno) references dept (deptno) --add foreign key constraint
)
tablespace USERS
;
comment on table emp
is ‘员工信息表‘;
comment on column empno
is ‘员工编号‘;
comment on column ename
is ‘员工姓名‘;
comment on column job
is ‘员工职位‘;
comment on column mgr
is ‘直接上级编号‘;
comment on column hiredate
is ‘入职日期‘;
comment on column sal
is ‘员工薪水‘;
comment on column deptno
is ‘部门编号‘;
--建表的时候添加外键约束, 加了级联删除
constraint fk_emp_deptno foreign key (deptno) references dept (deptno) on delete cascade
--add primary key constraint
alter table emp
add constraint pk_emp primary key (empno);
--add foreign key constraint
alter table emp
add constraint fk_emp foreign key (deptno)
references dept (deptno);
--添加外键约束, 带级联删除
alter table emp
add constraint fk_emp_deptno foreign key (deptno)
references dept (deptno)
on delete cascade;
--add unique key constraint
alter table emp
add constraint uk_emp_name unique (ename);
--add check constraint
alter table emp
add constraint ck_emp_age check (age between 0 and 150);
--add not null constraint
alter table emp
modify ename not null;
--drop not null contraint
alter table emp
modify ename null;
--drop constraint
alter table emp
drop constraint unique (name);
alter table emp
drop constraint fk_emp_deptno;
--禁用约束
alter table emp
disable constraint fk_emp_deptno;
--启用约束
alter table emp
enable constraint fk_emp_deptno;
--oracle 的连接分为内连接(inner join) 外连接(outer join) 全连接(full join)
--1.inner join, 交集
select * from A inner join B on A.field1 = B.field2;
select * from A, B where A.field1 = B.field2;
--内连接查询能查询出匹配的记录, 匹配不上的记录查询不出来
select * from dept inner join emp on dept.deptno = emp.deptno;
select * from dept, emp where dept.deptno = emp.deptno;
--2.outer join ,外连接, 可以分为左外连接 left outer join 右外连接 right outer join
--要求: (1).2张表, 部门表和员工表, 一个部门有多个员工, 一个员工只能对应一个部门, 部门和员工是1对多的关系
--(2)部门表和员工表进行关联查询,并要查询出部门的所有信息
--采用左连接查询, 左连接查询以left join 前面的表为主表, 即使记录关联不上, 主表的信息也能够查询出来
select * from A left join B on A.field1 = B.field2;
select * from dept left join emp dept.deptno = emp.deptno;
select * from A, B where A.field1 = B.field2(+);
select * from dept, emp where dept.deptno = emp.deptno(+);
--部门表和员工表进行关联查询,并要查询出员工表的所有信息
--右连接以 right join 后面的表为主表, 即使记录关联不上, 主表的信息也能够查询出来
select * from A right join B on A.field1 = B.field2;
select * from dept right join emp on dept.deptno = emp.deptno;
--外连接就是在关联不上的时候, 把其中的部分信息查询出来
--全连接 full join , full join ... on ...,全连接的查询结果: 左外连接和右外连接查询结果的并集,
--即使一些记录关联不上, 也能把部分信息查询出来
select * from A full join B on A.field1 = B.field2;
select * from dept full join emp on dept.deptno = emp.deptno;
select * from dept full join emp on 1 = 1;
oracle inner join | outer join | full join | add constraint | drop constraint | disable constraint | enable constraint