SQL__查询语句之多表查询

SQL__查询语句之多表查询

1.为数据库添加新的列名
  • 举例说明

    
    	USE AdventureWorks
    	go
    
    	alter table HumanResources.Department	
    	add 新的列名 date
    	
    
2.修改列名的数据类型
  • 例子如下
    
    	alter table HumanResources.Department	
    	alter column birthday char(20)
    	
    
3.删除xxx表中的不带约束的列
  • 例子如下

    
    	alter table HumanResources.Department	
    	drop column GroupName
    	
    
4.删除xxx表中的带约束的列
  • 例子如下

    
    	/*格式如下:
    	alter table 表名 drop constraint 约束名
    	alter table 表名 drop column 列名
    	(必先删除约束,后删除列)*/
    	alter table HumanResources.Employee drop constraint CK_Employee_BirthDate
    	alter table HumanResources.Employee drop column BirthDate
    	/*当多个数据表连接在一起的时候,想要删除的列不能是作为索引的列,修改数据类型的时候
    	也要考虑是否为索引*/
    
    	
    
5.使用 链接 查询数据
  • 使用内链接

    	
    	/*
    	select 列名 1,列名 2,列名 3,,,,,列名n
    	from 表名 (1...n) 别名  join  表名(1...n) 别名
    	on 表名.公共列名 别名 join 表名.公共列名 别名
    	where 条件
        */
    
    

    –补充链接条件必须是公共列,只有主、外键才能作为链接条件
    当公共列名作为链接条件时,必须给相应的表名起别名

  • 例子如下

    
    	select *
    	from HumanResources.Employee 
    	
    	select *
    	from HumanResources.EmployeePayHistory 
    	
    	--一次链接查询
    	select YYm.EmployeeID,YYm.Title,XHy.Rate,XHy.PayFrequency
    	from HumanResources.Employee YYm join HumanResources.EmployeePayHistory XHy
    	on YYm.EmployeeID=XHy.EmployeeID 
    	
    
6.二次链接查询( 内链接后的单表查询 )
  • 例子如下
    
    	--查询Employee表中工资率大于40的雇员ID以及目标职能
    	select '转换后的雇员ID' =e.EmployeeID, '转换后的头衔'=e.Title
    	from HumanResources.Employee e join HumanResources.EmployeePayHistory eph
    	on e.EmployeeID =eph.EmployeeID where eph.Rate>40
    
    
7.完整外链接( 查询两张表内的详细信息 )
  • 例子如下
    
    	/*使用关键字:表名  full outer join  表名
    	因为外链接会显示查询的所有信息,没有匹配的会显示为NULL,均返回两个表中所有不匹配和匹配的行*/
    	select st.Name as TerritoryName,sp.SalesPersonID
    	from Sales.SalesTerritory st
    	full outer join Sales.SalesPerson sp
    	on st.TerritoryID=sp.TerritoryID
    	
    
8.子查询
  • 例子如下
    
    	--查找目标职能与John相同的雇员?
    	--查询1--
    	/*select *
    	from HumanResources.EmployeeDetails
    	Where Designation=(Select Designation from EmployeeDetails where Name='John')
    	
    	---查询2---
    	第一步:
    	Select Designation 
    	from EmployeeDetails
    	where Names='John'
    	
    	第二步:
    	Select *
    	from EmployeeDetails
    	Where Designation='Executive'
    	*/
    	go
    	
    	----显示EmployeeID为46的雇员的部门名称
    	select Name 
    	from HumanResources.Department
    	where DepartmentID=
    	(select DepartmentID from HumanResources.EmployeeDepartmentHistory where EmployeeID=46 and EndDate is null)
    	
    	
    	----查询居住在Bothell的雇员的EmployeeID属性
    	select EmployeeID
    	from HumanResources.EmployeeAddress
    	where AddressID in 
    	(select AddressID 
    	from Person.Address
    	where City='Bothell')
    	
    
9.跟踪练习
  • 例子如下

    
    	/*--****使用表HuamnResources.Employee来添加一个列:YYc,并且添加约束为检查约束,
    	向列中插入新的数据*/ 
    	alter table HumanResources.Employee
    	add YYB char(10) constraint AdventureWorks_Employee_YYc check (YYB='110') 
    	go
    	Select top 2 *
    	from HumanResources.Employee
    	
    	
    	---****identity关键字(只可用于固定的数据类型)****-
    	----标识列 'YYG' 的数据类型必须是 int、bigint、smallint、tinyint 或 decimal,
    	----或者是小数位数为 0 的 numeric 数据类型,并且约束为不可为 Null。
    	alter table HumanResources.Employee
    	add YYG char identity(1,1)
    	Select  *
    	from HumanResources.Employee
    	
    	----更改表名----
    	use AdventureWorks
    	exec sp_rename 'HumanResources.NewName','NewNam+++()e'
    	
    
  • 结束语…………………………福利时间到………………………………

    大家同为程序员,在这里给大家真诚的送上福利。

    福利链接点击这里!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

男孩子小杨

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值