---
title: "内连接、左连接、右连接、全连接"
author: "斩不再"
date: "2013-08-05T12:28:57Z"
tags: []
categories: ["数据库"]
slug: "untitled"
draft: false
lastmod: "2013-08-05T12:28:57Z"
id: 1920147
url: "https://dongbowen.iteye.com/blog/1920147"
---

內连接仅选出两张表中互相匹配的记录．因此，这会导致有时我们需要的记录没有包含进来。内部连接是两个表中都必须有连接字段的对应值的记录，数据才能检索出来。
   左连接和右连接都是外部连接，也就是区别于内部连接，它对不满足连接条件的行并不是象内部连接一样将数据完全过滤掉，而是保留一部分数据，行数不会减少。
    左连接是只要左边表中有记录，数据就能检索出来，而右边有的记录必要在左边表中有的记录才能被检索出来
  右连接是只要右边表中有记录，数据就能检索出来 ；
左连接显示左边全部的和右边与左边相同的 
右连接显示右边全部的和左边与右边相同的
  全连接则会回返回两个表中的所有记录。
创建表
create table student(
       stuid NUMBER primary key,
       stuname varchar(20) not null
)
增加属性列
alter table student add classid NUMBER(4) not null
插入值
insert into student (stuid , stuname ,classid) values('1','老大','1');
insert into student (stuid , stuname,classid) values('2','老二','2');
insert into student (stuid , stuname,classid) values('3','老三','3')
select * from student

create table class(
classid number primary key,
classname varchar(20) not null
)

insert into class(classid,classname)values('1','一班');
insert into class(classid,classname)values('2','二班');
insert into class(classid,classname)values('4','四班')


表结构及查询效果显示
![](http://dl2.iteye.com/upload/attachment/0087/8532/31aca4ba-7ded-3373-9036-28beeabeb1a3.bmp)