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
************************************************************************************************************************************************************************************