our notes for our youngers

September 24, 2009

basic of SQL Statement (part 8)

Filed under: SQL Commands — Tags: , , , , — sevenlamp @ 9:32 AM

ဒီေန႔ ကြၽန္ေတာ္တို႔ SQL Server 2005 မွာ function ေတြကိုယ္တိုင္ ေဆာက္ျပီး သံုးတဲ႔ ပံုစံေလး ေလ့လာလိုက္ရေအာင္ဗ်ာ။ function ၂ မ်ိဳးေဆာက္လို႔ရတယ္ဗ်။ Table-valued function နဲ႔ Scalar-valued function တိုျဖစ္ပါတယ္။ Table-valued function ရဲ႕ return type က table တစ္ခုလိုမ်ိဳး column တစ္ခုထက္ပိုတဲ႔ data ေတြကို return ျပန္ပါတယ္။ Scalar-valued function ကေတာ့ return value အေနနဲ႔ column တစ္ခုပဲ return ျပန္ပါတယ္။ အရင္ဆံုး scalar-valued function ေလး တစ္ခုေလာက္ ေရးၾကည့္ရေအာင္။ မေန႔တုန္းက ေရးခဲ႔တဲ႔ category ေတြနဲ႔ total balance ထုတ္တဲ႔ query ေလးကို ျပန္ေရးပါ့မယ္။

select category.*,
(
	select sum(balance)
	from item
	where item.categoryid = category.categoryid
) as totalbalance
from category

result:

CategoryID CategoryName Status totalbalance
1 Computer Active 16
2 Stationary Active 15
3 Electronic Active 4
4 Clothes Inactive NULL
5 Kitchen wares Inactive NULL

အိုေက ဒီquery ထဲမွာ ေရးထားတဲ႔ subquery ေလးကို ကြၽန္ေတာ္က function ေလးခြဲျပီး ေရးခ်င္တာပါ။ အဲ့ဒီလိုေရးလိုက္ျခင္းအားျဖင့္ query ေရးတဲ႔ ေနရာမွာ ပိုျပီးရွင္းသြားမယ္။ ဒီေနရာမွာ ကြၽန္ေတာ္လိုခ်င္တာက totalbalance တစ္ခုထဲျဖစ္တဲ႔ အတြက္ scalar-valued function ေဆာက္ပါမယ္။ ဒီလိုပါ…

CREATE FUNCTION GetTotalBalance (@CategoryID int)
RETURNS int
AS
BEGIN
	DECLARE @TotalBalance int
	SELECT @TotalBalance = SUM(balance)
	FROM Item
	WHERE CategoryID = @CategoryID
	RETURN @TotalBalance
END

Function ေဆာက္ဖို႔ CREATE FUNCTION keyword ကိုသံုးပါတယ္။ ျပန္ျပင္ဖို႔အတြက္ေတာ့ ALTER FUNCTION နဲ႔

ALTER FUNCTION GetTotalBalance (@CategoryID int)
RETURNS int
AS
BEGIN
	DECLARE @TotalBalance int
	SELECT @TotalBalance = SUM(balance)
	FROM Item
	WHERE CategoryID = @CategoryID
	RETURN @TotalBalance
END

ဖ်က္ဖို႔အတြက္ေတာ့ DROP FUNCTION keyword တို႔ကို သံုးပါတယ္။

DROP FUNCTION GetTotalBalance

ဒီ function မွာ CategoryID ကို parameter လက္ခံျပီး item table ထဲမွာ အဲ့ဒီ categoryID ရဲ႕ totalbalance ကို return ျပန္ပါတယ္။ အိုေက အခုကြၽန္ေတာ္တို႔ ဒီ function ကို create လုပ္ျပီးသြားျပီဆိုရင္ ကြၽန္ေတာ္တို႔ ရဲ႕ query ေတြမွာ ျပန္ေခၚသံုးလို႔ ရပါျပီ။

select category.*, dbo.GetTotalBalance(categoryID)
from category

result:

CategoryID CategoryName Status totalbalance
1 Computer Active 16
2 Stationary Active 15
3 Electronic Active 4
4 Clothes Inactive NULL
5 Kitchen wares Inactive NULL

ကဲ ေနာက္ထပ္ FUNCTION တစ္မ်ိဳးက Table-valued function. အခုကြၽန္ေတာ္ categoryname, totalbalance နဲ႔ totalamount တို႔ကို ထုတ္ျပခ်င္ပါတယ္။ အဲ့ဒီ အတြက္ Table-value function ေလး ေဆာက္ရေအာင္။ သူလည္းပဲ Create function keyword ကိုပဲ သံုးပါတယ္။

CREATE FUNCTION GetItemByCategory()
RETURNS TABLE
AS
RETURN
(
    SELECT categoryname,
        sum(balance) as totalBalance,
        sum(price * balance) as totalamount
    FROM Item inner join category
	ON item.categoryID = category.categoryID
    GROUP BY categoryname
)

ျပီးရင္ အဲ့ဒီ function ေလးကို ဒီလို ေခၚပါမယ္

select * from GetItemByCategory()

result:

Categoryname totalBalance totalamount
Computer 16 105000
Electronic 4 960000
Stationary 15 7500

September 19, 2009

basic of SQL Statement (part 7)

Filed under: SQL Commands — Tags: , , — sevenlamp @ 2:10 PM

ဒီေန႔လည္း ကြၽန္ေတာ္တို႔ SQL statement ေတြအေၾကာင္းပဲ ဆက္ေျပာရေအာင္ဗ်ာ။ subquery အေၾကာင္းေျပာခ်င္လို႔ဗ်။ ကြၽန္ေတာ္က category table ထဲက categoryID 1, 3, 5 နဲ႔ တူတာေတြ ထုတ္ခ်င္တယ္ဆိုရင္… OR ကိုသံုးရင္ ဒီလိုေရးရမယ္…

select * from category
where categoryid = 1 or categoryid = 3 or categoryid = 5

result:

CategoryID CategoryName Status
1 Computer Active
2 Stationary Active
3 Electronic Active

categoryID အမ်ားၾကီး ကိုခုလိုမ်ိဳး စစ္မယ္ဆိုရင္ OR ကိုသံုးရတာ အဆင္မေျပေတာ့ပါဘူး။ ဒီအခါမွာ In keyword ကိုသံုးျပီး ခုလိုေရးလို႔ရပါတယ္…

select * from category
where categoryid in ( 1, 2, 3 )

result:

CategoryID CategoryName Status
1 Computer Active
2 Stationary Active
3 Electronic Active

အခုကြၽန္ေတာ္က စုစုေပါင္း ကုန္လက္က်န္ ၅ခု ထက္မ်ားတဲ႔ category ေတြကို ထုတ္ျပခ်င္တယ္ဆိုရင္…

select * from category
where categoryid in (
	select categoryid from item
	group by categoryid
	having SUM(balance)>5
)

result:

CategoryID CategoryName Status
1 Computer Active
2 Stationary Active

အေပၚက ေရးထားတဲ႔ query ကို တစ္ပိုင္းခ်င္း အရင္ run ၾကည့္လိုက္
select categoryid from item
group by categoryid
having SUM(balance)>5

ကို run ရင္ အေျဖက 1,2 ဆိုျပီးထြက္လာမယ္၊ အဲ့ဒီ result ကိုမွ ေပၚက query က in နဲ႔ ျပန္စစ္ယူသြားတာ။ ဒီလို query တစ္ခုထဲမွာပါတဲ႔ ေနာက္ထပ္ query ကို subquery ဒါမွမဟုတ္ inner query လို႔ေခၚပါတယ္။ အခု where ေနာက္မွာ ေရးတဲ႔ subquery မွာ column အမ်ားၾကီးထုတ္လို႔မရဘူး။ where ေနာက္က condition စစ္ထားတဲ႔ column တစ္ခုကိုပဲထုတ္ေပးရတယ္။ select ေနာက္မွာ column တစ္ခုအေနနဲ႔လည္း subquery ကိုသံုးလို႔ရပါေသးတယ္။ ဥပမာဗ်ာ.. ကြၽန္ေတာ္က category အားလံုးနဲ႔ သူတို႔ရဲ႕ ကုန္လက္က်န္စုစုေပါင္းကိုပါ ျပခ်င္တယ္ဆိုရင္….။

select *, (
	select sum(balance)
	from item
	where category.categoryid = item.categoryid
	group by item.categoryid
) as totalbalance
from category

result:

CategoryID CategoryName Statius totalbalance
1 Computer Active 16
2 Stationary Active 15
3 Electronic Active 4
4 Clothes Inactive NULL
5 Kitchen wares Inactive NULL

ဒီ query ေလးမွာ အဓိက ထုတ္တာကေတာ့ category table ပဲ၊ totalbalance column အတြက္မွ ေနာက္ထပ္ query ကိုထပ္ေခၚသြားတာ။ ဒီေနရာမွာ သတိထားရမွာက subquery ထဲကေန ထြက္လာမယ့္ value က only one row ပဲျဖစ္ရမယ္။ ဒါေၾကာင့္ where category.categoryid = item.categoryid ဒီအပိုင္းေလး ထည့္ေရးေပးရတယ္။ ဟုတ္ျပီ… ဒါဆိုရင္ ကြၽန္ေတာ္တို႔ subquery ဘယ္လိုေရးရလဲဆိုတာ နည္းနည္း သိေလာက္ပါျပီ။ အခု အေပၚက query ကိုပဲ ၾကည့္ပါ select ထဲမွာ ေနာက္ထပ္ select ေတြ ထပ္ေရးေတာ့ query ကိုၾကည့္လိုက္ရင္ ရႈပ္ေနသလို ျဖစ္ေနလိမ့္မယ္။ ဒီအခါမ်ိဳးမွာ ပိုျပီး ရွင္းသြားဖို႔အတြက္ ကြၽန္ေတာ္တို႔ ကိုယ္ပိုင္ function ေတြေဆာက္ျပီး ေရးလို႔ရပါတယ္။ အိုေက.. function ေတြအေၾကာင္းကို ေနာက္မွ ဆက္ေျပာရေအာင္။

September 18, 2009

basic of SQL Statement (part 6)

Filed under: SQL Commands — Tags: , , , , , , — sevenlamp @ 10:00 AM

Sql statement ေတြဘက္ကိုေတာ့ မလွည့္ျဖစ္တာ ေတာ္ေတာ္ၾကာသြားျပီ။ ကြၽန္ေတာ္လည္း ေရးခ်င္တာေတြကေတာ့ အမ်ားၾကီး အခ်ိန္ကမရွိဘူးျဖစ္ေနတာနဲ႔ ေရွ႕မဆက္နိုင္ဘူးျဖစ္ေနတာ။ ဘာဘဲျဖစ္ျဖစ္ ရတဲ႔အခ်ိန္ေလးလုျပီး ကြၽန္ေတာ္သိသေလာက္ေတာ့ ထပ္ေရးေနဦးမွာမို႔ အားေပးၾကပါဦးဗ်ာ။ ကဲ… ကြၽန္ေတာ္တို႔ select statement ကို ေျပာလက္စ ဆက္ပါဦးမယ္။ table တစ္ခုထဲက data ကို ထုတ္ၾကည့္တာကိုေတာ့ နားလည္ေလာက္ျပီထင္ပါတယ္။ အခု ကြၽန္ေတာ္တို႔ တစ္ခုထက္ပိုတဲ႔ table ေတြထဲက data ေတြကို ထုတ္ၾကည့္ရေအာင္။ ဥပမာဗ်ာ… item table ထဲက record အားလံုးျပမယ္ဆိုပါေတာ့။ ဒါေပမယ့္ categoryid အစား categoryname ကိုျပခ်င္တယ္ဆိုရင္၊ item table နဲ႔ category table ၂ ခုကို ခ်ိတ္ရေတာ့မွာပါ။ ဟုတ္ျပီ… ဒါဆိုရင္ table ေတြ ခ်ိတ္ဖို႔အတြက္ ဘာေတြလိုလဲ… ?

ဟုတ္ကဲ႔ table တစ္ခုနဲ႔ တစ္ခုခ်ိတ္ဆက္တာကို join လုပ္တယ္လို႔ ေခၚပါတယ္။ join နည္း ၃ မ်ိဳးရွိပါတယ္။

၁) cross join

Cross join ကေတာ့ table ၂ ခုကို comer(,) ခံျပီး join တာပါ။ ဒီလိုမ်ိဳးပါ

select item.*, category.categoryName
from item, category

result

ItemID ItemName CategoryID Price Balance Remark CategoryName
1 Sony Optical Mouse 1 2500 10 - Computer
2 A4 Tech Keyboard 1 5000 1 - Computer
3 ViewSonic 17” Flat CRT 1 15000 5 - Computer
4 First Eleven Book 2 500 15 - Computer
5 SONY 32” TV 3 900000 1 - Computer
6 Moon 16” Stand Fan 3 20000 3 - Computer
1 Sony Optical Mouse 1 2500 10 - Stationary
2 A4 Tech Keyboard 1 5000 1 - Stationary
3 ViewSonic 17” Flat CRT 1 15000 5 - Stationary
4 First Eleven Book 2 500 15 - Stationary
5 SONY 32” TV 3 900000 1 - Stationary
6 Moon 16” Stand Fan 3 20000 3 - Stationary
1 Sony Optical Mouse 1 2500 10 - Electronic
2 A4 Tech Keyboard 1 5000 1 - Electronic
3 ViewSonic 17” Flat CRT 1 15000 5 - Electronic
4 First Eleven Book 2 500 15 - Electronic
5 SONY 32” TV 3 900000 1 - Electronic
6 Moon 16” Stand Fan 3 20000 3 - Electronic
1 Sony Optical Mouse 1 2500 10 - Clothes
2 A4 Tech Keyboard 1 5000 1 - Clothes
3 ViewSonic 17” Flat CRT 1 15000 5 - Clothes
4 First Eleven Book 2 500 15 - Clothes
5 SONY 32” TV 3 900000 1 - Clothes
6 Moon 16” Stand Fan 3 20000 3 - Clothes
1 Sony Optical Mouse 1 2500 10 - Kitchen wares
2 A4 Tech Keyboard 1 5000 1 - Kitchen wares
3 ViewSonic 17” Flat CRT 1 15000 5 - Kitchen wares
4 First Eleven Book 2 500 15 - Kitchen wares
5 SONY 32” TV 3 900000 1 - Kitchen wares
6 Moon 16” Stand Fan 3 20000 3 - Kitchen wares

cross join လို႔ ဆိုတဲ႔ အတိုင္းပါပဲ။ ဒီပံုစံက table ၂ ခုကို ေျမွာက္လို႔ရတဲ႔ အတိုင္း result ထြက္ပါတယ္။ item ထဲမွာက record ၆ ေၾကာင္း၊ category ထဲမွာက record ၅ ေၾကာင္း ရွိပါတယ္။ ဒါေၾကာင့္ အေပၚက query ကို run လိုက္ရင္ record စုစုေပါင္း အေၾကာင္း ၃၀ ထြက္ပါလိမ့္မယ္။ item table ထဲက record ၁ေၾကာင္းစီ အတြက္ category table ထဲမွာ record ရွိသေလာက္ကို ထုတ္ျပသြားတာပါ။ ကြၽန္ေတာ္က ့ အကုန္မထုတ္ပဲ item table ထဲမွာ ပါတဲ႔ categoryID နဲ႔ category table ထဲမွာပါတဲ႔ categoryID တူတဲ႔ record ေတြပဲ ထုတ္ျပခ်င္တယ္ဆိုရင္ေတာ့ ထြက္လာတဲ႔ result ကို condition ျပန္စစ္ဖို႔ လိုပါတယ္။ ဒါေၾကာင့္ ကြၽန္ေတာ္တို႔ where ကို သံုးျပီး အခုလို ေရးရပါမယ္။

select item.*, category.categoryName
from item, category where item.categoryID = category.categoryID

result

ItemID ItemName CategoryID Price Balance Remark CategoryName
1 Sony Optical Mouse 1 2500 10 - Computer
2 A4 Tech Keyboard 1 5000 1 - Computer
3 ViewSonic 17” Flat CRT 1 15000 5 - Computer
4 First Eleven Book 2 500 15 - Stationary
5 SONY 32” TV 3 900000 1 - Electronic
6 Moon 16” Stand Fan 3 20000 3 - Electronic

၂) inner join
Inner join က cross join ကို where ထည့္ထားတဲ႔ ပံုစံနဲ႔ တူပါတယ္။ table ၂ ခုကို join ဖို႔အတြက္ On keyword ကိုသံုးပါတယ္။ ဒီလိုမ်ိဳးပါ..

select item.*, category.categoryName
from item inner join category
on item.categoryID = category.categoryID

result

ItemID ItemName CategoryID Price Balance Remark CategoryName
1 Sony Optical Mouse 1 2500 10 - Computer
2 A4 Tech Keyboard 1 5000 1 - Computer
3 ViewSonic 17” Flat CRT 1 15000 5 - Computer
4 First Eleven Book 2 500 15 - Stationary
5 SONY 32” TV 3 900000 1 - Electronic
6 Moon 16” Stand Fan 3 20000 3 - Electronic

inner join နဲ႔ေရးထားတဲ႔အတြက္ item table ထဲက categoryID က category table ထဲမွာ ရွိမေနဘူးဆိုရင္ အဲဒီ record ကို ထုတ္ျပေပးမွာမဟုတ္ပါဘူး။ ဟုတ္ျပီ တစ္ခါတေလမွာ ကြၽန္ေတာ္က item table ထဲက အားလံုးထုတ္ျပခ်င္တယ္၊ category table ထဲမွာ ရွိရင္ category name ျပျပီး၊ မရွိရင္ေတာ့ null ပဲေပၚလာခ်င္တယ္ဆိုရင္။ တနည္းအားျဖင့္ table တခုခုကို မူတည္ျပီး ျပခ်င္တယ္ဆိုရင္ ကြၽန္ေတာ္တို႔ inner join ကိုသံုးလို႔ မရေတာ့ပါဘူး။ ဒီအခါမွာ outer join ကိုသံုးရေတာ့မွာပါ။

၃) outer join
Outer join ကို ၂ ပိုင္းထပ္ခြဲနိုင္ပါတယ္။ Left outer join နဲ႔ Right outer join ပါ။ join keyword ရဲ႕ ဘယ္ဖက္က table ကို အကုန္ျပမယ္ဆိုရင္ left join နဲ႔ ေရးရျပီး။ join keyword ရဲ႕ ညာဖက္က table ကို အကုန္ျပခ်င္ရင္ေတာ့ right join နဲ႔ ေရးပါမယ္။

select item.*, category.categoryName
from item left join category
on item.categoryID = category.categoryID

result

ItemID ItemName CategoryID Price Balance Remark CategoryName
1 Sony Optical Mouse 1 2500 10 - Computer
2 A4 Tech Keyboard 1 5000 1 - Computer
3 ViewSonic 17” Flat CRT 1 15000 5 - Computer
4 First Eleven Book 2 500 15 - Stationary
5 SONY 32” TV 3 900000 1 - Electronic
6 Moon 16” Stand Fan 3 20000 3 - Electronic
select item.*, category.categoryName
from item right join category
on item.categoryID = category.categoryID

result

ItemID ItemName CategoryID Price Balance Remark CategoryName
1 Sony Optical Mouse 1 2500 10 - Computer
2 A4 Tech Keyboard 1 5000 1 - Computer
3 ViewSonic 17” Flat CRT 1 15000 5 - Computer
4 First Eleven Book 2 500 15 - Stationary
5 SONY 32” TV 3 900000 1 - Electronic
6 Moon 16” Stand Fan 3 20000 3 - Electronic
NULL NULL NULL NULL NULL NULL Clothes
NULL NULL NULL NULL NULL NULL Kitchen wares

left join လို႔ေရးေရး left outer join လို႔ပဲ ေရးေရး တူတူပါပဲ။ right join နဲ႔ right outer join လည္း တူတယ္ေနာ္။ ေနာက္ဆံုး query result မွာ null ေတြထြက္လာတာ ေတြ႔ရတယ္ေနာ္၊ ဘာလို႔လဲဆိုေတာ့ category table ကို မူတည္ျပီး ယူလိုက္ေတာ့ item table ထဲမွာ မရွိတာေတြအတြက္ null ေတြထြက္လာတာပါ။

August 3, 2009

basic of SQL Statement (part 5)

Filed under: SQL Commands — Tags: , , — sevenlamp @ 2:15 PM

အခု ကြၽန္ေတာ္တို႔ Select quary ေတြမွာ count တို႔ sum တို႔ စသည့္ျဖင့္ function ေတြထည့္သံုးၾကည္ရေအာင္။ အဲ့ဒီ function ေတြကို aggregate function ေတြလို႔ ေခၚတယ္။ ဒီလို aggregate function ေတြပါတဲ႔ query ေတြကိုလည္း aggregate query လို႔ေခၚပါတယ္။

Aggregate Functions List

AVG MIN
CHECKSUM SUM
CHECKSUM_AGG STDEV
COUNT STDEVP
COUNT_BIG VAR
GROUPING VARP
MAX

ဒီ function ေတြကို ကြၽန္ေတာ္တို႔ Select ရဲ့ ေနာက္မွာ ျဖစ္ေစ၊ Having ရဲ့ေနာက္မွာ ျဖစ္ေစ၊ သံုးနိုင္ပါတယ္။ ဥပမာ အခုကြၽန္ေတာ္က ItemTable ထဲမွာ ရွိတဲ႔ record အေရအတြက္ကို သိခ်င္တယ္ဆိုပါစို႔။ ဒါဆိုရင္ Count ဆိုတဲ႔ aggregate function ကိုသံုးျပီး ခုလိုေရးရမွာပါ။

SELECT count(*) AS ItemCount FROM item

Result:

ItemCount
6

Count ဆိုတဲ႔ function က ကြၽန္ေတာ္တို႔ Table ထဲမွာ ရွိတဲ႔ record အေရအတြက္ကိုစစ္ေပးပါတယ္။ အဲ့ဒီ function ထဲမွာ * ဒါမွမဟုတ္ ၾကိဳက္ရာ column name ထည့္ေပးရပါမယ္။ ဒီ function ေတြကိုသံုးရင္ ထြက္လာတဲ႔ result မွာ column name မရွိပါဘူး။ ဒါေၾကာင့္ ကြၽန္ေတာ္က column name ထည့္ေပးဖို႔အတြက္ AS keyword ကိုသံုးပါတယ္၊ AS ေနာက္မွာ ကြၽန္ေတာ္ေပးခ်င္တဲ႔ ItemCount ဆိုတဲ႔ နံမည္ေလးထည့္ေပးလိုက္ပါတယ္။ တျခား Function ေတြကိုလည္း စမ္းၾကည့္ရေအာင္။ ကြၽန္ေတာ္အခု စုစုေပါင္းကုန္လက္က်န္ကို သိခ်င္တယ္ဆိုရင္။

SELECT sum(balance) AS TotalBalance FROM item

Result:

TotalBalance
35

အခုကြၽန္ေတာ္တို႔ aggregate function ေတြကိုသံုးတဲ႔အခါမွာ table ထဲက record အားလံုးအတြက္ result တစ္ခုထဲထြက္တာကို ေတြ႔ရလိမ့္မယ္။ ဟုတ္ျပီ ဒါဆိုရင္ အခုကြၽန္ေတာ္က Category တစ္ခုစီအတြက္ ကုန္လက္က်န္ကို ၾကည့္ခ်င္တယ္ဆိုရင္ေရာ။ ဒီလိုလုပ္ဖို႔ဆိုရင္ေတာ့ ကြၽန္ေတာ္တို႔ Group By keyword နဲ႔ တြဲသံုးေပးဖို႔လိုလာပါျပီ။

SELECT categoryid,sum(balance) AS TotalBalance
FROM item
GROUP BY categoryid

Result:

categoryID TotalBalance
1 16
2 15
3 4

*** မွတ္ထားစရာ အခ်က္တစ္ခ်က္ရွိေသးတယ္ဗ်။ select ထဲမွာ aggregate function ေတြ ပါျပီဆိုရင္၊ Group By လုပ္ထားတဲ႔ column ေတြကလြဲျပီး တျခား column ေတြကို ထည့္ေရးခြင့္ မရွိပါဘူး***
အခု Having keyword အေၾကာင္းဆက္ေျပာရေအာင္။ ကြၽန္ေတာ္တို႔ေရးတဲ႔ query မွာ condition စစ္စရာေတြ ရွိရင္ where ကိုသံုးရတယ္ဆိုတာ သိျပီးၾကျပီေနာ္။ ဟုတ္ျပီ အဲ့ဒီ where keyword က database ထဲမွာ ရွိတဲ႔ column name ေတြကိုပဲ စစ္လို႔ရပါတယ္။ တခါတေလကြၽန္ေတာ္တို႔က aggregate function ကထြက္လာတဲ႔ result ေတြကို စစ္ဖို႔လိုလာျပီဆိုရင္ေတာ့ Having ကိုသံုးရေတာ့မွာပါပဲ။ ဥပမာ ကုန္လက္က်န္ စုစုေပါင္း ၁၀ ခုထက္နည္းနဲ႔ category ကိုၾကည့္ခ်င္တယ္ဆိုရင္။

SELECT categoryid,sum(balance) AS TotalBalance
FROM item
GROUP BY categoryid
HAVING sum(balance) < 10

Result:

categoryID TotalBalance
3 4

Having ကို where လိုမ်ိဳးလဲ column ေတြစစ္ဖို႔အတြက္ သံုးလို႔လည္း ရပါတယ္။ category id 1 ရဲ႕ totalbalance ကိုျပဖို႔အတြက္

SELECT categoryid,sum(balance) AS TotalBalance
FROM item
GROUP BY categoryid
HAVING categoryid = 1

Result:

categoryID TotalBalance
1 16

July 31, 2009

basic of SQL Statement (part 4)

Filed under: SQL Commands — Tags: , , , — sevenlamp @ 2:21 PM

ဒီေန႔ Select Statement အေၾကာင္းပဲ ဆက္ရေအာင္ဗ်ာ။ WHERE အေၾကာင္းပဲ ဆက္ေျပာပါ့မယ္။ အခုကြၽန္ေတာ္က ကုန္လက္က်န္ ၁၀ ခုထက္နည္းျပီး ေစ်းႏႈန္းကလည္း ၁၀၀၀၀ အထက္ရွိတဲ႔ item ေတြရဲ့ list ကိုၾကည့္ခ်င္တယ္။ ဒါဆိုရင္ ဒီလိုေရးရမွာေပါ့…

SELECT * FROM item
WHERE balance < 10 AND price > 10000

Result :

ItemID ItemName CategoryID Price Balance Description
3 ViewSonic 17” Flat CRT 11 15000 5 -
5 SONY 32” TV 3 900000 1 -
6 Moon 16” Stand Fan 3 20000 3 -

ဒီ query ေလးမွာ ကြၽန္ေတာ္စစ္ခ်င္တဲ႔ condition က ၂ ခုရွိေနတယ္။ balance ၁၀ ခုထက္နည္းရမွာရယ္၊ price ၁၀၀၀၀ ထက္မ်ားရမွာရယ္။ အဲ့ဒီ condition ၂ ခုလံုးမွန္တာကိုပဲ လိုခ်င္တာ ျဖစ္ေတာ့ Where ေနာက္မွာ condition ၂ ခုကို AND နဲ႔ဆက္ျပီးေရးရပါတယ္။ ကြၽန္ေတာ္တို႔ စစ္ခ်င္တဲ႔ condition ေတြ ၁ခုထက္ပိုမ်ားလာျပီဆိုရင္ အဲ့ဒီ condition ေတြၾကားမွာ AND ဒါမွမဟုတ္ OR ဆိုတဲ႔ keyword ေလးေတြထည့္ေရးေပးရမယ္။ အေပၚက query လိုမ်ိဳး condition ၂ ခုလံုးမွန္မွ result ထြက္ခ်င္တယ္ဆိုရင္ AND ကိုသံုးရျပီး။ condition တစ္ခုမဟုတ္ တစ္ခုမွန္ရံုနဲ႔ result ထြက္ခ်င္တယ္ဆိုရင္ေတာ့ OR ကိုသံုးရမွာပါ။ ဥပမာ အေနနဲ႔ ကြၽန္ေတာ္က electronic ပစၥည္းေတြနဲ႔ computer ပစၥည္းေတြကိုပဲ ၾကည့္ခ်င္တယ္ ဆိုပါစို။ ဒါဆို ဒီလိုေရးရမွာပါ…

SELECT * FROM item
WHERE CategoryID = 1 OR CategoryID = 3

Result :

ItemID ItemName CategoryID Price Balance Description
1 Sony Optical Mouse 1 2500 10 -
2 A4 Tech Keyboard 1 5000 1 -
5 SONY 32” TV 3 900000 1 -
6 Moon 16” Stand Fan 3 20000 3 -

တခါတေလမွာ ကြၽန္ေတာ္တို႔စစ္ခ်င္တဲ႔ condition က value ၂ ခုၾကားျဖစ္ခဲ႔ရင္ BETWEEN ကိုလည္း သံုးလို႔ရပါတယ္။ ဥပမာ balance ၂ ခုကေန ၁၀ ခု ၾကားထဲက record ေတြ လိုခ်င္တယ္ဆိုရင္…

SELECT * FROM item
WHERE balance BETWEEN 2 AND 10

Result :

ItemID ItemName CategoryID Price Balance Description
1 Sony Optical Mouse 1 2500 10 -
3 ViewSonic 17” Flat CRT 11 15000 5 -
6 Moon 16” Stand Fan 3 20000 3 -

ထြက္လာတဲ႔ result ရဲ့ ဆန္က်င္ဘက္ ေျပာင္းျပန္ကို လိုခ်င္တယ္ ဆိုရင္ေတာ့ NOT ကို တြဲသံုးလို႔ ရပါတယ္။

SELECT * FROM item
WHERE balance NOT BETWEEN 2 AND 10

Result:

ItemID ItemName CategoryID Price Balance Description
2 A4 Tech Keyboard 1 5000 1 -
4 First Eleven Book 2 500 15 -
5 SONY 32” TV 3 900000 1 -

တစ္ခါတစ္ေလ ကြၽန္ေတာ္တို႔ လိုခ်င္တဲ႔ record ေတြက အစီအစဥ္လိုက္မဟုတ္ဘူး ဆိုရင္ေတာ့ condition စစ္တဲ႔အခါ OR ေတြအမ်ားၾကီးသံုးရမယ့္ အေနအထားေတြ ရွိလာနိုင္ပါတယ္။ ဥပမာ ကြၽန္ေတာ္က ItemID 1,3,5 ကိုထုတ္ၾကည့္မယ္ဆိုရင္ OR သံုးျပီးစစ္ရင္ ဒီလိုမ်ိဳးျဖစ္ပါမယ္။

SELECT * FROM item
WHERE ItemID = 1 OR ItemID = 3 OR ItemID = 5

ဒီလို အေျခအေနမ်ိဳးမွာ ကြၽန္ေတာ္တို႔ IN ကိုသံုးျပီးေရးရင္ ပိုလြယ္မွာပါ။

SELECT * FROM item
WHERE ItemID IN ( 1,3,5 )

Result:

ItemID ItemName CategoryID Price Balance Description
1 Sony Optical Mouse 1 2500 10 -
3 ViewSonic 17” Flat CRT 11 15000 5 -
5 SONY 32” TV 3 900000 1 -

ItemID 1,3,5 ကလြဲျပီး က်န္တာေတြ ျပမယ္ဆိုရင္ေတာ့

SELECT * FROM item
WHERE ItemID NOT IN ( 1,3,5 )

Result:

ItemID ItemName CategoryID Price Balance Description
2 A4 Tech Keyboard 1 5000 1 -
4 First Eleven Book 2 500 15 -
6 Moon 16” Stand Fan 3 20000 3 -

ဒီေလာက္ဆိုရင္ေတာ့ ကြၽန္ေတာ္တို႔ WHERE Condition ေတာ္ေတာ္ေလးစစ္တက္ျပီလို႔ ေျပာလို႔ရပါျပီ။ အခုေနာက္ထပ္ ORDER BY အေၾကာင္း ဆက္ေျပာပါ့မယ္။ Select Query ကေနထြက္လာတဲ႔ result record ေတြကို sorting လုပ္ခ်င္ရင္ Order By keyword ကိုသံုးရမွာပါ။ အိုေက အခုကြၽန္ေတာ္ Item table ထဲက record ေတြအားလံုးၾကည့္ခ်င္တယ္။ ကုန္လက္က်န္အနည္းဆံုးက အေပၚဆံုးမွာျပခ်င္တယ္ဆိုရင္….

SELECT * FROM item
ORDER BY balance

Result:

ItemID ItemName CategoryID Price Balance Description
2 A4 Tech Keyboard 1 5000> 1 -
5 SONY 32” TV 3 900000 1 -
6 Moon 16” Stand Fan 3 20000 3 -
3 ViewSonic 17” Flat CRT 11 15000 5 -
1 Sony Optical Mouse 1 2500 10 -
4 First Eleven Book 2 500 15 -

Order by keyword က default အားျဖင့္ ငယ္စဥ္ၾကီးလိုက္(Ascending) စီေပးပါတယ္။ ၾကီးစဥ္ငယ္လိုက္(Descending) စီခ်င္တယ္ဆိုရင္ေတာ့ ကိုယ္စီခ်င္တဲ႔ column name ေနာက္မွာ DESC ဆိုျပီးထည့္ေပးရပါမယ္။ ဥပမာ ကုန္လက္က်န္အမ်ားဆံုးကို အေပၚဆံုးကေန ျပခ်င္ရင္…

SELECT * FROM item
ORDER BY balance DESC

Result:

ItemID ItemName CategoryID Price Balance Description
4 First Eleven Book 2 500 15 -
1 Sony Optical Mouse 1 2500 10 -
3 ViewSonic 17” Flat CRT 11 15000 5 -
6 Moon 16” Stand Fan 3 20000 3 -
2 A4 Tech Keyboard 1 5000 1 -
5 SONY 32” TV 3 900000 1 -

အခုလို sorting စီတဲ႔ေနရာမွာ Column တစ္ခုထက္ပိုျပီး စီခ်င္ရင္ comer(,) ေလးေတြ ျခားသြားရံုပါပဲ။ ကုန္လက္က်န္ အနည္းဆံုးကို အရင္စီမယ္၊ ကုန္လက္က်န္ အေရအတြက္ခ်င္း တူရင္ ေစ်းႏႈန္းအမ်ားဆံုးကို အေပၚကထားခ်င္တယ္ဆိုရင္….

SELECT * FROM item
ORDER BY Balance ASC, Price DESC

(*** ASC ကို ထည့္ေရးေရး၊ မေရးေရး ရပါ၏ ***)

Result:

ItemID ItemName CategoryID Price Balance Description
5 SONY 32” TV 3 900000 1 -
2 A4 Tech Keyboard 1 5000 1 -
6 Moon 16” Stand Fan 3 20000 3 -
3 ViewSonic 17” Flat CRT 11 15000 5 -
1 Sony Optical Mouse 1 2500 10 -
4 First Eleven Book 2 500 15 -

ဒီေလာက္ဆိုရင္ေတာ့ ကြၽန္ေတာ္တို႔ Simple Select Query ေတြ ေရးတက္ပါျပီ။

July 30, 2009

basic of SQL Statement (part 3)

Filed under: SQL Commands — Tags: , — sevenlamp @ 6:16 PM

Select Statement

SELECT [ALL | DISTINCT] columnname1 [,columnname2]
FROM tablename1 [,tablename2]
[WHERE condition] [ and|or condition...]
[GROUP BY column-list]
[HAVING "conditions]
[ORDER BY "column-list" [ASC | DESC] ]

SQL Statement ၄ မ်ိဳးမွာျဖင့္ ဒီ select က အရွဳပ္ဆံုးပါပဲဗ်ာ။ user ၾကည့္ခ်င္တဲ႔ result ရဖို႔အတြက္ ကြၽန္ေတာ္တို႔ select command ကို နည္းလမ္းမ်ိဳးစံုနဲ႔ သံုးရမွာပါ။ အရႈပ္ဆံုးဆိုလို႔လည္း လန္႔မသြားနဲ႔ဦးေနာ္၊ ဘယ္အရာမဆို မသိရင္ခက္ျပီး သိသြားရင္ေတာ့ လြယ္ပါတယ္။ ကဲ.. အလြယ္ဆံုးကေန စလိုက္ရေအာင္။ ပထမဆံုး ကြၽန္ေတာ္က Category Table ထဲက record ေတြအားလံုးကို user ကို ထုတ္ျပခ်င္တယ္ ဆိုပါေတာ့။ ဒါဆိုရင္ ဒီလိုေရးရမွာပါ။

SELECT * FROM Category

Result :

CategoryID CategoryName Status
1 Computer Active
2 Stationary Active
3 Electronic Active
4 Clothes Inactive
5 Kitchen wares Inactive

Select command ရဲ႕ ေနာက္မွာ ကိုယ္ထည့္ၾကည့္ခ်င္တဲ႔ column name ေတြေပးရမွာပါ။ ရွိသမွ် ေကာ္လံအားလံုးကို ထုတ္ျပမယ္ဆိုရင္ ကြၽန္ေတာ္အခုေရးလိုက္သလို (*) ကို သံုးလို႔လည္း ရပါတယ္။ ဒါဆို အခုကြၽန္ေတာ္က CategoryID နဲ႔ CategoryName ၂ ခုပဲ ၾကည့္ခ်င္တယ္ဆိုရင္ ဒီလိုျဖစ္သြားမယ္။

SELECT CategoryID,CategoryName FROM Category

Result :

CategoryID CategoryName
1 Computer
2 Stationary
3 Electronic
4 Clothes
5 Kitchen wares

ဘယ္လိုလဲ လြယ္ပါတယ္ေနာ္။ လြယ္မွာေပါ့ ဒါက အရိုးရွင္းဆံုး select ပံုစံကို။ ကဲ အခုကြၽန္ေတာ္က category table ထဲက record အားလံုးမၾကည့္ခ်င္ဘူး Status က Active ျဖစ္တဲ႔ Category ေတြပဲ ၾကည့္ခ်င္တယ္ ဆိုရင္ Condition ျပန္စစ္ဖို႔လိုလာပါျပီ။ select command မွာ condition စစ္ဖို႔အတြက္ where ရယ္၊ having ရယ္ ၂ ခု ၾကိဳက္တာသံုးခြင့္ရွိပါတယ္။ ဒီ ၂ ခုဘာကြာလဲဆိုတာ ေနာက္မွေျပာျပပါမယ္။ ေလာေလာဆယ္ေတာ့ ကြၽန္ေတာ္က where ကိုပဲ သံုးျပီး စစ္လုိက္ပါမယ္။

SELECT * FROM Category WHERE Status = ‘Active’

Result :

CategoryID CategoryName Status
1 Computer Active
2 Stationary Active
3 Electronic Active

ဒါဆိုရင္ေတာ့ record ၃ ေၾကာင္းပဲ ျပပါေတာ့မယ္။

နက္ဖန္မွဆက္ေရးေတာ့မယ္ဗ်ာ။ ဒီေန႔ ျပန္ရေတာ့မွာမို႔လို႔။

July 29, 2009

basic of SQL Statement (part 2)

Filed under: SQL Commands — Tags: , , — sevenlamp @ 5:14 PM

Update Statement

update tablename
set columnname = newvalue [,columnxname = newvaluex...]
where columnname OPERATOR value [and|or columnnamex OPERATOR valuex];

ကြၽန္ေတာ္တို႔ရဲ့ table ထဲက data ေတြကို ျပန္ျပီး ျပင္ခ်င္တယ္ဆိုရင္ေတာ့ SQL Update Command ကိုသံုးပါတယ္။ Update Command ကို Set keyword နဲ႔ တြဲျပီး သံုးပါတယ္။ ဥပမာအေနနဲ႔ ခုနတုန္းက insert quary 2 မွာ ကြၽန္ေတာ္က categoryid နဲ႔ categoryname ၂ ခုပဲထည့္လိုက္တယ္၊ status ကိုမထည့္ေပးလုိက္ဘူး။ ဒါဆိုရင္ status က NULL value အေနနဲ႔ ရွိေနမွာပါ။ ဒါကို အခု ကြၽန္ေတာ္က Active လို႔ထည့္ေပးခ်င္တယ္ဆိုပါေတာ့… ဒါဆိုရင္ ဒီလိုေရးရပါမယ္။

UPDATE Category
SET Status = ‘Active’
WHERE CategoryID = 2

ဒီ update quary ေလးမွာ ေရးလုိက္တာေတြကေတာ့ UPDATE command ရဲ႕ အေနာက္မွာ ကိုယ္ျပင္ခ်င္တဲ႔ tablename ထည့္ေပးရမယ္။ ျပီးရင္ SET keyword ရဲ႕ ေနာက္မွာ ကိုယ္ျပင္ခ်င္တဲ႔ column name = value ကို comer(,) ေလးခံျပီး ၾကိဳက္သေလာက္ထည့္ေပးလို႔ရပါတယ္။ အခု example မွာေတာ့ ကြၽန္ေတာ္က Status column တစ္ခုထဲျပင္ခ်င္တာမို႔တခုထဲေပးတာပါ။ ျပီးေနာက္မွာမွ WHERE keyword ကိုသံုးျပီး ကြၽန္ေတာ္ျပင္ခ်င္တဲ႔ report ကိုေရြးထုတ္ပါတယ္။ CategoryID 2 နဲ႔ ညီတဲ႔ record ကိုပဲျပင္မယ္ဆိုတဲ႔ သေဘာပါ။ WHERE မပါပဲလည္း ေရးလို႔ရပါတယ္။ အဲ့လိုေရးလိုက္မယ္ဆိုရင္ေတာ့ Category table ထဲက record ေတြအားလံုးကို ျပင္သြားမွာျဖစ္ပါတယ္။ ဒါေၾကာင့္ update statement ေရးရင္ WHERE ထည့္ေရးဖို႔ လိုေလ႔ရွိပါတယ္။

Delete Statement

delete from "tablename"
where columnname OPERATOR value [and|or
columnnamex OPERATOR valuex];

Delete statement ကိုေတာ့ ကြၽန္ေတာ္တို႔ table ထဲက record ေတြကိုျပန္ဖ်က္ခ်င္ရင္ သံုးရမွာပါ။ တခုသိထားဖို႔လိုတာက delete command ကိုသံုးရင္ record (row) တခုလံုးဖ်က္မွာ ျဖစ္ပါတယ္။ column value တစ္ခုထဲကို ဖ်က္ခ်င္တယ္ဆိုရင္ေတာ့ delete command မသံုးရပါဘူး။ update command ကိုပဲသံုးရပါမယ္။ ကဲ ကြၽန္ေတာ္က category table ထဲက record ေတြအားလံုးကို မသံုးခ်င္ေတာ့ဘူး ဆိုပါေတာ့…

DELETE FROM Category

Delete command ကို From နဲ႔ တြဲသံုးပါတယ္၊ ေနာက္မွာေတာ့ ဖ်က္မယ့္ table name ထည့္ေပးရမွာပါ။ record အားလံုးဖ်က္ခ်င္တာမဟုတ္ဘူးဆိုရင္ေတာ့ update command တုန္းကလိုမ်ိဳး Where statement ကိုသံုးျပီး ကိုယ္ဖ်က္ခ်င္တဲ႔ record ရေအာင္ျပန္စစ္ထုတ္ရပါမယ္။
Insert, Update နဲ႔ Delete Statement ေတြဟာ ေရးရတာရွင္းလင္းလြယ္ကူပါတယ္။ တကယ္ရႈပ္တာကေတာ့ Select Statement ပါ။ အခုကြၽန္ေတာ္တို႔ select statement ကိုမသြားခင္ ကြၽန္ေတာ္တို႔ေဆာက္ထားတဲ႔ category နဲ႔ item table ေတြထဲကို data ေလးေတြထည့္ေပးပါဦး။ ဒါမွ select ျပန္လုပ္လို႔ရမွာပါ။ insert command ကိုသံုးေနာ္။

Category Table

CategoryID CategoryName Status
1 Computer Active
2 Stationary Active
3 Electronic Active
4 Clothes Inactive
5 Kitchen wares Inactive

Item Table

ItemID ItemName CategoryID Price Balance Description
1 Sony Optical Mouse 1 2500 10 -
2 A4 Tech Keyboard 1 5000 1 -
3 ViewSonic 17” Flat CRT 1 150000 5 -
4 First Eleven Book 2 500 15 -
5 SONY 32” TV 3 900000 1 -
6 Moon 16” Stand Fan 3 20000 3 -

basic of SQL Statement (part 1)

Filed under: SQL Commands — Tags: , — sevenlamp @ 11:10 AM

SQL Statement ေလးေတြနဲ႔ ပတ္သက္ျပီး ကြၽန္ေတာ္သိသေလာက္ basic knowledge ေလးေတြကို ျပန္ျပီး မွ်ေ၀ေပးခ်င္လို႔ပါ။ ညီေလး ညီမေလးေတြ အတြက္ပဲေပါ့။ ကြၽန္ေတာ္တို႔ programming လုပ္တဲ႔ သူေတြအားလံုး တခ်ိန္မဟုတ္တခ်ိန္ေတာ့ SQL Statement ေတြနဲ႔ ပတ္သက္ရမွာ မလြဲပါဘူး။ C# နဲ႔ပဲေရးေရး VB နဲ႔ပဲေရးေရး PHP နဲ႔ပဲေရးေရး java ပဲျဖစ္ျဖစ္ေပါ့ ဘယ္ language ကိုပဲ သံုးသည္ျဖစ္ေစ data ေတြကို database တစ္ခုခုမွာ သိမ္းေတာ့မယ္၊ ျပန္ထုတ္ျပေတာ့မယ္ဆိုတာနဲ႔ ကြၽန္ေတာ္တို႔ SQL Statement ေတြကို သံုးရေတာ့မွာပါ။ ဒီေနရာမွာ Database လို႔ ေျပာလိုက္ေတာ့ Microsoft platform က သူေတြက MS SQL Server ကို ေျပးျမင္ၾကမွာပါ။ ဟုတ္ပါတယ္ ကြၽန္ေတာ္လည္း MS SQL Server 2005 ကို သံုးေနတာပါပဲ။ ဒါေပမယ့္ ဘယ္လို DBMS ကိုပဲ သံုးသည္ျဖစ္ေစ Basic SQL Statement ေတြကေတာ့ အတူတူပါပဲ။ ဒါေၾကာင့္ ကြၽန္ေတာ္တို႔ အဲ့ဒီ basic SQL Statement ေလးေတြအေၾကာင္း ေလ့လာၾကည့္ရေအာင္ဗ်ာ။

ဟိုးအရင္ Turbo C သံုးျပီး program ေတြ ေရးၾကတဲ႔ ေခတ္တုန္းကေတာ့ ကြၽန္ေတာ္တို႔ေတြ data ေတြသိမ္းဖို႔အတြက္ harddisk ထဲမွာ file ေတြကိုယ္တိုင္ ေဆာက္ျပီး ကိုယ္တိုင္ပဲ အသြင္းအထုတ္လုပ္ခဲ႔ၾကတယ္။ ေနာက္ပိုင္းမွာ DBMS(Database Management Software) ေတြ ေပၚလာေတာ့ ကြၽန္ေတာ္တို႔ကိုယ္တိုင္ မလုပ္ေတာ့ပဲ အဲ့ဒီ DBMS ေတြကိုပဲ ခိုင္းစားလာခဲ႔ၾကတယ္။ အဲ့ဒီလို ခိုင္းဖို႔အတြက္ ကြၽန္ေတာ္တို႔က SQL လို႔ေခၚတဲ႔ Structured Query Language ကို သံုးၾကရပါတယ္။ အဲ့ဒီ SQL မွာ အဓိက အပိုင္း ၄ ပိုင္းပါပါတယ္။

INSERT                ( database ထဲကို data အသစ္ေတြထည့္ဖို႔ )

UPDATE               ( database ထဲက data ေတြကို ျပင္ဖို႔ )

DELETE                ( database ထဲက data ေတြကို ဖ်က္ဖို႔ )

SELECT                 ( database ထဲက data ေတြကို user ကို ျပန္ထုတ္ျပဖို႔ )

ဒီ command ေတြ စမ္းၾကည့္ရေအာင္အတြက္ ကြၽန္ေတာ္ database ထဲမွာ table ၂ ခုေဆာက္လိုက္မယ္။ ကြၽန္ေတာ္လက္ရွိသံုးေနတာက SQL Server 2005 ဆိုေတာ့ အဲ့ဒါနဲ႔ပဲ ေျပာမယ္ေနာ္။ ညီတို႔က တျခား DBMS ေတြ သံုးရင္လည္း အရမ္းေတာ့ ကြာမယ္မထင္ပါဘူး။

Category Table Design

CategoryID                   int                Primary key
CategoryName               varchar(50)
Status                     varchar(50)

Item Table Design

ItemID                    int                Primary key
ItemName                varchar(50)
CategoryID                int
Price                     int
Balance                   int
Description             varchar(250)

Insert Statement

insert into tablename
(column1name,column2name...columnxname)
values (value1,value2...valuex);

Database ထဲကို data record အသစ္တခုထည့္ေတာ့မယ္ဆိုရင္ ကြၽန္ေတာ္တို႔ Insert Command ကိုသံုးရပါမယ္။ အခု အရင္ဆံုး Category Table ထဲကို record တစ္ခု ထည့္ၾကည့္ရေအာင္။ Insert Command ရဲ႕ အေနာက္မွာ into ဆိုတဲ႔ keyword ေလးထည့္ေရးရတယ္။ ျပီးရင္ ကိုယ္ data ထည့္ခ်င္တဲ႔ table name ေပးရတယ္။ ျပီးေတာ့မွ ကိုယ့္ထည့္မယ့္ data ေတြကို values ဆိုတဲ႔ keyword ရဲ့ ေနာက္မွာ ထည့္ေရးေပးရမယ္။ ဥပမာ…
*** SQL SERVER 2005 တြင္ character ႏွင့္ datetime မ်ားကို single code ( ‘ ) ထည့္ေပးရပါသည္။ ***

INSERT INTO Category
VALUES (1, ‘Computer’, ‘Active’)

အေပၚက example query ေလးအတိုင္း ေရးမယ္ဆိုရင္ category table ထဲမွာ ရွိတဲ႔ field ေတြအားလံုးအတြက္ data ထည့္ကိုထည့္ေပးရမယ္။ အစဥ္လိုက္လည္း ျဖစ္ရပါမယ္။ တခါတေလမွာ ကြၽန္ေတာ္တို႔က column ေတြအားလံုးကို data ထည့္မေပးခ်င္တာတို႔၊ အစဥ္လိုက္မျဖစ္တာတို႔ဆိုရင္ query ေရးတဲ႔အခါ table name ရဲ့ ေနာက္မွာ ကိုယ္ထည့္ခ်င္တဲ႔ column name ကိုပါထည့္ေရးေပးရမွာပါ။ ဥပမာ…

INSERT INTO Category (CategoryID, CategoryName)
VALUES (2, ‘Stationary’)

တခါတေလမွာ ကြၽန္ေတာ္တို႔ table တစ္ခုထဲက data ေတြကို တျခား table တစ္ခုထဲကို copy ကူးထည့္ခ်င္တဲ႔ အခါမ်ိဳးမွာ Insert command ကို Select command နဲ႔လည္း တြဲသံုးနိုင္ပါေသးတယ္။ ဒီလိုမ်ိဳးပါ…

INSERT INTO Temp_Category
SELECT * FROM Category

*** ဒီ query run ဖို႔အတြက္ Temp_Category table ေလးကိုအရင္ ေဆာက္ေပး ရမယ္ေနာ္။ Category table ရဲ့ structure အတိုင္းေဆာက္လုိက္ေပါ့ ***

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.