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'
    	
    
发布了56 篇原创文章 · 获赞 51 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_43495629/article/details/104212987