-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bff7104
commit 76bee35
Showing
3 changed files
with
40 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,21 @@ | ||
-- Ensure you've run SalesOrdersStructure.sql | ||
-- and SalesOrdersData.sql in the Sample Databases folder | ||
-- in order to run this example. | ||
-- Create a new schema, to make it easier to cleanup afterwards. | ||
CREATE SCHEMA Item02Example; | ||
|
||
ALTER SESSION SET CURRENT_SCHEMA = SalesOrdersSample; | ||
SET SCHEMA Item02Example; | ||
|
||
SELECT C.CustomerID, C.CustFirstName, C.CustLastName | ||
FROM Customers C | ||
WHERE C.CustomerID IN | ||
(SELECT Orders.CustomerID | ||
FROM Orders INNER JOIN Order_Details | ||
ON Orders.OrderNumber = Order_Details.OrderNumber | ||
INNER JOIN Products | ||
ON Products.ProductNumber = Order_Details.ProductNumber | ||
WHERE Products.ProductName = 'Skateboard') | ||
AND C.CustomerID NOT IN | ||
(SELECT Orders.CustomerID | ||
FROM Orders INNER JOIN Order_Details | ||
ON Orders.OrderNumber = Order_Details.OrderNumber | ||
INNER JOIN Products | ||
ON Products.ProductNumber = Order_Details.ProductNumber | ||
WHERE Products.ProductName | ||
IN ('Helmet', 'Gloves', 'Knee Pads')); | ||
CREATE TABLE Products ( | ||
ProductNumber int NOT NULL PRIMARY KEY, | ||
ProdDescription varchar(255) NOT NULL | ||
); | ||
|
||
-- Sample query that searches products correctly: | ||
SELECT C.CustomerID, C.CustFirstName, C.CustLastName | ||
FROM Customers C | ||
WHERE C.CustomerID IN | ||
(SELECT Orders.CustomerID | ||
FROM Orders INNER JOIN Order_Details | ||
ON Orders.OrderNumber = Order_Details.OrderNumber | ||
INNER JOIN Products | ||
ON Products.ProductNumber = Order_Details.ProductNumber | ||
WHERE Products.ProductName LIKE '%Skateboard%') | ||
AND C.CustomerID NOT IN | ||
(SELECT Orders.CustomerID | ||
FROM Orders INNER JOIN Order_Details | ||
ON Orders.OrderNumber = Order_Details.OrderNumber | ||
INNER JOIN Products | ||
ON Products.ProductNumber = Order_Details.ProductNumber | ||
WHERE Products.ProductName LIKE '%Helmet%' | ||
OR Products.ProductName LIKE '%Gloves%' | ||
OR Products.ProductName LIKE '%Knee Pads%'); | ||
CREATE TABLE ProductAttributes ( | ||
ProductNumber int NOT NULL, | ||
AttributeName varchar(255) NOT NULL, | ||
AttributeValue varchar(255) NOT NULL, | ||
CONSTRAINT PK_ProductAttributes PRIMARY KEY (ProductNumber, AttributeName) | ||
); | ||
|
||
ALTER TABLE ProductAttributes | ||
ADD CONSTRAINT FK_ProductAttributes_ProductNumber | ||
FOREIGN KEY (ProductNumber) | ||
REFERENCES Products (ProductNumber); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
-- Ensure you've run SalesOrdersStructure.sql | ||
-- and SalesOrdersData.sql in the Sample Databases folder | ||
-- in order to run this example. | ||
CREATE DATABASE Item07Example; | ||
GO | ||
|
||
USE SalesOrdersSample; | ||
USE Item07Example; | ||
GO | ||
|
||
-- A constraint already exists between Customers and | ||
-- Orders but has a different name. This will create a | ||
-- duplicate constraint. | ||
ALTER TABLE Orders ADD | ||
CONSTRAINT Orders_FK97 FOREIGN KEY | ||
( | ||
CustomerID | ||
) REFERENCES Customers ( | ||
CustomerID | ||
); | ||
CREATE TABLE Products ( | ||
ProductNumber int NOT NULL PRIMARY KEY, | ||
ProdDescription varchar(255) NOT NULL | ||
); | ||
GO | ||
|
||
CREATE TABLE ProductAttributes ( | ||
ProductNumber int NOT NULL, | ||
AttributeName varchar(255) NOT NULL, | ||
AttributeValue varchar(255) NOT NULL, | ||
CONSTRAINT PK_ProductAttributes PRIMARY KEY (ProductNumber, AttributeName) | ||
); | ||
GO | ||
|
||
ALTER TABLE ProductAttributes | ||
ADD CONSTRAINT FK_ProductAttributes_ProductNumber | ||
FOREIGN KEY (ProductNumber) | ||
REFERENCES Products (ProductNumber); | ||
GO |