Академический Документы
Профессиональный Документы
Культура Документы
1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, and then click
SQL Server Management Studio.
2. In the Connect to Server dialog box, specify the values in the following table, and then click
Connect.
Property Value
Server type Database Engine
Server name MIAMI
Authentication Windows Authentication
5. On the Project menu, click New Query. When prompted, use Windows authentication to
connect to MIAMI.
6. If Solution Explorer is not visible, click Solution Explorer on the View menu.
7. In Solution Explorer, right-click SQLQuery1.sql, and then click Rename. Rename the file
TableAndConstraints.sql.
Creating the JobCandidateHistory table and constraints
You must perform the following steps to create the JobCandidateHistory table and
constraints:
USE [AdventureWorks]
GO
CREATE TABLE [HumanResources].[JobCandidateHistory](
[JobCandidateID] [int] NOT NULL UNIQUE,
[Resume] [xml] NULL,
[Rating] [int] NOT NULL CONSTRAINT [DF_JobCandidateHistory_Rating]
Default (5),
[RejectedDate] [datetime] NOT NULL,
[ContactID] [int] NULL,
CONSTRAINT [FK_JobCandidateHistory_Contact_ContactID]
FOREIGN KEY(ContactID) REFERENCES [Person].[Contact] (ContactID),
CONSTRAINT [CK_JobCandidateHistory_Rating]
CHECK ([Rating]>=0 AND [Rating]<=10)
) ON [PRIMARY]
1. In SQL Server Management Studio, click the File menu, point to Open, and click File.
2. Browse to D:\Labfiles\Starter and open TestConstraints.sql. Connect to MIAMI by using
Windows authentication when prompted.
3. Select the code under the comment This should fail and click the Execute button. The
INSERT operation should fail because the Rating value conflicts with the CHECK
constraint.
4. Select the code under the comment This should succeed and click the Execute button. The
INSERT operation should succeed.
5. Keep SQL Server Management Studio open. You will use it in the next exercise.
Exercise 2: Creating Triggers
1. On the Project menu, click New Query. When prompted, use Windows authentication to
connect to MIAMI.
2. If Solution Explorer is not visible, click Solution Explorer on the View menu.
3. In Solution Explorer, right-click SQLQuery1.sql, and then click Rename. Rename the file
Trigger.sql.
1. In the query window, type the following Transact-SQL code to create the dJobCandidate
trigger on the HumanResources.JobCandidate table.
USE AdventureWorks
GO
CREATE TRIGGER [dJobCandidate] ON [HumanResources].[JobCandidate]
AFTER DELETE AS
BEGIN
SET NOCOUNT ON;
INSERT INTO
[HumanResources].[JobCandidateHistory] (JobCandidateID, Resume,
RejectedDate)
SELECT
JobCandidateID, Resume, getdate()
FROM
deleted
END;
1. In SQL Server Management Studio, open the TestTrigger.sql script file in the
D:\Labfiles\Starter folder. Connect to MIAMI by using Windows authentication when
prompted.
2. Execute the query script, and look in the Results Pane to verify that the record deleted from
the HumanResources.JobCandidate table is inserted into the
HumanResources.JobCandidateHistory table.
3. Keep SQL Server Management Studio open. You will use it in the next exercise.
Exercise 3: Implementing XML Schemas
1. On the Project menu, click New Query. When prompted, use Windows authentication to
connect to MIAMI.
2. In Solution Explorer, right-click SQLQuery1.sql, and then click Rename. Rename the file
XMLSchema.sql.
1. In SQL Server Management Studio, click File, point to Open, and click File. Then browse to
the D:\Labfiles\Starter folder and open HistoricResumeSchema.xml.
2. On the Edit menu, click Select All, and then on the Edit menu, click Copy to copy the XML
schema definition to the Clipboard.
3. In the query window, enter the following code to create the XML schema collection and alter
the JobCandidateHistory table, pasting the schema definition in the AS clause from the
Clipboard.
USE AdventureWorks
GO
CREATE XML SCHEMA COLLECTION
[HumanResources].[HistoricResumeSchemaCollection]
AS
N'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:t="http://schemas.microsoft.com/sqlserver/2004/07/adventure
-works/Resume"
targetNamespace="http://schemas.microsoft.com/sqlserver/2004/07/a
dventure-works/Resume" elementFormDefault="qualified">
<xsd:element name="Address" type="t:AddressType" />
<xsd:element name="Education" type="t:EducationType" />
<xsd:element name="Employment" type="t:EmploymentType" />
<xsd:element name="Location" type="t:LocationType" />
<xsd:element name="Name" type="t:NameType" />
<xsd:element name="Resume" type="t:ResumeType" />
<xsd:element name="Telephone" type="t:TelephoneType" />
<xsd:complexType name="AddressType">
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="Addr.Type" type="xsd:string" />
<xsd:element name="Addr.OrgName" type="xsd:string"
minOccurs="0" />
<xsd:element name="Addr.Street" type="xsd:string"
maxOccurs="unbounded" />
<xsd:element name="Addr.Location">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element ref="t:Location" />
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="Addr.PostalCode" type="xsd:string" />
<xsd:element name="Addr.Telephone" minOccurs="0">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element ref="t:Telephone"
maxOccurs="unbounded" />
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="EducationType">
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="Edu.Level" type="xsd:string" />
<xsd:element name="Edu.StartDate" type="xsd:date" />
<xsd:element name="Edu.EndDate" type="xsd:date" />
<xsd:element name="Edu.Degree" type="xsd:string"
minOccurs="0" />
<xsd:element name="Edu.Major" type="xsd:string" minOccurs="0"
/>
<xsd:element name="Edu.Minor" type="xsd:string" minOccurs="0"
/>
<xsd:element name="Edu.GPA" type="xsd:string" minOccurs="0"
/>
<xsd:element name="Edu.GPAAlternate" type="xsd:decimal"
minOccurs="0" />
<xsd:element name="Edu.GPAScale" type="xsd:decimal"
minOccurs="0" />
<xsd:element name="Edu.School" type="xsd:string"
minOccurs="0" />
<xsd:element name="Edu.Location" minOccurs="0">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element ref="t:Location" />
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="EmploymentType">
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="Emp.StartDate" type="xsd:date"
minOccurs="0" />
<xsd:element name="Emp.EndDate" type="xsd:date" minOccurs="0"
/>
<xsd:element name="Emp.OrgName" type="xsd:string" />
<xsd:element name="Emp.JobTitle" type="xsd:string" />
<xsd:element name="Emp.Responsibility" type="xsd:string" />
<xsd:element name="Emp.FunctionCategory" type="xsd:string"
minOccurs="0" />
<xsd:element name="Emp.IndustryCategory" type="xsd:string"
minOccurs="0" />
<xsd:element name="Emp.Location" minOccurs="0">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element ref="t:Location" />
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="LocationType">
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="Loc.CountryRegion" type="xsd:string" />
<xsd:element name="Loc.State" type="xsd:string" minOccurs="0"
/>
<xsd:element name="Loc.City" type="xsd:string" minOccurs="0"
/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="NameType">
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="Name.Prefix" type="xsd:string"
minOccurs="0" />
<xsd:element name="Name.First" type="xsd:string" />
<xsd:element name="Name.Middle" type="xsd:string"
minOccurs="0" />
<xsd:element name="Name.Last" type="xsd:string" />
<xsd:element name="Name.Suffix" type="xsd:string"
minOccurs="0" />
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="ResumeType">
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element ref="t:Name" />
<xsd:element name="Skills" type="xsd:string" minOccurs="0" />
<xsd:element ref="t:Employment" maxOccurs="unbounded" />
<xsd:element ref="t:Education" maxOccurs="unbounded" />
<xsd:element ref="t:Address" maxOccurs="unbounded" />
<xsd:element ref="t:Telephone" minOccurs="0" />
<xsd:element name="EMail" type="xsd:string" minOccurs="0" />
<xsd:element name="WebSite" type="xsd:string" minOccurs="0"
/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="TelephoneType">
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="Tel.Type" type="xsd:anyType" minOccurs="0"
/>
<xsd:element name="Tel.IntlCode" type="xsd:int" minOccurs="0"
/>
<xsd:element name="Tel.AreaCode" type="xsd:int" minOccurs="0"
/>
<xsd:element name="Tel.Number" type="xsd:string" />
<xsd:element name="Tel.Extension" type="xsd:int"
minOccurs="0" />
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>'
GO
1. In SQL Server Management Studio, open the TestXMLSchema.sql script file in the
D:\Labfiles\Starter folder. Connect to MIAMI by using Windows authentication when
prompted.
2. Select the code under the comment This should fail and click the Execute button. The
INSERT operation should fail because the Resume XML does not conform to the schema.
3. Select the code under the comment This should succeed and click the Execute button. The
INSERT operation should succeed.
4. Close SQL Server Management Studio.