您的位置:首页 > 数据库

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

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

什么是连接查询呢?

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

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

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

一般是用作关联两张或两张以上的数据表时用的。看起来有点抽象,我们举个例子,做两张表:学生表(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
-------------------------------------------
--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全部源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: