Professional Documents
Culture Documents
Hoc SQL Server 2000 PDF
Hoc SQL Server 2000 PDF
MC LC
http://ebooks.vdcmedia.com
http://ebooks.vdcmedia.com
http://ebooks.vdcmedia.com
http://ebooks.vdcmedia.com
http://ebooks.vdcmedia.com
Transact SQL
Gii Thiu S Lc V Transact SQL (T-SQL)
Transact-SQL l ngn ng SQL m rng da trn SQL chun ca ISO
(International Organization for Standardization) v ANSI (American National
Standards Institute) c s dng trong SQL Server khc vi P-SQL (ProceduralSQL) dng trong Oracle.
Trong bi ny chng ta s tm hiu s qua v T-SQL. Chng c chia lm 3
nhm:
Data Definition Language (DDL):
y l nhng lnh dng qun l cc thuc tnh ca mt database nh nh
ngha cc hng hoc ct ca mt table, hay v tr data file ca mt
database...thng c dng
Create object_Name
Alter object_Name
Drop object_Name
table
tn
Importers
vi
ct
USE Northwind
CREATE TABLE Importers(
CompanyID int NOT NULL,
CompanyName varchar(40) NOT NULL,
Contact varchar(40) NOT NULL
)
http://ebooks.vdcmedia.com
Grant
Revoke
Deny
V d:
Lnh sau s cho php user trong Public Role c quyn Select i vi table
Customer trong database Northwind (Role l mt khi nim ging nh Windows
Group s c bn k trong phn Security)
USE Northwind
GRANT SELECT
ON Customers
TO PUBLIC
http://ebooks.vdcmedia.com
Insert
USE Northwind
INSERT INTO Territories
VALUES (98101, 'Seattle', 2)
Update
USE Northwind
UPDATE Territories
SET TerritoryDescription = 'Downtown Seattle'
WHERE TerritoryID = 98101
Delete
USE Northwind
DELETE FROM Territories
WHERE TerritoryID = 98101
http://ebooks.vdcmedia.com
10
C Php Ca T-SQL:
Phn ny chng ta s bn v cc thnh phn to nn c php ca T-SQL
Identifiers
y chnh l tn ca cc database object. N dng xc nh mt object. (Ch
khi ni n Object trong SQL Server l chng ta mun cp n table, view,
stored procedure, index.....V hu nh mi th trong SQL Server u c thit
k theo kiu hng i tng (object-oriented)). Trong v d sau TableX,
KeyCol, Description l nhng identifiers
CREATE TABLE TableX
(KeyCol INT PRIMARY KEY, Description NVARCHAR(80))
http://ebooks.vdcmedia.com
11
V d:
SELECT * FROM [My Table]
WHERE [Order] = 10
Functions (Hm)
C 2 loi hm mt loi l built-in v mt loi user-defined
Cc hm Built-In c chia lm 3 nhm:
http://ebooks.vdcmedia.com
12
http://ebooks.vdcmedia.com
13
http://ebooks.vdcmedia.com
14
Data trong SQL Server c cha thnh tng Page 8KB v 8 page lin tc to
thnh mt Extent nh hnh v di y:
Trc khi SQL Server mun lu data vo mt table n cn phi dnh ring mt
khong trng trong data file cho table . Nhng khong trng chnh l cc
extents. C 2 loi Extents: Mixed Extents (loi hn hp) dng cha data
ca nhiu tables trong cng mt Extent v Uniform Extent (loi thun nht)
dng cha data ca mt table. u tin SQL Server dnh cc Page trong
Mixed Extent cha data cho mt table sau khi data tng trng th SQL
dnh hn mt Uniform Extent cho table .
http://ebooks.vdcmedia.com
15
http://ebooks.vdcmedia.com
16
http://ebooks.vdcmedia.com
17
ng dng
Sp_help ['object']
Sp_helpdb ['database']
Sp_monitor
Sp_spaceused ['object',
'updateusage' ]
Sp_who ['login']
V d:
sp_helpdb 'Northwind' s cho kt qu c dng nh bng di y
name
db_size
owner
dbid
created
status .....
------------------------------------------------------------------------------------------------------------------------------- ------Northwind
3.94 MB
sa
6
Aug 6 2000
Status=ONLINE,
Updateability=READ_WRITE, .....
stored procedure sp_spaceused nh v d sau
USE Northwind
Go
sp_spaceused 'Customers'
To Mt User Database
Chng ta c th to mt database d dng dng SQL Server Enterprise bng
cch right-click ln trn "database" v chn "New Database" nh hnh v sau:
http://ebooks.vdcmedia.com
18
http://ebooks.vdcmedia.com
19
http://ebooks.vdcmedia.com
20
Publisher
PubState
PubCity
PubCountry
CA
Berkely
USA
MA
Boston
USA
CA
Berkely
USA
New Riders
Title
PublisherID
P1
P2
P3
http://ebooks.vdcmedia.com
21
v
Publishers
PublisherID Publisher
PubState
PubCity
PubCountry
P1
Microsoft Press
CA
Berkely
USA
P2
New Riders
MA
Boston
USA
P3
Wrox
CA
Berkely
USA
Title
Authors
John Brown
hay
Books
BookID Title
Author1
Author2
Author3
Null
Null
Matthew
Bortniker
Rick Johnson
Null
Peter Wright
James Moon
John Brown
http://ebooks.vdcmedia.com
22
Books
BookID
Title
Authors
AuthID
First Name
Last Name
A1
John
Brown
A2
Matthew
Bortniker
A3
Rick
Johnson
A4
Peter
Wright
A5
James
Moon
AuthorBook
BookID
AuthID
A1
A2
A3
A4
A5
A1
http://ebooks.vdcmedia.com
23
http://ebooks.vdcmedia.com
24
a cha data file hay Transaction Log File hay system file b mt
Server b h hng
Cc Loi Backup
c th hiu cc kiu phc hi d liu khc nhau bn phi bit qua cc loi
backup trong SQL Server
http://ebooks.vdcmedia.com
25
Recovery Models
http://ebooks.vdcmedia.com
26
27
http://ebooks.vdcmedia.com
28
Backup Database
Trong phn ny chng ta s bn v cch backup database. Nhng trc ht
chng ta hy lm quen vi mt s thut ng dng trong qu trnh backup v
restore. C nhng t ta s nguyn ting Anh m khng dch.
Thut Ng
Gii Thch
Backup
Backup Device
Backup File
Backup Media
Disk hay tape c s dng cha mt backup set. Backup media c th cha
nhiu backup sets (v d nh t nhiu SQL Server 2000 backups v t nhiu
Windows 2000 backups).
Backup Set
http://ebooks.vdcmedia.com
29
http://ebooks.vdcmedia.com
30
Restore Database
Trc khi restore database ta phi xc nh c th t file cn restore. Cc
thng tin ny c SQL Server cha trong msdb database v s cho ta bit
backup device no, ai backup vo thi im no. Sau ta tin hnh restore.
restore bn Right-click->All Tasks->Restore database... s thy window
nh hnh v sau:
http://ebooks.vdcmedia.com
31
http://ebooks.vdcmedia.com
32
http://ebooks.vdcmedia.com
33
Constraints
Constraints l nhng thuc tnh (property) m ta p t ln mt table hay mt
ct trnh vic lu d liu khng chnh xc vo database (invalid data). Tht
ra NOT NULL hay DEFAULT cng c xem l mt dng constraint nhng chng
ta khng bao gm hai loi ny y m ch trnh by 4 loi constraints l
Primary Key Constraint, Unique Constraint, Foreign Key Constraint v Check
Constraint.
Primary Key Constraint:
Mt table thng c mt hay nhiu ct c gi tr mang tnh duy nht xc nh
mt hng bt k trong table. Ta thng gi l Primary Key v c to ra khi ta
Create hay Alter mt table vi Primary Key Constraint.
Mt table ch c th c mt Primary Key constraint. C th c nhiu ct
tham gia vo vic to nn mt Primary Key, cc ct ny khng th cha Null v
gi tr trong cc ct thnh vin c th trng nhau nhng gi tr ca tt c cc ct
to nn Primary Key phi mang tnh duy nht.
Khi mt Primary Key c to ra mt Unique Index s c t ng to ra
duy tr tnh duy nht. Nu trong table cha c Clustered Index th mt Unique
+ Clustered Index s c to ra.
C th to ra Primary Key Constraints nh sau:
CREATE TABLE Table1
(Col1 INT PRIMARY KEY,
Col2 VARCHAR(30)
)
hay
CREATE TABLE Table1
(Col1 INT,
Col2 VARCHAR(30),
CONSTRAINT table_pk PRIMARY KEY (Col1)
)
Unique Constraint
Bn c th to Unique Constraint m bo gi tr ca mt ct no khng
b trng lp. Tuy Unique Constraint v Primary Key Constraint u m bo tnh
duy nht nhng bn nn dng Unique Constraint trong nhng trng hp sau:
http://ebooks.vdcmedia.com
34
hay
CREATE TABLE Table1
(Col1 INT PRIMARY KEY,
Col2 INT,
CONSTRAINT col2_fk FOREIGN KEY (Col2)
REFERENCES Employees (EmployeeID)
)
i khi chng ta cng cn Disable Foreign Key Constraint trong trng hp:
http://ebooks.vdcmedia.com
35
Check Constraint
Check Constraint dng gii hn hay kim sot gi tr c php insert vo
mt ct. Check Constraint ging Foreign Key Constraint ch n kim sot gi
tr a vo mt ct nhng khc ch Foreign Key Constraint da trn gi tr
table cha cho php mt gi tr c chp nhn hay khng trong khi Check
Constraint da trn mt biu thc logic (logic expression) kim tra xem mt
gi tr c hp l khng. V d ta c th p t mt Check Constraint ln ct
salary ch chp nhn tin lng t $15000 n $100000/nm.
Ta c th to ra nhiu Check Constraint trn mt ct. Ngoi ra ta c th to mt
Check Constraint trn nhiu ct bng cch to ra Check Constraint mc table
(table level).
C th to ra Check Constraint nh sau:
CREATE TABLE Table1
(Col1 INT PRIMARY KEY,
Col2 INT
CONSTRAINT limit_amount CHECK (Col2 BETWEEN 0 AND
1000),
Col3 VARCHAR(30)
)
http://ebooks.vdcmedia.com
36
http://ebooks.vdcmedia.com
37
USE pubs
SELECT p.pub_id, p.pub_name AS PubName
FROM publishers AS p
http://ebooks.vdcmedia.com
38
Mt cch khc cng select data t mt hay nhiu table v insert vo mt table
khc l dng "Insert Into...Select...". Nhng cu lnh ny khng to ra mt
table mi. Ngha l ta table phi tn ti trc. V d:
INSERT INTO EmployeeNames
SELECT FirstName, LastName
FROM Employers
Trong v d trn u tin ta select nhng order ring bit (distinct) t Order
Details table vi gi > 100. Sau tip tc select OrderID, "Units Sold", Revenue
t kt qu trn trong "Units Sold" v Revenue l nhng aggregate columns
(cho gi tr tng mt ct ca nhng hng c cng OrderID). HAVING Clause
ng vai tr nh mt filter dng lc li cc gi tr cn select m thi. HAVING
Clause thng i chung vi GROUP BY mc d c th xut hin ring l.
UNION
Uninon keyword c nhim v ghp ni kt qu ca 2 hay nhiu queries li thnh
mt kt qu.
V d:
Gi s c table1(ColumnA varchar(10), ColumnB int) v table2(ColumnC
varchar(10), ColumnD int). Ta mun select data t table1 v ghp vi data t
table2 to thnh mt kt qu duy nht ta lm nh sau:
SELECT * FROM Table1
UNION ALL
SELECT * FROM Table2
http://ebooks.vdcmedia.com
39
Using JOINS
Trong phn ny chng ta s tm hiu v cc loi Join trong SQL Server. Bng
cch s dng JOIN bn c th select data t nhiu table da trn mi quan h
logic gia cc table (logical relationships). C th tm tt cc loi Join thng
dng bng cc hnh sau:
Th t t tri sang phi: Inner Join, Left Outer Join, Right Outer Join, Full Outer
Join
Inner Joins
Dng Inner Join select data t 2 hay nhiu tables trong gi tr ca cc ct
c join phi xut hin c 2 tables tc l phn gch cho trn hnh. V d:
SELECT t.Title, p.Pub_name
FROM Publishers AS p INNER JOIN Titles AS t
ON p.Pub_id = t.Pub_id
ORDER BY Title ASC
http://ebooks.vdcmedia.com
40
Cross Joins
Dng Cross Join ghp data t hai table trong s hng thu c bng vi s
hng ca table th nht nhn vi s hng ca table th hai. V d:
USE pubs
SELECT au_fname, au_lname, pub_name
FROM authors CROSS JOIN publishers
WHERE authors.city = publishers.city
ORDER BY au_lname DESC
http://ebooks.vdcmedia.com
41
Trong v d trn ta mun bulk copy table Orders ra mt text file trong :
http://ebooks.vdcmedia.com
42
out: copy data t table hay view ra mt data file (c:\Orders.txt). Ngc li ta c
th dng switch in import data t text file vo SQL Server.
-c: bulk copy dng kiu d liu Character (Char) (nu khng ch r th SQL
Server s dng "TAB" character (\t) phn nh cc ct v dng new line
character (\n) phn nh cc hng nh cc gi tr default).
-t;: du ";" i sau switch "t" cho bit ta mun dng ";" phn nh cc ct
(nu khng s dng gi tr mc nh nh trn)
-T: dng (NT) Trust connection kt ni vi database. Ngha l nu user
authenticated (cho php) vo c Windows system th ng nhin c s
dng SQL Server m khng cn dng thm username v password no khc.
V d 2: Thay v copy ton b table ta c th dng query select mt phn
data v export ra text file nh sau:
bcp "Select * From practiceDB..Orders" queryout c:\Orders.txt -c
-SVinhtai -Usa -Pabc
Trong v d ny ta select ton b data trong Orders table ra mt text file dng
query v SQL Server authentication.
queryout : cho bit y l mt query ch khng phi l table.
-S : tn ca SQL Server (hay tn ca mt Instance)
-U : SQL user name dng log on
-P : password dng log on.
V d 3 : dng BULK INSERT bulk copy data t text file vo SQL Server
database. M Query Analyser (BULK INSERT l mt T-SQL command ch khng
phi l mt command prompt utility) v nh vo cc dng sau :
BULK INSERT PracticeDB..Orders FROM 'c:\Orders.txt ' WITH
(DATAFILETYPE = 'CHAR')
Trong v d trn DATAFILETYPE= 'CHAR' cho bit data c cha dng Char
data type. N mun dng data type dng unicode th dng 'WIDECHAR'
Ch : Cc switch trong bcp command l case-sensitive. Ngha l ch hoa v
ch thng s c ngha khc nhau.
http://ebooks.vdcmedia.com
43
Distributed Queries
i khi chng ta mun select data t nhng database system khc nh MS
Access, Oracle, DB2... hay thm ch t mt SQL Server khc ta cn phi dng
distributed query. SQL Server s dng k thut OLEDB v cc API chuyn cc
query ny ti cc database system khc. C 2 cch truy cp vo cc database
system khc l dng LINKED SERVER v Ad Hoc Computer Name.
Linked Server:
Linked Server l mt server o c dng truy cp vo cc database system
khc. Mt khi setup th ta c th query data dng four-part name :
linked_server_name.catalog.schema.object_name . Trong catalog thng
tng ng vi database name, Schema tng ng vi database owner v
object_name tng ng vi table hay view.
V d: Gi s ta setup mt Linked Server vo Access database "PracticeDB.mdb"
trong cc table u tng t nh PracticeDB database trong SQL Server
(c to ra trong phn bi tp s 1).
M Enterprise Manager -> Chn node Security ca local server ->
Right-Click ln node Linked Server chn New Linked Server. Sau nhp
vo tn ca Linked Server LinkedPracticeDB, trong phn Provider Name chn
Microsoft Jet 4.0 OLEDB Provider. Trong phn Data Source nhp vo v tr ca
Access database (C:\PracticeDB.mdb) v click OK.
Ta s c Linked Server tn LinkedPracticeDB xut hin di phn Security/Linked
Server. Gi s ta mun select data t Linked Server ny ta c th dng Query
Analyser nh sau:
Select * from LinkedPracticeDB...Customers
http://ebooks.vdcmedia.com
44
Cursors
Nu gii thch mt cch ngn gn th cursor tng t nh recordset hay dataset
trong programming. Ngha l ta select mt s data vo memory sau c th
ln lt lm vic vi tng record bng cch Move Next...
http://ebooks.vdcmedia.com
45
C 3 loi cursors l Transact- SQL Cursors, API Cursors v Client Cursors. Trong
Transact-SQL v API thuc loi Server Cursors ngha l cursors c load ln
v lm vic bn pha server. Trong khun kh bi hc ny ta ch nghin cu
Transact-SQL cursors.
Transact-SQL cursors c to ra trn server bng cc cu lnh Transact-SQL v
ch yu c dng trong stored procedures v triggers. Trc ht hy xem qua
mt v d v cursor:
DECLARE @au_lname varchar(40), @au_fname varchar(20)
DECLARE Employee_Cursor CURSOR FOR
SELECT LastName, FirstName FROM Northwind.dbo.Employees
OPEN Employee_Cursor
FETCH NEXT FROM Employee_Cursor INTO @au_lname, @au_fname
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'Author:' + @au_fname + ' ' + @au_lname
FETCH NEXT FROM Employee_Cursor INTO @au_lname,
@au_fname
END
CLOSE Employee_Cursor
DEALLOCATE Employee_Cursor
http://ebooks.vdcmedia.com
46
Stored Procedures
Trong nhng bi hc trc y khi dng Query Analyzer chng ta c th t tn
v save cc nhm cu lnh SQL vo mt file di dng script c th s dng
tr li sau ny. Tuy nhin thay v save vo text file ta c th save vo trong SQL
Server di dng Stored Procedure. Stored Procedure l mt nhm cu
lnh Transact-SQL c compiled (bin dch) v cha trong SQL
Server di mt tn no v c x l nh mt n v (ch khng
phi nhiu cu SQL ring l).
u im Ca Stored Procedure
Stored Procedure c mt s u im chnh nh sau:
http://ebooks.vdcmedia.com
47
http://ebooks.vdcmedia.com
48
http://ebooks.vdcmedia.com
49
50
khng lm g c- all or nothing). Trc ht ta kim tra xem ngi khch hng l
ngi mi hay c. Nu l ngi mi th ta "tin tay" insert vo Customers table
lun cn nu khng th ch insert vo Orders table m thi. Nu l ngi
customer mi ta ly CustomerID ln nht t Customers table b vo bin
@CustomerID v sau tng ln mt n v dng cho vic Insert Customer
dng k tip.
Sau khi insert ngi customer mi ta tip tc cng vic bng vic kim tra xem
Order mun insert c tn ti cha (v nu order tn ti th khi insert SQL
Server s bo li do OrderID l Primary key). Nu nh order trn v l do no
c trong DB th ta roll back v tr kt qu =1 cn nu khng th ta insert mt
order mi vo v commit transaction vi kt qu tr v =0.
Tng t nh vy nu ngi customer tn ti (sau ch else u tin) th ta
ch vic insert order ging nh trn. Trong mi trng hp k trn ta u in ra
kt qu v return.
V d trn y ch mang tnh hc hi cn trn thc t database c th phc tp
hn nhiu nn vic vit stored procedure i hi kin thc vng chc v SQL v
k nng v programming.
Mun hiu r hn v bi hc ny bn cn lm bi tp s 2.
Tm li trong bi ny chng ta tm hiu mt s k thut import v export
data . ng thi bit qua cc cch select data t cc database system khc dng
distributed query. Nhng quan trng nht v thng dng nht l cc stored
procedures. Bn cn hiu r vai tr ca stored procedure v bit cch to ra
chng.
V kin thc v database ni chung v SQL Server ni ring kh rng nn trong
khun kh mt bi hc chng ti khng th trnh by cn k tng chi tit v i
khi c hi dn p cho nn bn cn c i c li nhiu ln nm c chnh
v phi xem thm sch (nu khng c sch th phi xem thm SQL Books
Online). Sau bi hc ny cc bn cn lm bi tp s 2 h thng ha li kin
thc hc. Khi lm bi tp nn lm theo th t v tun th theo cc yu cu
ca bi tp t ra. Khng nn b qua bc no.
http://ebooks.vdcmedia.com
51
Triggers
Trigger l mt loi stored procedure c bit c execute (thc thi) mt cch
t ng khi c mt data modification event xy ra nh Update, Insert hay
Delete. Trigger c dng m bo Data Integrity hay thc hin cc business
rules no .
http://ebooks.vdcmedia.com
52
c im ca Trigger:
http://ebooks.vdcmedia.com
53
To Mt Trigger Nh Th No?
C php cn bn to ra mt trigger c dng nh sau:
CREATE TRIGGER trigger_name
ON table_name or view_name
FOR trigger_class and trigger_type(s)
AS Transact-SQL statements
Nh vy khi to ra mt trigger ta phi ch r l to ra trigger trn table no v
c trigger khi no (insert, update hay delete. Sau ch AS l cc cu lnh SQL
x l cng vic.
Ta hy nghin cu mt ng dng thc tin sau. Gi s ta vit mt application
cho php user c th Insert, Update v Delete nhng thng tin nm trong
database. User ny thng l nhng ngi khng thng tho lm v computer
m chng ti thng gi a l "b tm". Vo mt ngy p tri, "b tm" mt
my ti xanh n cu cu ta v l tay "delete" nhng thng tin kh quan
trng v hy vng ta c th phc hi d liu dm. Nu chng ta khng phng xa
trc khi vit application th coi nh cng v phng cu cha v data hon
ton b delete.
Nhng nu bn l mt "guru" bn s gt g "chuyn ny kh lm!" nhng sau
bn ch tn vi pht ng h rollback. Mun lm c chuyn ny chng
ta phi dng mt "chiu" gi l Audit (kim tra hay gim st). Tc l ngoi cc
table chnh ta s thm cc table ph gi l Audit tables. Bt k hot ng no
ng chm vo mt s table quan trng trong database ta u ghi nhn vo
trong Audit table. V d khi user update hay delete mt record trong table no
th trc khi update hay delete ta s m thm di chuyn record sang Audit
table ri mi update hay delete table chnh. Nh vy nu c chuyn g xy ra ta
c th d dng rollback (tr record v ch c).
http://ebooks.vdcmedia.com
54
V d:
Ta c table Orders trong PracticeDB. audit cc hot ng din ra trn table
ny ta to ra mt audit table vi tn Aud_Orders vi cc column ging y ht
vi Orders table. Ngoi ra ta thm vo 2 columns :
(Nu trong mi trng nhiu user th ta thm mt column UserID ghi nhn
user no thay i).
Sau ta s to ra 3 trigger dng cho vic audit nh sau:
--Insert Trigger
CREATE TRIGGER [AuditInsertOrders]
ON [dbo].[Orders]
FOR Insert
AS
insert into aud_orders select *,'I',getdate() From inserted
--Update Trigger
CREATE TRIGGER [AuditUpdateOrders]
ON [dbo].[Orders]
for UPDATE
AS
insert into aud_orders select *,'U',Getdate() from deleted
--Delete Trigger
CREATE TRIGGER [AuditDeleteOrders]
ON [dbo].[Orders]
FOR DELETE
AS
insert into aud_orders select *,'D',getdate() From deleted
http://ebooks.vdcmedia.com
55
Views
nh ngha mt cch n gin th view trong SQL Server tng t nh Query
trong Access database. View c th c xem nh mt table o m data ca n
c select t mt stored query. i vi programmer th view khng khc chi so
vi table v c th t v tr ca table trong cc cu lnh SQL. c im ca
View l ta c th join data t nhiu table v tr v mt recordset n. Ngoi ra
ta c th "xo nu" data (manipulate data) trc khi tr v cho user bng cch
dng mt s logic checking nh (if, case...).
V d:
Create View OrderReport
As
Select OrderID,
(case when [Name] is null then 'New Customer'
else [Name]
end )As CustomerName,
ProductName,
DateProcessed
From Customers Right Outer Join Orders on
Customers.CustomerID=Orders.CustomerID
http://ebooks.vdcmedia.com
56
http://ebooks.vdcmedia.com
57
Exercise 1: Advanced
Query
DateProcessed
(datetime)
2002-12-01
2000-03-02
2003-08-03
2001-05-12
2002-10-04
2002-03-08
58
7
8
Milk
2002-05-02
3. Query data using Select statement with the following Join (use QA)
a. Inner Join
b. Left Outter Join
c. Right Outer Join
d. Full Outer Join
e. Cross Join
The result of the query must be displayed in the following format
OrderID CustomerName ProductName DateProcessed
( Hints: Based on the data supplied and your knowledge about various kind of joins
guess the results then after each select compare it with your guessing to check if what
you understand is correct. You also use As keyword or alias in the queries)
4. Using Select * Into...From statement to create a new table called Processed
Orders and populate the new table with the data selecting from Orders table
Where DateProcessed is earlier than 2002-10-05. (use QA)
5. Using Insert Into...Select statement to get the top 1 record from Orders table
and insert into the ProcessedOrders (use QA)
6. Delete a record from ProcessedOrders where the date processed is 2002-10-04.
(use QA)
7. Using Union to merge the two data set from Orders and ProcessedOrders into one
data set. (use QA)
8. Apply Constraints (use EP or QA)
a.Apply the Primary Constraint to the ID column in the tables
b. Apply the Foreign Key Constraint in the Orders table.
c. Apply the Check Constraint to the DateProcessed column so that the date is
within 1970-01-01 2005-01-01 and try to insert invalid data to see if SQL
rejects.
d. Apply Unique Constraint to the CustomerName column of Customers
9. Back up database and Restore to somewhere else (on a different server or on the
same server but with different database name) (use EP or QA)
10. Truncate and Drop the ProcessedOrders table (use QA)
http://ebooks.vdcmedia.com
59
http://ebooks.vdcmedia.com
60
61
http://ebooks.vdcmedia.com