SQL Prepared for Interview

With SQL, how do you select all the records from a table named "Persons" where the "LastName" is alphabetically between (and including) "Hansen" and "Pettersen"?

SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'


******************************************************************************************************************

Store Procedure 基础

-- 插入数据的SP
-- 创建
create procedure SP_addNewAd
@adimage nvarchar(100),
@url nvarchar(50),
@type int
as 
insert into ads(adimage,url,type) values(@adimage,@url,@type)
go

-- 执行
exec SP_addNewAd @adimage='aaa', @url='aaa.com', @type=1

--#####################################################################
-- 带有输出参数的SP
-- 创建
create procedure SP_adOutput
@s int output
as
select @s=SUM([type]) from (select * from ads) as temtable
go

-- 执行
declare @SU int
exec SP_adOutput @SU output
select @SU as result

**********************************************************************************************************************************************************************************

Function

--创建
CREATE FUNCTION FU_Country(@CID INT)
RETURNS NVARCHAR(50)
AS

BEGIN
DECLARE @countryName NVARCHAR(50)
SELECT @countryName = country FROM country WHERE countryID = @CID

RETURN @countryName
END
GO

-- 执行
SELECT [rec_ie].dbo.FU_Country(2)
GO


**********************************************************************************************************************************************************************************

六大范式

第一范式(1NF)无重复的列

第二范式(2NF)要求实体的属性完全依赖于主关键字(主键)

第三范式(3NF)要求一个关系中不包含已在其它关系已包含的非主关键字信息

当关系型表中功能上互相依赖的那些列的每一列都是一个候选关键字时候,该满足BCNF

第四范式(4NF)表中不能包含一个实体的两个或多个互相独立的多值因子

第五范式(5NF)表必须可以分解为较小的表,除非那些表在逻辑上拥有与原始表相同的主键。

**********************************************************************************************************************************************************************************

Select Into Command

用来查询以后 将一个结果插入一个新的表中

select c.name, c.email, cg.cid into test from cat_users cu
left join candidates c on c.uid = cu.uid
left join category cg on cg.cid = cu.cid

***********************************************************************************************************************************************************************************

Group By && Having

 select location, AVG(age) as AVGAge,count(age) as CountAge  from candidates group by location
 having count(age) > 1

Select Top X percent result

 select top 100 percent * from location
************************************************************************************************************************************************************************************



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章