Skip to content

Commit

Permalink
Sample files
Browse files Browse the repository at this point in the history
  • Loading branch information
TexanInParis committed Sep 6, 2016
1 parent f5a335f commit d3e28e9
Show file tree
Hide file tree
Showing 201 changed files with 1,128 additions and 877 deletions.
19 changes: 10 additions & 9 deletions IBM DB2/Chapter 01/Listing 1.004.sql
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
-- Ensure you've run Item04StructureAndData.sql in the
-- Sample Databases folder in order to run this example.
-- Sample Databases folder and Listing 1.003.sql in order
-- to run this example.

SET SCHEMA Item04Example;

SELECT AuthorID AS AuthID, CONCAT(AuthFirst,
SELECT AuthorID AS AuthID, AuthFirst ||
CASE
WHEN AuthMid IS NULL
THEN ' '
ELSE CONCAT(' ', CONCAT(AuthMid, ' '))
END, AuthLast) AS AuthName,
CONCAT(AuthStNum, CONCAT(' ', CONCAT(AuthStreet,
CONCAT(' ', CONCAT(AuthCity,
CONCAT(', ', CONCAT(AuthStProv,
CONCAT(' ', CONCAT(AuthPostal,
CONCAT(', ', AuthCountry))))))))))
ELSE ' ' || AuthMid || ' '
END || AuthLast AS AuthName,
AuthStNum || ' ' || AuthStreet
|| ' ' || AuthCity
|| ', ' || AuthStProv
|| ' ' || AuthPostal
|| ', ' || AuthCountry
AS AuthAddress
FROM Authors;

Expand Down
14 changes: 7 additions & 7 deletions IBM DB2/Chapter 01/Listing 1.009.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ SET SCHEMA SalesOrdersSample;
-- Included as comments for reference.
-- CREATE TABLE Customers (
-- CustomerID int NOT NULL PRIMARY KEY ,
-- CustFirstName nvarchar (25) NULL ,
-- CustLastName nvarchar (25) NULL ,
-- CustStreetAddress nvarchar (50) NULL ,
-- CustCity nvarchar (30) NULL ,
-- CustState nvarchar (2) NULL ,
-- CustZipCode nvarchar (10) NULL ,
-- CustFirstName varchar (25) NULL ,
-- CustLastName varchar (25) NULL ,
-- CustStreetAddress varchar (50) NULL ,
-- CustCity varchar (30) NULL ,
-- CustState varchar (2) NULL ,
-- CustZipCode varchar (10) NULL ,
-- CustAreaCode smallint NULL DEFAULT 0 ,
-- CustPhoneNumber nvarchar (8) NULL );
-- CustPhoneNumber varchar (8) NULL );
2 changes: 1 addition & 1 deletion IBM DB2/Chapter 02/Listing 2.008.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SET SCHEMA Item11Example;
-- Listing 2.8 Table Creation SQL

CREATE TABLE Customers (
CustomerID int PRIMARY KEY NOT NULL ,
CustomerID int NOT NULL PRIMARY KEY ,
CustFirstName varchar (25) NULL ,
CustLastName varchar (25) NULL ,
CustStreetAddress varchar (50) NULL ,
Expand Down
4 changes: 2 additions & 2 deletions IBM DB2/Chapter 02/Listing 2.010.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ SET SCHEMA Item11Example;
-- If you've run Listing 2.009.sql, you'll need to drop the index created
-- in that listing, since we re-used the index name.
-- If you have not run Listing 2.009.sql, comment out the DROP INDEX statement.
DROP INDEX CustName ON Customers;
DROP INDEX CustName;

CREATE INDEX CustName ON Customers(CustFirstName, CustLastName);

-- Run the following if you do not want to keep the index.
-- DROP INDEX CustName ON Customers;
-- DROP INDEX CustName;
44 changes: 23 additions & 21 deletions IBM DB2/Chapter 03/Listing 3.006.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ SET SCHEMA Item20DB2Example;
CREATE TABLE Sales (
SalesId int PRIMARY KEY NOT NULL,
RetailerId int NULL,
Sales int NULL,
Cost float NULL,
Quantity int NULL,
GrossProfit float NULL,
Sales int NOT NULL,
Cost float NOT NULL,
Quantity int NOT NULL,
GrossProfit float NOT NULL,
ProductId int NULL,
OrderDay date NULL
);
Expand Down Expand Up @@ -57,32 +57,34 @@ CREATE TABLE ProductType (
);

CREATE SUMMARY TABLE SalesSummary AS (
SELECT SUM(T1.Sales) AS Sales,
SUM(T1.Cost * T1.Quantity) AS Cost,
SUM(T1.Quantity) AS Quantity,
SUM(T1.GrossProfit) AS GrossProfit,
SELECT
T5.RegionName AS RegionName,
T5.CountryCode AS CountryCode,
T6.ProductTypeCode AS ProductTypeCode,
T4.CurrentYear AS CurrentYear,
T4.CurrentQuarter AS CurrentQuarter,
T4.CurrentMonth AS CurrentMonth
FROM Sales AS T1
INNER JOIN Retailer AS T2
ON T1.RetailerId = T2.RetailerId
INNER JOIN Product AS T3
ON T1.ProductId = T3.ProductId
INNER JOIN datTime AS T4
ON T1.OrderDay = T4.DayKey
INNER JOIN Region AS T5
ON T2.RetailerCountryCode = T5.CountryCode
INNER JOIN ProductType AS T6
ON T3.ProductTypeId = T6.ProductTypeId
T4.CurrentMonth AS CurrentMonth,
COUNT(*) AS RowCount,
SUM(T1.Sales) AS Sales,
SUM(T1.Cost * T1.Quantity) AS Cost,
SUM(T1.Quantity) AS Quantity,
SUM(T1.GrossProfit) AS GrossProfit
FROM Sales AS T1,
Retailer AS T2,
Product AS T3,
datTime AS T4,
Region AS T5,
ProductType AS T6
WHERE T1.RetailerId = T2.RetailerId
AND T1.ProductId = T3.ProductId
AND T1.OrderDay = T4.DayKey
AND T2.RetailerCountryCode = T5.CountryCode
AND T3.ProductTypeId = T6.ProductTypeId
GROUP BY T5.RegionName, T5.CountryCode, T6.ProductTypeCode,
T4.CurrentYear, T4.CurrentQuarter, T4.CurrentMonth
)
DATA INITIALLY DEFERRED
REFRESH DEFERRED
REFRESH IMMEDIATE
ENABLE QUERY OPTIMIZATION
MAINTAINED BY SYSTEM
NOT LOGGED INITIALLY;
Expand Down
19 changes: 19 additions & 0 deletions IBM DB2/Chapter 04/Listing 4.017.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,23 @@ INNER JOIN Order_Details
INNER JOIN Products
ON Products.ProductNumber = Order_Details.ProductNumber;

DROP VIEW CustomerProducts;

-- Sample query that searches products correctly:
CREATE VIEW CustomerProducts AS
SELECT DISTINCT Customers.CustomerID, Customers.CustFirstName,
Customers.CustLastName,
CASE WHEN Products.ProductName LIKE '%Skateboard%' THEN 'Skateboard'
WHEN Products.ProductName LIKE '%Helmet%' THEN 'Helmet'
WHEN Products.ProductName LIKE '%Knee Pads%' THEN 'Knee Pads'
WHEN Products.ProductName LIKE '%Gloves%' THEN 'Gloves'
ELSE NULL
END AS ProductCategory
FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
INNER JOIN Order_Details
ON Orders.OrderNumber = Order_Details.OrderNumber
INNER JOIN Products
ON Products.ProductNumber = Order_Details.ProductNumber;

DROP VIEW CustomerProducts;
14 changes: 10 additions & 4 deletions IBM DB2/Chapter 04/Listing 4.018.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ DROP VIEW ProdsOfInterest;

-- Sample query that searches products correctly:
CREATE VIEW ProdsOfInterest AS
SELECT Products.ProductName
SELECT DISTINCT
CASE WHEN Products.ProductName LIKE '%Skateboard%' THEN 'Skateboard'
WHEN Products.ProductName LIKE '%Helmet%' THEN 'Helmet'
WHEN Products.ProductName LIKE '%Knee Pads%' THEN 'Knee Pads'
WHEN Products.ProductName LIKE '%Gloves%' THEN 'Gloves'
ELSE NULL
END AS ProductCategory
FROM Products
WHERE ProductName LIKE '%Skateboard%'
OR ProductName LIKE '%Helmet%'
OR ProductName LIKE '%Knee Pads%'
OR ProductName LIKE '%Helmet%'
OR ProductName LIKE '%Knee Pads%'
OR ProductName LIKE '%Gloves%';

DROP VIEW ProdsOfInterest;

22 changes: 17 additions & 5 deletions IBM DB2/Chapter 04/Listing 4.019.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ DROP VIEW CustomerProducts;

-- Sample query that searches products correctly:
CREATE VIEW CustomerProducts AS
SELECT DISTINCT Customers.CustomerID, Customers.CustFirstName,
Customers.CustLastName, Products.ProductName
SELECT DISTINCT Customers.CustomerID, Customers.CustFirstName,
Customers.CustLastName,
CASE WHEN Products.ProductName LIKE '%Skateboard%' THEN 'Skateboard'
WHEN Products.ProductName LIKE '%Helmet%' THEN 'Helmet'
WHEN Products.ProductName LIKE '%Knee Pads%' THEN 'Knee Pads'
WHEN Products.ProductName LIKE '%Gloves%' THEN 'Gloves'
ELSE NULL
END AS ProductCategory
FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
INNER JOIN Order_Details
Expand All @@ -44,7 +50,13 @@ INNER JOIN Products
ON Products.ProductNumber = Order_Details.ProductNumber;

CREATE VIEW ProdsOfInterest AS
SELECT Products.ProductName
SELECT DISTINCT
CASE WHEN Products.ProductName LIKE '%Skateboard%' THEN 'Skateboard'
WHEN Products.ProductName LIKE '%Helmet%' THEN 'Helmet'
WHEN Products.ProductName LIKE '%Knee Pads%' THEN 'Knee Pads'
WHEN Products.ProductName LIKE '%Gloves%' THEN 'Gloves'
ELSE NULL
END AS ProductCategory
FROM Products
WHERE ProductName LIKE '%Skateboard%'
OR ProductName LIKE '%Helmet%'
Expand All @@ -54,11 +66,11 @@ WHERE ProductName LIKE '%Skateboard%'
SELECT DISTINCT CustomerID, CustFirstName, CustLastName
FROM CustomerProducts AS CP1
WHERE NOT EXISTS
(SELECT ProductName FROM ProdsOfInterest AS PofI
(SELECT ProductCategory FROM ProdsOfInterest AS PofI
WHERE NOT EXISTS
(SELECT CustomerID FROM CustomerProducts AS CP2
WHERE CP2.CustomerID = CP1.CustomerID
AND CP2.ProductName = PofI.ProductName));
AND CP2.ProductCategory = PofI.ProductCategory));

DROP VIEW ProdsOfInterest;
DROP VIEW CustomerProducts;
26 changes: 19 additions & 7 deletions IBM DB2/Chapter 04/Listing 4.020.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ DROP VIEW CustomerProducts;

-- Sample query that searches products correctly:
CREATE VIEW CustomerProducts AS
SELECT DISTINCT Customers.CustomerID, Customers.CustFirstName,
Customers.CustLastName, Products.ProductName
SELECT DISTINCT Customers.CustomerID, Customers.CustFirstName,
Customers.CustLastName,
CASE WHEN Products.ProductName LIKE '%Skateboard%' THEN 'Skateboard'
WHEN Products.ProductName LIKE '%Helmet%' THEN 'Helmet'
WHEN Products.ProductName LIKE '%Knee Pads%' THEN 'Knee Pads'
WHEN Products.ProductName LIKE '%Gloves%' THEN 'Gloves'
ELSE NULL
END AS ProductCategory
FROM Customers INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
INNER JOIN Order_Details
Expand All @@ -42,19 +48,25 @@ INNER JOIN Products
ON Products.ProductNumber = Order_Details.ProductNumber;

CREATE VIEW ProdsOfInterest AS
SELECT Products.ProductName
SELECT DISTINCT
CASE WHEN Products.ProductName LIKE '%Skateboard%' THEN 'Skateboard'
WHEN Products.ProductName LIKE '%Helmet%' THEN 'Helmet'
WHEN Products.ProductName LIKE '%Knee Pads%' THEN 'Knee Pads'
WHEN Products.ProductName LIKE '%Gloves%' THEN 'Gloves'
ELSE NULL
END AS ProductCategory
FROM Products
WHERE ProductName LIKE '%Skateboard%'
OR ProductName LIKE '%Helmet%'
OR ProductName LIKE '%Knee Pads%'
OR ProductName LIKE '%Gloves%';

SELECT CP.CustomerID, CP.CustFirstName, CP.CustLastName
FROM CustomerProducts AS CP, ProdsOfInterest AS PofI
WHERE CP.ProductName = PofI.ProductName
FROM CustomerProducts AS CP CROSS JOIN ProdsOfInterest AS PofI
WHERE CP.ProductCategory = PofI.ProductCategory
GROUP BY CP.CustomerID, CP.CustFIrstName, CP.CustLastName
HAVING COUNT(CP.ProductName) =
(SELECT COUNT(ProductName) FROM ProdsOfInterest);
HAVING COUNT(CP.ProductCategory) =
(SELECT COUNT(ProductCategory) FROM ProdsOfInterest);

DROP VIEW ProdsOfInterest;
DROP VIEW CustomerProducts;
4 changes: 2 additions & 2 deletions IBM DB2/Chapter 04/Listing 4.028.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ SET SCHEMA Item28Example;
-- Listing 4.28 Table and Index creation DDL
CREATE TABLE Employees (
EmployeeID int GENERATED BY DEFAULT AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL PRIMARY KEY ,
EmpFirstName nvarchar (25) NULL ,
EmpLastName nvarchar (25) NULL ,
EmpFirstName varchar (25) NULL ,
EmpLastName varchar (25) NULL ,
EmpDOB date NULL ,
EmpSalary decimal(19,4) NULL
);
Expand Down
3 changes: 2 additions & 1 deletion IBM DB2/Chapter 05/Listing 5.015.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ HAVING SUM(OD.QuotedPrice * OD.QuantityOrdered) > (
SELECT a.AverageOfCategory
FROM AveragePerCategory AS a
WHERE a.CategoryID = P.CategoryID
);
)
ORDER BY CategoryDescription, ProductName;
3 changes: 2 additions & 1 deletion IBM DB2/Chapter 05/Listing 5.016.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ HAVING SUM(D.QuotedPrice * D.QuantityOrdered) >
SELECT a.AverageOfCategory
FROM AveragePerCategory AS a
WHERE a.CategoryID = D.CategoryID
);
)
ORDER BY CategoryDescription, ProductName;
2 changes: 1 addition & 1 deletion IBM DB2/Chapter 05/Listing 5.026.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ FROM (Recipe_Classes
Ingredient_Classes.IngredientClassDescription = 'Spice')
AS RI
ON Recipes.RecipeID = RI.RecipeID
WHERE Recipe_Classes.RecipeClassID = 1 --RecipeClassDescription = 'Main course'
WHERE RecipeClassDescription = 'Main course'
GROUP BY Recipes.RecipeTitle
HAVING COUNT(RI.RecipeID) < 3;
8 changes: 4 additions & 4 deletions IBM DB2/Chapter 06/Listing 6.002.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ FROM Customers
INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
WHERE EXISTS
(SELECT *
(SELECT NULL
FROM (Orders AS O2
INNER JOIN Order_Details
ON O2.OrderNumber = Order_Details.OrderNumber)
Expand All @@ -19,7 +19,7 @@ WHERE EXISTS
WHERE Products.ProductName = 'Skateboard'
AND O2.OrderNumber = Orders.OrderNumber)
AND EXISTS
(SELECT *
(SELECT NULL
FROM (Orders AS O3
INNER JOIN Order_Details
ON O3.OrderNumber = Order_Details.OrderNumber)
Expand All @@ -35,7 +35,7 @@ FROM Customers
INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
WHERE EXISTS
(SELECT *
(SELECT NULL
FROM (Orders AS O2
INNER JOIN Order_Details
ON O2.OrderNumber = Order_Details.OrderNumber)
Expand All @@ -44,7 +44,7 @@ WHERE EXISTS
WHERE Products.ProductName LIKE '%Skateboard%'
AND O2.OrderNumber = Orders.OrderNumber)
AND EXISTS
(SELECT *
(SELECT NULL
FROM (Orders AS O3
INNER JOIN Order_Details
ON O3.OrderNumber = Order_Details.OrderNumber)
Expand Down
4 changes: 2 additions & 2 deletions IBM DB2/Chapter 06/Listing 6.011.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ SET SCHEMA RecipesSample;
SELECT Recipes.RecipeTitle
FROM Recipes
WHERE EXISTS
(SELECT Recipe_Ingredients.RecipeID
(SELECT NULL
FROM Ingredients INNER JOIN Recipe_Ingredients
ON Ingredients.IngredientID =
Recipe_Ingredients.IngredientID
WHERE Ingredients.IngredientName = 'Beef'
AND Recipe_Ingredients.RecipeID = Recipes.RecipeID)
AND EXISTS
(SELECT Recipe_Ingredients.RecipeID
(SELECT NULL
FROM Ingredients INNER JOIN Recipe_Ingredients
ON Ingredients.IngredientID =
Recipe_Ingredients.IngredientID
Expand Down
2 changes: 1 addition & 1 deletion IBM DB2/Chapter 06/Listing 6.018.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ SET SCHEMA BeerStylesExample;

SELECT S.StyleNm
FROM Styles AS S
WHERE EXISTS (SELECT 1 FROM Countries WHERE CountryNM = 'Belgium'
WHERE EXISTS (SELECT NULL FROM Countries WHERE CountryNM = 'Belgium'
AND Countries.CountryID = S.CountryFK);
6 changes: 6 additions & 0 deletions IBM DB2/Chapter 07/Listing 7.013.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Listing 7.14 Get a list of tables and views

-- Not supported on DB2 For Linux, Unix, and Windows; only on DB2 for i Series. Untested.
SELECT T.TABLE_NAME, T.TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES AS T
WHERE T.TABLE_TYPE IN ('BASE TABLE', 'VIEW');
7 changes: 4 additions & 3 deletions IBM DB2/Chapter 07/Listing 7.014.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-- Listing 7.14 Get a list of tables and views
-- Listing 7.15 Get a list of constraints

-- Not supported on DB2 For Linux, Unix, and Windows; only on DB2 for i Series. Untested.
SELECT T.TABLE_NAME, T.TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES AS T;
SELECT TC.CONSTRAINT_NAME, TC.TABLE_NAME, TC.CONSTRAINT_TYPE
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC;

Loading

0 comments on commit d3e28e9

Please sign in to comment.