在查询多个表时,我们经常会用“连接查询”。连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志。

什么是连接查询呢?

概念:根据两个表或多个表的列之间的关系,从这些表中查询数据。

目的:实现多个表查询操作。

知道了连接查询的概念之后,什么时候用连接查询呢?

一般是用作关联两张或两张以上的数据表时用的。看起来有点抽象,我们举个例子,做两张表:学生表(T_student)和班级表(T_class)

 --创建DB
--filename修改为自己电脑上MSSQL存储的位置
create database MyTestDB
on primary
(
name='MyTestDB',
filename='D:\yangZ_MSSQL\MyTestDB.mdf',
size=10mb,
maxsize=100mb,
filegrowth=10%
)
log on
(
name='MyTestDB_log',
filename='D:\yangZ_MSSQL\MyTestDB_log.ldf',
size=2mb,
maxsize=100mb,
filegrowth=5mb
)
---------------------------------------------
use MyTestDB
go create table T_Student
(
id int identity(1,1) primary key,
student nvarchar(100) not null,
classId int not null
) create table T_Class
(
classId int identity(1,1) primary key,
chassName nvarchar(100) not null
)
-------------------------------------------
---设置T_Student classId字段为外键
alter table T_Student add constraint FK_T_Student_classId foreign key(classId) references T_Class(classId)
-- 增加外键约束时,设置【级联更新、级联删除】:来保证,当主键表中的记录发生改变时候,对应的外键表中的数据也发生相应的改变。
on delete cascade
on update cascade ---删除外键
alter table T_Student drop constraint FK_T_Student_classId -------------------------------------------
insert into T_Class
select '一班' union all
select '二班' union all
select '三班' union all
select '四班'
go insert into T_Student
select '老赵',1 union all
select '老钱',2 union all
select '老孙',3 union all
select '老李',5
go
-------------------------------------------
select * from T_Student
select * from T_Class

MSSQL create DB and Table(T_Student/T_Class)

连接标准语法格式:

  SQL-92标准所定义的FROM子句的连接语法格式为:

  FROM join_table join_type join_table[ON (join_condition)]

  其中join_table指出参与连接操作的表名,连接可以对同一个表操作,也可以对多表操作,对同一个表操作的连接又称做自连接。join_type 指出连接类型。join_condition指连接条件。

连接类型:

  连接分为三种:内连接(INNER JOIN)外连接交叉连接(CROSS JOIN)[也称迪卡尔积]。

内连接: 等值连接、不等连接、自然连接

外连接: 左连接(LEFT JOIN/LEFT OUTER JOIN)、右连接(RIGHT JOIN/RIGHT OUTER JOIN)和全连接(FULL JOIN/FULL OUTER JOIN)

交叉连接: 不带where、有where子句

内连接(INNER JOIN)

  使用比较运算符(包括=、>、<、<>、>=、<=、!>和!<)进行表间的比较操作,查询与连接条件相匹配的数据。根据比较运算符不同,内连接分为等值连接、自然连接和不等连接三种。

1、等值连接

概念:在连接条件中使用等于号(=)运算符,其查询结果中列出被连接表中的所有列,包括其中的重复列。

 select * from T_Student as s,T_Class as c
where s.classId = c.classId
--等价于下面的写法
select * from T_student s inner join T_class c on s.classId = c.classId

MSSQL查询结果为:

2、不等连接

概念:在连接条件中使用除等于号之外运算符(>、<、<>、>=、<=、!>和!<)

 select * from T_Student as s, T_Class as c
where s.classId <> c.classId
--等价于下面的写法
select * from T_Student s inner join T_Class c on s.classId <> c.classId

MSSQL查询结果为:

3、自然连接

概念:连接条件和等值连接相同,但是会删除连接表中的重复列。

查询语句同等值连接基本相同:

select s.*,c.chassName from T_student s inner join T_class c on s.classId = c.classId

MSSQL查询结果为:

总结:内连接是只显示满足条件的!

外连接  

  外连接分为左连接(LEFT JOIN)或左外连接(LEFT OUTER JOIN)、右连接(RIGHT JOIN)或右外连接(RIGHT OUTER JOIN)、全连接(FULL JOIN)或全外连接(FULL OUTER JOIN)。我们就简单的叫:左连接、右连接和全连接。

1、左连接:

概念:返回左表中的所有行,如果左表中行在右表中没有匹配行,则结果中右表中的列返回空值。

select * from T_student s left join T_class c on s.classId = c.classId

MSSQL查询结果为:

总结:左连接显示左表全部行,和右表与左表相同行。

2、右连接:

概念:恰与左连接相反,返回右表中的所有行,如果右表中行在左表中没有匹配行,则结果中左表中的列返回空值

select * from T_student s right join T_class c on s.classId = c.classId

MSSQL查询结果为:

总结:右连接恰与左连接相反,显示右表全部行,和左表与右表相同行。

3、全连接:

概念:返回左表和右表中的所有行。当某行在另一表中没有匹配行,则另一表中的列返回空值

select * from T_student s full join T_class c on s.classId = c.classId

MSSQL查询结果为:

总结:返回左表和右表中的所有行。

交叉连接(CROSS JOIN):也称迪卡尔积

概念:不带WHERE条件子句,它将会返回被连接的两个表的笛卡尔积,返回结果的行数等于两个表行数的乘积(例如:T_student和T_class,返回4*4=16条记录),

     如果带where,返回或显示的是匹配的行数。

1、不带where:

 select *from T_student cross join T_class
--等于
select *from T_student, T_class

MSSQL查询结果为:

总结:相当与笛卡尔积,左表和右表组合。

2、有where子句,往往会先生成两个表行数乘积的数据表,然后才根据where条件从中选择。

 select * from T_student s cross join T_class c where s.classId = c.classId
--(注:cross join后加条件只能用where,不能用on)

MSSQL查询结果为:

查询结果跟等值连接的查询结果是一样。

MSSQL全部源码参考:

 --创建DB
--filename修改为自己电脑上MSSQL存储的位置
create database MyTestDB
on primary
(
name='MyTestDB',
filename='D:\yangZ_MSSQL\MyTestDB.mdf',
size=10mb,
maxsize=100mb,
filegrowth=10%
)
log on
(
name='MyTestDB_log',
filename='D:\yangZ_MSSQL\MyTestDB_log.ldf',
size=2mb,
maxsize=100mb,
filegrowth=5mb
)
---------------------------------------------
use MyTestDB
go create table T_Student
(
id int identity(1,1) primary key,
student nvarchar(100) not null,
classId int not null
) create table T_Class
(
classId int identity(1,1) primary key,
chassName nvarchar(100) not null
)
-------------------------------------------
---设置T_Student classId字段为外键
alter table T_Student add constraint FK_T_Student_classId foreign key(classId) references T_Class(classId)
-- 增加外键约束时,设置【级联更新、级联删除】:来保证,当主键表中的记录发生改变时候,对应的外键表中的数据也发生相应的改变。
on delete cascade
on update cascade ---删除外键
alter table T_Student drop constraint FK_T_Student_classId -------------------------------------------
insert into T_Class
select '一班' union all
select '二班' union all
select '三班' union all
select '四班'
go insert into T_Student
select '老赵',1 union all
select '老钱',2 union all
select '老孙',3 union all
select '老李',5
go
-------------------------------------------
select * from T_Student
select * from T_Class
-------------------------------------------
--1、等值连接
--概念:在连接条件中使用等于号(=)运算符,其查询结果中列出被连接表中的所有列,包括其中的重复列。
select * from T_Student as s,T_Class as c
where s.classId = c.classId
--等价于下面的写法
select * from T_student s inner join T_class c on s.classId = c.classId
-------------------------------------------
--2、不等连接
--概念:在连接条件中使用除等于号之外运算符(>、<、<>、>=、<=、!>和!<)
select * from T_Student as s, T_Class as c
where s.classId <> c.classId
--等价于下面的写法
select * from T_Student s inner join T_Class c on s.classId <> c.classId
-------------------------------------------
--3、自然连接
--概念:连接条件和等值连接相同,但是会删除连接表中的重复列。
--查询语句同等值连接基本相同:
select s.*,c.chassName from T_student s inner join T_class c on s.classId = c.classId ------------------------------------------
--1、左连接:
--概念:返回左表中的所有行,如果左表中行在右表中没有匹配行,则结果中右表中的列返回空值。
select * from T_student s left join T_class c on s.classId = c.classId
------------------------------------------
--2、右连接:
--概念:恰与左连接相反,返回右表中的所有行,如果右表中行在左表中没有匹配行,则结果中左表中的列返回空值
select * from T_student s right join T_class c on s.classId = c.classId
------------------------------------------
--3、全连接:
--概念:返回左表和右表中的所有行。当某行在另一表中没有匹配行,则另一表中的列返回空值
select * from T_student s full join T_class c on s.classId = c.classId
-------------------------------------------
--交叉连接(CROSS JOIN):也称迪卡尔积
--1、不带where:
select *from T_student cross join T_class
--等于
select *from T_student, T_class
-------------------------------------------
--2、有where子句,往往会先生成两个表行数乘积的数据表,然后才根据where条件从中选择。
select * from T_student s cross join T_class c where s.classId = c.classId
--(注:cross join后加条件只能用where,不能用on)

MSSQL全部源码

MSSQL 详解SQL Server连接(内连接、外连接、交叉连接)的更多相关文章

  1. 详解SQL Server连接(内连接、外连接、交叉连接)

    在查询多个表时,我们经常会用“连接查询”.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 什么是连接查询呢? 概念:根据两个表或多个表的列之间的关系,从这些表中查询数据 ...

  2. 详解SQL Server 2005 Express下的事件探查器

    安装Visual Studio 2008会有附带的SQL Server 2005 Express版 我们开发一般都用那个都不单独安装SQL Server的 大家都知道express版的sql是没有 事 ...

  3. SQL查询优化:详解SQL Server非聚集索引(转载)

    本文是转载,原文地址 http://tech.it168.com/a2011/1228/1295/000001295176.shtml 在SQL SERVER中,非聚集索引其实可以看作是一个含有聚集索 ...

  4. 详解SQL Server 2008工具SQL Server Profiler

    一.SQL Profiler工具简介 SQL Profiler是一个图形界面和一组系统存储过程,其作用如下: 1.图形化监视SQL Server查询: 2.在后台收集查询信息: 3.分析性能: 4.诊 ...

  5. Exec msdb.dbo.sp_send_dbmail 参数详解(SQL Server 存储过程发邮件)

    转载oriency755 发布于2012-12-04 11:34:45 阅读数 6870 收藏   sp_send_dbmail [ [ @profile_name = ] 'profile_name ...

  6. 详解SQL Server数据修复命令DBCC的使用

    严重级别为 21 表示可能存在数据损坏. 可能的原因包括损坏的页链.损坏的 IAM 或该对象的 sys.objects目录视图中存在无效条目. 这些错误通常由硬件或磁盘设备驱动程序故障而引起. MS ...

  7. SQL Server中内连接和外连接的区别

    SQL Server中内连接和外连接的区别 假设一个数据库中有两张表,一张是学生表StudentInfo,一张是班级表ClassInfo,两张表之间用ClassId字段进行关联. 如果用内连接,正常的 ...

  8. Sql Server系列:多表连接查询

    连接查询是关系数据中最主要的查询,包括内连接.外连接等.通过连接运算符可以实现多个表查询.内连接查询操作列出与连接条件匹配的数据行,它使用比较运算符比较被连接列的列值.SQL Server中的内连接有 ...

  9. SQL Server 2005 不允许远程连接解决方法

    刚刚安装的数据库系统,按照默认安装的话,很可能在进行远程连接时报错,通常是错误:“在连接到 SQL Server 2005 时,在默认的设 置下 SQL Server 不允许进行远程连接可能会导致此失 ...

随机推荐

  1. HDU 2162 Add ‘em

    http://acm.hdu.edu.cn/showproblem.php?pid=2162 Problem Description Write a program to determine the ...

  2. Spring-MVC理解之二:前置控制器

    原文链接:http://www.cnblogs.com/brolanda/p/4265749.html 一.前置控制器配置与讲解 上篇中理解了IOC容器的初始化时机,并理解了webApplicatio ...

  3. Spring Boot 学习资料【m了以后看】(转)

    推荐博客: 程序员DD SpringBoot集成 liaokailin的专栏 纯洁的微笑 SpringBoot揭秘与实战 catoop的专栏 方志朋Spring Boot 专栏 简书Spring Bo ...

  4. 第84天:jQuery动态创建表格

    jQuery动态创建表格 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  5. xpath定位相邻元素方法

    在定位页面元素时,有时候需要根据某个元素特征,去定位其相邻元素/兄弟元素,或者定位其父元素的兄弟元素(或叔伯元素的子元素).这里引入xpath的两个定位方法: preceding-sibling fo ...

  6. Ubuntu18.04 创建与编辑热点的方法

    在终端输入 nm-connection-editor 修改Hotspot,里边有热点名称及密码 当修改完了这些,要关闭热点,重新打开,这样才会生效!

  7. [洛谷P4341][BJWC2010]外星联络

    题目大意:给你一个长度为$n(n\leqslant3\times10^3)$的字符串,要你求出其中出现次数大于$1$的子串,并按字典序输出次数. 题解:建$SAM$后求出每个点的$size$,最后按字 ...

  8. OpenFlow协议中如何提高交换机流表的匹配成功率

    写在前面 这段时间一直在研究如何提高流表空间的利用率.一直没能想到好的idea.有一篇文献中比较了现有研究中提到的手段,在这里记录一下都有哪些类型的手段以及这些手段存在的不足.这些手段不仅局限于如何提 ...

  9. CAS单点登录详细流程

    一.CAS简介和整体流程 CAS 是 Yale 大学发起的一个开源项目,旨在为 Web 应用系统提供一种可靠的单点登录方法,CAS 在 2004 年 12 月正式成为 JA-SIG 的一个项目.CAS ...

  10. SNMP-网络管理协议

    SNMP协议简介: a. 轮询(Polling) -- 定时获取状态, 中断(Interrupt)--出问题通知 b. 共同体名(community) -- 口令--只读口令 --读写口令 使用SNM ...