Skip to content

Commit

Permalink
Sample files
Browse files Browse the repository at this point in the history
  • Loading branch information
TexanInParis committed Nov 5, 2016
1 parent bff7104 commit 76bee35
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 54 deletions.
58 changes: 18 additions & 40 deletions IBM DB2/Chapter 01/Listing 1.012.sql
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);
1 change: 1 addition & 0 deletions Master Database List.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ EntertainmentAgencySample
Item02Example
Item03Example
Item04Example
Item07Example *
Item11Example *
Item13Example *
Item14Example *
Expand Down
35 changes: 21 additions & 14 deletions Microsoft SQL Server/Chapter 01/Listing 1.012.sql
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

0 comments on commit 76bee35

Please sign in to comment.