oracle表之间的连接

来源:互联网 发布:pdm 软件 编辑:程序博客网 时间:2024/05/17 12:51

内连接:两个表根据一定的条件进行关联查询

Select 列名 from 表1 inner join 表2 on 条件

例:

select * from student s inner join class cons.classid=c.id

相当于

select * from student s, class cwhere s.classid=c.id

 

交叉连接:笛卡尔积

Select 列名 from 表1 cross join 表2 [where 条件]

例:

select * from student s cross join class c

相当于

select * from student s, class c

 

自连接

Select 列名 from 表 join 表 on 条件

例:

select e1.name,e1.job, e2.name from t_emp e1joint_emp e2 on e1.manager=e2.id

相当于

select e1.name, e1.job, e2.name from t_emp e1, t_emp e2wheree1.manager=e2.id

 

外连接:

左外连接:显示左表的全部内部,右表有不匹配的显示空值

Select 列名from 表1 left [outer] join 表2 on 条件

例:

select * from student s left join class cons.classid=c.id

相当于

select * from student s, class cwhere s.classid=c.id(+)

 

右外连接:显示右表的全部内部,左表有不匹配的显示空值

Select 列名from 表right [outer] join 表2 on 条件

例:

select * from student s right join class cons.classid=c.id

相当于

select * from student s, class cwhere s.classid(+)=c.id

 

全外连接

Select 列名 from 表1 full join 表2 on 条件

例:

select * from student s full join class cons.classid=c.id

 

 

 

 

序列:可以帮助我们自动生成主键

create sequence stu_sequence  --序列名

 minvalue 115  --最小值
maxvalue 1000000  --最大值
start with 115 –-起始值
increment by 1 –-步长,每次增长的多少
cache 100 –-缓存

 

stu_sequence.nextval返回序列的下一个值

 

 

 

子查询:实际上就是查询语句的嵌套,例如where条件是另一个查询语句的结果

 

查询数学成绩比赵云高的学生信息

select * from student where math>(select mathfromstudent where name='赵云')