├── .gitignore ├── README.md ├── Week 3 ├── lab3 │ ├── week3-Lab.pdf │ └── SQL Tables Normalization Example.pdf └── hw3 │ └── Homework-03.pdf ├── Week 1 ├── hw1 │ ├── Homework-01.pdf │ └── Homework-01-Solutions.pdf ├── slides │ └── Week 01.pptx ├── lab1 │ ├── week1-Lab-solutions.pdf │ ├── week1-Lab-Only-Questions.pdf │ └── ychen220_lab1.sql └── sql │ └── ddl │ ├── 00 RecipesStructurePG.sql │ ├── 00 SalesOrdersStructureModifyPG.sql │ ├── 00 EntertainmentAgencyStructureModifyPG.sql │ ├── 00 BowlingLeagueStructurePG.sql │ ├── 00 BowlingLeagueStructureModifyPG.sql │ ├── 00 SalesOrdersStructurePG.sql │ ├── 02 SchoolSchedulingModifyProcsPG.sql │ ├── 00 EntertainmentAgencyStructurePG.sql │ ├── 00 SchoolSchedulingStructureModifyPG.sql │ ├── 02 EntertainmentAgencyModifyProcsPG.sql │ ├── 00 SchoolSchedulingStructurePG.sql │ ├── 02 BowlingLeagueModifyProcsPG.sql │ ├── 02 SalesOrdersModifyProcsPG.sql │ ├── 01 RecipesDataPG.sql │ └── 02 RecipesViewsPG.sql ├── Week 2 ├── hw2 │ └── Homework-02.pdf ├── lab2 │ ├── Lab2 Solutions.pdf │ └── week2-Lab-Only-Questions.pdf └── sql │ ├── 00 SalesOrdersStructurePG.sql │ └── 00 EntertainmentAgencyStructurePG.sql └── Week 4 ├── hw4 └── Homework-04.pdf └── Introduction to Database Management.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQL 2 | USC Business Analytics: SQL for Business Analysts 3 | -------------------------------------------------------------------------------- /Week 3/lab3/week3-Lab.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishi7s/SQL/HEAD/Week 3/lab3/week3-Lab.pdf -------------------------------------------------------------------------------- /Week 1/hw1/Homework-01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishi7s/SQL/HEAD/Week 1/hw1/Homework-01.pdf -------------------------------------------------------------------------------- /Week 1/slides/Week 01.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishi7s/SQL/HEAD/Week 1/slides/Week 01.pptx -------------------------------------------------------------------------------- /Week 2/hw2/Homework-02.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishi7s/SQL/HEAD/Week 2/hw2/Homework-02.pdf -------------------------------------------------------------------------------- /Week 3/hw3/Homework-03.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishi7s/SQL/HEAD/Week 3/hw3/Homework-03.pdf -------------------------------------------------------------------------------- /Week 4/hw4/Homework-04.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishi7s/SQL/HEAD/Week 4/hw4/Homework-04.pdf -------------------------------------------------------------------------------- /Week 2/lab2/Lab2 Solutions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishi7s/SQL/HEAD/Week 2/lab2/Lab2 Solutions.pdf -------------------------------------------------------------------------------- /Week 1/lab1/week1-Lab-solutions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishi7s/SQL/HEAD/Week 1/lab1/week1-Lab-solutions.pdf -------------------------------------------------------------------------------- /Week 1/hw1/Homework-01-Solutions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishi7s/SQL/HEAD/Week 1/hw1/Homework-01-Solutions.pdf -------------------------------------------------------------------------------- /Week 1/lab1/week1-Lab-Only-Questions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishi7s/SQL/HEAD/Week 1/lab1/week1-Lab-Only-Questions.pdf -------------------------------------------------------------------------------- /Week 2/lab2/week2-Lab-Only-Questions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishi7s/SQL/HEAD/Week 2/lab2/week2-Lab-Only-Questions.pdf -------------------------------------------------------------------------------- /Week 3/lab3/SQL Tables Normalization Example.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishi7s/SQL/HEAD/Week 3/lab3/SQL Tables Normalization Example.pdf -------------------------------------------------------------------------------- /Week 4/Introduction to Database Management.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rishi7s/SQL/HEAD/Week 4/Introduction to Database Management.pdf -------------------------------------------------------------------------------- /Week 1/lab1/ychen220_lab1.sql: -------------------------------------------------------------------------------- 1 | -- START Q1 2 | 3 | SELECT * 4 | FROM orders 5 | LIMIT 20; 6 | 7 | -- END Q1 8 | -- START Q2 9 | 10 | SELECT * 11 | FROM sales 12 | LIMIT 5; 13 | 14 | -- END Q2 15 | -------------------------------------------------------------------------------- /Week 1/sql/ddl/00 RecipesStructurePG.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA RecipesExample; 2 | 3 | SET search_path TO RecipesExample; 4 | 5 | CREATE TABLE Ingredient_Classes ( 6 | IngredientClassID smallint NOT NULL DEFAULT 0 , 7 | IngredientClassDescription varchar (255) NULL 8 | ); 9 | 10 | CREATE TABLE Ingredients ( 11 | IngredientID int NOT NULL DEFAULT 0 , 12 | IngredientName varchar (255) NULL , 13 | IngredientClassID smallint NULL DEFAULT 0 , 14 | MeasureAmountID smallint NULL DEFAULT 0 15 | ); 16 | 17 | CREATE TABLE Measurements ( 18 | MeasureAmountID smallint NOT NULL DEFAULT 0, 19 | MeasurementDescription varchar (255) NULL 20 | ); 21 | 22 | CREATE TABLE Recipe_Classes ( 23 | RecipeClassID smallint NOT NULL DEFAULT 0 , 24 | RecipeClassDescription varchar (255) NULL 25 | ); 26 | 27 | CREATE TABLE Recipe_Ingredients ( 28 | RecipeID int NOT NULL DEFAULT 0 , 29 | RecipeSeqNo smallint NOT NULL DEFAULT 0 , 30 | IngredientID int NULL DEFAULT 0 , 31 | MeasureAmountID smallint NULL DEFAULT 0 , 32 | Amount real NULL DEFAULT 0 33 | ); 34 | 35 | CREATE TABLE Recipes ( 36 | RecipeID int NOT NULL DEFAULT 0 , 37 | RecipeTitle varchar(255) NULL , 38 | RecipeClassID smallint NULL DEFAULT 0, 39 | Preparation text NULL , 40 | Notes text NULL 41 | ); 42 | 43 | ALTER TABLE Ingredient_Classes ADD 44 | CONSTRAINT Ingredient_Classes_PK PRIMARY KEY 45 | ( 46 | IngredientClassID 47 | ) 48 | ; 49 | 50 | ALTER TABLE Ingredients ADD 51 | CONSTRAINT Ingredients_PK PRIMARY KEY 52 | ( 53 | IngredientID 54 | ) 55 | ; 56 | 57 | CREATE INDEX Ingredients_Ingredient_ClassesIngredients ON Ingredients(IngredientClassID); 58 | 59 | CREATE INDEX Ingredients_MeasurementsIngredients ON Ingredients(MeasureAmountID); 60 | 61 | ALTER TABLE Measurements ADD 62 | CONSTRAINT Measurements_PK PRIMARY KEY 63 | ( 64 | MeasureAmountID 65 | ) 66 | ; 67 | 68 | ALTER TABLE Recipe_Classes ADD 69 | CONSTRAINT Recipe_Classes_PK PRIMARY KEY 70 | ( 71 | RecipeClassID 72 | ) 73 | ; 74 | 75 | ALTER TABLE Recipe_Ingredients ADD 76 | CONSTRAINT Recipe_Ingredients_PK PRIMARY KEY 77 | ( 78 | RecipeID, 79 | RecipeSeqNo 80 | ) 81 | ; 82 | 83 | CREATE INDEX Recipe_Ingredients_IngredientID ON Recipe_Ingredients(IngredientID); 84 | 85 | CREATE INDEX Recipe_Ingredients_MeasureAmountID ON Recipe_Ingredients(MeasureAmountID); 86 | 87 | CREATE INDEX Recipe_Ingredients_RecipeID ON Recipe_Ingredients(RecipeID); 88 | 89 | ALTER TABLE Recipes ADD 90 | CONSTRAINT Recipes_PK PRIMARY KEY 91 | ( 92 | RecipeID 93 | ) 94 | ; 95 | 96 | CREATE INDEX Recipes_ClassesRecipes ON Recipes(RecipeClassID); 97 | 98 | ALTER TABLE Ingredients 99 | ADD CONSTRAINT Ingredients_FK00 FOREIGN KEY 100 | ( 101 | IngredientClassID 102 | ) REFERENCES Ingredient_Classes ( 103 | IngredientClassID 104 | ), 105 | ADD CONSTRAINT Ingredients_FK01 FOREIGN KEY 106 | ( 107 | MeasureAmountID 108 | ) REFERENCES Measurements ( 109 | MeasureAmountID 110 | ) 111 | ; 112 | 113 | ALTER TABLE Recipes 114 | ADD CONSTRAINT Recipes_FK00 FOREIGN KEY 115 | ( 116 | RecipeClassID 117 | ) REFERENCES Recipe_Classes ( 118 | RecipeClassID 119 | ) 120 | ; 121 | 122 | ALTER Table Recipe_Ingredients 123 | ADD CONSTRAINT Recipe_Ingredients_FK00 FOREIGN KEY 124 | ( 125 | RecipeID 126 | ) REFERENCES Recipes ( 127 | RecipeID 128 | ), 129 | ADD CONSTRAINT Recipe_Ingredients_FK01 FOREIGN KEY 130 | ( 131 | IngredientID 132 | ) REFERENCES Ingredients ( 133 | IngredientID 134 | ), 135 | ADD CONSTRAINT Recipe_Ingredients_FK02 FOREIGN KEY 136 | ( 137 | MeasureAmountID 138 | ) REFERENCES Measurements ( 139 | MeasureAmountID 140 | ) 141 | ; -------------------------------------------------------------------------------- /Week 1/sql/ddl/00 SalesOrdersStructureModifyPG.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA SalesOrdersModify; 2 | 3 | SET search_path TO SalesOrdersModify; 4 | 5 | CREATE TABLE Categories ( 6 | CategoryID serial PRIMARY KEY , 7 | CategoryDescription varchar (75) NULL 8 | ); 9 | 10 | CREATE TABLE Customers ( 11 | CustomerID serial PRIMARY KEY , 12 | CustFirstName varchar (25) NULL , 13 | CustLastName varchar (25) NULL , 14 | CustStreetAddress varchar (50) NULL , 15 | CustCity varchar (30) NULL , 16 | CustState varchar (2) NULL , 17 | CustZipCode varchar (10) NULL , 18 | CustAreaCode smallint NULL DEFAULT 0 , 19 | CustPhoneNumber varchar (8) NULL 20 | ); 21 | 22 | CREATE TABLE Employees ( 23 | EmployeeID serial PRIMARY KEY , 24 | EmpFirstName varchar (25) NULL , 25 | EmpLastName varchar (25) NULL , 26 | EmpStreetAddress varchar (50) NULL , 27 | EmpCity varchar (30) NULL , 28 | EmpState varchar (2) NULL , 29 | EmpZipCode varchar (10) NULL , 30 | EmpAreaCode smallint NULL DEFAULT 0 , 31 | EmpPhoneNumber varchar (8) NULL 32 | ); 33 | 34 | CREATE TABLE Order_Details ( 35 | OrderNumber int NOT NULL DEFAULT 0 , 36 | ProductNumber int NOT NULL DEFAULT 0 , 37 | QuotedPrice decimal (15,2) NULL DEFAULT 0 , 38 | QuantityOrdered smallint NULL DEFAULT 0 39 | ); 40 | 41 | CREATE TABLE Order_Details_Archive ( 42 | OrderNumber int NOT NULL DEFAULT 0 , 43 | ProductNumber int NOT NULL DEFAULT 0 , 44 | QuotedPrice decimal (15,2) NULL DEFAULT 0 , 45 | QuantityOrdered smallint NULL DEFAULT 0 46 | ); 47 | 48 | CREATE TABLE Orders ( 49 | OrderNumber serial PRIMARY KEY , 50 | OrderDate date NULL , 51 | ShipDate date NULL , 52 | CustomerID int NULL DEFAULT 0 , 53 | EmployeeID int NULL DEFAULT 0 , 54 | OrderTotal decimal (15,2) NULL DEFAULT 0 55 | ); 56 | 57 | CREATE TABLE Orders_Archive ( 58 | OrderNumber int NOT NULL PRIMARY KEY DEFAULT 0 , 59 | OrderDate date NULL , 60 | ShipDate date NULL , 61 | CustomerID int NULL DEFAULT 0 , 62 | EmployeeID int NULL DEFAULT 0 , 63 | OrderTotal decimal (15,2) NULL DEFAULT 0 64 | ); 65 | 66 | 67 | CREATE TABLE Product_Vendors ( 68 | ProductNumber int NOT NULL DEFAULT 0 , 69 | VendorID int NOT NULL DEFAULT 0 , 70 | WholesalePrice decimal (15,2) NULL DEFAULT 0 , 71 | DaysToDeliver smallint NULL DEFAULT 0 72 | ); 73 | 74 | CREATE TABLE Products ( 75 | ProductNumber serial PRIMARY KEY , 76 | ProductName varchar (50) NULL , 77 | ProductDescription varchar (100) NULL , 78 | RetailPrice decimal (15,2) NULL DEFAULT 0 , 79 | QuantityOnHand smallint NULL DEFAULT 0 , 80 | CategoryID int NULL DEFAULT 0 81 | ); 82 | 83 | CREATE TABLE Vendors ( 84 | VendorID serial PRIMARY KEY , 85 | VendName varchar (25) NULL , 86 | VendStreetAddress varchar (50) NULL , 87 | VendCity varchar (30) NULL , 88 | VendState varchar (2) NULL , 89 | VendZipCode varchar (10) NULL , 90 | VendPhoneNumber varchar (15) NULL , 91 | VendFaxNumber varchar (15) NULL , 92 | VendWebPage text NULL , 93 | VendEMailAddress varchar (50) NULL 94 | ); 95 | 96 | CREATE INDEX CustAreaCode ON Customers(CustAreaCode); 97 | 98 | CREATE INDEX CustZipCode ON Customers(CustZipCode); 99 | 100 | CREATE INDEX EmpAreaCode ON Employees(EmpAreaCode); 101 | 102 | CREATE INDEX EmpZipCode ON Employees(EmpZipCode); 103 | 104 | ALTER TABLE Order_Details 105 | ADD CONSTRAINT Order_Details_PK PRIMARY KEY 106 | ( 107 | OrderNumber, 108 | ProductNumber 109 | ); 110 | 111 | CREATE INDEX OrdersOrderDetails ON Order_Details(OrderNumber); 112 | 113 | CREATE INDEX ProductsOrderDetails ON Order_Details(ProductNumber); 114 | 115 | ALTER TABLE Order_Details_Archive 116 | ADD CONSTRAINT Order_Details_Archive_PK PRIMARY KEY 117 | ( 118 | OrderNumber, 119 | ProductNumber 120 | ); 121 | 122 | CREATE INDEX Orders_ArchiveOrder_Details_Archive ON Order_Details_Archive(OrderNumber); 123 | 124 | CREATE INDEX O_CustomerID ON Orders(CustomerID); 125 | 126 | CREATE INDEX O_EmployeeID ON Orders(EmployeeID); 127 | 128 | CREATE INDEX OA_CustomerID ON Orders_Archive(CustomerID); 129 | 130 | CREATE INDEX OA_EmployeeID ON Orders_Archive(EmployeeID); 131 | 132 | ALTER TABLE Product_Vendors 133 | ADD CONSTRAINT Product_Vendors_PK PRIMARY KEY 134 | ( 135 | ProductNumber, 136 | VendorID 137 | ); 138 | 139 | CREATE INDEX ProductsProductVendors ON Product_Vendors(ProductNumber); 140 | 141 | CREATE INDEX VendorID ON Product_Vendors(VendorID); 142 | 143 | CREATE INDEX CategoryID ON Products(CategoryID); 144 | 145 | CREATE INDEX VendZipCode ON Vendors(VendZipCode); 146 | 147 | ALTER TABLE Order_Details 148 | ADD CONSTRAINT Order_Details_FK00 FOREIGN KEY 149 | ( 150 | OrderNumber 151 | ) REFERENCES Orders ( 152 | OrderNumber 153 | ), 154 | ADD CONSTRAINT Order_Details_FK01 FOREIGN KEY 155 | ( 156 | ProductNumber 157 | ) REFERENCES Products ( 158 | ProductNumber 159 | ); 160 | 161 | ALTER TABLE Order_Details_Archive 162 | ADD CONSTRAINT Order_Details_Archive_FK00 FOREIGN KEY 163 | ( 164 | OrderNumber 165 | ) REFERENCES Orders_Archive ( 166 | OrderNumber 167 | ); 168 | 169 | ALTER TABLE Orders 170 | ADD CONSTRAINT Orders_FK00 FOREIGN KEY 171 | ( 172 | CustomerID 173 | ) REFERENCES Customers ( 174 | CustomerID 175 | ), 176 | ADD CONSTRAINT Orders_FK01 FOREIGN KEY 177 | ( 178 | EmployeeID 179 | ) REFERENCES Employees ( 180 | EmployeeID 181 | ); 182 | 183 | ALTER TABLE Product_Vendors 184 | ADD CONSTRAINT Product_Vendors_FK00 FOREIGN KEY 185 | ( 186 | ProductNumber 187 | ) REFERENCES Products ( 188 | ProductNumber 189 | ), 190 | ADD CONSTRAINT Product_Vendors_FK01 FOREIGN KEY 191 | ( 192 | VendorID 193 | ) REFERENCES Vendors ( 194 | VendorID 195 | ); 196 | 197 | ALTER TABLE Products 198 | ADD CONSTRAINT Products_FK00 FOREIGN KEY 199 | ( 200 | CategoryID 201 | ) REFERENCES Categories ( 202 | CategoryID 203 | ); -------------------------------------------------------------------------------- /Week 1/sql/ddl/00 EntertainmentAgencyStructureModifyPG.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA EntertainmentAgencyModify; 2 | 3 | SET search_path TO EntertainmentAgencyModify; 4 | 5 | CREATE TABLE Agents ( 6 | AgentID SERIAL PRIMARY KEY, 7 | AgtFirstName varchar (25) NULL , 8 | AgtLastName varchar (25) NULL , 9 | AgtStreetAddress varchar (50) NULL , 10 | AgtCity varchar (30) NULL , 11 | AgtState varchar (2) NULL , 12 | AgtZipCode varchar (10) NULL , 13 | AgtPhoneNumber varchar (15) NULL , 14 | DateHired date NULL , 15 | Salary decimal(15,2) NULL DEFAULT 0 , 16 | CommissionRate float(24) NULL DEFAULT 0 ); 17 | 18 | CREATE TABLE Customers ( 19 | CustomerID SERIAL PRIMARY KEY, 20 | CustFirstName varchar (25) NULL , 21 | CustLastName varchar (25) NULL , 22 | CustStreetAddress varchar (50) NULL , 23 | CustCity varchar (30) NULL , 24 | CustState varchar (2) NULL , 25 | CustZipCode varchar (10) NULL , 26 | CustPhoneNumber varchar (15) NULL ); 27 | 28 | CREATE TABLE Engagements ( 29 | EngagementNumber SERIAL PRIMARY KEY, 30 | StartDate date NULL , 31 | EndDate date NULL , 32 | StartTime time NULL , 33 | StopTime time NULL , 34 | ContractPrice decimal(15,2) NULL DEFAULT 0 , 35 | CustomerID int NULL DEFAULT 0 , 36 | AgentID int NULL DEFAULT 0 , 37 | EntertainerID int NULL DEFAULT 0 ); 38 | 39 | CREATE TABLE Engagements_Archive ( 40 | EngagementNumber int NOT NULL PRIMARY KEY , 41 | StartDate date NULL , EndDate date NULL , 42 | StartTime time NULL , StopTime time NULL , 43 | ContractPrice decimal(15,2) NULL , 44 | CustomerID int NULL , 45 | AgentID int NULL , 46 | EntertainerID int NULL ); 47 | 48 | CREATE TABLE Entertainer_Members ( 49 | EntertainerID int NOT NULL , 50 | MemberID int NOT NULL DEFAULT 0 , 51 | Status smallint NULL DEFAULT 0 ); 52 | 53 | CREATE TABLE Entertainer_Styles ( 54 | EntertainerID int NOT NULL , 55 | StyleID int NOT NULL DEFAULT 0 ); 56 | 57 | CREATE TABLE Entertainers ( 58 | EntertainerID SERIAL PRIMARY KEY, 59 | EntStageName varchar (50) NULL , 60 | EntSSN varchar (12) NULL , 61 | EntStreetAddress varchar (50) NULL , 62 | EntCity varchar (30) NULL , 63 | EntState varchar (2) NULL , 64 | EntZipCode varchar (10) NULL , 65 | EntPhoneNumber varchar (15) NULL , 66 | EntWebPage varchar (50) NULL , 67 | EntEMailAddress varchar (50) NULL , 68 | DateEntered date NULL , 69 | EntPricePerDay decimal(15,2) NULL ); 70 | 71 | CREATE TABLE Members ( 72 | MemberID SERIAL PRIMARY KEY, 73 | MbrFirstName varchar (25) NULL , 74 | MbrLastName varchar (25) NULL , 75 | MbrPhoneNumber varchar (15) NULL , 76 | Gender varchar (2) NULL ); 77 | 78 | CREATE TABLE Musical_Preferences ( 79 | CustomerID int NOT NULL DEFAULT 0 , 80 | StyleID int NOT NULL DEFAULT 0 ); 81 | 82 | CREATE TABLE Musical_Styles ( 83 | StyleID SERIAL PRIMARY KEY, 84 | StyleName varchar (75) NULL ); 85 | 86 | CREATE INDEX AgtZipCode ON Agents(AgtZipCode); 87 | 88 | CREATE INDEX CustZipCode ON Customers(CustZipCode); 89 | 90 | CREATE INDEX AgentsEngagements ON Engagements(AgentID); 91 | 92 | CREATE INDEX CustomerID ON Engagements(CustomerID); 93 | 94 | CREATE INDEX EmployeeID ON Engagements(AgentID); 95 | 96 | CREATE INDEX EntertainerID ON Engagements(EntertainerID); 97 | 98 | CREATE INDEX EnA_CustomerID ON Engagements_Archive(CustomerID); 99 | 100 | CREATE INDEX EnA_EmployeeID ON Engagements_Archive(AgentID); 101 | 102 | CREATE INDEX EnA_EntertainerID ON Engagements_Archive(EntertainerID); 103 | 104 | ALTER TABLE Entertainer_Members 105 | ADD CONSTRAINT Entertainer_Members_PK PRIMARY KEY 106 | ( 107 | EntertainerID, 108 | MemberID 109 | ); 110 | 111 | CREATE INDEX EntertainersEntertainer_Members ON Entertainer_Members(EntertainerID); 112 | 113 | CREATE INDEX MembersEntertainer_Members ON Entertainer_Members(MemberID); 114 | 115 | ALTER TABLE Entertainer_Styles 116 | ADD CONSTRAINT Entertainer_Styles_PK PRIMARY KEY 117 | ( 118 | EntertainerID, 119 | StyleID 120 | ); 121 | 122 | CREATE INDEX EntertainersEntertainer_Styles ON Entertainer_Styles(EntertainerID); 123 | 124 | CREATE INDEX Musical_StylesEntertainer_Styles ON Entertainer_Styles(StyleID); 125 | 126 | CREATE INDEX EntZipCode ON Entertainers(EntZipCode); 127 | 128 | ALTER TABLE Musical_Preferences 129 | ADD CONSTRAINT Musical_Preferences_PK PRIMARY KEY 130 | ( 131 | CustomerID, 132 | StyleID 133 | ); 134 | 135 | CREATE INDEX MP_CustomerID ON Musical_Preferences(CustomerID); 136 | 137 | CREATE INDEX StyleID ON Musical_Preferences(StyleID); 138 | 139 | ALTER TABLE Engagements 140 | ADD CONSTRAINT Engagements_FK00 FOREIGN KEY 141 | ( 142 | AgentID 143 | ) REFERENCES Agents ( 144 | AgentID 145 | ), 146 | ADD CONSTRAINT Engagements_FK01 FOREIGN KEY 147 | ( 148 | CustomerID 149 | ) REFERENCES Customers ( 150 | CustomerID 151 | ), 152 | ADD CONSTRAINT Engagements_FK02 FOREIGN KEY 153 | ( 154 | EntertainerID 155 | ) REFERENCES Entertainers ( 156 | EntertainerID 157 | ); 158 | 159 | ALTER TABLE Entertainer_Members 160 | ADD CONSTRAINT Entertainer_Members_FK00 FOREIGN KEY 161 | ( 162 | EntertainerID 163 | ) REFERENCES Entertainers ( 164 | EntertainerID 165 | ), 166 | ADD CONSTRAINT Entertainer_Members_FK01 FOREIGN KEY 167 | ( 168 | MemberID 169 | ) REFERENCES Members ( 170 | MemberID 171 | ); 172 | 173 | ALTER TABLE Entertainer_Styles 174 | ADD CONSTRAINT Entertainer_Styles_FK00 FOREIGN KEY 175 | ( 176 | EntertainerID 177 | ) REFERENCES Entertainers ( 178 | EntertainerID 179 | ), 180 | ADD CONSTRAINT Entertainer_Styles_FK01 FOREIGN KEY 181 | ( 182 | StyleID 183 | ) REFERENCES Musical_Styles 184 | ( 185 | StyleID 186 | ); 187 | 188 | ALTER TABLE Musical_Preferences 189 | ADD CONSTRAINT Musical_Preferences_FK00 FOREIGN KEY 190 | ( 191 | CustomerID 192 | ) REFERENCES Customers ( 193 | CustomerID 194 | ), 195 | ADD CONSTRAINT Musical_Preferences_FK01 FOREIGN KEY 196 | ( 197 | StyleID 198 | ) REFERENCES Musical_Styles ( 199 | StyleID 200 | ); 201 | 202 | -------------------------------------------------------------------------------- /Week 1/sql/ddl/00 BowlingLeagueStructurePG.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA BowlingLeagueExample; 2 | 3 | SET search_path TO BowlingLeagueExample; 4 | 5 | 6 | CREATE TABLE Bowler_Scores ( 7 | 8 | MatchID int NOT NULL DEFAULT 0 , 9 | 10 | GameNumber smallint NOT NULL DEFAULT 0 , 11 | 12 | BowlerID int NOT NULL DEFAULT 0 , 13 | 14 | RawScore smallint NULL DEFAULT 0 , 15 | 16 | HandiCapScore smallint NULL DEFAULT 0 , 17 | 18 | WonGame smallint NOT NULL DEFAULT 0 19 | ) ; 20 | 21 | 22 | 23 | CREATE TABLE Bowlers ( 24 | 25 | BowlerID int NOT NULL DEFAULT 0 , 26 | 27 | BowlerLastName varchar (50) NULL , 28 | 29 | BowlerFirstName varchar (50) NULL , 30 | 31 | BowlerMiddleInit varchar (1) NULL , 32 | 33 | BowlerAddress varchar (50) NULL , 34 | 35 | BowlerCity varchar (50) NULL , 36 | 37 | BowlerState varchar (2) NULL , 38 | 39 | BowlerZip varchar (10) NULL , 40 | 41 | BowlerPhoneNumber varchar (14) NULL , 42 | 43 | TeamID int NULL 44 | ) ; 45 | 46 | 47 | 48 | CREATE TABLE Match_Games ( 49 | 50 | MatchID int NOT NULL DEFAULT 0 , 51 | 52 | GameNumber smallint NOT NULL DEFAULT 0 , 53 | 54 | WinningTeamID int NULL DEFAULT 0 55 | ) ; 56 | 57 | 58 | 59 | CREATE TABLE Teams ( 60 | 61 | TeamID int NOT NULL DEFAULT 0 , 62 | 63 | TeamName varchar (50) NOT NULL , 64 | 65 | CaptainID int NULL 66 | ) ; 67 | 68 | 69 | 70 | CREATE TABLE Tournaments ( 71 | 72 | TourneyID int NOT NULL DEFAULT 0 , 73 | 74 | TourneyDate date NULL , 75 | 76 | TourneyLocation varchar (50) NULL 77 | ) ; 78 | 79 | 80 | 81 | CREATE TABLE Tourney_Matches ( 82 | 83 | MatchID int NOT NULL DEFAULT 0 , 84 | 85 | TourneyID int NULL DEFAULT 0 , 86 | 87 | Lanes varchar (5) NULL , 88 | 89 | OddLaneTeamID int NULL DEFAULT 0 , 90 | 91 | EvenLaneTeamID int NULL DEFAULT 0 92 | ) ; 93 | 94 | 95 | CREATE TABLE ztblBowlerRatings ( 96 | BowlerRating varchar (15) NOT NULL , 97 | BowlerLowAvg smallint NULL , 98 | BowlerHighAvg smallint NULL ) ; 99 | 100 | CREATE TABLE ztblSkipLabels ( 101 | LabelCount int NOT NULL ) ; 102 | 103 | CREATE TABLE ztblWeeks ( 104 | WeekStart date NOT NULL , 105 | WeekEnd date NULL ) ; 106 | 107 | 108 | ALTER TABLE Bowler_Scores ADD 109 | 110 | CONSTRAINT Bowler_Scores_PK PRIMARY KEY 111 | 112 | ( 113 | 114 | MatchID, 115 | 116 | GameNumber, 117 | 118 | BowlerID 119 | 120 | ) ; 121 | 122 | 123 | 124 | CREATE INDEX BS_BowlerID ON Bowler_Scores(BowlerID) ; 125 | 126 | 127 | 128 | CREATE INDEX MatchGamesBowlerScores ON Bowler_Scores(MatchID, GameNumber) ; 129 | 130 | 131 | 132 | ALTER TABLE Bowlers ADD 133 | 134 | CONSTRAINT Bowlers_PK PRIMARY KEY 135 | 136 | ( 137 | 138 | BowlerID 139 | 140 | ) ; 141 | 142 | CREATE INDEX BowlerLastName ON Bowlers(BowlerLastName) ; 143 | 144 | 145 | 146 | CREATE INDEX BowlersTeamID ON Bowlers(TeamID) ; 147 | 148 | 149 | 150 | ALTER TABLE Match_Games ADD 151 | 152 | CONSTRAINT Match_Games_PK PRIMARY KEY 153 | 154 | ( 155 | 156 | MatchID, 157 | 158 | GameNumber 159 | 160 | ) ; 161 | 162 | 163 | 164 | CREATE INDEX Team1ID ON Match_Games(WinningTeamID) ; 165 | 166 | 167 | 168 | CREATE INDEX TourneyMatchesMatchGames ON Match_Games(MatchID) ; 169 | 170 | 171 | 172 | ALTER TABLE Teams ADD 173 | CONSTRAINT Teams_PK PRIMARY KEY 174 | 175 | ( 176 | 177 | TeamID 178 | 179 | ) ; 180 | 181 | 182 | 183 | CREATE UNIQUE INDEX TeamID ON Teams(TeamID) ; 184 | 185 | 186 | 187 | ALTER TABLE Tournaments ADD 188 | 189 | CONSTRAINT Tournaments_PK PRIMARY KEY 190 | 191 | ( 192 | 193 | TourneyID 194 | 195 | ) ; 196 | 197 | 198 | 199 | ALTER TABLE Tourney_Matches ADD 200 | 201 | CONSTRAINT Tourney_Matches_PK PRIMARY KEY 202 | 203 | ( 204 | 205 | MatchID 206 | 207 | ) ; 208 | 209 | 210 | 211 | CREATE INDEX Tourney_MatchesEven ON Tourney_Matches(EvenLaneTeamID) ; 212 | 213 | 214 | 215 | CREATE INDEX TourneyMatchesOdd ON Tourney_Matches(OddLaneTeamID) ; 216 | 217 | 218 | 219 | CREATE INDEX TourneyMatchesTourneyID ON Tourney_Matches(TourneyID) ; 220 | 221 | 222 | 223 | ALTER TABLE ztblBowlerRatings ADD 224 | CONSTRAINT ztblBowlerRatings_PK PRIMARY KEY 225 | ( 226 | BowlerRating 227 | ) ; 228 | 229 | ALTER TABLE ztblSkipLabels ADD 230 | CONSTRAINT ztblSkipLabels_PK PRIMARY KEY 231 | ( 232 | LabelCount 233 | ) ; 234 | 235 | ALTER TABLE ztblWeeks ADD 236 | CONSTRAINT ztblWeeks_PK PRIMARY KEY 237 | ( 238 | WeekStart 239 | ) ; 240 | 241 | ALTER TABLE Bowler_Scores ADD 242 | 243 | CONSTRAINT Bowler_Scores_FK00 FOREIGN KEY 244 | 245 | ( 246 | 247 | BowlerID 248 | 249 | ) REFERENCES Bowlers ( 250 | 251 | BowlerID 252 | 253 | ), 254 | 255 | ADD CONSTRAINT Bowler_Scores_FK01 FOREIGN KEY 256 | 257 | ( 258 | 259 | MatchID, 260 | 261 | GameNumber 262 | 263 | ) REFERENCES Match_Games ( 264 | 265 | MatchID, 266 | 267 | GameNumber 268 | 269 | ); 270 | 271 | 272 | 273 | ALTER TABLE Bowlers ADD 274 | 275 | CONSTRAINT Bowlers_FK00 FOREIGN KEY 276 | 277 | ( 278 | 279 | TeamID 280 | 281 | ) REFERENCES Teams ( 282 | 283 | TeamID 284 | 285 | ); 286 | 287 | 288 | 289 | ALTER TABLE Match_Games ADD 290 | 291 | CONSTRAINT Match_Games_FK00 FOREIGN KEY 292 | 293 | ( 294 | 295 | MatchID 296 | 297 | ) REFERENCES Tourney_Matches ( 298 | 299 | MatchID 300 | 301 | ); 302 | 303 | 304 | 305 | ALTER TABLE Tourney_Matches ADD 306 | 307 | CONSTRAINT Tourney_Matches_FK00 FOREIGN KEY 308 | 309 | ( 310 | 311 | EvenLaneTeamID 312 | 313 | ) REFERENCES Teams ( 314 | 315 | TeamID 316 | 317 | ), 318 | 319 | ADD CONSTRAINT Tourney_Matches_FK01 FOREIGN KEY 320 | 321 | ( 322 | 323 | OddLaneTeamID 324 | 325 | ) REFERENCES Teams ( 326 | 327 | TeamID 328 | 329 | ), 330 | 331 | ADD CONSTRAINT Tourney_Matches_FK02 FOREIGN KEY 332 | 333 | ( 334 | 335 | TourneyID 336 | 337 | ) REFERENCES Tournaments ( 338 | 339 | TourneyID 340 | 341 | ); -------------------------------------------------------------------------------- /Week 1/sql/ddl/00 BowlingLeagueStructureModifyPG.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA BowlingLeagueModify; 2 | 3 | SET search_path TO BowlingLeagueModify; 4 | 5 | CREATE TABLE Bowler_Scores ( 6 | MatchID int NOT NULL DEFAULT 0 , 7 | GameNumber smallint NOT NULL DEFAULT 0 , 8 | BowlerID int NOT NULL DEFAULT 0 , 9 | RawScore smallint NULL DEFAULT 0 , 10 | HandiCapScore smallint NULL DEFAULT 0 , 11 | WonGame smallint NOT NULL DEFAULT 0 12 | ); 13 | 14 | CREATE TABLE Bowler_Scores_Archive ( 15 | MatchID int NOT NULL DEFAULT 0 , 16 | GameNumber smallint NOT NULL DEFAULT 0 , 17 | BowlerID int NOT NULL DEFAULT 0 , 18 | RawScore smallint NULL DEFAULT 0 , 19 | HandiCapScore smallint NULL DEFAULT 0 , 20 | WonGame smallint NOT NULL DEFAULT 0 21 | ); 22 | 23 | CREATE TABLE Bowlers ( 24 | BowlerID SERIAL PRIMARY KEY , 25 | BowlerLastName varchar (50) NULL , 26 | BowlerFirstName varchar (50) NULL , 27 | BowlerMiddleInit varchar (1) NULL , 28 | BowlerAddress varchar (50) NULL , 29 | BowlerCity varchar (50) NULL , 30 | BowlerState varchar (2) NULL , 31 | BowlerZip varchar (10) NULL , 32 | BowlerPhoneNumber varchar (14) NULL , 33 | TeamID int NULL , 34 | BowlerTotalPins int NULL DEFAULT 0 , 35 | BowlerGamesBowled int NULL DEFAULT 0 , 36 | BowlerCurrentAverage smallint NULL DEFAULT 0 , 37 | BowlerCurrentHcp smallint NULL DEFAULT 0 38 | ); 39 | 40 | CREATE TABLE Match_Games ( 41 | MatchID int NOT NULL DEFAULT 0 , 42 | GameNumber smallint NOT NULL DEFAULT 0 , 43 | WinningTeamID int NULL DEFAULT 0 44 | ); 45 | 46 | CREATE TABLE Match_Games_Archive ( 47 | MatchID int NOT NULL DEFAULT 0 , 48 | GameNumber smallint NOT NULL DEFAULT 0 , 49 | WinningTeamID int NULL DEFAULT 0 50 | ); 51 | 52 | CREATE TABLE Teams ( 53 | TeamID SERIAL PRIMARY KEY , 54 | TeamName varchar (50) NOT NULL , 55 | CaptainID int NULL 56 | ); 57 | 58 | CREATE TABLE Tournaments ( 59 | TourneyID SERIAL PRIMARY KEY , 60 | TourneyDate date NULL , 61 | TourneyLocation varchar (50) NULL 62 | ); 63 | 64 | CREATE TABLE Tournaments_Archive ( 65 | TourneyID int NOT NULL DEFAULT 0 , 66 | TourneyDate date NULL , 67 | TourneyLocation varchar (50) NULL 68 | ); 69 | 70 | CREATE TABLE Tourney_Matches ( 71 | MatchID SERIAL PRIMARY KEY , 72 | TourneyID int NULL DEFAULT 0 , 73 | Lanes varchar (5) NULL , 74 | OddLaneTeamID int NULL DEFAULT 0 , 75 | EvenLaneTeamID int NULL DEFAULT 0 76 | ); 77 | 78 | CREATE TABLE Tourney_Matches_Archive ( 79 | MatchID int NOT NULL DEFAULT 0 , 80 | TourneyID int NULL DEFAULT 0 , 81 | Lanes varchar (5) NULL , 82 | OddLaneTeamID int NULL DEFAULT 0 , 83 | EvenLaneTeamID int NULL DEFAULT 0 84 | ); 85 | 86 | CREATE TABLE WAZips ( 87 | ZIP varchar (5) NOT NULL , 88 | City varchar (255) NULL , 89 | State varchar (255) NULL 90 | ); 91 | 92 | ALTER TABLE Bowler_Scores ADD 93 | CONSTRAINT Bowler_Scores_PK PRIMARY KEY 94 | ( 95 | MatchID, 96 | GameNumber, 97 | BowlerID 98 | ) ; 99 | 100 | CREATE INDEX BS_BowlerID ON Bowler_Scores(BowlerID) ; 101 | 102 | CREATE INDEX MatchGamesBowlerScores ON Bowler_Scores(MatchID, GameNumber) ; 103 | 104 | ALTER TABLE Bowler_Scores_Archive ADD 105 | CONSTRAINT Bowler_Scores_Archive_PK PRIMARY KEY 106 | ( 107 | MatchID, 108 | GameNumber, 109 | BowlerID 110 | ) ; 111 | 112 | CREATE INDEX BSA_BowlerID ON Bowler_Scores_Archive(BowlerID); 113 | 114 | CREATE INDEX Match_Games_ArchiveBowler_Scores_Archive ON Bowler_Scores_Archive(MatchID, GameNumber); 115 | 116 | CREATE INDEX BowlerLastName ON Bowlers(BowlerLastName) ; 117 | 118 | CREATE INDEX BowlersTeamID ON Bowlers(TeamID) ; 119 | 120 | ALTER TABLE Match_Games ADD 121 | CONSTRAINT Match_Games_PK PRIMARY KEY 122 | ( 123 | MatchID, 124 | GameNumber 125 | ) ; 126 | 127 | CREATE INDEX MG_Team1ID ON Match_Games(WinningTeamID) ; 128 | 129 | CREATE INDEX TourneyMatchesMatchGames ON Match_Games(MatchID) ; 130 | 131 | ALTER TABLE Match_Games_Archive ADD 132 | CONSTRAINT Match_Games_Archive_PK PRIMARY KEY 133 | ( 134 | MatchID, 135 | GameNumber 136 | ) ; 137 | 138 | CREATE INDEX MGA_Team1ID ON Match_Games_Archive(WinningTeamID); 139 | 140 | CREATE INDEX Tourney_Matches_ArchiveMatch_Games_Archive ON Match_Games_Archive(MatchID); 141 | 142 | ALTER TABLE Tournaments_Archive ADD 143 | CONSTRAINT Tournaments_Archive_PK PRIMARY KEY 144 | ( 145 | TourneyID 146 | ) ; 147 | 148 | CREATE INDEX TeamsTourney_Matches_Even ON Tourney_Matches(EvenLaneTeamID) ; 149 | 150 | CREATE INDEX TeamsTourneyMatches_Odd ON Tourney_Matches(OddLaneTeamID) ; 151 | 152 | CREATE INDEX TourneyMatchesTourneyID ON Tourney_Matches(TourneyID) ; 153 | 154 | ALTER TABLE Tourney_Matches_Archive ADD 155 | CONSTRAINT Tourney_Matches_Archive_PK PRIMARY KEY 156 | ( 157 | MatchID 158 | ) ; 159 | 160 | CREATE INDEX TMA_Team1ID ON Tourney_Matches_Archive(OddLaneTeamID); 161 | 162 | CREATE INDEX TMA_Team2ID ON Tourney_Matches_Archive(EvenLaneTeamID); 163 | 164 | CREATE INDEX TMA_TourneyID ON Tourney_Matches_Archive(TourneyID); 165 | 166 | ALTER TABLE WAZips ADD 167 | CONSTRAINT WAZips_PK PRIMARY KEY 168 | ( 169 | ZIP 170 | ) ; 171 | 172 | ALTER TABLE Bowler_Scores ADD 173 | CONSTRAINT Bowler_Scores_FK00 FOREIGN KEY 174 | ( 175 | BowlerID 176 | ) REFERENCES Bowlers ( 177 | BowlerID 178 | ), 179 | ADD CONSTRAINT Bowler_Scores_FK01 FOREIGN KEY 180 | ( 181 | MatchID, 182 | GameNumber 183 | ) REFERENCES Match_Games ( 184 | MatchID, 185 | GameNumber 186 | ); 187 | 188 | ALTER TABLE Bowler_Scores_Archive ADD 189 | CONSTRAINT Bowler_Scores_Archive_FK00 FOREIGN KEY 190 | ( 191 | MatchID, 192 | GameNumber 193 | ) REFERENCES Match_Games_Archive ( 194 | MatchID, 195 | GameNumber 196 | ); 197 | 198 | ALTER TABLE Bowlers ADD 199 | CONSTRAINT Bowlers_FK00 FOREIGN KEY 200 | ( 201 | TeamID 202 | ) REFERENCES Teams ( 203 | TeamID 204 | ); 205 | 206 | ALTER TABLE Match_Games ADD 207 | CONSTRAINT Match_Games_FK00 FOREIGN KEY 208 | ( 209 | MatchID 210 | ) REFERENCES Tourney_Matches ( 211 | MatchID 212 | ); 213 | 214 | ALTER TABLE Match_Games_Archive ADD 215 | CONSTRAINT Match_Games_Archive_FK00 FOREIGN KEY 216 | ( 217 | MatchID 218 | ) REFERENCES Tourney_Matches_Archive ( 219 | MatchID 220 | ); 221 | 222 | ALTER TABLE Tourney_Matches ADD 223 | CONSTRAINT Tourney_Matches_FK00 FOREIGN KEY 224 | ( 225 | EvenLaneTeamID 226 | ) REFERENCES Teams ( 227 | TeamID 228 | ), 229 | ADD CONSTRAINT Tourney_Matches_FK01 FOREIGN KEY 230 | ( 231 | OddLaneTeamID 232 | ) REFERENCES Teams ( 233 | TeamID 234 | ), 235 | ADD CONSTRAINT Tourney_Matches_FK02 FOREIGN KEY 236 | ( 237 | TourneyID 238 | ) REFERENCES Tournaments ( 239 | TourneyID 240 | ); 241 | 242 | ALTER TABLE Tourney_Matches_Archive ADD 243 | CONSTRAINT Tourney_Matches_Archive_FK00 FOREIGN KEY 244 | ( 245 | TourneyID 246 | ) REFERENCES Tournaments_Archive ( 247 | TourneyID 248 | ); 249 | 250 | 251 | 252 | -------------------------------------------------------------------------------- /Week 2/sql/00 SalesOrdersStructurePG.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA SalesOrdersExample; 2 | 3 | SET search_path TO SalesOrdersExample; 4 | 5 | CREATE TABLE Categories ( 6 | CategoryID int NOT NULL DEFAULT 0 , 7 | CategoryDescription varchar (75) NULL 8 | ); 9 | 10 | CREATE TABLE Customers ( 11 | CustomerID int NOT NULL , 12 | CustFirstName varchar (25) NULL , 13 | CustLastName varchar (25) NULL , 14 | CustStreetAddress varchar (50) NULL , 15 | CustCity varchar (30) NULL , 16 | CustState varchar (2) NULL , 17 | CustZipCode varchar (10) NULL , 18 | CustAreaCode smallint NULL DEFAULT 0 , 19 | CustPhoneNumber varchar (8) NULL 20 | ); 21 | 22 | CREATE TABLE Employees ( 23 | EmployeeID int NOT NULL , 24 | EmpFirstName varchar (25) NULL , 25 | EmpLastName varchar (25) NULL , 26 | EmpStreetAddress varchar (50) NULL , 27 | EmpCity varchar (30) NULL , 28 | EmpState varchar (2) NULL , 29 | EmpZipCode varchar (10) NULL , 30 | EmpAreaCode smallint NULL DEFAULT 0 , 31 | EmpPhoneNumber varchar (8) NULL , 32 | EmpBirthDate date NULL 33 | ); 34 | 35 | CREATE TABLE Order_Details ( 36 | OrderNumber int NOT NULL DEFAULT 0 , 37 | ProductNumber int NOT NULL DEFAULT 0 , 38 | QuotedPrice decimal(19,4) NULL DEFAULT 0 , 39 | QuantityOrdered smallint NULL DEFAULT 0 40 | ); 41 | 42 | CREATE TABLE Orders ( 43 | OrderNumber int NOT NULL DEFAULT 0 , 44 | OrderDate date NULL , 45 | ShipDate date NULL , 46 | CustomerID int NULL DEFAULT 0 , 47 | EmployeeID int NULL DEFAULT 0 48 | ); 49 | 50 | CREATE TABLE Product_Vendors ( 51 | ProductNumber int NOT NULL DEFAULT 0 , 52 | VendorID int NOT NULL DEFAULT 0 , 53 | WholesalePrice decimal(19,4) NULL DEFAULT 0 , 54 | DaysToDeliver smallint NULL DEFAULT 0 55 | ); 56 | 57 | CREATE TABLE Products ( 58 | ProductNumber int NOT NULL DEFAULT 0 , 59 | ProductName varchar (50) NULL , 60 | ProductDescription varchar (100) NULL , 61 | RetailPrice decimal(19,4) NULL DEFAULT 0 , 62 | QuantityOnHand smallint NULL DEFAULT 0 , 63 | CategoryID int NULL DEFAULT 0 64 | ); 65 | 66 | CREATE TABLE Vendors ( 67 | VendorID int NOT NULL , 68 | VendName varchar (25) NULL , 69 | VendStreetAddress varchar (50) NULL , 70 | VendCity varchar (30) NULL , 71 | VendState varchar (2) NULL , 72 | VendZipCode varchar (10) NULL , 73 | VendPhoneNumber varchar (15) NULL , 74 | VendFaxNumber varchar (15) NULL , 75 | VendWebPage text NULL , 76 | VendEMailAddress varchar (50) NULL 77 | ); 78 | 79 | CREATE TABLE ztblMonths ( 80 | MonthYear varchar (15) NOT NULL , 81 | YearNumber smallint NOT NULL , 82 | MonthNumber smallint NOT NULL , 83 | MonthStart timestamp NOT NULL , 84 | MonthEnd timestamp NOT NULL , 85 | January smallint NOT NULL DEFAULT 0 , 86 | February smallint NOT NULL DEFAULT 0 , 87 | March smallint NOT NULL DEFAULT 0 , 88 | April smallint NOT NULL DEFAULT 0 , 89 | May smallint NOT NULL DEFAULT 0 , 90 | June smallint NOT NULL DEFAULT 0 , 91 | July smallint NOT NULL DEFAULT 0 , 92 | August smallint NOT NULL DEFAULT 0 , 93 | September smallint NOT NULL DEFAULT 0 , 94 | October smallint NOT NULL DEFAULT 0 , 95 | November smallint NOT NULL DEFAULT 0 , 96 | December smallint NOT NULL DEFAULT 0 97 | ); 98 | 99 | CREATE TABLE ztblPriceRanges ( 100 | PriceCategory varchar (20) NOT NULL , 101 | LowPrice decimal(15,2) NULL , 102 | HighPrice decimal(15,2) NULL 103 | ); 104 | 105 | CREATE TABLE ztblPurchaseCoupons ( 106 | LowSpend decimal(15,2) NOT NULL , 107 | HighSpend decimal(15,2) NULL , 108 | NumCoupons smallint NULL DEFAULT 0 109 | ); 110 | 111 | CREATE TABLE ztblSeqNumbers ( 112 | Sequence int NOT NULL DEFAULT 0 113 | ); 114 | 115 | ALTER TABLE Categories ADD 116 | CONSTRAINT Categories_PK PRIMARY KEY 117 | ( 118 | CategoryID 119 | ) 120 | ; 121 | 122 | ALTER TABLE Customers 123 | ADD CONSTRAINT Customers_PK PRIMARY KEY 124 | ( 125 | CustomerID 126 | ) 127 | ; 128 | 129 | CREATE INDEX Customers_CustAreaCode ON Customers(CustAreaCode); 130 | 131 | CREATE INDEX CustZipCode ON Customers(CustZipCode); 132 | 133 | ALTER TABLE Employees 134 | ADD CONSTRAINT Employees_PK PRIMARY KEY 135 | ( 136 | EmployeeID 137 | ) 138 | ; 139 | 140 | CREATE INDEX Employees_EmpAreaCode ON Employees(EmpAreaCode); 141 | 142 | CREATE INDEX Employees_EmpZipCode ON Employees(EmpZipCode); 143 | 144 | ALTER TABLE Order_Details 145 | ADD CONSTRAINT Order_Details_PK PRIMARY KEY 146 | ( 147 | OrderNumber, 148 | ProductNumber 149 | ) 150 | ; 151 | 152 | CREATE INDEX Order_Details_OrderNumber ON Order_Details(OrderNumber); 153 | 154 | CREATE INDEX Order_Details_ProductNumber ON Order_Details(ProductNumber); 155 | 156 | ALTER TABLE Orders 157 | ADD CONSTRAINT Orders_PK PRIMARY KEY 158 | ( 159 | OrderNumber 160 | ) 161 | ; 162 | 163 | CREATE INDEX Orders_CustomerID ON Orders(CustomerID); 164 | 165 | CREATE INDEX Orders_EmployeeID ON Orders(EmployeeID); 166 | 167 | ALTER TABLE Product_Vendors 168 | ADD CONSTRAINT Product_Vendors_PK PRIMARY KEY 169 | ( 170 | ProductNumber, 171 | VendorID 172 | ) 173 | ; 174 | 175 | CREATE INDEX Product_Vendors_ProductNumber ON Product_Vendors(ProductNumber); 176 | 177 | CREATE INDEX Product_Vendors_VendorID ON Product_Vendors(VendorID); 178 | 179 | ALTER TABLE Products 180 | ADD CONSTRAINT Products_PK PRIMARY KEY 181 | ( 182 | ProductNumber 183 | ) 184 | ; 185 | 186 | CREATE INDEX Products_CategoryID ON Products(CategoryID); 187 | 188 | ALTER TABLE Vendors 189 | ADD CONSTRAINT Vendors_PK PRIMARY KEY 190 | ( 191 | VendorID 192 | ) 193 | ; 194 | 195 | CREATE INDEX Vendors_VendZipCode ON Vendors(VendZipCode); 196 | 197 | ALTER TABLE ztblMonths 198 | ADD CONSTRAINT ztblMonths_PK PRIMARY KEY 199 | ( 200 | YearNumber, 201 | MonthNumber 202 | ) 203 | ; 204 | 205 | CREATE UNIQUE INDEX ztblMonths_Month_End ON ztblMonths(MonthEnd); 206 | 207 | CREATE UNIQUE INDEX ztblMonths_Month_Start ON ztblMonths(MonthStart); 208 | 209 | CREATE UNIQUE INDEX ztblMonths_Month_Year ON ztblMonths(MonthYear); 210 | 211 | ALTER TABLE ztblPriceRanges 212 | ADD CONSTRAINT ztblPriceRanges_PK PRIMARY KEY 213 | ( 214 | PriceCategory 215 | ) 216 | ; 217 | 218 | ALTER TABLE ztblPurchaseCoupons 219 | ADD CONSTRAINT ztblPurchaseCoupons_PK PRIMARY KEY 220 | ( 221 | LowSpend 222 | ) 223 | ; 224 | 225 | CREATE INDEX ztblPurchaseCoupons_Num_Coupons ON ztblPurchaseCoupons(NumCoupons); 226 | 227 | ALTER TABLE ztblSeqNumbers 228 | ADD CONSTRAINT ztblSeqNumbers_PK PRIMARY KEY 229 | ( 230 | Sequence 231 | ) 232 | ; 233 | 234 | ALTER TABLE Order_Details 235 | ADD CONSTRAINT Order_Details_FK00 FOREIGN KEY 236 | ( 237 | OrderNumber 238 | ) REFERENCES Orders ( 239 | OrderNumber 240 | ), 241 | ADD CONSTRAINT Order_Details_FK01 FOREIGN KEY 242 | ( 243 | ProductNumber 244 | ) REFERENCES Products ( 245 | ProductNumber 246 | ) 247 | ; 248 | 249 | ALTER TABLE Orders 250 | ADD CONSTRAINT Orders_FK00 FOREIGN KEY 251 | ( 252 | CustomerID 253 | ) REFERENCES Customers ( 254 | CustomerID 255 | ), 256 | ADD CONSTRAINT Orders_FK01 FOREIGN KEY 257 | ( 258 | EmployeeID 259 | ) REFERENCES Employees ( 260 | EmployeeID 261 | ) 262 | ; 263 | 264 | ALTER TABLE Product_Vendors 265 | ADD CONSTRAINT Product_Vendors_FK00 FOREIGN KEY 266 | ( 267 | ProductNumber 268 | ) REFERENCES Products ( 269 | ProductNumber 270 | ), 271 | ADD CONSTRAINT Product_Vendors_FK01 FOREIGN KEY 272 | ( 273 | VendorID 274 | ) REFERENCES Vendors ( 275 | VendorID 276 | ) 277 | ; 278 | 279 | ALTER TABLE Products 280 | ADD CONSTRAINT Products_FK00 FOREIGN KEY 281 | ( 282 | CategoryID 283 | ) REFERENCES Categories ( 284 | CategoryID 285 | ) 286 | ; 287 | -------------------------------------------------------------------------------- /Week 1/sql/ddl/00 SalesOrdersStructurePG.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA SalesOrdersExample; 2 | 3 | SET search_path TO SalesOrdersExample; 4 | 5 | CREATE TABLE Categories ( 6 | CategoryID int NOT NULL DEFAULT 0 , 7 | CategoryDescription varchar (75) NULL 8 | ); 9 | 10 | CREATE TABLE Customers ( 11 | CustomerID int NOT NULL , 12 | CustFirstName varchar (25) NULL , 13 | CustLastName varchar (25) NULL , 14 | CustStreetAddress varchar (50) NULL , 15 | CustCity varchar (30) NULL , 16 | CustState varchar (2) NULL , 17 | CustZipCode varchar (10) NULL , 18 | CustAreaCode smallint NULL DEFAULT 0 , 19 | CustPhoneNumber varchar (8) NULL 20 | ); 21 | 22 | CREATE TABLE Employees ( 23 | EmployeeID int NOT NULL , 24 | EmpFirstName varchar (25) NULL , 25 | EmpLastName varchar (25) NULL , 26 | EmpStreetAddress varchar (50) NULL , 27 | EmpCity varchar (30) NULL , 28 | EmpState varchar (2) NULL , 29 | EmpZipCode varchar (10) NULL , 30 | EmpAreaCode smallint NULL DEFAULT 0 , 31 | EmpPhoneNumber varchar (8) NULL , 32 | EmpBirthDate date NULL 33 | ); 34 | 35 | CREATE TABLE Order_Details ( 36 | OrderNumber int NOT NULL DEFAULT 0 , 37 | ProductNumber int NOT NULL DEFAULT 0 , 38 | QuotedPrice decimal(19,4) NULL DEFAULT 0 , 39 | QuantityOrdered smallint NULL DEFAULT 0 40 | ); 41 | 42 | CREATE TABLE Orders ( 43 | OrderNumber int NOT NULL DEFAULT 0 , 44 | OrderDate date NULL , 45 | ShipDate date NULL , 46 | CustomerID int NULL DEFAULT 0 , 47 | EmployeeID int NULL DEFAULT 0 48 | ); 49 | 50 | CREATE TABLE Product_Vendors ( 51 | ProductNumber int NOT NULL DEFAULT 0 , 52 | VendorID int NOT NULL DEFAULT 0 , 53 | WholesalePrice decimal(19,4) NULL DEFAULT 0 , 54 | DaysToDeliver smallint NULL DEFAULT 0 55 | ); 56 | 57 | CREATE TABLE Products ( 58 | ProductNumber int NOT NULL DEFAULT 0 , 59 | ProductName varchar (50) NULL , 60 | ProductDescription varchar (100) NULL , 61 | RetailPrice decimal(19,4) NULL DEFAULT 0 , 62 | QuantityOnHand smallint NULL DEFAULT 0 , 63 | CategoryID int NULL DEFAULT 0 64 | ); 65 | 66 | CREATE TABLE Vendors ( 67 | VendorID int NOT NULL , 68 | VendName varchar (25) NULL , 69 | VendStreetAddress varchar (50) NULL , 70 | VendCity varchar (30) NULL , 71 | VendState varchar (2) NULL , 72 | VendZipCode varchar (10) NULL , 73 | VendPhoneNumber varchar (15) NULL , 74 | VendFaxNumber varchar (15) NULL , 75 | VendWebPage text NULL , 76 | VendEMailAddress varchar (50) NULL 77 | ); 78 | 79 | CREATE TABLE ztblMonths ( 80 | MonthYear varchar (15) NOT NULL , 81 | YearNumber smallint NOT NULL , 82 | MonthNumber smallint NOT NULL , 83 | MonthStart timestamp NOT NULL , 84 | MonthEnd timestamp NOT NULL , 85 | January smallint NOT NULL DEFAULT 0 , 86 | February smallint NOT NULL DEFAULT 0 , 87 | March smallint NOT NULL DEFAULT 0 , 88 | April smallint NOT NULL DEFAULT 0 , 89 | May smallint NOT NULL DEFAULT 0 , 90 | June smallint NOT NULL DEFAULT 0 , 91 | July smallint NOT NULL DEFAULT 0 , 92 | August smallint NOT NULL DEFAULT 0 , 93 | September smallint NOT NULL DEFAULT 0 , 94 | October smallint NOT NULL DEFAULT 0 , 95 | November smallint NOT NULL DEFAULT 0 , 96 | December smallint NOT NULL DEFAULT 0 97 | ); 98 | 99 | CREATE TABLE ztblPriceRanges ( 100 | PriceCategory varchar (20) NOT NULL , 101 | LowPrice decimal(15,2) NULL , 102 | HighPrice decimal(15,2) NULL 103 | ); 104 | 105 | CREATE TABLE ztblPurchaseCoupons ( 106 | LowSpend decimal(15,2) NOT NULL , 107 | HighSpend decimal(15,2) NULL , 108 | NumCoupons smallint NULL DEFAULT 0 109 | ); 110 | 111 | CREATE TABLE ztblSeqNumbers ( 112 | Sequence int NOT NULL DEFAULT 0 113 | ); 114 | 115 | ALTER TABLE Categories ADD 116 | CONSTRAINT Categories_PK PRIMARY KEY 117 | ( 118 | CategoryID 119 | ) 120 | ; 121 | 122 | ALTER TABLE Customers 123 | ADD CONSTRAINT Customers_PK PRIMARY KEY 124 | ( 125 | CustomerID 126 | ) 127 | ; 128 | 129 | CREATE INDEX Customers_CustAreaCode ON Customers(CustAreaCode); 130 | 131 | CREATE INDEX CustZipCode ON Customers(CustZipCode); 132 | 133 | ALTER TABLE Employees 134 | ADD CONSTRAINT Employees_PK PRIMARY KEY 135 | ( 136 | EmployeeID 137 | ) 138 | ; 139 | 140 | CREATE INDEX Employees_EmpAreaCode ON Employees(EmpAreaCode); 141 | 142 | CREATE INDEX Employees_EmpZipCode ON Employees(EmpZipCode); 143 | 144 | ALTER TABLE Order_Details 145 | ADD CONSTRAINT Order_Details_PK PRIMARY KEY 146 | ( 147 | OrderNumber, 148 | ProductNumber 149 | ) 150 | ; 151 | 152 | CREATE INDEX Order_Details_OrderNumber ON Order_Details(OrderNumber); 153 | 154 | CREATE INDEX Order_Details_ProductNumber ON Order_Details(ProductNumber); 155 | 156 | ALTER TABLE Orders 157 | ADD CONSTRAINT Orders_PK PRIMARY KEY 158 | ( 159 | OrderNumber 160 | ) 161 | ; 162 | 163 | CREATE INDEX Orders_CustomerID ON Orders(CustomerID); 164 | 165 | CREATE INDEX Orders_EmployeeID ON Orders(EmployeeID); 166 | 167 | ALTER TABLE Product_Vendors 168 | ADD CONSTRAINT Product_Vendors_PK PRIMARY KEY 169 | ( 170 | ProductNumber, 171 | VendorID 172 | ) 173 | ; 174 | 175 | CREATE INDEX Product_Vendors_ProductNumber ON Product_Vendors(ProductNumber); 176 | 177 | CREATE INDEX Product_Vendors_VendorID ON Product_Vendors(VendorID); 178 | 179 | ALTER TABLE Products 180 | ADD CONSTRAINT Products_PK PRIMARY KEY 181 | ( 182 | ProductNumber 183 | ) 184 | ; 185 | 186 | CREATE INDEX Products_CategoryID ON Products(CategoryID); 187 | 188 | ALTER TABLE Vendors 189 | ADD CONSTRAINT Vendors_PK PRIMARY KEY 190 | ( 191 | VendorID 192 | ) 193 | ; 194 | 195 | CREATE INDEX Vendors_VendZipCode ON Vendors(VendZipCode); 196 | 197 | ALTER TABLE ztblMonths 198 | ADD CONSTRAINT ztblMonths_PK PRIMARY KEY 199 | ( 200 | YearNumber, 201 | MonthNumber 202 | ) 203 | ; 204 | 205 | CREATE UNIQUE INDEX ztblMonths_Month_End ON ztblMonths(MonthEnd); 206 | 207 | CREATE UNIQUE INDEX ztblMonths_Month_Start ON ztblMonths(MonthStart); 208 | 209 | CREATE UNIQUE INDEX ztblMonths_Month_Year ON ztblMonths(MonthYear); 210 | 211 | ALTER TABLE ztblPriceRanges 212 | ADD CONSTRAINT ztblPriceRanges_PK PRIMARY KEY 213 | ( 214 | PriceCategory 215 | ) 216 | ; 217 | 218 | ALTER TABLE ztblPurchaseCoupons 219 | ADD CONSTRAINT ztblPurchaseCoupons_PK PRIMARY KEY 220 | ( 221 | LowSpend 222 | ) 223 | ; 224 | 225 | CREATE INDEX ztblPurchaseCoupons_Num_Coupons ON ztblPurchaseCoupons(NumCoupons); 226 | 227 | ALTER TABLE ztblSeqNumbers 228 | ADD CONSTRAINT ztblSeqNumbers_PK PRIMARY KEY 229 | ( 230 | Sequence 231 | ) 232 | ; 233 | 234 | ALTER TABLE Order_Details 235 | ADD CONSTRAINT Order_Details_FK00 FOREIGN KEY 236 | ( 237 | OrderNumber 238 | ) REFERENCES Orders ( 239 | OrderNumber 240 | ), 241 | ADD CONSTRAINT Order_Details_FK01 FOREIGN KEY 242 | ( 243 | ProductNumber 244 | ) REFERENCES Products ( 245 | ProductNumber 246 | ) 247 | ; 248 | 249 | ALTER TABLE Orders 250 | ADD CONSTRAINT Orders_FK00 FOREIGN KEY 251 | ( 252 | CustomerID 253 | ) REFERENCES Customers ( 254 | CustomerID 255 | ), 256 | ADD CONSTRAINT Orders_FK01 FOREIGN KEY 257 | ( 258 | EmployeeID 259 | ) REFERENCES Employees ( 260 | EmployeeID 261 | ) 262 | ; 263 | 264 | ALTER TABLE Product_Vendors 265 | ADD CONSTRAINT Product_Vendors_FK00 FOREIGN KEY 266 | ( 267 | ProductNumber 268 | ) REFERENCES Products ( 269 | ProductNumber 270 | ), 271 | ADD CONSTRAINT Product_Vendors_FK01 FOREIGN KEY 272 | ( 273 | VendorID 274 | ) REFERENCES Vendors ( 275 | VendorID 276 | ) 277 | ; 278 | 279 | ALTER TABLE Products 280 | ADD CONSTRAINT Products_FK00 FOREIGN KEY 281 | ( 282 | CategoryID 283 | ) REFERENCES Categories ( 284 | CategoryID 285 | ) 286 | ; 287 | -------------------------------------------------------------------------------- /Week 1/sql/ddl/02 SchoolSchedulingModifyProcsPG.sql: -------------------------------------------------------------------------------- 1 | -- This requires that the tables from 00 SchoolSchedulingStructureModifyPG.sql be created first. 2 | 3 | SET search_path TO SchoolSchedulingModify; 4 | 5 | CREATE FUNCTION CH15_Fix_Staff_AreaCode() RETURNS void AS $$ 6 | UPDATE Staff 7 | SET StfAreaCode = '360' 8 | WHERE (StfZipCode IN ('98270', '98271')); 9 | $$ LANGUAGE SQL; 10 | 11 | CREATE VIEW CH15_Fix_Staff_AreaCode_Query 12 | AS 13 | SELECT StfAreaCode, '360' As NewAreaCode 14 | FROM Staff 15 | WHERE (StfZipCode IN ('98270', '98271')); 16 | 17 | 18 | CREATE FUNCTION CH15_Fix_Student_AreaCode() RETURNS void AS $$ 19 | UPDATE Students 20 | SET StudAreaCode = '360' 21 | WHERE (StudZipCode IN ('98270', '98271')); 22 | $$ LANGUAGE SQL; 23 | 24 | CREATE VIEW CH15_Fix_Student_AreaCode_Query 25 | AS 26 | SELECT StudentID, StudFirstName, StudLastName, StudAreaCode, '360' AS NewAreaCode 27 | FROM Students 28 | WHERE (StudZipCode IN ('98270', '98271')); 29 | 30 | 31 | CREATE FUNCTION CH15_Give_FullTime_Tenured_Raise() RETURNS void AS $$ 32 | UPDATE Staff 33 | SET Salary = ROUND(Salary * 1.05, 0) 34 | WHERE (StaffID IN 35 | (SELECT StaffID 36 | FROM Faculty 37 | WHERE Faculty.Status = 'Full Time' AND Faculty.Tenured = 1)); 38 | $$ LANGUAGE SQL; 39 | 40 | CREATE VIEW CH15_Give_FullTime_Tenured_Raise_Query 41 | AS 42 | SELECT Salary, ROUND(Salary * 1.05, 0) AS NewSalary 43 | FROM Staff 44 | WHERE (StaffID IN 45 | (SELECT StaffID 46 | FROM Faculty 47 | WHERE Faculty.Status = 'Full Time' AND Faculty.Tenured = 1)); 48 | 49 | 50 | CREATE FUNCTION CH15_Update_Student_GPA() RETURNS void AS $$ 51 | UPDATE Students 52 | SET StudGPA = 53 | COALESCE((SELECT Round(SUM(Classes.Credits * 54 | CAST(Student_Schedules.Grade AS decimal(15,4))) / 55 | SUM(Classes.Credits), 3) 56 | FROM Classes INNER JOIN Student_Schedules 57 | ON Classes.ClassID = Student_Schedules.ClassID 58 | WHERE (Student_Schedules.ClassStatus = 2) AND 59 | (Student_Schedules.StudentID = Students.StudentID)), 0); 60 | $$ LANGUAGE SQL; 61 | 62 | CREATE VIEW CH15_Update_Student_GPA_Query 63 | AS 64 | SELECT StudentID, StudFirstName, StudLastName, StudGPA, 65 | COALESCE((SELECT Round(SUM(Classes.Credits * 66 | CAST(Student_Schedules.Grade AS decimal(15,4))) / 67 | SUM(Classes.Credits), 3) 68 | FROM Classes INNER JOIN Student_Schedules 69 | ON Classes.ClassID = Student_Schedules.ClassID 70 | WHERE (Student_Schedules.ClassStatus = 2) AND 71 | (Student_Schedules.StudentID = Students.StudentID)), 0) AS NewStudGPA 72 | FROM Students; 73 | 74 | 75 | CREATE FUNCTION CH16_Add_Category() RETURNS void AS $$ 76 | INSERT INTO Categories 77 | SELECT 'ITA' AS CategoryID, 'Italian' AS CategoryDescription, DepartmentID 78 | FROM Departments 79 | WHERE (DeptName = 'Humanities'); 80 | $$ LANGUAGE SQL; 81 | 82 | CREATE VIEW CH16_Add_Category_Query 83 | AS 84 | SELECT 'ITA' AS CategoryID, 'Italian' AS CategoryDescription, DepartmentID 85 | FROM Departments 86 | WHERE (DeptName = 'Humanities'); 87 | 88 | 89 | CREATE FUNCTION CH16_Add_New_Accounting_Class() RETURNS void AS $$ 90 | INSERT INTO Classes (SubjectID, ClassRoomID, Credits, 91 | StartDate, StartTime, Duration, TuesdaySchedule, ThursdaySchedule) 92 | VALUES (4, 3315, 5, '2018-01-16', '15:00:00', 80, 1, 1); 93 | $$ LANGUAGE SQL; 94 | 95 | CREATE VIEW CH16_Add_New_Accounting_Class_Query 96 | AS 97 | SELECT 4 As SubjectID, 3315 As ClassRoomID, 5 As Credits, '2018-01-16' As StartDate, 98 | '15:00:00' As StartTime, 80 As Duration, -1 As TuesdaySchedule, -1 As ThursdaySchedule 99 | FROM Classes 100 | WHERE ClassID = 1000; 101 | 102 | 103 | CREATE FUNCTION CH16_Add_Student() RETURNS void AS $$ 104 | INSERT INTO Students (StudFirstName, StudLastName, StudStreetAddress, StudCity, StudState, 105 | StudZipCode, StudAreaCode, StudPhoneNumber) 106 | SELECT 'Angel' AS StudFirstName, StudLastName, StudStreetAddress, StudCity, StudState, 107 | StudZipCode, StudAreaCode, StudPhoneNumber 108 | FROM Students 109 | WHERE (StudFirstName = 'John') AND (StudLastName = 'Kennedy'); 110 | $$ LANGUAGE SQL; 111 | 112 | CREATE VIEW CH16_Add_Student_Query 113 | AS 114 | SELECT 'Angel' AS StudFirstName, StudLastName, StudStreetAddress, StudCity, StudState, 115 | StudZipCode, StudAreaCode, StudPhoneNumber 116 | FROM Students 117 | WHERE (StudFirstName = 'John') AND (StudLastName = 'Kennedy'); 118 | 119 | 120 | CREATE FUNCTION CH16_Enroll_Staff() RETURNS void AS $$ 121 | INSERT INTO Students (StudFirstName, StudLastName, StudStreetAddress, StudCity, StudState, 122 | StudZipCode, StudAreaCode, StudPhoneNumber) 123 | SELECT StfFirstName, StfLastname, StfStreetAddress, StfCity, StfState, 124 | StfZipCode, StfAreaCode, StfPhoneNumber 125 | FROM Staff 126 | WHERE (StfFirstName = 'Tim') AND (StfLastname = 'Smith'); 127 | $$ LANGUAGE SQL; 128 | 129 | CREATE VIEW CH16_Enroll_Staff_Query 130 | AS 131 | SELECT StfFirstName, StfLastname, StfStreetAddress, StfCity, StfState, 132 | StfZipCode, StfAreaCode, StfPhoneNumber 133 | FROM Staff 134 | WHERE (StfFirstName = 'Tim') AND (StfLastname = 'Smith'); 135 | 136 | 137 | CREATE FUNCTION CH17_Delete_Classes_No_Students_1() RETURNS void AS $$ 138 | DELETE FROM Faculty_Classes 139 | WHERE (ClassID NOT IN 140 | (SELECT ClassID 141 | FROM Student_Schedules)); 142 | $$ LANGUAGE SQL; 143 | 144 | CREATE VIEW CH17_Delete_Classes_No_Students_1_Query 145 | AS 146 | SELECT * 147 | FROM Faculty_Classes 148 | WHERE (ClassID NOT IN 149 | (SELECT ClassID 150 | FROM Student_Schedules)); 151 | 152 | 153 | CREATE FUNCTION CH17_Delete_Classes_No_Students_2() RETURNS void AS $$ 154 | DELETE FROM Classes 155 | WHERE (ClassID NOT IN 156 | (SELECT ClassID 157 | FROM Student_Schedules)); 158 | $$ LANGUAGE SQL; 159 | 160 | CREATE VIEW CH17_Delete_Classes_No_Students_2_Query 161 | AS 162 | SELECT * 163 | FROM Classes 164 | WHERE (ClassID NOT IN 165 | (SELECT ClassID 166 | FROM Student_Schedules)); 167 | 168 | 169 | CREATE FUNCTION CH17_Delete_Students_No_Classes() RETURNS void AS $$ 170 | DELETE FROM Students 171 | WHERE (StudentID NOT IN 172 | (SELECT StudentID 173 | FROM Student_Schedules)); 174 | $$ LANGUAGE SQL; 175 | 176 | CREATE VIEW CH17_Delete_Students_No_Classes_Query 177 | AS 178 | SELECT * 179 | FROM Students 180 | WHERE (StudentID NOT IN 181 | (SELECT StudentID 182 | FROM Student_Schedules)); 183 | 184 | 185 | CREATE FUNCTION CH17_Delete_Subjects_No_Classes_1() RETURNS void AS $$ 186 | DELETE FROM Faculty_Subjects 187 | WHERE (SubjectID NOT IN 188 | (SELECT SubjectID 189 | FROM Classes)); 190 | $$ LANGUAGE SQL; 191 | 192 | CREATE VIEW CH17_Delete_Subjects_No_Classes_1_Query 193 | AS 194 | SELECT * 195 | FROM Faculty_Subjects 196 | WHERE (SubjectID NOT IN 197 | (SELECT SubjectID 198 | FROM Classes)); 199 | 200 | 201 | CREATE FUNCTION CH17_Delete_Subjects_No_Classes_2() RETURNS void AS $$ 202 | DELETE FROM Subjects 203 | WHERE (SubjectID NOT IN 204 | (SELECT SubjectID 205 | FROM Classes)); 206 | $$ LANGUAGE SQL; 207 | 208 | CREATE VIEW CH17_Delete_Subjects_No_Classes_2_Query 209 | AS 210 | SELECT * 211 | FROM Subjects 212 | WHERE (SubjectID NOT IN 213 | (SELECT SubjectID 214 | FROM Classes)); 215 | -------------------------------------------------------------------------------- /Week 2/sql/00 EntertainmentAgencyStructurePG.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA EntertainmentAgencyExample; 2 | 3 | SET search_path TO EntertainmentAgencyExample; 4 | 5 | CREATE TABLE Agents ( 6 | AgentID serial NOT NULL , 7 | AgtFirstName varchar (25) NULL , 8 | AgtLastName varchar (25) NULL , 9 | AgtStreetAddress varchar (50) NULL , 10 | AgtCity varchar (30) NULL , 11 | AgtState varchar (2) NULL , 12 | AgtZipCode varchar (10) NULL , 13 | AgtPhoneNumber varchar (15) NULL , 14 | DateHired date NULL , 15 | Salary decimal(15,2) NULL DEFAULT 0 , 16 | CommissionRate float(24) NULL DEFAULT 0 17 | ); 18 | 19 | CREATE TABLE Customers ( 20 | CustomerID serial NOT NULL , 21 | CustFirstName varchar (25) NULL , 22 | CustLastName varchar (25) NULL , 23 | CustStreetAddress varchar (50) NULL , 24 | CustCity varchar (30) NULL , 25 | CustState varchar (2) NULL , 26 | CustZipCode varchar (10) NULL , 27 | CustPhoneNumber varchar (15) NULL 28 | ); 29 | 30 | CREATE TABLE Engagements ( 31 | EngagementNumber serial NOT NULL , 32 | StartDate date NULL , 33 | EndDate date NULL , 34 | StartTime time NULL , 35 | StopTime time NULL , 36 | ContractPrice decimal(15,2) NULL DEFAULT 0 , 37 | CustomerID int NULL DEFAULT 0 , 38 | AgentID int NULL DEFAULT 0 , 39 | EntertainerID int NULL DEFAULT 0 40 | ); 41 | 42 | CREATE TABLE Entertainer_Members ( 43 | EntertainerID int NOT NULL , 44 | MemberID int NOT NULL DEFAULT 0 , 45 | Status smallint NULL DEFAULT 0 46 | ); 47 | 48 | CREATE TABLE Entertainer_Styles ( 49 | EntertainerID int NOT NULL , 50 | StyleID int NOT NULL , 51 | StyleStrength smallint NOT NULL 52 | ); 53 | 54 | CREATE TABLE Entertainers ( 55 | EntertainerID serial NOT NULL , 56 | EntStageName varchar (50) NULL , 57 | EntSSN varchar (12) NULL , 58 | EntStreetAddress varchar (50) NULL , 59 | EntCity varchar (30) NULL , 60 | EntState varchar (2) NULL , 61 | EntZipCode varchar (10) NULL , 62 | EntPhoneNumber varchar (15) NULL , 63 | EntWebPage varchar (50) NULL , 64 | EntEMailAddress varchar (50) NULL , 65 | DateEntered date NULL 66 | ); 67 | 68 | CREATE TABLE Members ( 69 | MemberID serial NOT NULL , 70 | MbrFirstName varchar (25) NULL , 71 | MbrLastName varchar (25) NULL , 72 | MbrPhoneNumber varchar (15) NULL , 73 | Gender varchar (2) NULL 74 | ); 75 | 76 | CREATE TABLE Musical_Preferences ( 77 | CustomerID int NOT NULL , 78 | StyleID int NOT NULL , 79 | PreferenceSeq smallint NOT NULL DEFAULT 0 80 | ); 81 | 82 | CREATE TABLE Musical_Styles ( 83 | StyleID serial NOT NULL , 84 | StyleName varchar (75) NULL 85 | ); 86 | 87 | CREATE TABLE ztblDays ( 88 | DateField date NOT NULL 89 | ); 90 | 91 | CREATE TABLE ztblMonths ( 92 | MonthYear varchar (15) NULL , 93 | YearNumber smallint NOT NULL , 94 | MonthNumber smallint NOT NULL , 95 | MonthStart date NULL , 96 | MonthEnd date NULL , 97 | January smallint NULL DEFAULT 0 , 98 | February smallint NULL DEFAULT 0 , 99 | March smallint NULL DEFAULT 0 , 100 | April smallint NULL DEFAULT 0 , 101 | May smallint NULL DEFAULT 0 , 102 | June smallint NULL DEFAULT 0 , 103 | July smallint NULL DEFAULT 0 , 104 | August smallint NULL DEFAULT 0 , 105 | September smallint NULL DEFAULT 0 , 106 | October smallint NULL DEFAULT 0 , 107 | November smallint NULL DEFAULT 0 , 108 | December smallint NULL DEFAULT 0 109 | ); 110 | 111 | CREATE TABLE ztblSkipLabels ( 112 | LabelCount int NOT NULL 113 | ); 114 | 115 | CREATE TABLE ztblWeeks ( 116 | WeekStart date NOT NULL , 117 | WeekEnd date NULL 118 | ); 119 | 120 | ALTER TABLE Agents 121 | ADD CONSTRAINT Agents_PK PRIMARY KEY 122 | ( 123 | AgentID 124 | ) 125 | ; 126 | 127 | CREATE INDEX Agents_AgtZipCode ON Agents(AgtZipCode); 128 | 129 | ALTER TABLE Customers 130 | ADD CONSTRAINT Customers_PK PRIMARY KEY 131 | ( 132 | CustomerID 133 | ) 134 | ; 135 | 136 | CREATE INDEX Customers_CustZipCode ON Customers(CustZipCode); 137 | 138 | ALTER TABLE Engagements 139 | ADD CONSTRAINT Engagements_PK PRIMARY KEY 140 | ( 141 | EngagementNumber 142 | ) 143 | ; 144 | 145 | CREATE INDEX Engagements_AgentsEngagements ON Engagements(AgentID); 146 | 147 | CREATE INDEX Engagements_CustomerID ON Engagements(CustomerID); 148 | 149 | CREATE INDEX Engagements_EmployeeID ON Engagements(AgentID); 150 | 151 | CREATE INDEX Engagements_EntertainerID ON Engagements(EntertainerID); 152 | 153 | ALTER TABLE Entertainer_Members 154 | ADD CONSTRAINT Entertainer_Members_PK PRIMARY KEY 155 | ( 156 | EntertainerID, 157 | MemberID 158 | ) 159 | ; 160 | 161 | CREATE INDEX EntertainersEntertainerMembers ON Entertainer_Members(EntertainerID); 162 | 163 | CREATE INDEX MembersEntertainerMembers ON Entertainer_Members(MemberID); 164 | 165 | ALTER TABLE Entertainer_Styles 166 | ADD CONSTRAINT Entertainer_Styles_PK PRIMARY KEY 167 | ( 168 | EntertainerID, 169 | StyleID 170 | ) 171 | ; 172 | 173 | CREATE INDEX Entertainer_Styles_EntertainersEntertainerStyles ON Entertainer_Styles(EntertainerID); 174 | 175 | CREATE INDEX Entertainer_Styles_MusicalStylesEntStyles ON Entertainer_Styles(StyleID); 176 | 177 | ALTER TABLE Entertainers 178 | ADD CONSTRAINT Entertainers_PK PRIMARY KEY 179 | ( 180 | EntertainerID 181 | ) 182 | ; 183 | 184 | CREATE UNIQUE INDEX Entertainers_EntertainerID ON Entertainers(EntertainerID); 185 | 186 | CREATE INDEX EntZipCode ON Entertainers(EntZipCode); 187 | 188 | ALTER TABLE Members ADD 189 | CONSTRAINT Members_PK PRIMARY KEY 190 | ( 191 | MemberID 192 | ) 193 | ; 194 | 195 | CREATE INDEX MemberID ON Members(MemberID); 196 | 197 | ALTER TABLE Musical_Preferences 198 | ADD CONSTRAINT Musical_Preferences_PK PRIMARY KEY 199 | ( 200 | CustomerID, 201 | StyleID 202 | ) 203 | ; 204 | 205 | CREATE INDEX Musical_Preferences_CustomerID ON Musical_Preferences(CustomerID); 206 | 207 | CREATE INDEX Musical_Preferences_StyleID ON Musical_Preferences(StyleID); 208 | 209 | ALTER TABLE Musical_Styles 210 | ADD CONSTRAINT Musical_Styles_PK PRIMARY KEY 211 | ( 212 | StyleID 213 | ) 214 | ; 215 | 216 | ALTER TABLE ztblDays 217 | ADD CONSTRAINT ztblDays_PK PRIMARY KEY 218 | ( 219 | DateField 220 | ) 221 | ; 222 | 223 | ALTER TABLE ztblMonths 224 | ADD CONSTRAINT ztblMonths_PK PRIMARY KEY 225 | ( 226 | YearNumber, 227 | MonthNumber 228 | ) 229 | ; 230 | 231 | CREATE UNIQUE INDEX ztblMontths_MonthEnd ON ztblMonths(MonthEnd); 232 | 233 | CREATE UNIQUE INDEX ztblMonths_MonthStart ON ztblMonths(MonthStart); 234 | 235 | CREATE UNIQUE INDEX ztblMonths_MonthYear ON ztblMonths(MonthYear); 236 | 237 | ALTER TABLE ztblSkipLabels 238 | ADD CONSTRAINT ztblSkipLabels_PK PRIMARY KEY 239 | ( 240 | LabelCount 241 | ) 242 | ; 243 | 244 | ALTER TABLE ztblWeeks 245 | ADD CONSTRAINT ztblWeeks_PK PRIMARY KEY 246 | ( 247 | WeekStart 248 | ) 249 | ; 250 | 251 | ALTER TABLE Engagements 252 | ADD CONSTRAINT Engagements_FK00 FOREIGN KEY 253 | ( 254 | AgentID 255 | ) REFERENCES Agents ( 256 | AgentID 257 | ), 258 | ADD CONSTRAINT Engagements_FK01 FOREIGN KEY 259 | ( 260 | CustomerID 261 | ) REFERENCES Customers ( 262 | CustomerID 263 | ), 264 | ADD CONSTRAINT Engagements_FK02 FOREIGN KEY 265 | ( 266 | EntertainerID 267 | ) REFERENCES Entertainers ( 268 | EntertainerID 269 | ) 270 | ; 271 | 272 | ALTER TABLE Entertainer_Members 273 | ADD CONSTRAINT Entertainer_Members_FK00 FOREIGN KEY 274 | ( 275 | EntertainerID 276 | ) REFERENCES Entertainers ( 277 | EntertainerID 278 | ), 279 | ADD CONSTRAINT Entertainer_Members_FK01 FOREIGN KEY 280 | ( 281 | MemberID 282 | ) REFERENCES Members ( 283 | MemberID 284 | ) 285 | ; 286 | 287 | ALTER TABLE Entertainer_Styles 288 | ADD CONSTRAINT Entertainer_Styles_FK00 FOREIGN KEY 289 | ( 290 | EntertainerID 291 | ) REFERENCES Entertainers ( 292 | EntertainerID 293 | ) ON DELETE CASCADE, 294 | ADD CONSTRAINT Entertainer_Styles_FK01 FOREIGN KEY 295 | ( 296 | StyleID 297 | ) REFERENCES Musical_Styles ( 298 | StyleID 299 | ) 300 | ; 301 | 302 | ALTER TABLE Musical_Preferences 303 | ADD CONSTRAINT Musical_Preferences_FK00 FOREIGN KEY 304 | ( 305 | CustomerID 306 | ) REFERENCES Customers ( 307 | CustomerID 308 | ) ON DELETE CASCADE, 309 | ADD CONSTRAINT Musical_Preferences_FK01 FOREIGN KEY 310 | ( 311 | StyleID 312 | ) REFERENCES Musical_Styles ( 313 | StyleID 314 | ) 315 | ; 316 | 317 | -------------------------------------------------------------------------------- /Week 1/sql/ddl/00 EntertainmentAgencyStructurePG.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA EntertainmentAgencyExample; 2 | 3 | SET search_path TO EntertainmentAgencyExample; 4 | 5 | CREATE TABLE Agents ( 6 | AgentID serial NOT NULL , 7 | AgtFirstName varchar (25) NULL , 8 | AgtLastName varchar (25) NULL , 9 | AgtStreetAddress varchar (50) NULL , 10 | AgtCity varchar (30) NULL , 11 | AgtState varchar (2) NULL , 12 | AgtZipCode varchar (10) NULL , 13 | AgtPhoneNumber varchar (15) NULL , 14 | DateHired date NULL , 15 | Salary decimal(15,2) NULL DEFAULT 0 , 16 | CommissionRate float(24) NULL DEFAULT 0 17 | ); 18 | 19 | CREATE TABLE Customers ( 20 | CustomerID serial NOT NULL , 21 | CustFirstName varchar (25) NULL , 22 | CustLastName varchar (25) NULL , 23 | CustStreetAddress varchar (50) NULL , 24 | CustCity varchar (30) NULL , 25 | CustState varchar (2) NULL , 26 | CustZipCode varchar (10) NULL , 27 | CustPhoneNumber varchar (15) NULL 28 | ); 29 | 30 | CREATE TABLE Engagements ( 31 | EngagementNumber serial NOT NULL , 32 | StartDate date NULL , 33 | EndDate date NULL , 34 | StartTime time NULL , 35 | StopTime time NULL , 36 | ContractPrice decimal(15,2) NULL DEFAULT 0 , 37 | CustomerID int NULL DEFAULT 0 , 38 | AgentID int NULL DEFAULT 0 , 39 | EntertainerID int NULL DEFAULT 0 40 | ); 41 | 42 | CREATE TABLE Entertainer_Members ( 43 | EntertainerID int NOT NULL , 44 | MemberID int NOT NULL DEFAULT 0 , 45 | Status smallint NULL DEFAULT 0 46 | ); 47 | 48 | CREATE TABLE Entertainer_Styles ( 49 | EntertainerID int NOT NULL , 50 | StyleID int NOT NULL , 51 | StyleStrength smallint NOT NULL 52 | ); 53 | 54 | CREATE TABLE Entertainers ( 55 | EntertainerID serial NOT NULL , 56 | EntStageName varchar (50) NULL , 57 | EntSSN varchar (12) NULL , 58 | EntStreetAddress varchar (50) NULL , 59 | EntCity varchar (30) NULL , 60 | EntState varchar (2) NULL , 61 | EntZipCode varchar (10) NULL , 62 | EntPhoneNumber varchar (15) NULL , 63 | EntWebPage varchar (50) NULL , 64 | EntEMailAddress varchar (50) NULL , 65 | DateEntered date NULL 66 | ); 67 | 68 | CREATE TABLE Members ( 69 | MemberID serial NOT NULL , 70 | MbrFirstName varchar (25) NULL , 71 | MbrLastName varchar (25) NULL , 72 | MbrPhoneNumber varchar (15) NULL , 73 | Gender varchar (2) NULL 74 | ); 75 | 76 | CREATE TABLE Musical_Preferences ( 77 | CustomerID int NOT NULL , 78 | StyleID int NOT NULL , 79 | PreferenceSeq smallint NOT NULL DEFAULT 0 80 | ); 81 | 82 | CREATE TABLE Musical_Styles ( 83 | StyleID serial NOT NULL , 84 | StyleName varchar (75) NULL 85 | ); 86 | 87 | CREATE TABLE ztblDays ( 88 | DateField date NOT NULL 89 | ); 90 | 91 | CREATE TABLE ztblMonths ( 92 | MonthYear varchar (15) NULL , 93 | YearNumber smallint NOT NULL , 94 | MonthNumber smallint NOT NULL , 95 | MonthStart date NULL , 96 | MonthEnd date NULL , 97 | January smallint NULL DEFAULT 0 , 98 | February smallint NULL DEFAULT 0 , 99 | March smallint NULL DEFAULT 0 , 100 | April smallint NULL DEFAULT 0 , 101 | May smallint NULL DEFAULT 0 , 102 | June smallint NULL DEFAULT 0 , 103 | July smallint NULL DEFAULT 0 , 104 | August smallint NULL DEFAULT 0 , 105 | September smallint NULL DEFAULT 0 , 106 | October smallint NULL DEFAULT 0 , 107 | November smallint NULL DEFAULT 0 , 108 | December smallint NULL DEFAULT 0 109 | ); 110 | 111 | CREATE TABLE ztblSkipLabels ( 112 | LabelCount int NOT NULL 113 | ); 114 | 115 | CREATE TABLE ztblWeeks ( 116 | WeekStart date NOT NULL , 117 | WeekEnd date NULL 118 | ); 119 | 120 | ALTER TABLE Agents 121 | ADD CONSTRAINT Agents_PK PRIMARY KEY 122 | ( 123 | AgentID 124 | ) 125 | ; 126 | 127 | CREATE INDEX Agents_AgtZipCode ON Agents(AgtZipCode); 128 | 129 | ALTER TABLE Customers 130 | ADD CONSTRAINT Customers_PK PRIMARY KEY 131 | ( 132 | CustomerID 133 | ) 134 | ; 135 | 136 | CREATE INDEX Customers_CustZipCode ON Customers(CustZipCode); 137 | 138 | ALTER TABLE Engagements 139 | ADD CONSTRAINT Engagements_PK PRIMARY KEY 140 | ( 141 | EngagementNumber 142 | ) 143 | ; 144 | 145 | CREATE INDEX Engagements_AgentsEngagements ON Engagements(AgentID); 146 | 147 | CREATE INDEX Engagements_CustomerID ON Engagements(CustomerID); 148 | 149 | CREATE INDEX Engagements_EmployeeID ON Engagements(AgentID); 150 | 151 | CREATE INDEX Engagements_EntertainerID ON Engagements(EntertainerID); 152 | 153 | ALTER TABLE Entertainer_Members 154 | ADD CONSTRAINT Entertainer_Members_PK PRIMARY KEY 155 | ( 156 | EntertainerID, 157 | MemberID 158 | ) 159 | ; 160 | 161 | CREATE INDEX EntertainersEntertainerMembers ON Entertainer_Members(EntertainerID); 162 | 163 | CREATE INDEX MembersEntertainerMembers ON Entertainer_Members(MemberID); 164 | 165 | ALTER TABLE Entertainer_Styles 166 | ADD CONSTRAINT Entertainer_Styles_PK PRIMARY KEY 167 | ( 168 | EntertainerID, 169 | StyleID 170 | ) 171 | ; 172 | 173 | CREATE INDEX Entertainer_Styles_EntertainersEntertainerStyles ON Entertainer_Styles(EntertainerID); 174 | 175 | CREATE INDEX Entertainer_Styles_MusicalStylesEntStyles ON Entertainer_Styles(StyleID); 176 | 177 | ALTER TABLE Entertainers 178 | ADD CONSTRAINT Entertainers_PK PRIMARY KEY 179 | ( 180 | EntertainerID 181 | ) 182 | ; 183 | 184 | CREATE UNIQUE INDEX Entertainers_EntertainerID ON Entertainers(EntertainerID); 185 | 186 | CREATE INDEX EntZipCode ON Entertainers(EntZipCode); 187 | 188 | ALTER TABLE Members ADD 189 | CONSTRAINT Members_PK PRIMARY KEY 190 | ( 191 | MemberID 192 | ) 193 | ; 194 | 195 | CREATE INDEX MemberID ON Members(MemberID); 196 | 197 | ALTER TABLE Musical_Preferences 198 | ADD CONSTRAINT Musical_Preferences_PK PRIMARY KEY 199 | ( 200 | CustomerID, 201 | StyleID 202 | ) 203 | ; 204 | 205 | CREATE INDEX Musical_Preferences_CustomerID ON Musical_Preferences(CustomerID); 206 | 207 | CREATE INDEX Musical_Preferences_StyleID ON Musical_Preferences(StyleID); 208 | 209 | ALTER TABLE Musical_Styles 210 | ADD CONSTRAINT Musical_Styles_PK PRIMARY KEY 211 | ( 212 | StyleID 213 | ) 214 | ; 215 | 216 | ALTER TABLE ztblDays 217 | ADD CONSTRAINT ztblDays_PK PRIMARY KEY 218 | ( 219 | DateField 220 | ) 221 | ; 222 | 223 | ALTER TABLE ztblMonths 224 | ADD CONSTRAINT ztblMonths_PK PRIMARY KEY 225 | ( 226 | YearNumber, 227 | MonthNumber 228 | ) 229 | ; 230 | 231 | CREATE UNIQUE INDEX ztblMontths_MonthEnd ON ztblMonths(MonthEnd); 232 | 233 | CREATE UNIQUE INDEX ztblMonths_MonthStart ON ztblMonths(MonthStart); 234 | 235 | CREATE UNIQUE INDEX ztblMonths_MonthYear ON ztblMonths(MonthYear); 236 | 237 | ALTER TABLE ztblSkipLabels 238 | ADD CONSTRAINT ztblSkipLabels_PK PRIMARY KEY 239 | ( 240 | LabelCount 241 | ) 242 | ; 243 | 244 | ALTER TABLE ztblWeeks 245 | ADD CONSTRAINT ztblWeeks_PK PRIMARY KEY 246 | ( 247 | WeekStart 248 | ) 249 | ; 250 | 251 | ALTER TABLE Engagements 252 | ADD CONSTRAINT Engagements_FK00 FOREIGN KEY 253 | ( 254 | AgentID 255 | ) REFERENCES Agents ( 256 | AgentID 257 | ), 258 | ADD CONSTRAINT Engagements_FK01 FOREIGN KEY 259 | ( 260 | CustomerID 261 | ) REFERENCES Customers ( 262 | CustomerID 263 | ), 264 | ADD CONSTRAINT Engagements_FK02 FOREIGN KEY 265 | ( 266 | EntertainerID 267 | ) REFERENCES Entertainers ( 268 | EntertainerID 269 | ) 270 | ; 271 | 272 | ALTER TABLE Entertainer_Members 273 | ADD CONSTRAINT Entertainer_Members_FK00 FOREIGN KEY 274 | ( 275 | EntertainerID 276 | ) REFERENCES Entertainers ( 277 | EntertainerID 278 | ), 279 | ADD CONSTRAINT Entertainer_Members_FK01 FOREIGN KEY 280 | ( 281 | MemberID 282 | ) REFERENCES Members ( 283 | MemberID 284 | ) 285 | ; 286 | 287 | ALTER TABLE Entertainer_Styles 288 | ADD CONSTRAINT Entertainer_Styles_FK00 FOREIGN KEY 289 | ( 290 | EntertainerID 291 | ) REFERENCES Entertainers ( 292 | EntertainerID 293 | ) ON DELETE CASCADE, 294 | ADD CONSTRAINT Entertainer_Styles_FK01 FOREIGN KEY 295 | ( 296 | StyleID 297 | ) REFERENCES Musical_Styles ( 298 | StyleID 299 | ) 300 | ; 301 | 302 | ALTER TABLE Musical_Preferences 303 | ADD CONSTRAINT Musical_Preferences_FK00 FOREIGN KEY 304 | ( 305 | CustomerID 306 | ) REFERENCES Customers ( 307 | CustomerID 308 | ) ON DELETE CASCADE, 309 | ADD CONSTRAINT Musical_Preferences_FK01 FOREIGN KEY 310 | ( 311 | StyleID 312 | ) REFERENCES Musical_Styles ( 313 | StyleID 314 | ) 315 | ; 316 | 317 | -------------------------------------------------------------------------------- /Week 1/sql/ddl/00 SchoolSchedulingStructureModifyPG.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA SchoolSchedulingModify; 2 | 3 | SET search_path TO SchoolSchedulingModify; 4 | 5 | CREATE TABLE Buildings ( 6 | BuildingCode varchar (3) NOT NULL , 7 | BuildingName varchar (25) NULL , 8 | NumberOfFloors smallint NULL , 9 | ElevatorAccess int NOT NULL DEFAULT 0 , 10 | SiteParkingAvailable int NOT NULL DEFAULT 0 11 | ); 12 | 13 | CREATE TABLE Categories ( 14 | CategoryID varchar (10) NOT NULL , 15 | CategoryDescription varchar (75) NULL , 16 | DepartmentID int NULL DEFAULT 0 17 | ); 18 | 19 | CREATE TABLE Class_Rooms ( 20 | ClassRoomID serial PRIMARY KEY , 21 | BuildingCode varchar (3) NULL , 22 | PhoneAvailable int NOT NULL DEFAULT 0 23 | ); 24 | 25 | CREATE TABLE Classes ( 26 | ClassID serial PRIMARY KEY , 27 | SubjectID int NULL DEFAULT 0 , 28 | ClassRoomID int NULL DEFAULT 0 , 29 | Credits smallint NULL DEFAULT 0 , 30 | StartDate date NULL , 31 | StartTime time NULL , 32 | Duration smallint NULL DEFAULT 0 , 33 | MondaySchedule int NOT NULL DEFAULT 0 , 34 | TuesdaySchedule int NOT NULL DEFAULT 0 , 35 | WednesdaySchedule int NOT NULL DEFAULT 0 , 36 | ThursdaySchedule int NOT NULL DEFAULT 0 , 37 | FridaySchedule int NOT NULL DEFAULT 0 , 38 | SaturdaySchedule int NOT NULL DEFAULT 0 39 | ); 40 | 41 | CREATE TABLE Departments ( 42 | DepartmentID serial PRIMARY KEY , 43 | DeptName varchar (50) NULL , 44 | DeptChair int NULL DEFAULT 0 45 | ); 46 | 47 | CREATE TABLE Faculty ( 48 | StaffID int NOT NULL DEFAULT 0 , 49 | Title varchar (50) NULL , 50 | Status varchar (12) NULL , 51 | Tenured int NOT NULL DEFAULT 0 52 | ); 53 | 54 | CREATE TABLE Faculty_Categories ( 55 | StaffID int NOT NULL , 56 | CategoryID varchar (10) NOT NULL DEFAULT 'ACC' 57 | ); 58 | 59 | CREATE TABLE Faculty_Classes ( 60 | ClassID int NOT NULL , 61 | StaffID int NOT NULL 62 | ); 63 | 64 | CREATE TABLE Faculty_Subjects ( 65 | StaffID int NOT NULL DEFAULT 0 , 66 | SubjectID int NOT NULL DEFAULT 0 , 67 | ProficiencyRating real NULL DEFAULT 0 68 | ); 69 | 70 | CREATE TABLE Majors ( 71 | MajorID int NOT NULL , 72 | Major varchar (20) NULL 73 | ); 74 | 75 | CREATE TABLE Staff ( 76 | StaffID serial PRIMARY KEY , 77 | StfFirstName varchar (25) NULL , 78 | StfLastname varchar (25) NULL , 79 | StfStreetAddress varchar (50) NULL , 80 | StfCity varchar (30) NULL , 81 | StfState varchar (2) NULL , 82 | StfZipCode varchar (5) NULL , 83 | StfAreaCode varchar (5) NULL , 84 | StfPhoneNumber varchar (8) NULL , 85 | Salary decimal (15,2) NULL , 86 | DateHired date NULL , 87 | Position varchar (50) NULL 88 | ); 89 | 90 | CREATE TABLE Student_Class_Status ( 91 | ClassStatus serial PRIMARY KEY , 92 | ClassStatusDescription varchar (50) NULL 93 | ); 94 | 95 | CREATE TABLE Student_Schedules ( 96 | StudentID int NOT NULL , 97 | ClassID int NOT NULL , 98 | ClassStatus int NULL DEFAULT 0 , 99 | Grade float NULL DEFAULT 0 100 | ); 101 | 102 | CREATE TABLE Students ( 103 | StudentID serial PRIMARY KEY , 104 | StudFirstName varchar (25) NULL , 105 | StudLastName varchar (25) NULL , 106 | StudStreetAddress varchar (50) NULL , 107 | StudCity varchar (30) NULL , 108 | StudState varchar (2) NULL , 109 | StudZipCode varchar (5) NULL , 110 | StudAreaCode varchar (5) NULL , 111 | StudPhoneNumber varchar (8) NULL , 112 | StudGPA float NULL DEFAULT 0 , 113 | StudMajor int NULL 114 | ); 115 | 116 | CREATE TABLE Subjects ( 117 | SubjectID serial PRIMARY KEY , 118 | CategoryID varchar (10) NULL , 119 | SubjectCode varchar (8) NULL , 120 | SubjectName varchar (50) NULL , 121 | SubjectPreReq varchar (8) NULL DEFAULT NULL , 122 | SubjectDescription text NULL 123 | ); 124 | 125 | ALTER TABLE Buildings 126 | ADD CONSTRAINT Buildings_PK PRIMARY KEY 127 | ( 128 | BuildingCode 129 | ); 130 | 131 | CREATE INDEX NumberOfFloors ON Buildings(NumberOfFloors); 132 | 133 | ALTER TABLE Categories 134 | ADD CONSTRAINT Categories_PK PRIMARY KEY 135 | ( 136 | CategoryID 137 | ); 138 | 139 | CREATE INDEX DepartmentID ON Categories(DepartmentID); 140 | 141 | CREATE INDEX BuildingNumber ON Class_Rooms(BuildingCode); 142 | 143 | CREATE INDEX ClassesCategoryID ON Classes(SubjectID); 144 | 145 | CREATE INDEX ClassRoomID ON Classes(ClassRoomID); 146 | 147 | CREATE INDEX StaffDepartments ON Departments(DeptChair); 148 | 149 | ALTER TABLE Faculty 150 | ADD CONSTRAINT Faculty_PK PRIMARY KEY 151 | ( 152 | StaffID 153 | ); 154 | 155 | ALTER TABLE Faculty_Categories 156 | ADD CONSTRAINT Faculty_Categories_PK PRIMARY KEY 157 | ( 158 | StaffID, 159 | CategoryID 160 | ); 161 | 162 | CREATE INDEX CategoryIDFaculty_Categories ON Faculty_Categories(CategoryID); 163 | 164 | CREATE INDEX StaffIDFaculty_Categories ON Faculty_Categories(StaffID); 165 | 166 | ALTER TABLE Faculty_Classes 167 | ADD CONSTRAINT Faculty_Classes_PK PRIMARY KEY 168 | ( 169 | ClassID, 170 | StaffID 171 | ); 172 | 173 | CREATE INDEX ClassesFaculty_Classes ON Faculty_Classes(ClassID); 174 | 175 | CREATE INDEX StaffFaculty_Classes ON Faculty_Classes(StaffID); 176 | 177 | ALTER TABLE Faculty_Subjects 178 | ADD CONSTRAINT Faculty_Subjects_PK PRIMARY KEY 179 | ( 180 | StaffID, 181 | SubjectID 182 | ); 183 | 184 | CREATE INDEX StaffIDFaculty_Subjects ON Faculty_Subjects(StaffID); 185 | 186 | CREATE INDEX SubjectsFaculty_Subjects ON Faculty_Subjects(SubjectID); 187 | 188 | CREATE INDEX StaffZipCode ON Staff(StfZipCode); 189 | 190 | CREATE INDEX StaffAreaCode ON Staff(StfAreaCode); 191 | 192 | ALTER TABLE Majors 193 | ADD CONSTRAINT Majors_PK PRIMARY KEY 194 | ( 195 | MajorID 196 | ); 197 | 198 | ALTER TABLE Student_Schedules 199 | ADD CONSTRAINT Student_Schedules_PK PRIMARY KEY 200 | ( 201 | StudentID, 202 | ClassID 203 | ); 204 | 205 | CREATE INDEX ClassesStudent_Schedules ON Student_Schedules(ClassID); 206 | 207 | CREATE INDEX Student_Class_StatusStudent_Schedules ON Student_Schedules(ClassStatus); 208 | 209 | CREATE INDEX StudentsStudent_Schedules ON Student_Schedules(StudentID); 210 | 211 | CREATE INDEX StudAreaCode ON Students(StudAreaCode); 212 | 213 | CREATE INDEX StudZipCode ON Students(StudZipCode); 214 | 215 | CREATE INDEX StudMajor ON Students(StudMajor); 216 | 217 | CREATE INDEX CategoriesSubjects ON Subjects(CategoryID); 218 | 219 | CREATE UNIQUE INDEX SubjectCode ON Subjects(SubjectCode); 220 | 221 | CREATE INDEX SubjPreReq ON Subjects(SubjectPreReq); 222 | 223 | ALTER TABLE Categories 224 | ADD CONSTRAINT Categories_FK00 FOREIGN KEY 225 | ( 226 | DepartmentID 227 | ) REFERENCES Departments ( 228 | DepartmentID 229 | ); 230 | 231 | ALTER TABLE Class_Rooms 232 | ADD CONSTRAINT Class_Rooms_FK00 FOREIGN KEY 233 | ( 234 | BuildingCode 235 | ) REFERENCES Buildings ( 236 | BuildingCode 237 | ); 238 | 239 | ALTER TABLE Classes 240 | ADD CONSTRAINT Classes_FK00 FOREIGN KEY 241 | ( 242 | ClassRoomID 243 | ) REFERENCES Class_Rooms ( 244 | ClassRoomID 245 | ), 246 | ADD CONSTRAINT Classes_FK01 FOREIGN KEY 247 | ( 248 | SubjectID 249 | ) REFERENCES Subjects ( 250 | SubjectID 251 | ); 252 | 253 | ALTER TABLE Departments 254 | ADD CONSTRAINT Departments_FK00 FOREIGN KEY 255 | ( 256 | DeptChair 257 | ) REFERENCES Staff ( 258 | StaffID 259 | ); 260 | 261 | ALTER TABLE Faculty 262 | ADD CONSTRAINT Faculty_FK00 FOREIGN KEY 263 | ( 264 | StaffID 265 | ) REFERENCES Staff ( 266 | StaffID 267 | ); 268 | 269 | ALTER TABLE Faculty_Categories 270 | ADD CONSTRAINT Faculty_Categories_FK00 FOREIGN KEY 271 | ( 272 | CategoryID 273 | ) REFERENCES Categories ( 274 | CategoryID 275 | ), 276 | ADD CONSTRAINT Faculty_Categories_FK01 FOREIGN KEY 277 | ( 278 | StaffID 279 | ) REFERENCES Faculty ( 280 | StaffID 281 | ); 282 | 283 | ALTER TABLE Faculty_Classes 284 | ADD CONSTRAINT Faculty_Classes_FK00 FOREIGN KEY 285 | ( 286 | ClassID 287 | ) REFERENCES Classes ( 288 | ClassID 289 | ), 290 | ADD CONSTRAINT Faculty_Classes_FK01 FOREIGN KEY 291 | ( 292 | StaffID 293 | ) REFERENCES Staff ( 294 | StaffID 295 | ), 296 | ADD CONSTRAINT Faculty_CLasses_FK02 FOREIGN KEY 297 | ( 298 | StaffID 299 | ) REFERENCES Faculty ( 300 | StaffID 301 | ); 302 | 303 | ALTER TABLE Faculty_Subjects 304 | ADD CONSTRAINT Faculty_Subjects_FK00 FOREIGN KEY 305 | ( 306 | StaffID 307 | ) REFERENCES Faculty ( 308 | StaffID 309 | ), 310 | ADD CONSTRAINT Faculty_Subjects_FK01 FOREIGN KEY 311 | ( 312 | SubjectID 313 | ) REFERENCES Subjects ( 314 | SubjectID 315 | ); 316 | 317 | ALTER TABLE Students 318 | ADD CONSTRAINT Students_FK00 FOREIGN KEY 319 | ( 320 | StudMajor 321 | ) REFERENCES Majors ( 322 | MajorID 323 | ); 324 | 325 | ALTER TABLE Student_Schedules 326 | ADD CONSTRAINT Student_Schedules_FK00 FOREIGN KEY 327 | ( 328 | ClassID 329 | ) REFERENCES Classes ( 330 | ClassID 331 | ), 332 | ADD CONSTRAINT Student_Schedules_FK01 FOREIGN KEY 333 | ( 334 | ClassStatus 335 | ) REFERENCES Student_Class_Status ( 336 | ClassStatus 337 | ), 338 | ADD CONSTRAINT Student_Schedules_FK02 FOREIGN KEY 339 | ( 340 | StudentID 341 | ) REFERENCES Students ( 342 | StudentID 343 | ); 344 | 345 | ALTER TABLE Subjects 346 | ADD CONSTRAINT Subjects_FK00 FOREIGN KEY 347 | ( 348 | CategoryID 349 | ) REFERENCES Categories ( 350 | CategoryID 351 | ), 352 | ADD CONSTRAINT Subjects_FK01 FOREIGN KEY 353 | ( 354 | SubjectPreReq 355 | ) REFERENCES Subjects ( 356 | SubjectCode 357 | ); -------------------------------------------------------------------------------- /Week 1/sql/ddl/02 EntertainmentAgencyModifyProcsPG.sql: -------------------------------------------------------------------------------- 1 | -- This requires that the tables from 00 EntertainmentAgencyStructureModifyPG.sql be created first. 2 | 3 | SET search_path TO EntertainmentAgencyModify; 4 | 5 | CREATE FUNCTION CH15_Calculate_Entertainment_ContractPrice() RETURNS void AS $$ 6 | UPDATE Engagements 7 | SET ContractPrice = ROUND(1.15 * (EndDate - StartDate + 1) * 8 | (SELECT EntPricePerDay 9 | FROM Entertainers 10 | WHERE Entertainers.EntertainerID = Engagements.EntertainerID), 0); 11 | $$ LANGUAGE SQL; 12 | 13 | CREATE VIEW CH15_Calculate_Entertainment_ContractPrice_Query 14 | AS 15 | SELECT EngagementNumber, StartDate, EndDate, ContractPrice, ROUND(1.15 * (EndDate - StartDate + 1) * 16 | (SELECT EntPricePerDay 17 | FROM Entertainers 18 | WHERE Entertainers.EntertainerID = Engagements.EntertainerID), 0) 19 | AS NewContractPrice, EntertainerID 20 | FROM Engagements; 21 | 22 | 23 | CREATE FUNCTION CH15_Discount_Good_Customers_October() RETURNS void AS $$ 24 | UPDATE Engagements 25 | SET ContractPrice = ROUND(ContractPrice * 0.98, 0) 26 | WHERE (CustomerID IN 27 | (SELECT Engagements.CustomerID 28 | FROM Engagements 29 | WHERE (Engagements.StartDate <= CAST('2017-10-31' AS Date)) 30 | AND (Engagements.EndDate >= CAST('2017-10-1' AS Date)) 31 | GROUP BY Engagements.CustomerID 32 | HAVING SUM(Engagements.ContractPrice) > 3000)); 33 | $$ LANGUAGE SQL; 34 | 35 | CREATE VIEW CH15_Discount_Good_Customers_October_Query 36 | AS 37 | SELECT ContractPrice, ROUND(ContractPrice * 0.98, 0) AS NewContractPrice 38 | FROM Engagements 39 | WHERE (CustomerID IN 40 | (SELECT Engagements.CustomerID 41 | FROM Engagements 42 | WHERE (Engagements.StartDate <= CAST('2017-10-31' AS Date)) 43 | AND (Engagements.EndDate >= CAST('2017-10-1' AS Date)) 44 | GROUP BY Engagements.CustomerID 45 | HAVING SUM(Engagements.ContractPrice) > 3000)) ; 46 | 47 | 48 | CREATE FUNCTION CH15_Give_Agents_6Percent_Raise() RETURNS void AS $$ 49 | UPDATE Agents 50 | SET Salary = ROUND(Salary * 1.06, 0); 51 | $$ LANGUAGE SQL; 52 | 53 | CREATE VIEW CH15_Give_Agents_6Percent_Raise_Query 54 | AS 55 | SELECT AgentID, AgtFirstName, AgtLastName, Salary, ROUND(Salary * 1.06, 0) As NewSalary 56 | FROM Agents; 57 | 58 | 59 | CREATE FUNCTION CH15_Reward_Good_Agents() RETURNS void AS $$ 60 | UPDATE Agents 61 | SET CommissionRate = CommissionRate + 0.005 62 | WHERE (AgentID IN 63 | (SELECT AgentID 64 | FROM Engagements 65 | GROUP BY AgentID 66 | HAVING SUM(ContractPrice) > 20000)); 67 | $$ LANGUAGE SQL; 68 | 69 | CREATE VIEW CH15_Reward_Good_Agents_Query 70 | AS 71 | SELECT CommissionRate, CommissionRate + 0.005 AS NewCommissionRate 72 | FROM Agents 73 | WHERE (AgentID IN 74 | (SELECT AgentID 75 | FROM Engagements 76 | GROUP BY AgentID 77 | HAVING SUM(ContractPrice) > 20000)); 78 | 79 | 80 | CREATE FUNCTION CH16_Add_Ent_Customer() RETURNS void AS $$ 81 | INSERT INTO Customers 82 | (CustFirstName, CustLastName, CustStreetAddress, CustCity, CustState, CustZipCode, CustPhoneNumber) 83 | VALUES ('Kendra', 'Hernandez', '457 211th St NE', 'Bothell', 'WA', '98200', '555-3945'); 84 | $$ LANGUAGE SQL; 85 | 86 | CREATE VIEW CH16_Add_Ent_Customer_Query 87 | AS 88 | SELECT 'Kendra' As NewCustFirstName, 'Hernandez' As NewCustLastName, '457 211th St NE' As NewCustStreetAddress, 89 | 'Bothell' As NewCustCity, 'WA' As NewCustState, '98200' As NewCustZipCode, '555-3945' As NewCustPhoneNumber; 90 | 91 | 92 | CREATE FUNCTION CH16_Add_Engagement() RETURNS void AS $$ 93 | INSERT INTO Engagements 94 | (CustomerID, EntertainerID, StartDate, EndDate, StartTime, StopTime, ContractPrice, AgentID) 95 | SELECT Customers.CustomerID, Entertainers.EntertainerID, '2018-8-15' AS StartDate, 96 | '2018-8-16' AS EndDate, '19:00:00' AS StartTime, '23:00:00' AS StopTime, 97 | ROUND(Entertainers.EntPricePerDay * 2 * 1.15, 0) AS ContractPrice, Agents.AgentID 98 | FROM Customers CROSS JOIN Entertainers CROSS JOIN Agents 99 | WHERE (Customers.CustFirstName = 'Matt') AND (Customers.CustLastName = 'Berg') 100 | AND (Entertainers.EntStageName = 'Jazz Persuasion') 101 | AND (Agents.AgtFirstName = 'Karen') AND (Agents.AgtLastName = 'Smith'); 102 | $$ LANGUAGE SQL; 103 | 104 | CREATE VIEW CH16_Add_Engagement_Query 105 | AS 106 | SELECT Customers.CustomerID, Entertainers.EntertainerID, '2018-8-15' AS StartDate, 107 | '2018-8-16' AS EndDate, '19:00:00' AS StartTime, '23:00:00' AS StopTime, 108 | ROUND(Entertainers.EntPricePerDay * 2 * 1.15, 0) AS ContractPrice, Agents.AgentID 109 | FROM Customers CROSS JOIN Entertainers CROSS JOIN Agents 110 | WHERE (Customers.CustFirstName = 'Matt') AND (Customers.CustLastName = 'Berg') 111 | AND (Entertainers.EntStageName = 'Jazz Persuasion') 112 | AND (Agents.AgtFirstName = 'Karen') AND (Agents.AgtLastName = 'Smith'); 113 | 114 | 115 | CREATE FUNCTION CH16_Add_Style() RETURNS void AS $$ 116 | INSERT INTO Musical_Styles (StyleName) 117 | VALUES ('New Age'); 118 | $$ LANGUAGE SQL; 119 | 120 | CREATE VIEW CH16_Add_Style_Query 121 | AS 122 | SELECT 'New Age' As NewStyeName; 123 | 124 | 125 | CREATE FUNCTION CH16_Archive_Engagements() RETURNS void AS $$ 126 | INSERT INTO Engagements_Archive 127 | (EngagementNumber, StartDate, EndDate, StartTime, StopTime, ContractPrice, 128 | CustomerID, AgentID, EntertainerID) 129 | SELECT Engagements.EngagementNumber, Engagements.StartDate, Engagements.EndDate, 130 | Engagements.StartTime, Engagements.StopTime, Engagements.ContractPrice, Engagements.CustomerID, 131 | Engagements.AgentID, Engagements.EntertainerID 132 | FROM Engagements 133 | WHERE Engagements.EndDate < CAST('2018-01-01' AS Date); 134 | $$ LANGUAGE SQL; 135 | 136 | CREATE VIEW CH16_Archive_Engagements_Query 137 | AS 138 | SELECT Engagements.EngagementNumber, Engagements.StartDate, Engagements.EndDate, 139 | Engagements.StartTime, Engagements.StopTime, Engagements.ContractPrice, Engagements.CustomerID, 140 | Engagements.AgentID, Engagements.EntertainerID 141 | FROM Engagements 142 | WHERE Engagements.EndDate < CAST('2018-01-01' AS Date); 143 | 144 | 145 | CREATE FUNCTION CH16_Copy_Agent_To_Customer() RETURNS void AS $$ 146 | INSERT INTO Customers 147 | (CustFirstName, CustLastName, CustStreetAddress, CustCity, CustState, CustZipCode, CustPhoneNumber) 148 | SELECT AgtFirstName, AgtLastName, AgtStreetAddress, AgtCity, AgtState, AgtZipCode, AgtPhoneNumber 149 | FROM Agents 150 | WHERE (AgtFirstName = 'Marianne') AND (AgtLastName = 'Wier'); 151 | $$ LANGUAGE SQL; 152 | 153 | CREATE VIEW CH16_Copy_Agent_To_Customer_Query 154 | AS 155 | SELECT AgtFirstName, AgtLastName, AgtStreetAddress, AgtCity, AgtState, AgtZipCode, AgtPhoneNumber 156 | FROM Agents 157 | WHERE (AgtFirstName = 'Marianne') AND (AgtLastName = 'Wier'); 158 | 159 | 160 | CREATE FUNCTION CH16_Duplicate_Engagement() RETURNS void AS $$ 161 | INSERT INTO Engagements 162 | (StartDate, EndDate, StartTime, StopTime, ContractPrice, CustomerID, AgentID, EntertainerID) 163 | SELECT '2018-08-01' AS StartDate, '2018-08-04' AS EndDate, Engagements.StartTime, 164 | Engagements.StopTime, Engagements.ContractPrice, 165 | Engagements.CustomerID, Engagements.AgentID, Engagements.EntertainerID 166 | FROM Customers INNER JOIN Engagements 167 | ON Customers.CustomerID = Engagements.CustomerID 168 | WHERE (Customers.CustFirstName = 'Doris') AND (Customers.CustLastName = 'Hartwig') 169 | AND (Engagements.StartDate = CAST('2017-12-02' AS Date)); 170 | $$ LANGUAGE SQL; 171 | 172 | CREATE VIEW CH16_Duplicate_Engagement_Query 173 | AS 174 | SELECT '2018-08-01' AS StartDate, '2018-08-04' AS EndDate, Engagements.StartTime, 175 | Engagements.StopTime, Engagements.ContractPrice, 176 | Engagements.CustomerID, Engagements.AgentID, Engagements.EntertainerID 177 | FROM Customers INNER JOIN 178 | Engagements ON Customers.CustomerID = Engagements.CustomerID 179 | WHERE (Customers.CustFirstName = 'Doris') AND (Customers.CustLastName = 'Hartwig') 180 | AND (Engagements.StartDate = CAST('2017-12-02' AS Date)); 181 | 182 | 183 | CREATE FUNCTION CH17_Delete_Customers_Never_Booked1() RETURNS void AS $$ 184 | DELETE FROM Musical_Preferences 185 | WHERE (CustomerID NOT IN 186 | (SELECT CustomerID 187 | FROM Engagements)); 188 | $$ LANGUAGE SQL; 189 | 190 | CREATE VIEW CH17_Delete_Customers_Never_Booked1_Query 191 | AS 192 | SELECT * 193 | FROM Musical_Preferences 194 | WHERE (CustomerID NOT IN 195 | (SELECT CustomerID 196 | FROM Engagements)); 197 | 198 | 199 | CREATE FUNCTION CH17_Delete_Customers_Never_Booked2() RETURNS void AS $$ 200 | DELETE FROM Customers 201 | WHERE (CustomerID NOT IN 202 | (SELECT CustomerID 203 | FROM Engagements)); 204 | $$ LANGUAGE SQL; 205 | 206 | CREATE VIEW CH17_Delete_Customers_Never_Booked2_Query 207 | AS 208 | SELECT * 209 | FROM Customers 210 | WHERE (CustomerID NOT IN 211 | (SELECT CustomerID 212 | FROM Engagements)); 213 | 214 | 215 | CREATE FUNCTION CH17_Delete_Entertainers_Not_Booked1() RETURNS void AS $$ 216 | DELETE FROM Entertainer_Members 217 | WHERE (EntertainerID NOT IN 218 | (SELECT EntertainerID 219 | FROM Engagements)); 220 | $$ LANGUAGE SQL; 221 | 222 | CREATE VIEW CH17_Delete_Entertainers_Not_Booked1_Query 223 | AS 224 | SELECT * 225 | FROM Entertainer_Members 226 | WHERE (EntertainerID NOT IN 227 | (SELECT EntertainerID 228 | FROM Engagements)); 229 | 230 | 231 | CREATE FUNCTION CH17_Delete_Entertainers_Not_Booked2() RETURNS void AS $$ 232 | DELETE FROM Entertainer_Styles 233 | WHERE (EntertainerID NOT IN 234 | (SELECT EntertainerID 235 | FROM Engagements)); 236 | $$ LANGUAGE SQL; 237 | 238 | CREATE VIEW CH17_Delete_Entertainers_Not_Booked2_Query 239 | AS 240 | SELECT * 241 | FROM Entertainer_Styles 242 | WHERE (EntertainerID NOT IN 243 | (SELECT EntertainerID 244 | FROM Engagements)); 245 | 246 | 247 | CREATE FUNCTION CH17_Delete_Entertainers_Not_Booked3() RETURNS void AS $$ 248 | DELETE FROM Entertainers 249 | WHERE (EntertainerID NOT IN 250 | (SELECT EntertainerID 251 | FROM Engagements)); 252 | $$ LANGUAGE SQL; 253 | 254 | CREATE VIEW CH17_Delete_Entertainers_Not_Booked3_Query 255 | AS 256 | SELECT * 257 | FROM Entertainers 258 | WHERE (EntertainerID NOT IN 259 | (SELECT EntertainerID 260 | FROM Engagements)); 261 | 262 | 263 | CREATE FUNCTION CH17_Delete_Members_Not_In_Group() RETURNS void AS $$ 264 | DELETE FROM Members 265 | WHERE (MemberID NOT IN 266 | (SELECT MemberID 267 | FROM Entertainer_Members)); 268 | $$ LANGUAGE SQL; 269 | 270 | CREATE VIEW CH17_Delete_Members_Not_In_Group_Query 271 | AS 272 | SELECT * 273 | FROM Members 274 | WHERE (MemberID NOT IN 275 | (SELECT MemberID 276 | FROM Entertainer_Members)); 277 | 278 | 279 | CREATE FUNCTION CH17_Delete_Styles_No_Entertainer() RETURNS void AS $$ 280 | DELETE FROM Musical_Styles 281 | WHERE (StyleID NOT IN 282 | (SELECT StyleID 283 | FROM Entertainer_Styles)) 284 | AND (StyleID NOT IN 285 | (SELECT StyleID 286 | FROM Musical_Preferences)); 287 | $$ LANGUAGE SQL; 288 | 289 | CREATE VIEW CH17_Delete_Styles_No_Entertainer_Query 290 | AS 291 | SELECT * 292 | FROM Musical_Styles 293 | WHERE (StyleID NOT IN 294 | (SELECT StyleID 295 | FROM Entertainer_Styles)) 296 | AND (StyleID NOT IN 297 | (SELECT StyleID 298 | FROM Musical_Preferences)); 299 | 300 | 301 | CREATE FUNCTION CH17_Remove_Archived_Engagements() RETURNS void AS $$ 302 | DELETE FROM Engagements 303 | WHERE (EngagementNumber IN 304 | (SELECT EngagementNumber 305 | FROM Engagements_Archive)); 306 | $$ LANGUAGE SQL; 307 | 308 | CREATE VIEW CH17_Remove_Archived_Engagements_Query 309 | AS 310 | SELECT * 311 | FROM Engagements 312 | WHERE (EngagementNumber IN 313 | (SELECT EngagementNumber 314 | FROM Engagements_Archive)); 315 | -------------------------------------------------------------------------------- /Week 1/sql/ddl/00 SchoolSchedulingStructurePG.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA SchoolSchedulingExample; 2 | 3 | 4 | 5 | SET search_path TO SchoolSchedulingExample; 6 | 7 | CREATE TABLE Buildings ( 8 | BuildingCode varchar (3) NOT NULL , 9 | BuildingName varchar (25) NULL , 10 | NumberOfFloors smallint NULL , 11 | ElevatorAccess int NOT NULL DEFAULT 0 , 12 | SiteParkingAvailable int NOT NULL DEFAULT 0 13 | ); 14 | 15 | CREATE TABLE Categories ( 16 | CategoryID varchar (10) NOT NULL , 17 | CategoryDescription varchar (75) NULL , 18 | DepartmentID int NULL DEFAULT 0 19 | ); 20 | 21 | CREATE TABLE Class_Rooms ( 22 | ClassRoomID int NOT NULL , 23 | BuildingCode varchar (3) NULL , 24 | PhoneAvailable int NOT NULL DEFAULT 0 , 25 | Capacity smallint NOT NULL DEFAULT 0 26 | ); 27 | 28 | CREATE TABLE Classes ( 29 | ClassID int NOT NULL , 30 | SubjectID int NULL DEFAULT 0 , 31 | ClassRoomID int NULL DEFAULT 0 , 32 | Credits smallint NULL DEFAULT 0 , 33 | SemesterNumber smallint , 34 | StartDate date NULL , 35 | StartTime time NULL , 36 | Duration smallint NULL DEFAULT 0 , 37 | MondaySchedule int NOT NULL DEFAULT 0 , 38 | TuesdaySchedule int NOT NULL DEFAULT 0 , 39 | WednesdaySchedule int NOT NULL DEFAULT 0 , 40 | ThursdaySchedule int NOT NULL DEFAULT 0 , 41 | FridaySchedule int NOT NULL DEFAULT 0 , 42 | SaturdaySchedule int NOT NULL DEFAULT 0 43 | ); 44 | 45 | CREATE TABLE Departments ( 46 | DepartmentID int NOT NULL , 47 | DeptName varchar (50) NULL , 48 | DeptChair int NULL DEFAULT 0 49 | ); 50 | 51 | CREATE TABLE Faculty ( 52 | StaffID int NOT NULL , 53 | Title varchar (50) NULL , 54 | Status varchar (12) NULL , 55 | Tenured int NOT NULL DEFAULT 0 56 | ); 57 | 58 | CREATE TABLE Faculty_Categories ( 59 | StaffID int NOT NULL , 60 | CategoryID varchar (10) NOT NULL DEFAULT 'ACC' 61 | ); 62 | 63 | CREATE TABLE Faculty_Classes ( 64 | ClassID int NOT NULL , 65 | StaffID int NOT NULL 66 | ); 67 | 68 | CREATE TABLE Faculty_Subjects ( 69 | StaffID int NOT NULL DEFAULT 0 , 70 | SubjectID int NOT NULL DEFAULT 0 , 71 | ProficiencyRating real NULL DEFAULT 0 72 | ); 73 | 74 | CREATE TABLE Majors ( 75 | MajorID int NOT NULL , 76 | Major varchar (20) NULL 77 | ); 78 | 79 | CREATE TABLE Staff ( 80 | StaffID int NOT NULL , 81 | StfFirstName varchar (25) NULL , 82 | StfLastname varchar (25) NULL , 83 | StfStreetAddress varchar (50) NULL , 84 | StfCity varchar (30) NULL , 85 | StfState varchar (2) NULL , 86 | StfZipCode varchar (5) NULL , 87 | StfAreaCode varchar (5) NULL , 88 | StfPhoneNumber varchar (8) NULL , 89 | Salary decimal(15,2) NULL , 90 | DateHired date NULL , 91 | Position varchar (50) NULL 92 | ); 93 | 94 | CREATE TABLE Student_Class_Status ( 95 | ClassStatus int NOT NULL DEFAULT 0 , 96 | ClassStatusDescription varchar (50) NULL 97 | ); 98 | 99 | CREATE TABLE Student_Schedules ( 100 | StudentID int NOT NULL , 101 | ClassID int NOT NULL , 102 | ClassStatus int NULL DEFAULT 0 , 103 | Grade real NULL DEFAULT 0 104 | ); 105 | 106 | CREATE TABLE Students ( 107 | StudentID int NOT NULL , 108 | StudFirstName varchar (25) NULL , 109 | StudLastName varchar (25) NULL , 110 | StudStreetAddress varchar (50) NULL , 111 | StudCity varchar (30) NULL , 112 | StudState varchar (2) NULL , 113 | StudZipCode varchar (5) NULL , 114 | StudAreaCode varchar (5) NULL , 115 | StudPhoneNumber varchar (8) NULL , 116 | StudBirthDate date NULL , 117 | StudGender varchar (1) NULL, 118 | StudMaritalStatus varchar (1) NULL, 119 | StudMajor int NULL 120 | ); 121 | 122 | CREATE TABLE Subjects ( 123 | SubjectID int NOT NULL DEFAULT 0 , 124 | CategoryID varchar (10) NULL , 125 | SubjectCode varchar (8) NULL , 126 | SubjectName varchar (50) NULL , 127 | SubjectPreReq varchar (8) NULL DEFAULT NULL , 128 | SubjectDescription text NULL , 129 | SubjectEstClassSize smallint NOT NULL DEFAULT 0 130 | ); 131 | 132 | CREATE TABLE ztblGenderMatrix ( 133 | Gender varchar (1) NOT NULL , 134 | Male smallint NULL , 135 | Female smallint NULL 136 | ) ; 137 | 138 | 139 | CREATE TABLE ztblLetterGrades ( 140 | LetterGrade varchar (3) NOT NULL , 141 | LowGradePoint real NULL , 142 | HighGradePoint real NULL 143 | ) ; 144 | 145 | 146 | CREATE TABLE ztblMaritalStatusMatrix ( 147 | MaritalStatus varchar (1) NOT NULL , 148 | Married smallint NULL , 149 | Single smallint NULL , 150 | Widowed smallint NULL , 151 | Divorced smallint NULL 152 | ) ; 153 | 154 | 155 | CREATE TABLE ztblProfRatings ( 156 | ProfRatingDesc varchar (12) NULL , 157 | ProfRatingLow float (53) NOT NULL , 158 | ProfRatingHigh float (53) NULL 159 | ) ; 160 | 161 | 162 | CREATE TABLE ztblSemesterDays ( 163 | SemesterNo smallint NOT NULL , 164 | SemDate date NOT NULL , 165 | SemDayName varchar (10) NULL 166 | ) ; 167 | 168 | 169 | CREATE TABLE ztblSeqNumbers ( 170 | Sequence int NOT NULL DEFAULT 0 171 | ) ; 172 | 173 | 174 | ALTER TABLE Buildings 175 | ADD CONSTRAINT Buildings_PK PRIMARY KEY 176 | ( 177 | BuildingCode 178 | ); 179 | 180 | CREATE INDEX NumberOfFloors ON Buildings(NumberOfFloors); 181 | 182 | ALTER TABLE Categories 183 | ADD CONSTRAINT Categories2_PK PRIMARY KEY 184 | ( 185 | CategoryID 186 | ); 187 | 188 | CREATE INDEX DepartmentID ON Categories(DepartmentID); 189 | 190 | ALTER TABLE Class_Rooms 191 | ADD CONSTRAINT Class_Rooms_PK PRIMARY KEY 192 | ( 193 | ClassRoomID 194 | ); 195 | 196 | CREATE INDEX BuildingNumber ON Class_Rooms(BuildingCode); 197 | 198 | ALTER TABLE Classes 199 | ADD CONSTRAINT Classes_PK PRIMARY KEY 200 | ( 201 | ClassID 202 | ); 203 | 204 | CREATE INDEX SubjectID ON Classes(SubjectID); 205 | 206 | CREATE INDEX ClassRoomID ON Classes(ClassRoomID); 207 | 208 | ALTER TABLE Departments 209 | ADD CONSTRAINT Departments_PK PRIMARY KEY 210 | ( 211 | DepartmentID 212 | ); 213 | 214 | CREATE INDEX StaffDepartments ON Departments(DeptChair); 215 | 216 | ALTER TABLE Faculty 217 | ADD CONSTRAINT Faculty_PK PRIMARY KEY 218 | ( 219 | StaffID 220 | ); 221 | 222 | ALTER TABLE Faculty_Categories 223 | ADD CONSTRAINT Faculty_Categories_PK PRIMARY KEY 224 | ( 225 | StaffID, 226 | CategoryID 227 | ); 228 | 229 | CREATE INDEX CategoriesFaculty_Categories ON Faculty_Categories(CategoryID); 230 | 231 | CREATE INDEX FacultyFaculty_Categories ON Faculty_Categories(StaffID); 232 | 233 | ALTER TABLE Faculty_Classes 234 | ADD CONSTRAINT Faculty_Classes_PK PRIMARY KEY 235 | ( 236 | ClassID, 237 | StaffID 238 | ); 239 | 240 | CREATE INDEX ClassesFacultyClasses ON Faculty_Classes(ClassID); 241 | 242 | CREATE INDEX StaffFacultyClasses ON Faculty_Classes(StaffID); 243 | 244 | ALTER TABLE Faculty_Subjects 245 | ADD CONSTRAINT Faculty_Subjects_PK PRIMARY KEY 246 | ( 247 | StaffID, 248 | SubjectID 249 | ); 250 | 251 | CREATE INDEX FacultyFacultySubjects ON Faculty_Subjects(StaffID); 252 | 253 | CREATE INDEX SubjectsFacultySubjects ON Faculty_Subjects(SubjectID); 254 | 255 | ALTER TABLE Majors 256 | ADD CONSTRAINT Majors_PK PRIMARY KEY 257 | ( 258 | MajorID 259 | ); 260 | 261 | ALTER TABLE Staff 262 | ADD CONSTRAINT Staff_PK PRIMARY KEY 263 | ( 264 | StaffID 265 | ); 266 | 267 | CREATE INDEX StaffZipCode ON Staff(StfZipCode); 268 | 269 | CREATE INDEX StaffAreaCode ON Staff(StfAreaCode); 270 | 271 | ALTER TABLE Student_Class_Status 272 | ADD CONSTRAINT Student_Class_Status_PK PRIMARY KEY 273 | ( 274 | ClassStatus 275 | ); 276 | 277 | ALTER TABLE Student_Schedules 278 | ADD CONSTRAINT Student_Schedules_PK PRIMARY KEY 279 | ( 280 | StudentID, 281 | ClassID 282 | ); 283 | 284 | CREATE INDEX ClassesStudent_Schedules ON Student_Schedules(ClassID); 285 | 286 | CREATE INDEX Student_Class_StatusStudent_Schedules ON Student_Schedules(ClassStatus); 287 | 288 | CREATE INDEX StudentsStudent_Schedules ON Student_Schedules(StudentID); 289 | 290 | ALTER TABLE Students 291 | ADD CONSTRAINT Students_PK PRIMARY KEY 292 | ( 293 | StudentID 294 | ); 295 | 296 | CREATE INDEX StudAreaCode ON Students(StudAreaCode); 297 | 298 | CREATE INDEX StudZipCode ON Students(StudZipCode); 299 | 300 | CREATE INDEX StudMajor ON Students(StudMajor); 301 | 302 | ALTER TABLE Subjects 303 | ADD CONSTRAINT Subjects_PK PRIMARY KEY 304 | ( 305 | SubjectID 306 | ); 307 | 308 | CREATE INDEX CategoryID ON Subjects(CategoryID); 309 | 310 | CREATE UNIQUE INDEX SubjectCode ON Subjects(SubjectCode); 311 | 312 | CREATE INDEX SubjectPreReq ON Subjects(SubjectPreReq); 313 | 314 | ALTER TABLE ztblGenderMatrix ADD 315 | CONSTRAINT ztblGenderMatrix_PK PRIMARY KEY 316 | ( 317 | Gender 318 | ); 319 | 320 | 321 | ALTER TABLE ztblLetterGrades ADD 322 | CONSTRAINT ztblLetterGrades_PK PRIMARY KEY 323 | ( 324 | LetterGrade 325 | ); 326 | 327 | 328 | ALTER TABLE ztblMaritalStatusMatrix ADD 329 | CONSTRAINT ztblMaritalStatusMatrix_PK PRIMARY KEY 330 | ( 331 | MaritalStatus 332 | ); 333 | 334 | 335 | ALTER TABLE ztblProfRatings ADD 336 | CONSTRAINT ztblProfRatings_PK PRIMARY KEY 337 | ( 338 | ProfRatingLow 339 | ); 340 | 341 | 342 | ALTER TABLE ztblSemesterDays ADD 343 | CONSTRAINT ztblSemesterDays_PK PRIMARY KEY 344 | ( 345 | SemesterNo , 346 | SemDate 347 | ); 348 | 349 | 350 | ALTER TABLE ztblSeqNumbers ADD 351 | CONSTRAINT ztblSeqNumbers_PK PRIMARY KEY 352 | ( 353 | Sequence 354 | ); 355 | 356 | 357 | ALTER TABLE Categories ADD 358 | CONSTRAINT Categories_FK00 FOREIGN KEY 359 | ( 360 | DepartmentID 361 | ) REFERENCES Departments ( 362 | DepartmentID 363 | ); 364 | 365 | ALTER TABLE Class_Rooms 366 | ADD CONSTRAINT Class_Rooms_FK00 FOREIGN KEY 367 | ( 368 | BuildingCode 369 | ) REFERENCES Buildings ( 370 | BuildingCode 371 | ); 372 | 373 | ALTER TABLE Classes 374 | ADD CONSTRAINT Classes_FK00 FOREIGN KEY 375 | ( 376 | ClassRoomID 377 | ) REFERENCES Class_Rooms ( 378 | ClassRoomID 379 | ), 380 | ADD CONSTRAINT Classes_FK01 FOREIGN KEY 381 | ( 382 | SubjectID 383 | ) REFERENCES Subjects ( 384 | SubjectID 385 | ); 386 | 387 | ALTER TABLE Departments ADD 388 | CONSTRAINT Departments_FK00 FOREIGN KEY 389 | ( 390 | DeptChair 391 | ) REFERENCES Staff ( 392 | StaffID 393 | ); 394 | 395 | ALTER TABLE Faculty 396 | ADD CONSTRAINT Faculty_FK00 FOREIGN KEY 397 | ( 398 | StaffID 399 | ) REFERENCES Staff ( 400 | StaffID 401 | ); 402 | 403 | ALTER TABLE Faculty_Categories 404 | ADD CONSTRAINT Faculty_Categories_FK00 FOREIGN KEY 405 | ( 406 | CategoryID 407 | ) REFERENCES Categories ( 408 | CategoryID 409 | ), 410 | ADD CONSTRAINT Faculty_Categories_FK01 FOREIGN KEY 411 | ( 412 | StaffID 413 | ) REFERENCES Faculty ( 414 | StaffID 415 | ); 416 | 417 | ALTER TABLE Faculty_Classes 418 | ADD CONSTRAINT Faculty_Classes_FK00 FOREIGN KEY 419 | ( 420 | ClassID 421 | ) REFERENCES Classes ( 422 | ClassID 423 | ), 424 | ADD CONSTRAINT Faculty_Classes_FK01 FOREIGN KEY 425 | ( 426 | StaffID 427 | ) REFERENCES Staff ( 428 | StaffID 429 | ), 430 | ADD CONSTRAINT Faculty_CLasses_FK02 FOREIGN KEY 431 | ( 432 | StaffID 433 | ) REFERENCES Faculty ( 434 | StaffID 435 | ); 436 | 437 | ALTER TABLE Faculty_Subjects 438 | ADD CONSTRAINT Faculty_Subjects_FK00 FOREIGN KEY 439 | ( 440 | StaffID 441 | ) REFERENCES Faculty ( 442 | StaffID 443 | ), 444 | ADD CONSTRAINT Faculty_Subjects_FK01 FOREIGN KEY 445 | ( 446 | SubjectID 447 | ) REFERENCES Subjects ( 448 | SubjectID 449 | ); 450 | 451 | ALTER TABLE Students 452 | ADD CONSTRAINT Students_FK00 FOREIGN KEY 453 | ( 454 | StudMajor 455 | ) REFERENCES Majors ( 456 | MajorID 457 | ); 458 | 459 | ALTER TABLE Student_Schedules 460 | ADD CONSTRAINT Student_Schedules_FK00 FOREIGN KEY 461 | ( 462 | ClassID 463 | ) REFERENCES Classes ( 464 | ClassID 465 | ), 466 | ADD CONSTRAINT Student_Schedules_FK01 FOREIGN KEY 467 | ( 468 | ClassStatus 469 | ) REFERENCES Student_Class_Status ( 470 | ClassStatus 471 | ), 472 | ADD CONSTRAINT Student_Schedules_FK02 FOREIGN KEY 473 | ( 474 | StudentID 475 | ) REFERENCES Students ( 476 | StudentID 477 | ); 478 | 479 | ALTER TABLE Subjects 480 | ADD CONSTRAINT Subjects_FK00 FOREIGN KEY 481 | ( 482 | CategoryID 483 | ) REFERENCES Categories ( 484 | CategoryID 485 | ), 486 | ADD CONSTRAINT Subjects_FK01 FOREIGN KEY 487 | ( 488 | SubjectPreReq 489 | ) REFERENCES Subjects ( 490 | SubjectCode 491 | ); -------------------------------------------------------------------------------- /Week 1/sql/ddl/02 BowlingLeagueModifyProcsPG.sql: -------------------------------------------------------------------------------- 1 | -- This requires that the tables from 00 BowlingLeagueStructureModifyPG.sql be created first. 2 | 3 | SET search_path TO BowlingLeagueModify; 4 | 5 | CREATE FUNCTION CH15_Calc_Bowler_Pins_Avg_Hcp() RETURNS void AS $$ 6 | UPDATE Bowlers 7 | SET BowlerTotalPins = 8 | (SELECT SUM(RawScore) 9 | FROM Bowler_Scores 10 | WHERE Bowler_Scores.BowlerID = Bowlers.BowlerID), BowlerGamesBowled = 11 | (SELECT COUNT(Bowler_Scores.RawScore) 12 | FROM Bowler_Scores 13 | WHERE Bowler_Scores.BowlerID = Bowlers.BowlerID), BowlerCurrentAverage = 14 | COALESCE((SELECT Round(AVG(Bowler_Scores.RawScore), 0) 15 | FROM Bowler_Scores 16 | WHERE Bowler_Scores.BowlerID = Bowlers.BowlerID), 0), BowlerCurrentHcp = 17 | COALESCE((SELECT Round(0.9 * (200 - Round(AVG(Bowler_Scores.RawScore), 0)), 0) 18 | FROM Bowler_Scores 19 | WHERE Bowler_Scores.BowlerID = Bowlers.BowlerID), 0); 20 | $$ LANGUAGE SQL; 21 | 22 | CREATE VIEW CH15_Calc_Bowler_Pins_Avg_Hcp_Query 23 | AS 24 | SELECT Bowlers.BowlerID, Bowlers.BowlerTotalPins, 25 | (SELECT SUM(RawScore) 26 | FROM Bowler_Scores 27 | WHERE Bowler_Scores.BowlerID = Bowlers.BowlerID) 28 | AS NewBowlerTotalPins, Bowlers.BowlerGamesBowled, 29 | (SELECT COUNT(Bowler_Scores.RawScore) 30 | FROM Bowler_Scores 31 | WHERE Bowler_Scores.BowlerID = Bowlers.BowlerID) 32 | AS NewBowlerGamesBowled, Bowlers.BowlerCurrentAverage, 33 | COALESCE((SELECT Round(AVG(Bowler_Scores.RawScore), 0) 34 | FROM Bowler_Scores 35 | WHERE Bowler_Scores.BowlerID = Bowlers.BowlerID), 0) 36 | AS NewBowlerCurrentAverage, Bowlers.BowlerCurrentHcp, 37 | COALESCE((SELECT Round(0.9 * (200 - Round(AVG(Bowler_Scores.RawScore), 0)), 0) 38 | FROM Bowler_Scores 39 | WHERE Bowler_Scores.BowlerID = Bowlers.BowlerID), 0) 40 | AS NewBowlerCurrentHcp 41 | FROM Bowlers; 42 | CREATE FUNCTION CH15_Change_Huckleberry_Name() RETURNS void AS $$ 43 | 44 | UPDATE Teams 45 | SET TeamName = 'Manta Rays' 46 | WHERE (TeamName = 'Huckleberrys'); 47 | $$ LANGUAGE SQL; 48 | 49 | CREATE VIEW CH15_Change_Huckleberry_Name_Query 50 | AS 51 | SELECT Teams.TeamName, 'Manta Rays' AS NewTeamName 52 | FROM Teams 53 | WHERE Teams.TeamName = 'Huckleberrys'; 54 | 55 | CREATE FUNCTION CH15_Change_Tourney_Location() RETURNS void AS $$ 56 | UPDATE Tournaments 57 | SET TourneyLocation = 'Oasis Lanes' 58 | WHERE (TourneyLocation = 'Sports World Lanes'); 59 | $$ LANGUAGE SQL; 60 | 61 | CREATE VIEW CH15_Change_Tourney_Location_Query 62 | AS 63 | SELECT Tournaments.TourneyID, Tournaments.TourneyLocation, 'Oasis Lanes' AS NewTourneyLocation 64 | FROM Tournaments 65 | WHERE (Tournaments.TourneyLocation = 'Sports World Lanes'); 66 | 67 | CREATE FUNCTION CH15_Update_Bowler_City_State() RETURNS void AS $$ 68 | UPDATE Bowlers 69 | SET BowlerCity = 70 | (SELECT City 71 | FROM WAZips 72 | WHERE Bowlers.BowlerZip = WAZips.ZIP), BowlerState = 73 | (SELECT State 74 | FROM WAZips 75 | WHERE Bowlers.BowlerZip = WAZips.ZIP) 76 | WHERE (BowlerCity <> 77 | (SELECT City 78 | FROM WAZips 79 | WHERE Bowlers.BowlerZip = WAZips.ZIP)) OR 80 | (BowlerState <> 81 | (SELECT State 82 | FROM WAZips 83 | WHERE Bowlers.BowlerZip = WAZips.ZIP)); 84 | $$ LANGUAGE SQL; 85 | 86 | CREATE VIEW CH15_Update_Bowler_City_State_Query 87 | AS 88 | SELECT Bowlers.BowlerCity, 89 | (SELECT City 90 | FROM WAZips 91 | WHERE Bowlers.BowlerZip = WAZips.ZIP) AS NewBowlerCity, Bowlers.BowlerState, 92 | (SELECT State 93 | FROM WAZips 94 | WHERE Bowlers.BowlerZip = WAZips.ZIP) AS NewBowlerState 95 | FROM Bowlers 96 | WHERE (BowlerCity <> 97 | (SELECT City 98 | FROM WAZips 99 | WHERE Bowlers.BowlerZip = WAZips.ZIP)) OR 100 | (BowlerState <> 101 | (SELECT State 102 | FROM WAZips 103 | WHERE Bowlers.BowlerZip = WAZips.ZIP)); 104 | 105 | CREATE FUNCTION CH16_Add_Bowler() RETURNS void AS $$ 106 | INSERT INTO Bowlers 107 | (BowlerLastName, BowlerFirstName, BowlerAddress, BowlerCity, BowlerState, BowlerZip, BowlerPhoneNumber, TeamID, BowlerTotalPins, 108 | BowlerGamesBowled, BowlerCurrentAverage, BowlerCurrentHcp) 109 | SELECT BowlerLastName, 'Matthew' AS BowlerFirstName, BowlerAddress, BowlerCity, BowlerState, BowlerZip, BowlerPhoneNumber, TeamID, 0 AS BowlerTotalPins, 0 AS BowlerGamesBowled, 0 AS BolwerCurrentAverage, 0 AS BowlerCurrentHcp 110 | FROM Bowlers 111 | WHERE (BowlerLastName = 'Patterson') AND (BowlerFirstName = 'Neil'); 112 | $$ LANGUAGE SQL; 113 | 114 | CREATE VIEW CH16_Add_Bowler_Query 115 | AS 116 | SELECT Bowlers.BowlerLastName, 'Matthew' AS NewBowlerFirstName, Bowlers.BowlerAddress, Bowlers.BowlerCity, Bowlers.BowlerState, Bowlers.BowlerZip, Bowlers.BowlerPhoneNumber, Bowlers.TeamID, 0 AS BowlerTotalPins, 0 AS BowlerGamesBowled, 0 AS BowlerCurrentAverage, 0 AS BowlerCurrentHcp 117 | FROM Bowlers 118 | WHERE (((Bowlers.BowlerLastName)='Patterson') AND ((Bowlers.BowlerFirstName)='Neil')); 119 | 120 | CREATE FUNCTION CH16_Add_Team() RETURNS void AS $$ 121 | INSERT INTO Teams 122 | (TeamName, CaptainID) 123 | VALUES ('Aardvarks', NULL); 124 | $$ LANGUAGE SQL; 125 | 126 | CREATE VIEW CH16_Add_Team_Query 127 | AS 128 | SELECT 'Aardvarks' As TeamName, Null As CaptainID; 129 | 130 | CREATE FUNCTION CH16_Archive_2017_Tournaments_1() RETURNS void AS $$ 131 | INSERT INTO Tournaments_Archive 132 | SELECT TourneyID, TourneyDate, TourneyLocation 133 | FROM Tournaments 134 | WHERE (TourneyDate BETWEEN CAST('2017-01-01' AS Date) AND CAST('2017-12-31' AS Date) ); 135 | $$ LANGUAGE SQL; 136 | 137 | CREATE VIEW CH16_Archive_2017_Tournaments_1_Query 138 | AS 139 | SELECT TourneyID, TourneyDate, TourneyLocation 140 | FROM Tournaments 141 | WHERE (TourneyDate BETWEEN CAST('2017-01-01' AS Date) AND CAST('2017-12-31' AS Date) ); 142 | 143 | CREATE FUNCTION CH16_Archive_2017_Tournaments_2() RETURNS void AS $$ 144 | INSERT INTO Tourney_Matches_Archive 145 | SELECT MatchID, TourneyID, Lanes, OddLaneTeamID, EvenLaneTeamID 146 | FROM Tourney_Matches 147 | WHERE (TourneyID IN 148 | (SELECT TourneyID 149 | FROM Tournaments 150 | WHERE TourneyDate BETWEEN CAST('2017-01-01' AS Date) AND CAST('2017-12-31' AS Date) )); 151 | $$ LANGUAGE SQL; 152 | 153 | CREATE VIEW CH16_Archive_2017_Tournaments_2_Query 154 | AS 155 | SELECT MatchID, TourneyID, Lanes, OddLaneTeamID, EvenLaneTeamID 156 | FROM Tourney_Matches 157 | WHERE (TourneyID IN 158 | (SELECT TourneyID 159 | FROM Tournaments 160 | WHERE TourneyDate BETWEEN CAST('2017-01-01' AS Date) AND CAST('2017-12-31' AS Date) )); 161 | 162 | CREATE FUNCTION CH16_Archive_2017_Tournaments_3() RETURNS void AS $$ 163 | INSERT INTO Match_Games_Archive 164 | SELECT MatchID, GameNumber, NULL AS WinTeam 165 | FROM Match_Games 166 | WHERE (MatchID IN 167 | (SELECT Tourney_Matches.MatchID 168 | FROM Tourney_Matches INNER JOIN 169 | Tournaments ON Tournaments.TourneyID = Tourney_Matches.TourneyID 170 | WHERE Tournaments.TourneyDate BETWEEN CAST('2017-01-01' AS Date) AND CAST('2017-12-31' AS Date) )); 171 | $$ LANGUAGE SQL; 172 | 173 | CREATE VIEW CH16_Archive_2017_Tournaments_3_Query 174 | AS 175 | SELECT MatchID, GameNumber, NULL AS WinTeam 176 | FROM Match_Games 177 | WHERE (MatchID IN 178 | (SELECT Tourney_Matches.MatchID 179 | FROM Tourney_Matches INNER JOIN 180 | Tournaments ON Tournaments.TourneyID = Tourney_Matches.TourneyID 181 | WHERE Tournaments.TourneyDate BETWEEN CAST('2017-01-01' AS Date) AND CAST('2017-12-31' AS Date) )); 182 | 183 | CREATE FUNCTION CH16_Archive_2017_Tournaments_4() RETURNS void AS $$ 184 | INSERT INTO Bowler_Scores_Archive 185 | SELECT MatchID, GameNumber, BowlerID, RawScore, HandiCapScore, WonGame 186 | FROM Bowler_Scores 187 | WHERE (MatchID IN 188 | (SELECT Tourney_Matches.MatchID 189 | FROM Tourney_Matches INNER JOIN 190 | Tournaments ON Tournaments.TourneyID = Tourney_Matches.TourneyID 191 | WHERE Tournaments.TourneyDate BETWEEN CAST('2017-01-01' AS Date) AND CAST('2017-12-31' AS Date) )); 192 | $$ LANGUAGE SQL; 193 | 194 | CREATE VIEW CH16_Archive_2017_Tournaments_4_Query 195 | AS 196 | SELECT MatchID, GameNumber, BowlerID, RawScore, HandiCapScore, WonGame 197 | FROM Bowler_Scores 198 | WHERE (MatchID IN 199 | (SELECT Tourney_Matches.MatchID 200 | FROM Tourney_Matches INNER JOIN 201 | Tournaments ON Tournaments.TourneyID = Tourney_Matches.TourneyID 202 | WHERE Tournaments.TourneyDate BETWEEN CAST('2017-01-01' AS Date) AND CAST('2017-12-31' AS Date) )); 203 | 204 | CREATE FUNCTION CH16_Copy_2017_Tournaments_1() RETURNS void AS $$ 205 | INSERT INTO Tournaments 206 | (TourneyID, TourneyDate, TourneyLocation) 207 | SELECT TourneyID + 25 AS NewTourneyID, TourneyDate + 728 AS NewTourneyDate, TourneyLocation 208 | FROM Tournaments 209 | WHERE (TourneyDate BETWEEN CAST('2017-01-01' AS Date) AND CAST('2017-12-31' AS Date) ); 210 | SELECT setval('tournaments_tourneyID_seq', (SELECT MAX(TourneyID) FROM Tournaments)); 211 | $$ LANGUAGE SQL; 212 | 213 | CREATE VIEW CH16_Copy_2017_Tournaments_1_Query 214 | AS 215 | SELECT TourneyID + 25 AS NewTourneyID, TourneyDate + 728 AS NewTourneyDate, TourneyLocation 216 | FROM Tournaments 217 | WHERE (TourneyDate BETWEEN CAST('2017-01-01' AS Date) AND CAST('2017-12-31' AS Date) ); 218 | 219 | CREATE FUNCTION CH16_Copy_2017_Tournaments_2() RETURNS void AS $$ 220 | INSERT INTO Tourney_Matches 221 | (TourneyID, Lanes, OddLaneTeamID, EvenLaneTeamID) 222 | SELECT MatchID, TourneyID + 25 AS NewTourneyID, Lanes, OddLaneTeamID, EvenLaneTeamID 223 | FROM Tourney_Matches 224 | WHERE Tourney_Matches.TourneyID IN 225 | (SELECT TourneyID 226 | FROM Tournaments 227 | WHERE Tournaments.TourneyDate Between CAST('2017-01-01' AS DATE) AND CAST('2017-12-31' AS Date) ); 228 | $$ LANGUAGE SQL; 229 | 230 | CREATE VIEW CH16_Copy_2017_Tournaments_2_Query 231 | AS 232 | SELECT TourneyID + 25 AS NewTourneyID, Lanes, OddLaneTeamID, EvenLaneTeamID 233 | FROM Tourney_Matches 234 | WHERE Tourney_Matches.TourneyID IN 235 | (SELECT TourneyID 236 | FROM Tournaments 237 | WHERE Tournaments.TourneyDate Between CAST('2017-01-01' AS DATE) AND CAST('2017-12-31' AS Date) ); 238 | 239 | CREATE FUNCTION CH17_Delete_Archived_2017_Tournaments_1() RETURNS void AS $$ 240 | DELETE FROM Bowler_Scores 241 | WHERE (MatchID IN 242 | (SELECT Tourney_Matches_Archive.MatchID 243 | FROM Tourney_Matches_Archive)); 244 | $$ LANGUAGE SQL; 245 | 246 | CREATE VIEW CH17_Delete_Archived_2017_Tournaments_1_Query 247 | AS 248 | SELECT * FROM Bowler_Scores 249 | WHERE (MatchID IN 250 | (SELECT Tourney_Matches_Archive.MatchID 251 | FROM Tourney_Matches_Archive)); 252 | 253 | CREATE FUNCTION CH17_Delete_Archived_2017_Tournaments_1_WRONG() RETURNS void AS $$ 254 | DELETE FROM Bowler_Scores 255 | WHERE (MatchID IN 256 | (SELECT Tourney_Matches.MatchID 257 | FROM Tourney_Matches INNER JOIN 258 | Tournaments ON Tournaments.TourneyID = Tourney_Matches.TourneyID 259 | WHERE Tournaments.TourneyDate BETWEEN CAST('2017-01-01' AS Date) AND CAST('2017-12-31' AS Date) )); 260 | $$ LANGUAGE SQL; 261 | 262 | CREATE VIEW CH17_Delete_Archived_2017_Tournaments_1_WRONG_Query 263 | AS 264 | SELECT * FROM Bowler_Scores 265 | WHERE (MatchID IN 266 | (SELECT Tourney_Matches.MatchID 267 | FROM Tourney_Matches INNER JOIN 268 | Tournaments ON Tournaments.TourneyID = Tourney_Matches.TourneyID 269 | WHERE Tournaments.TourneyDate BETWEEN CAST('2017-01-01' AS Date) AND CAST('2017-12-31' AS Date) )); 270 | 271 | CREATE FUNCTION CH17_Delete_Archived_2017_Tournaments_2() RETURNS void AS $$ 272 | DELETE FROM Match_Games 273 | WHERE (MatchID IN 274 | (SELECT Tourney_Matches_Archive.MatchID 275 | FROM Tourney_Matches_Archive)); 276 | $$ LANGUAGE SQL; 277 | 278 | CREATE VIEW CH17_Delete_Archived_2017_Tournaments_2_Query 279 | AS 280 | SELECT * FROM Match_Games 281 | WHERE (MatchID IN 282 | (SELECT Tourney_Matches_Archive.MatchID 283 | FROM Tourney_Matches_Archive)); 284 | 285 | CREATE FUNCTION CH17_Delete_Archived_2017_Tournaments_3() RETURNS void AS $$ 286 | 287 | DELETE FROM Tourney_Matches 288 | WHERE (MatchID IN 289 | (SELECT MatchID 290 | FROM Tourney_Matches_Archive)); 291 | $$ LANGUAGE SQL; 292 | 293 | CREATE VIEW CH17_Delete_Archived_2017_Tournaments_3_Query 294 | AS 295 | SELECT * FROM Tourney_Matches 296 | WHERE (MatchID IN 297 | (SELECT MatchID 298 | FROM Tourney_Matches_Archive)); 299 | 300 | CREATE FUNCTION CH17_Delete_Archived_2017_Tournaments_4() RETURNS void AS $$ 301 | DELETE FROM Tournaments 302 | WHERE (TourneyID IN 303 | (SELECT Tournaments_Archive.TourneyID 304 | FROM Tournaments_Archive)); 305 | $$ LANGUAGE SQL; 306 | 307 | CREATE VIEW CH17_Delete_Archived_2017_Tournaments_4_Query 308 | AS 309 | SELECT * FROM Tournaments 310 | WHERE (TourneyID IN 311 | (SELECT Tournaments_Archive.TourneyID 312 | FROM Tournaments_Archive)); 313 | 314 | CREATE FUNCTION CH17_Delete_Bowlers_No_Games() RETURNS void AS $$ 315 | DELETE FROM Bowlers 316 | WHERE (BowlerGamesBowled = 0); 317 | $$ LANGUAGE SQL; 318 | 319 | CREATE VIEW CH17_Delete_Bowlers_No_Games_Query 320 | AS 321 | SELECT * FROM Bowlers 322 | WHERE (BowlerGamesBowled = 0); 323 | 324 | CREATE FUNCTION CH17_Delete_Bowlers_No_Games_Safe() RETURNS void AS $$ 325 | DELETE FROM Bowlers 326 | WHERE (BowlerID NOT IN 327 | (SELECT BowlerID 328 | FROM Bowler_Scores)); 329 | $$ LANGUAGE SQL; 330 | 331 | CREATE VIEW CH17_Delete_Bowlers_No_Games_Safe_Query 332 | AS 333 | SELECT * FROM Bowlers 334 | WHERE (BowlerID NOT IN 335 | (SELECT BowlerID 336 | FROM Bowler_Scores)); 337 | 338 | CREATE FUNCTION CH17_Delete_Matches_Not_Played() RETURNS void AS $$ 339 | DELETE FROM Tourney_Matches 340 | WHERE (MatchID NOT IN 341 | (SELECT MatchID 342 | FROM Bowler_Scores)) 343 | AND (MatchID NOT IN 344 | (SELECT MatchID 345 | FROM Match_Games)); 346 | $$ LANGUAGE SQL; 347 | 348 | CREATE VIEW CH17_Delete_Matches_Not_Played_Query 349 | AS 350 | SELECT * FROM Tourney_Matches 351 | WHERE (MatchID NOT IN 352 | (SELECT MatchID 353 | FROM Bowler_Scores)) 354 | AND (MatchID NOT IN 355 | (SELECT MatchID 356 | FROM Match_Games)); 357 | 358 | -------------------------------------------------------------------------------- /Week 1/sql/ddl/02 SalesOrdersModifyProcsPG.sql: -------------------------------------------------------------------------------- 1 | -- This requires that the tables from 00 SalesOrdersStructureModifyPG.sql be created first. 2 | 3 | SET search_path TO SalesOrdersModify; 4 | 5 | CREATE FUNCTION CH15_Adjust_Accessory_Retail_Price () RETURNS void AS $$ 6 | UPDATE Products SET RetailPrice = ROUND(1.35 * 7 | (SELECT DISTINCT WholesalePrice 8 | FROM Product_Vendors 9 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber 10 | AND WholesalePrice = 11 | (SELECT max(WholesalePrice) 12 | FROM Product_Vendors 13 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber)), 0) 14 | WHERE RetailPrice < 1.35 * 15 | (SELECT DISTINCT WholesalePrice 16 | FROM Product_Vendors 17 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber 18 | AND WholesalePrice = 19 | (SELECT max(WholesalePrice) 20 | FROM Product_Vendors 21 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber) LIMIT 1) 22 | AND CategoryID = 1; 23 | $$ LANGUAGE SQL; 24 | 25 | CREATE VIEW CH15_Adjust_Accessory_Retail_Price_Query 26 | AS 27 | SELECT RetailPrice, ROUND(1.35 * 28 | (SELECT DISTINCT WholesalePrice 29 | FROM Product_Vendors 30 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber 31 | AND WholesalePrice = 32 | (SELECT max(WholesalePrice) 33 | FROM Product_Vendors 34 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber)), 0) As UpdatedPrice 35 | FROM Products 36 | WHERE RetailPrice < 1.35 * 37 | (SELECT DISTINCT WholesalePrice 38 | FROM Product_Vendors 39 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber 40 | AND WholesalePrice = 41 | (SELECT max(WholesalePrice) 42 | FROM Product_Vendors 43 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber) LIMIT 1) 44 | AND CategoryID = 1 ; 45 | 46 | 47 | CREATE FUNCTION CH15_Adjust_Bike_Retail_Price () RETURNS void AS $$ 48 | UPDATE Products SET RetailPrice = ROUND(1.45 * 49 | (SELECT DISTINCT WholesalePrice 50 | FROM Product_Vendors 51 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber 52 | AND WholesalePrice = 53 | (SELECT min(WholesalePrice) 54 | FROM Product_Vendors 55 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber)), 0) 56 | WHERE RetailPrice < 1.45 * 57 | (SELECT DISTINCT WholesalePrice 58 | FROM Product_Vendors 59 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber 60 | AND WholesalePrice = 61 | (SELECT min(WholesalePrice) 62 | FROM Product_Vendors 63 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber)) AND CategoryID = 2 ; 64 | $$ LANGUAGE SQL; 65 | 66 | CREATE VIEW CH15_Adjust_Bike_Retail_Price_Query 67 | AS 68 | SELECT ProductNumber, ProductName, RetailPrice, ROUND(1.45 * 69 | (SELECT DISTINCT WholesalePrice 70 | FROM Product_Vendors 71 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber 72 | AND WholesalePrice = 73 | (SELECT min(WholesalePrice) 74 | FROM Product_Vendors 75 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber)), 0) As UpdatedPrice 76 | FROM Products 77 | WHERE RetailPrice < 1.45 * 78 | (SELECT DISTINCT WholesalePrice 79 | FROM Product_Vendors 80 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber 81 | AND WholesalePrice = 82 | (SELECT min(WholesalePrice) 83 | FROM Product_Vendors 84 | WHERE Product_Vendors.ProductNumber = Products.ProductNumber)) AND CategoryID = 2 ; 85 | 86 | 87 | CREATE FUNCTION CH15_Adjust_Late_Order_Prices () RETURNS void AS $$ 88 | UPDATE Order_Details 89 | SET QuotedPrice = ROUND(QuotedPrice * 0.98, 2) 90 | WHERE (OrderNumber IN 91 | (SELECT Orders.OrderNumber 92 | FROM Orders 93 | WHERE (Orders.ShipDate - Orders.OrderDate) > 30)); 94 | $$ LANGUAGE SQL; 95 | 96 | CREATE VIEW CH15_Adjust_Late_Order_Prices_Query 97 | AS 98 | SELECT OrderNumber, ProductNumber, QuotedPrice, ROUND(QuotedPrice * 0.98, 2) As UpdatedPrice 99 | FROM Order_Details 100 | WHERE (OrderNumber IN 101 | (SELECT Orders.OrderNumber 102 | FROM Orders 103 | WHERE (Orders.ShipDate - Orders.OrderDate) > 30)); 104 | 105 | 106 | CREATE FUNCTION CH15_Give_Discount_To_Good_October_Customers () RETURNS void AS $$ 107 | UPDATE Order_Details 108 | SET QuotedPrice = ROUND(QuotedPrice * .95, 2) 109 | WHERE (OrderNumber IN 110 | (SELECT Orders.OrderNumber 111 | FROM Orders 112 | WHERE Orders.CustomerID IN 113 | (SELECT O2.CustomerID 114 | FROM Orders AS O2 INNER JOIN Order_Details 115 | ON O2.OrderNumber = Order_Details.OrderNumber 116 | WHERE O2.OrderDate BETWEEN CAST('2017-10-01' AS Date) 117 | AND CAST('2017-10-31' As Date) 118 | GROUP BY O2.CustomerID 119 | HAVING SUM(Order_Details.QuotedPrice * Order_Details.QuantityOrdered) 120 | > 50000))); 121 | $$ LANGUAGE SQL; 122 | 123 | CREATE VIEW CH15_Give_Discount_To_Good_October_Customers_Query 124 | AS 125 | SELECT QuotedPrice, ROUND(QuotedPrice * .95, 2) As UpdatedPrice 126 | FROM Order_Details 127 | WHERE (OrderNumber IN 128 | (SELECT Orders.OrderNumber 129 | FROM Orders 130 | WHERE Orders.CustomerID IN 131 | (SELECT O2.CustomerID 132 | FROM Orders AS O2 INNER JOIN Order_Details 133 | ON O2.OrderNumber = Order_Details.OrderNumber 134 | WHERE O2.OrderDate BETWEEN CAST('2017-10-01' AS Date) 135 | AND CAST('2017-10-31' As Date) 136 | GROUP BY O2.CustomerID 137 | HAVING SUM(Order_Details.QuotedPrice * Order_Details.QuantityOrdered) 138 | > 50000))); 139 | 140 | 141 | CREATE FUNCTION CH15_Update_Order_Totals_Subquery () RETURNS void AS $$ 142 | UPDATE Orders 143 | SET OrderTotal = 144 | (SELECT SUM(Order_Details.QuantityOrdered * Order_Details.QuotedPrice) 145 | FROM Order_Details 146 | WHERE Order_Details.OrderNumber = Orders.OrderNumber); 147 | $$ LANGUAGE SQL; 148 | 149 | CREATE VIEW CH15_Update_Order_Totals_Subquery_Query 150 | AS 151 | SELECT OrderTotal, 152 | (SELECT SUM(Order_Details.QuantityOrdered * Order_Details.QuotedPrice) 153 | FROM Order_Details 154 | WHERE Order_Details.OrderNumber = Orders.OrderNumber) As NewTotal 155 | FROM Orders; 156 | 157 | 158 | CREATE FUNCTION CH16_Add_Sales_Customer () RETURNS void AS $$ 159 | INSERT INTO Customers 160 | (CustFirstName, CustLastName, CustStreetAddress, CustCity, CustState, 161 | CustZipCode, CustAreaCode, CustPhoneNumber) 162 | VALUES ('Mary', 'Baker', '7834 W 32nd Ct', 'Bothell', 'WA', '98011', 425, '555-9876'); 163 | $$ LANGUAGE SQL; 164 | 165 | CREATE VIEW CH16_Add_Sales_Customer_Query 166 | AS 167 | SELECT 'Mary' As CustFirstName, 'Baker' As CustLastName, '7834 W 32nd Ct' As CustStreetAddress, 168 | 'Bothell' As CustCity, 'WA' As CUstState, '98011' As CustZipCode, 425 As CustAreaCode, 169 | '555-9876' As CustPhoneNumber 170 | FROM Customers 171 | WHERE CustomerID = 1001; 172 | 173 | 174 | CREATE FUNCTION CH16_Add_Employee () RETURNS void AS $$ 175 | INSERT INTO Employees 176 | (EmpFirstName, EmpLastName, EmpStreetAddress, EmpCity, EmpState, 177 | EmpZipCode, EmpAreaCode, EmpPhoneNumber) 178 | VALUES ('Susan', 'Metters', '16547 NE 132nd St', 'Woodinville', 'WA', '98072', 425, '555-7825'); 179 | $$ LANGUAGE SQL; 180 | 181 | CREATE VIEW CH16_Add_Employee_Query 182 | AS 183 | SELECT 'Susan' As EmpFirstName, 'Metters' As EmpLastName, '16547 NE 132nd St' As EmpStreetAddress, 184 | 'Woodinville' As EmpCity, 'WA' As EmpState, '98072' As EmpZipCode, 425 As EmpAreaCode, 185 | '555-7825' As EmpPhoneNumber 186 | FROM Employees 187 | WHERE EmployeeID = 701; 188 | 189 | 190 | CREATE FUNCTION CH16_Add_Product () RETURNS void AS $$ 191 | INSERT INTO Products ( ProductName, RetailPrice, CategoryID ) 192 | SELECT 'Hot Dog Spinner', 895, CategoryID 193 | FROM Categories 194 | WHERE CategoryDescription = 'Bikes'; 195 | $$ LANGUAGE SQL; 196 | 197 | CREATE VIEW CH16_Add_Product_Query 198 | AS 199 | SELECT 'Hot Dog Spinner' As ProductName, 895 As RetailPrice, CategoryID 200 | FROM Categories 201 | WHERE 202 | CategoryDescription = 'Bikes'; 203 | 204 | 205 | CREATE FUNCTION CH16_Add_Vendor () RETURNS void AS $$ 206 | INSERT INTO Vendors 207 | (VendName, VendStreetAddress, VendCity, VendState, VendZipCode, VendPhoneNumber, 208 | VendFaxNumber, VendWebPage, VendEMailAddress) 209 | VALUES ('Hot Dog Bikes', '1234 Main Street', 'Chicago', 'IL', '60620', '(773) 555-6543', 210 | '(773) 555-6542', 'http://www.hotdogbikes.com/', 'Sales@hotdogbikes.com'); 211 | $$ LANGUAGE SQL; 212 | 213 | CREATE VIEW CH16_Add_Vendor_Query 214 | AS 215 | SELECT 'Hot Dog Bikes' As VendName, '1234 Main Street' As VendStreetAddress, 216 | 'Chicago' As VendCity, 'IL' As VendState, '60620' As VendZipCode, 217 | '(773) 555-6543' As VendPhoneNumber, '(773) 555-6542' As VendFaxNumber, 218 | 'http://www.hotdogbikes.com/' As VendWebPage, 'Sales@hotdogbikes.com' As VendEMailAddress 219 | FROM Vendors 220 | WHERE VendorID = 1; 221 | 222 | 223 | CREATE FUNCTION CH16_Archive_2017_Order_Details () RETURNS void AS $$ 224 | INSERT INTO Order_Details_Archive 225 | SELECT OrderNumber AS OrderNumber, ProductNumber AS ProductNumber, QuotedPrice AS QuotedPrice, 226 | QuantityOrdered AS QuantityOrdered 227 | FROM Order_Details 228 | WHERE (OrderNumber IN 229 | (SELECT OrderNumber 230 | FROM Orders 231 | WHERE (Orders.OrderDate < Cast('2018-01-01' As Date)))); 232 | $$ LANGUAGE SQL; 233 | 234 | CREATE VIEW CH16_Archive_2017_Order_Details_Query 235 | AS 236 | SELECT OrderNumber, ProductNumber, QuotedPrice, QuantityOrdered 237 | FROM Order_Details 238 | WHERE (OrderNumber IN 239 | (SELECT OrderNumber 240 | FROM Orders 241 | WHERE (Orders.OrderDate < Cast('2018-01-01' As Date)))); 242 | 243 | 244 | CREATE FUNCTION CH16_Archive_2017_Orders () RETURNS void AS $$ 245 | INSERT INTO Orders_Archive 246 | SELECT OrderNumber, OrderDate, ShipDate, CustomerID, EmployeeID, OrderTotal 247 | FROM Orders 248 | WHERE (OrderDate < Cast('2018-01-01' As Date)); 249 | $$ LANGUAGE SQL; 250 | 251 | CREATE VIEW CH16_Archive_2017_Orders_Query 252 | AS 253 | SELECT OrderNumber, OrderDate, ShipDate, CustomerID, EmployeeID, OrderTotal 254 | FROM Orders 255 | WHERE (OrderDate < Cast('2018-01-01' As Date)); 256 | 257 | 258 | CREATE FUNCTION CH16_Copy_Customer_To_Employee () RETURNS void AS $$ 259 | INSERT INTO Employees 260 | (EmpFirstName, EmpLastName, EmpStreetAddress, EmpCity, EmpState, EmpZipCode, 261 | EmpAreaCode, EmpPhoneNumber) 262 | SELECT CustFirstName, CustLastName, CustStreetAddress, CustCity, CustState, CustZipCode, 263 | CustAreaCode, CustPhoneNumber 264 | FROM Customers 265 | WHERE (CustFirstName = 'David') AND (CustLastName = 'Smith'); 266 | $$ LANGUAGE SQL; 267 | 268 | CREATE VIEW CH16_Copy_Customer_To_Employee_Query 269 | AS 270 | SELECT CustFirstName, CustLastName, CustStreetAddress, CustCity, CustState, CustZipCode, 271 | CustAreaCode, CustPhoneNumber 272 | FROM Customers 273 | WHERE (CustFirstName = 'David') AND (CustLastName = 'Smith'); 274 | 275 | 276 | CREATE FUNCTION CH16_Copy_Dec12_OrderDetails_For_Keyser () RETURNS void AS $$ 277 | INSERT INTO Order_Details 278 | (OrderNumber, ProductNumber, QuotedPrice, QuantityOrdered) 279 | SELECT OrderNumber + 1000 AS NewOrderNo, ProductNumber, QuotedPrice, QuantityOrdered 280 | FROM Order_Details 281 | WHERE (OrderNumber IN 282 | (SELECT OrderNumber 283 | FROM Orders 284 | WHERE OrderDate = Cast('2017-12-12' As Date) 285 | AND CustomerID = 286 | (SELECT CustomerID 287 | FROM Customers 288 | WHERE CustFirstName = 'Liz' AND CustLastName = 'Keyser'))); 289 | $$ LANGUAGE SQL; 290 | 291 | CREATE VIEW CH16_Copy_Dec12_OrderDetails_For_Keyser_Query 292 | AS 293 | SELECT OrderNumber + 1000 AS NewOrderNo, ProductNumber, QuotedPrice, QuantityOrdered 294 | FROM Order_Details 295 | WHERE (OrderNumber IN 296 | (SELECT OrderNumber 297 | FROM Orders 298 | WHERE OrderDate = Cast('2017-12-12' As Date) 299 | AND CustomerID = 300 | (SELECT CustomerID 301 | FROM Customers 302 | WHERE CustFirstName = 'Liz' AND CustLastName = 'Keyser'))); 303 | 304 | 305 | CREATE FUNCTION CH16_Copy_Dec12_Order_For_Keyser () RETURNS bigint AS $$ 306 | INSERT INTO Orders 307 | (OrderNumber, OrderDate, ShipDate, CustomerID, EmployeeID, OrderTotal) 308 | SELECT OrderNumber + 1000 AS NewOrderNo, Cast('2018-12-06' As Date) AS OrderDate, 309 | Cast('2018-06-15' As Date) AS ShipDate, CustomerID, EmployeeID, OrderTotal 310 | FROM Orders 311 | WHERE (OrderDate = Cast('2017-12-12' As Date)) AND (CustomerID = 312 | (SELECT CustomerID 313 | FROM Customers 314 | WHERE CustFirstName = 'Liz' AND CustLastName = 'Keyser')); 315 | SELECT setval('Orders_OrderNumber_seq', 316 | (SELECT OrderNumber + 1000 317 | FROM Orders 318 | WHERE (OrderDate = CAST('2017-12-12' AS Date)) 319 | AND (CustomerID = 320 | (SELECT CustomerID 321 | FROM Customers 322 | WHERE CustFirstName = 'Liz' AND CustLastName = 'Keyser')))); 323 | $$ LANGUAGE SQL; 324 | 325 | CREATE VIEW CH16_Copy_Dec12_Order_For_Keyser_Query 326 | AS 327 | SELECT OrderNumber + 1000 AS NewOrderNo, Cast('2018-12-06' As Date) AS OrderDate, 328 | Cast('2018-06-15' As Date) AS ShipDate, CustomerID, EmployeeID, OrderTotal 329 | FROM Orders 330 | WHERE (OrderDate = Cast('2017-12-12' As Date)) AND (CustomerID = 331 | (SELECT CustomerID 332 | FROM Customers 333 | WHERE CustFirstName = 'Liz' AND CustLastName = 'Keyser')); 334 | 335 | 336 | CREATE FUNCTION CH16_Copy_November_OrderDetails_For_AKennedy () RETURNS void AS $$ 337 | INSERT INTO Order_Details 338 | (OrderNumber, ProductNumber, QuotedPrice, QuantityOrdered) 339 | SELECT OrderNumber + 1000 AS NewOrderNo, ProductNumber, QuotedPrice, QuantityOrdered 340 | FROM Order_Details 341 | WHERE (OrderNumber IN 342 | (SELECT OrderNumber 343 | FROM Orders 344 | WHERE OrderDate BETWEEN Cast('2017-11-01' As Date) AND Cast('2017-11-30' As Date) 345 | AND CustomerID = 346 | (SELECT CustomerID 347 | FROM Customers 348 | WHERE CustFirstName = 'Angel' AND CustLastName = 'Kennedy'))); 349 | $$ LANGUAGE SQL; 350 | 351 | CREATE VIEW CH16_Copy_November_OrderDetails_For_AKennedy_Query 352 | AS 353 | SELECT OrderNumber + 1000 AS NewOrderNo, ProductNumber, QuotedPrice, QuantityOrdered 354 | FROM Order_Details 355 | WHERE (OrderNumber IN 356 | (SELECT OrderNumber 357 | FROM Orders 358 | WHERE OrderDate BETWEEN Cast('2017-11-01' As Date) AND Cast('2017-11-30' As Date) 359 | AND CustomerID = 360 | (SELECT CustomerID 361 | FROM Customers 362 | WHERE CustFirstName = 'Angel' AND CustLastName = 'Kennedy'))); 363 | 364 | 365 | CREATE FUNCTION CH16_Copy_November_Orders_For_AKennedy () RETURNS bigint AS $$ 366 | INSERT INTO Orders 367 | (OrderNumber, OrderDate, ShipDate, CustomerID, EmployeeID, OrderTotal) 368 | SELECT OrderNumber + 1000 AS NewOrderNo, Cast('2018-06-15' As Date) AS OrderDate, 369 | Cast('2018-06-18' As Date) AS ShipDate, CustomerID, EmployeeID, OrderTotal 370 | FROM Orders 371 | WHERE (OrderDate BETWEEN Cast('2017-11-01' As Date) AND Cast('2017-11-30' As Date)) 372 | AND (CustomerID = 373 | (SELECT CustomerID 374 | FROM Customers 375 | WHERE CustFirstName = 'Angel' AND CustLastName = 'Kennedy')); 376 | SELECT setval('Orders_OrderNumber_seq', 377 | (SELECT Max(OrderNumber) FROM Orders)); 378 | $$ LANGUAGE SQL; 379 | 380 | CREATE VIEW CH16_Copy_November_Orders_For_AKennedy_Query 381 | AS 382 | SELECT OrderNumber + 1000 AS NewOrderNo, Cast('2018-06-15' As Date) AS OrderDate, 383 | Cast('2018-06-18' As Date) AS ShipDate, CustomerID, EmployeeID, OrderTotal 384 | FROM Orders 385 | WHERE (OrderDate BETWEEN Cast('2017-11-01' As Date) AND Cast('2017-11-30' As Date)) 386 | AND (CustomerID = 387 | (SELECT CustomerID 388 | FROM Customers 389 | WHERE CustFirstName = 'Angel' AND CustLastName = 'Kennedy')); 390 | 391 | 392 | CREATE FUNCTION CH17_Delete_Archived_Order_Details_OK () RETURNS void AS $$ 393 | DELETE FROM Order_Details 394 | WHERE (OrderNumber IN 395 | (SELECT OrderNumber 396 | FROM Order_Details_Archive)); 397 | $$ LANGUAGE SQL; 398 | 399 | CREATE VIEW CH17_Delete_Archived_Order_Details_OK_Query 400 | AS 401 | SELECT * FROM Order_Details 402 | WHERE (OrderNumber IN 403 | (SELECT OrderNumber 404 | FROM Order_Details_Archive)); 405 | 406 | 407 | CREATE FUNCTION CH17_Delete_Archived_Order_Details_Unsafe () RETURNS void AS $$ 408 | DELETE FROM Order_Details 409 | WHERE (OrderNumber IN 410 | (SELECT OrderNumber 411 | FROM Orders 412 | WHERE Orders.OrderDate < Cast('2018-01-01' As Date))); 413 | $$ LANGUAGE SQL; 414 | 415 | CREATE VIEW CH17_Delete_Archived_Order_Details_Unsafe_Query 416 | AS 417 | SELECT * FROM Order_Details 418 | WHERE (OrderNumber IN 419 | (SELECT OrderNumber 420 | FROM Orders 421 | WHERE Orders.OrderDate < Cast('2018-01-01' As Date))); 422 | 423 | CREATE FUNCTION CH17_Delete_Archived_Orders_OK () RETURNS void AS $$ 424 | DELETE FROM Orders 425 | WHERE (OrderNumber IN 426 | (SELECT OrderNumber 427 | FROM Orders_Archive)); 428 | $$ LANGUAGE SQL; 429 | 430 | CREATE VIEW CH17_Delete_Archived_Orders_OK_Query 431 | AS 432 | SELECT * FROM Orders 433 | WHERE (OrderNumber IN 434 | (SELECT OrderNumber 435 | FROM Orders_Archive)); 436 | 437 | 438 | CREATE FUNCTION CH17_Delete_Archived_Orders_Unsafe () RETURNS void AS $$ 439 | DELETE FROM Orders 440 | WHERE (OrderDate < Cast('2018-01-01' As Date)); 441 | $$ LANGUAGE SQL; 442 | 443 | CREATE VIEW CH17_Delete_Archived_Orders_Unsafe_Query 444 | AS 445 | SELECT * FROM Orders 446 | WHERE (OrderDate < Cast('2018-01-01' As Date)); 447 | 448 | 449 | CREATE FUNCTION CH17_Delete_Categories_No_Products () RETURNS void AS $$ 450 | DELETE FROM Categories 451 | WHERE (CategoryID NOT IN 452 | (SELECT CategoryID 453 | FROM Products)); 454 | $$ LANGUAGE SQL; 455 | 456 | CREATE VIEW CH17_Delete_Categories_No_Products_Query 457 | AS 458 | SELECT * FROM Categories 459 | WHERE (CategoryID NOT IN 460 | (SELECT CategoryID 461 | FROM Products)); 462 | 463 | 464 | CREATE FUNCTION CH17_Delete_Customers_Never_Ordered () RETURNS void AS $$ 465 | DELETE FROM Customers 466 | WHERE (CustomerID NOT IN 467 | (SELECT CustomerID 468 | FROM Orders)); 469 | $$ LANGUAGE SQL; 470 | 471 | 472 | CREATE VIEW CH17_Delete_Customers_Never_Ordered_Query 473 | AS 474 | SELECT * FROM Customers 475 | WHERE (CustomerID NOT IN 476 | (SELECT CustomerID 477 | FROM Orders)); 478 | 479 | 480 | CREATE FUNCTION CH17_Delete_Employees_No_Orders () RETURNS void AS $$ 481 | DELETE FROM Employees 482 | WHERE (EmployeeID NOT IN 483 | (SELECT EmployeeID 484 | FROM Orders)); 485 | $$ LANGUAGE SQL; 486 | 487 | CREATE VIEW CH17_Delete_Employees_No_Orders_Query 488 | AS 489 | SELECT * FROM Employees 490 | WHERE (EmployeeID NOT IN 491 | (SELECT EmployeeID 492 | FROM Orders)); 493 | 494 | 495 | CREATE FUNCTION CH17_Delete_Products_Never_Ordered_1 () RETURNS void AS $$ 496 | DELETE 497 | FROM Product_Vendors 498 | WHERE (Product_Vendors.ProductNumber NOT IN 499 | (SELECT ProductNumber FROM Order_Details)); 500 | $$ LANGUAGE SQL; 501 | 502 | CREATE VIEW CH17_Delete_Products_Never_Ordered_1_Query 503 | AS 504 | SELECT * 505 | FROM Product_Vendors 506 | WHERE (Product_Vendors.ProductNumber NOT IN 507 | (SELECT ProductNumber FROM Order_Details)); 508 | 509 | 510 | CREATE FUNCTION CH17_Delete_Products_Never_Ordered_2 () RETURNS void AS $$ 511 | DELETE FROM Products 512 | WHERE (ProductNumber NOT IN 513 | (SELECT ProductNumber 514 | FROM Order_Details)); 515 | $$ LANGUAGE SQL; 516 | 517 | CREATE VIEW CH17_Delete_Products_Never_Ordered_2_Query 518 | AS 519 | SELECT * FROM Products 520 | WHERE (ProductNumber NOT IN 521 | (SELECT ProductNumber 522 | FROM Order_Details)); 523 | 524 | 525 | CREATE FUNCTION CH17_Delete_Vendors_No_Products () RETURNS void AS $$ 526 | DELETE FROM Vendors 527 | WHERE (VendorID NOT IN 528 | (SELECT VendorID 529 | FROM Product_Vendors)); 530 | $$ LANGUAGE SQL; 531 | 532 | CREATE VIEW CH17_Delete_Vendors_No_Products_Query 533 | AS 534 | SELECT * FROM Vendors 535 | WHERE (VendorID NOT IN 536 | (SELECT VendorID 537 | FROM Product_Vendors)); 538 | 539 | 540 | CREATE FUNCTION CH17_DELETE_Zero_OrdersA () RETURNS void AS $$ 541 | DELETE FROM Orders 542 | WHERE (OrderTotal = 0); 543 | $$ LANGUAGE SQL; 544 | 545 | CREATE VIEW CH17_DELETE_Zero_OrdersA_Query 546 | AS 547 | SELECT * FROM Orders 548 | WHERE (OrderTotal = 0); 549 | 550 | 551 | CREATE FUNCTION CH17_DELETE_Zero_OrdersB () RETURNS void AS $$ 552 | DELETE FROM Orders 553 | WHERE (OrderNumber NOT IN 554 | (SELECT OrderNumber 555 | FROM Order_Details)); 556 | $$ LANGUAGE SQL; 557 | 558 | CREATE VIEW CH17_DELETE_Zero_OrdersB_Query 559 | AS 560 | SELECT * FROM Orders 561 | WHERE (OrderNumber NOT IN 562 | (SELECT OrderNumber 563 | FROM Order_Details)); 564 | -------------------------------------------------------------------------------- /Week 1/sql/ddl/01 RecipesDataPG.sql: -------------------------------------------------------------------------------- 1 | -- This requires that the tables from 00 RecipesStructurePG.sql be created first. 2 | 3 | SET search_path TO RecipesExample; 4 | 5 | INSERT INTO Ingredient_Classes 6 | VALUES (1, 'Spice'); 7 | INSERT INTO Ingredient_Classes 8 | VALUES (2, 'Meat'); 9 | INSERT INTO Ingredient_Classes 10 | VALUES (3, 'Vegetable'); 11 | INSERT INTO Ingredient_Classes 12 | VALUES (4, 'Oil'); 13 | INSERT INTO Ingredient_Classes 14 | VALUES (5, 'Pasta'); 15 | INSERT INTO Ingredient_Classes 16 | VALUES (6, 'Grain'); 17 | INSERT INTO Ingredient_Classes 18 | VALUES (7, 'Flour'); 19 | INSERT INTO Ingredient_Classes 20 | VALUES (8, 'Dairy'); 21 | INSERT INTO Ingredient_Classes 22 | VALUES (9, 'Liquid'); 23 | INSERT INTO Ingredient_Classes 24 | VALUES (10, 'Seafood'); 25 | INSERT INTO Ingredient_Classes 26 | VALUES (11, 'Butter'); 27 | INSERT INTO Ingredient_Classes 28 | VALUES (12, 'Cheese'); 29 | INSERT INTO Ingredient_Classes 30 | VALUES (13, 'Sauce'); 31 | INSERT INTO Ingredient_Classes 32 | VALUES (14, 'Dressing'); 33 | INSERT INTO Ingredient_Classes 34 | VALUES (15, 'Gravy'); 35 | INSERT INTO Ingredient_Classes 36 | VALUES (16, 'Topping'); 37 | INSERT INTO Ingredient_Classes 38 | VALUES (17, 'Fruit'); 39 | INSERT INTO Ingredient_Classes 40 | VALUES (18, 'Chips'); 41 | INSERT INTO Ingredient_Classes 42 | VALUES (19, 'Condiment'); 43 | INSERT INTO Ingredient_Classes 44 | VALUES (20, 'Bottle'); 45 | INSERT INTO Ingredient_Classes 46 | VALUES (21, 'Packaged food'); 47 | INSERT INTO Ingredient_Classes 48 | VALUES (22, 'Herb'); 49 | INSERT INTO Ingredient_Classes 50 | VALUES (23, 'Sorbet'); 51 | INSERT INTO Ingredient_Classes 52 | VALUES (24, 'Liquor'); 53 | 54 | INSERT INTO Measurements 55 | VALUES (1, 'Ounce'); 56 | INSERT INTO Measurements 57 | VALUES (2, 'Cup'); 58 | INSERT INTO Measurements 59 | VALUES (3, 'Teaspoon'); 60 | INSERT INTO Measurements 61 | VALUES (4, 'Tablespoon'); 62 | INSERT INTO Measurements 63 | VALUES (5, 'Pound'); 64 | INSERT INTO Measurements 65 | VALUES (6, 'Pinch'); 66 | INSERT INTO Measurements 67 | VALUES (7, 'Piece'); 68 | INSERT INTO Measurements 69 | VALUES (8, 'Slice'); 70 | INSERT INTO Measurements 71 | VALUES (9, 'Dash'); 72 | INSERT INTO Measurements 73 | VALUES (10, 'Can'); 74 | INSERT INTO Measurements 75 | VALUES (11, 'Bag'); 76 | INSERT INTO Measurements 77 | VALUES (12, 'Bottle'); 78 | INSERT INTO Measurements 79 | VALUES (13, 'Head'); 80 | INSERT INTO Measurements 81 | VALUES (14, 'Bunch'); 82 | INSERT INTO Measurements 83 | VALUES (15, 'Ear'); 84 | INSERT INTO Measurements 85 | VALUES (16, 'Clove'); 86 | INSERT INTO Measurements 87 | VALUES (17, 'Whole'); 88 | INSERT INTO Measurements 89 | VALUES (18, 'Pint'); 90 | INSERT INTO Measurements 91 | VALUES (19, 'To Taste'); 92 | INSERT INTO Measurements 93 | VALUES (20, 'Package'); 94 | INSERT INTO Measurements 95 | VALUES (21, 'Jar'); 96 | INSERT INTO Measurements 97 | VALUES (22, 'Sprig'); 98 | INSERT INTO Measurements 99 | VALUES (23, 'Quarts'); 100 | INSERT INTO Measurements 101 | VALUES (24, 'sticks'); 102 | INSERT INTO Measurements 103 | VALUES (25, 'filets'); 104 | INSERT INTO Measurements 105 | VALUES (26, 'Scoop'); 106 | 107 | INSERT INTO Recipe_Classes 108 | VALUES (1, 'Main course'); 109 | INSERT INTO Recipe_Classes 110 | VALUES (2, 'Vegetable'); 111 | INSERT INTO Recipe_Classes 112 | VALUES (3, 'Starch'); 113 | INSERT INTO Recipe_Classes 114 | VALUES (4, 'Salad'); 115 | INSERT INTO Recipe_Classes 116 | VALUES (5, 'Hors d''oeuvres'); 117 | INSERT INTO Recipe_Classes 118 | VALUES (6, 'Dessert'); 119 | INSERT INTO Recipe_Classes 120 | VALUES (7, 'Soup'); 121 | 122 | INSERT INTO Recipes 123 | VALUES (1, 'Irish Stew', 1, 'Cut the beef into 1" chunks 124 | Braise the meat 125 | Add water and Guinness 126 | Chop onions, potatoes, and carrots into 1/2" chunks. 127 | Add to stew. 128 | Simmer until vegetables are done.', 129 | NULL); 130 | INSERT INTO Recipes 131 | VALUES (2, 'Salsa Buena', 5, 'Coarsely dice the jalapenos . 132 | Mix all ingredients thoroughly in a bowl and let stand in the refrigerator for about an hour. 133 | Serve with your favorite corn chips.', 134 | NULL); 135 | INSERT INTO Recipes 136 | VALUES (3, 'Machos Nachos', 5, 'Slice the jalapenos crosswise (in circles) and set aside. 137 | Grate the cheddar cheese and set aside. 138 | Dice the onion and set aside. 139 | Spread the tortilla chips on a large microwavable dish. 140 | Top the chips with the grated cheese, diced onion, and jalapenos. 141 | Place the dish in the micowave and cook until the cheese just melts, about 3-4 minutes on high. 142 | When the cheese has melted, remove the dish and top with the black olives.', 143 | 'You can add a half a cup of diced tomatoes to this dish if you like. You can either add them prior to cooking in the microwave or afterwards, just before you add the black olives.'); 144 | INSERT INTO Recipes 145 | VALUES (4, 'Garlic Green Beans', 2, 'Snap off and discard the ends of the beans, then rinse them in cold water. 146 | Mince the two cloves of garlic. 147 | Heat the oil in a frying pan on medium high heat. 148 | When the oil is hot, add the green beans and garlic. 149 | Stir contiuously for about four minutes. 150 | Place the beans on a serving dish when they''re done and sprinkle silvered almonds on the top.', 151 | 'Be sure not to burn the oil. Watch it carefully while it''s heating.'); 152 | INSERT INTO Recipes 153 | VALUES (5, 'Fettuccini Alfredo', 1, 'Fill a large pot two thirds full with water. Add one tablespoon each of salt and vegetable oil. Bring to a rollling boil. 154 | Reduce heat, add pasta, and stir briefly. Cook until pasta is al dente. 155 | Just before the pasta is ready (about five minutes), melt the butter in a frying pan on low heat. 156 | After the butter has melted, add the heavy cream to the pan. Increase the heat to medium and stir until the mixture is slightly thickened. Remove the pan from the heat once the mixture is ready. 157 | Drain the fettuccini when it''s done and add it to the mixture in the frying pan. 158 | Mix three ounces of the cheese the the fettuccini and toss the entire mixture. 159 | Add another three ounces of cheese and the white pepper, and toss again lightly.', 160 | NULL); 161 | INSERT INTO Recipes 162 | VALUES (6, 'Pollo Picoso', 1, 'Wash chicken pieces thoroughly in cold water. Pat dry and set aside. 163 | Mince garlic and then mix it with the salt, pepper, and cayenne. Make sure the mixture is combined as thoroughly as possible. 164 | Coat each chicken piece (to taste) with the mixture. 165 | Place pieces in the broiler pan and cook for 15 minutes. Turn pieces and cook for another 15 minutes. Turn pieces once more and cook for 35 - 40 minutes. 166 | When the chicken is cooked, remove from stove and let it stand for 10 minutes.', 167 | 'Pre-heat the oven to 400 degrees. 168 | Cover the bottom of a broiler pan with a sheet of aluminum foil and then pour in about 1/2 an inch of water. The water will keep the chicken grease drippings from splattering throughout the inside of the stove and causing smoke. The foil makes the pan easier to clean.'); 169 | INSERT INTO Recipes 170 | VALUES (7, 'Mike''s Summer Salad', 4, 'Rinse lettuce and cut into bite size pieces. (You can tear them if you really want to.) 171 | Dust off mushrooms, remove stems, and slice into thick pieces, about 1/4". 172 | Peel the cucumber and slice it into 1/4" thick circles. 173 | Slice the tomatoes into 1/2" wedges. 174 | Wash radishes, remove leafy head and root, and slice into 1/8" circles. 175 | Mix all ingredients together in a large salad bowl and add your favorite balsamic vinaigrette dressing.', 176 | NULL); 177 | INSERT INTO Recipes 178 | VALUES (8, 'Trifle', 6, 'Prepare the Jello and custard per package directions. 179 | In tall dessert cups, place a 1/2" layer of the sponge cake. Soak the layer of cake in Jello. Spoon on a thin layer of raspberry jam. Add a layer of custard. 180 | Continue adding layers of sponge cake, jam, and custard until the cup is full (with a layer of custard on top) -- 2 to 3 layers. 181 | Chill for 2 hours. Sprinkle colored sugar on top and serve.', 182 | 'For a "spicier" dessert, replace half the liquid in the Jello with brandy or your favorite liqueur.'); 183 | INSERT INTO Recipes 184 | VALUES (9, 'Roast Beef', 1, 'Place the beef on a roasting rack in a roasting pan. 185 | Make a paste of ground garlic, salt, and pepper. Smother the outside of the roast with the paste. 186 | Roast at 325 for about 20 minutes per pound or to an internal temperature of 160F for medium-rare. 187 | Remove from oven and let stand 15 minutes before carving. Reserve the beef drippings for gravy or Yorkshire Pudding.', 188 | NULL); 189 | INSERT INTO Recipes 190 | VALUES (10, 'Yorkshire Pudding', 3, 'Place the eggs and water in a blender. While running the blender, slowly add the flour. Add the salt and milk and blend for 30 seconds more. 191 | Let stand in a refrigerator for 1 hour or more. 192 | Heat the roasting pan with beef drippings to 450F. Pour in the pudding mixture. Cook in a very hot oven for 20-25 minutes or until puffed up and golden brown. Remove from the pan immediately and cut into slices. Serve with brown gravy.', 193 | NULL); 194 | INSERT INTO Recipes 195 | VALUES (11, 'Huachinango Veracruzana (Red Snapper, Veracruz style)', 196 | 1, 'Heat one ounce of olive oil in a 1.5 quart saucepan. Slice the onion and sautee lightly in the olive oil. 197 | Drain the canned tomatoes (you can use peeled fresh tomatoes if you like) and puree in a blender. 198 | To the pureed tomatoes, add all the spices, thinly sliced Jalapenos, and green olives. Pour the tomato and spice mixture into the saucepan with the onions and simmer on a very low heat covered for 30 minutes. 199 | While the sauce is cooking, peel and boil the potatoes. 200 | Just before the potatoes are done, heat the remaining olive oil in a large frypan. Wash and lightly dust the fish pieces in a mixture of flour, salt, and pepper. Fry the fish, turning once, until just done. 201 | Place the cooked fish in a large platter. Surround the fish with the boiled potatoes. Pour the sauce over the fish, sprinkle with chopped parsley, and serve immediately. 202 | Serves 6.', 203 | 'You can substitute any firm white fish filets for the Red Snapper. If you use salted canned tomatoes, reduce the salt in the sauce by half. Adjust the amount of the Jalapenos in the sauce to suit your taste for spicy food!'); 204 | INSERT INTO Recipes 205 | VALUES (12, 'Asparagus', 2, 'Wash the asparagus and break off the tough part (if any) at the bottom of the stalks. Arrange on a steaming rack in a large saucepan. Dab liberally with pats of butter and sprinkle on the chopped garlic. Steam until just tender -- no more than 5 minutes for large stalks. Serve immediately.', 206 | 'You can chill the cooked asparagus and serve with your favorite dip as an appetizer.'); 207 | INSERT INTO Recipes 208 | VALUES (13, 'Tourti?re (French-Canadian Pork Pie)', 209 | 1, 'Brown ground pork and chopped onion, stirring and breaking up pork. Add spices and salt and pepper. Simmer, uncovered, for 45 minutes, stirring occasionally. Preheat oven to 375 degrees. Prepare pie dough for 2-crust pie. Line 9" pie plate with half of the rolled-out dough. Drain pork and stir bread crumbs into pork. Taste and add more salt and pepper if desired. Fill pie with pork mixture and top with second half of the rolled-out dough. Crimp edges of pie and slit the top in several places. Bake in 375-degree oven for one hour, covering edges of pie crust with foil if necessary. Serve hot or cold.', 210 | 'Be sure to use fresh ground pork, not sausage. Can be made with half ground pork and half ground beef, if desired.'); 211 | INSERT INTO Recipes 212 | VALUES (14, 'Salmon Filets in Parchment Paper', 1, 213 | 'Julienne carrots, leeks, and bell peppers and steam for several minutes. Drain and rinse vegetables in ice water and set aside. Preheat oven to 400 degrees. Butter 4 large rounds of parchment paper. Distribute half of vegetables on one side of each round of parchment. Place a salmon filet on the vegetables on each round. Top the filets with the rest of the vegetables. Combine white wine and lemon juice and spoon 1 tablespoon on each filet. Pour melted butter on filets. Place a thinly-sliced lemon round on each. Salt and pepper very lightly. Fold over parchment paper into half circles and roll and crimp edges tightly. Bake packets at 400 degrees for 10-15 minutes, depending on thickness of the filets.', 214 | 'Serve the salmon in the parchment packets. A salad and boiled new potatoes, buttered and sprinkled with fresh parsley, are the perfect complements.'); 215 | INSERT INTO Recipes 216 | VALUES (15, 'Coupe Colonel', 6, 'For each person, place 2 scoops of lemon sorbet in a stemmed glass. Top with vodka.', 217 | 'This is a lovely, light, and refreshing dessert. Use the best sorbet and vodka you can find. Serve with crisp cookies.'); 218 | 219 | INSERT INTO Ingredients 220 | VALUES (1, 'Beef', 2, 1); 221 | INSERT INTO Ingredients 222 | VALUES (2, 'Onion', 3, 17); 223 | INSERT INTO Ingredients 224 | VALUES (3, 'Water', 9, 1); 225 | INSERT INTO Ingredients 226 | VALUES (4, 'Guinness Beer', 9, 1); 227 | INSERT INTO Ingredients 228 | VALUES (5, 'Potato', 3, 2); 229 | INSERT INTO Ingredients 230 | VALUES (6, 'Carrot', 3, 2); 231 | INSERT INTO Ingredients 232 | VALUES (7, 'Tomato', 3, 8); 233 | INSERT INTO Ingredients 234 | VALUES (8, 'Jalapeno', 3, 2); 235 | INSERT INTO Ingredients 236 | VALUES (9, 'Garlic', 1, 16); 237 | INSERT INTO Ingredients 238 | VALUES (10, 'Black Pepper (ground)', 1, 3); 239 | INSERT INTO Ingredients 240 | VALUES (11, 'Salt', 1, 3); 241 | INSERT INTO Ingredients 242 | VALUES (12, 'Halibut', 10, 5); 243 | INSERT INTO Ingredients 244 | VALUES (13, 'Chicken, Fryer', 2, 5); 245 | INSERT INTO Ingredients 246 | VALUES (14, 'Bacon', 2, 8); 247 | INSERT INTO Ingredients 248 | VALUES (15, 'Romaine Lettuce', 3, 13); 249 | INSERT INTO Ingredients 250 | VALUES (16, 'Iceberg Lettuce', 3, 13); 251 | INSERT INTO Ingredients 252 | VALUES (17, 'Butterhead Lettuce', 3, 13); 253 | INSERT INTO Ingredients 254 | VALUES (18, 'Scallop', 10, 5); 255 | INSERT INTO Ingredients 256 | VALUES (19, 'Salmon', 10, 5); 257 | INSERT INTO Ingredients 258 | VALUES (20, 'Vinegar', 9, 1); 259 | INSERT INTO Ingredients 260 | VALUES (21, 'Olive Oil', 4, 1); 261 | INSERT INTO Ingredients 262 | VALUES (22, 'Cucumber', 3, 17); 263 | INSERT INTO Ingredients 264 | VALUES (23, 'Mushrooms', 3, 2); 265 | INSERT INTO Ingredients 266 | VALUES (24, 'Red Wine', 9, 2); 267 | INSERT INTO Ingredients 268 | VALUES (25, 'White Wine', 9, 2); 269 | INSERT INTO Ingredients 270 | VALUES (26, 'Milk', 8, 2); 271 | INSERT INTO Ingredients 272 | VALUES (27, 'Cheddar Cheese', 12, 2); 273 | INSERT INTO Ingredients 274 | VALUES (28, 'Tortilla Chips', 18, 11); 275 | INSERT INTO Ingredients 276 | VALUES (29, 'Black Olives', 19, 2); 277 | INSERT INTO Ingredients 278 | VALUES (30, 'Green Beans', 3, 14); 279 | INSERT INTO Ingredients 280 | VALUES (31, 'Fettuccini Pasta', 5, 1); 281 | INSERT INTO Ingredients 282 | VALUES (32, 'Heavy Cream', 8, 1); 283 | INSERT INTO Ingredients 284 | VALUES (33, 'Chicken, Pre-cut', 2, 17); 285 | INSERT INTO Ingredients 286 | VALUES (34, 'T-bone Steak', 2, 17); 287 | INSERT INTO Ingredients 288 | VALUES (35, 'Chicken Breast', 2, 7); 289 | INSERT INTO Ingredients 290 | VALUES (36, 'Chicken Leg', 2, 17); 291 | INSERT INTO Ingredients 292 | VALUES (37, 'Chicken Wing', 2, 7); 293 | INSERT INTO Ingredients 294 | VALUES (38, 'Chicken Thigh', 2, 7); 295 | INSERT INTO Ingredients 296 | VALUES (39, 'New York Steak', 2, 17); 297 | INSERT INTO Ingredients 298 | VALUES (40, 'Spaghetti', 5, 1); 299 | INSERT INTO Ingredients 300 | VALUES (41, 'Mustard, Regular', 19, 1); 301 | INSERT INTO Ingredients 302 | VALUES (42, 'Mustard, Dijon', 19, 1); 303 | INSERT INTO Ingredients 304 | VALUES (43, 'Ketchup', 19, 1); 305 | INSERT INTO Ingredients 306 | VALUES (44, 'Salsa', 13, 1); 307 | INSERT INTO Ingredients 308 | VALUES (45, 'Parmesan Cheese', 12, 1); 309 | INSERT INTO Ingredients 310 | VALUES (46, 'Blue Cheese', 12, 1); 311 | INSERT INTO Ingredients 312 | VALUES (47, 'Butter', 11, 4); 313 | INSERT INTO Ingredients 314 | VALUES (48, 'Green Onion', 3, 14); 315 | INSERT INTO Ingredients 316 | VALUES (49, 'Green Olives', 19, 2); 317 | INSERT INTO Ingredients 318 | VALUES (50, 'Vegetable Oil', 4, 4); 319 | INSERT INTO Ingredients 320 | VALUES (51, 'White Pepper (ground)', 1, 1); 321 | INSERT INTO Ingredients 322 | VALUES (52, 'Cayenne Pepper, Ground', 1, 1); 323 | INSERT INTO Ingredients 324 | VALUES (53, 'Radishes', 3, 14); 325 | INSERT INTO Ingredients 326 | VALUES (54, 'Balsamic Vinaigrette Dressing', 20, 327 | 1); 328 | INSERT INTO Ingredients 329 | VALUES (55, 'Sponge Cake', 21, 20); 330 | INSERT INTO Ingredients 331 | VALUES (56, 'Raspberry Jello', 21, 20); 332 | INSERT INTO Ingredients 333 | VALUES (57, 'Bird''s Custard Powder', 21, 20); 334 | INSERT INTO Ingredients 335 | VALUES (58, 'Colored sugar sprinkles', 1, 3); 336 | INSERT INTO Ingredients 337 | VALUES (59, 'Raspberry Jam', 21, 21); 338 | INSERT INTO Ingredients 339 | VALUES (61, 'Flour', 6, 2); 340 | INSERT INTO Ingredients 341 | VALUES (62, 'Eggs', 8, 7); 342 | INSERT INTO Ingredients 343 | VALUES (63, 'Beef drippings', 4, 3); 344 | INSERT INTO Ingredients 345 | VALUES (64, 'Red Snapper', 10, 1); 346 | INSERT INTO Ingredients 347 | VALUES (65, 'Canned tomatoes', 3, 10); 348 | INSERT INTO Ingredients 349 | VALUES (66, 'Nutmeg', 1, 3); 350 | INSERT INTO Ingredients 351 | VALUES (67, 'Cinnamon', 1, 3); 352 | INSERT INTO Ingredients 353 | VALUES (68, 'Lime Juice', 9, 3); 354 | INSERT INTO Ingredients 355 | VALUES (69, 'Asparagus', 3, 5); 356 | INSERT INTO Ingredients 357 | VALUES (70, 'Parsley', 22, 22); 358 | INSERT INTO Ingredients 359 | VALUES (71, 'Pie dough for 2-crust pie', 21, 24); 360 | INSERT INTO Ingredients 361 | VALUES (72, 'Ground Pork', 2, 5); 362 | INSERT INTO Ingredients 363 | VALUES (73, 'Ground Clove', 1, 3); 364 | INSERT INTO Ingredients 365 | VALUES (74, 'Bread Crumbs', 21, 2); 366 | INSERT INTO Ingredients 367 | VALUES (75, 'Leek', 3, 7); 368 | INSERT INTO Ingredients 369 | VALUES (76, 'Red Bell Pepper', 3, 7); 370 | INSERT INTO Ingredients 371 | VALUES (77, 'Lemon Juice', 9, 4); 372 | INSERT INTO Ingredients 373 | VALUES (78, 'Lemon ', 17, 7); 374 | INSERT INTO Ingredients 375 | VALUES (79, 'Lemon Sorbet', 23, 26); 376 | INSERT INTO Ingredients 377 | VALUES (80, 'Vodka', 24, 4); 378 | 379 | INSERT INTO Recipe_Ingredients 380 | VALUES (1, 1, 1, 5, 1); 381 | INSERT INTO Recipe_Ingredients 382 | VALUES (1, 2, 2, 17, 2); 383 | INSERT INTO Recipe_Ingredients 384 | VALUES (1, 3, 5, 17, 4); 385 | INSERT INTO Recipe_Ingredients 386 | VALUES (1, 4, 6, 17, 6); 387 | INSERT INTO Recipe_Ingredients 388 | VALUES (1, 5, 3, 23, 4); 389 | INSERT INTO Recipe_Ingredients 390 | VALUES (1, 6, 4, 1, 12); 391 | INSERT INTO Recipe_Ingredients 392 | VALUES (2, 1, 8, 17, 6); 393 | INSERT INTO Recipe_Ingredients 394 | VALUES (2, 2, 7, 17, 2); 395 | INSERT INTO Recipe_Ingredients 396 | VALUES (2, 3, 2, 17, 0.5); 397 | INSERT INTO Recipe_Ingredients 398 | VALUES (2, 4, 10, 4, 1); 399 | INSERT INTO Recipe_Ingredients 400 | VALUES (2, 5, 11, 3, 1); 401 | INSERT INTO Recipe_Ingredients 402 | VALUES (3, 1, 8, 17, 4); 403 | INSERT INTO Recipe_Ingredients 404 | VALUES (3, 2, 27, 2, 1); 405 | INSERT INTO Recipe_Ingredients 406 | VALUES (3, 3, 2, 17, 0.5); 407 | INSERT INTO Recipe_Ingredients 408 | VALUES (3, 4, 28, 11, 0.5); 409 | INSERT INTO Recipe_Ingredients 410 | VALUES (3, 5, 29, 2, 0.25); 411 | INSERT INTO Recipe_Ingredients 412 | VALUES (4, 1, 30, 5, 0.5); 413 | INSERT INTO Recipe_Ingredients 414 | VALUES (4, 2, 9, 16, 2); 415 | INSERT INTO Recipe_Ingredients 416 | VALUES (4, 3, 21, 4, 1); 417 | INSERT INTO Recipe_Ingredients 418 | VALUES (5, 1, 31, 1, 16); 419 | INSERT INTO Recipe_Ingredients 420 | VALUES (5, 2, 50, 4, 1); 421 | INSERT INTO Recipe_Ingredients 422 | VALUES (5, 3, 11, 3, 3); 423 | INSERT INTO Recipe_Ingredients 424 | VALUES (5, 4, 47, 4, 6); 425 | INSERT INTO Recipe_Ingredients 426 | VALUES (5, 5, 32, 2, 0.25); 427 | INSERT INTO Recipe_Ingredients 428 | VALUES (5, 12, 45, 1, 6); 429 | INSERT INTO Recipe_Ingredients 430 | VALUES (5, 16, 51, 19, 0); 431 | INSERT INTO Recipe_Ingredients 432 | VALUES (6, 1, 36, 7, 2); 433 | INSERT INTO Recipe_Ingredients 434 | VALUES (6, 2, 38, 7, 2); 435 | INSERT INTO Recipe_Ingredients 436 | VALUES (6, 3, 11, 3, 1.5); 437 | INSERT INTO Recipe_Ingredients 438 | VALUES (6, 4, 10, 3, 1.5); 439 | INSERT INTO Recipe_Ingredients 440 | VALUES (6, 5, 9, 16, 3); 441 | INSERT INTO Recipe_Ingredients 442 | VALUES (6, 6, 52, 19, 0); 443 | INSERT INTO Recipe_Ingredients 444 | VALUES (7, 1, 15, 13, 1); 445 | INSERT INTO Recipe_Ingredients 446 | VALUES (7, 2, 23, 7, 12); 447 | INSERT INTO Recipe_Ingredients 448 | VALUES (7, 3, 22, 17, 1); 449 | INSERT INTO Recipe_Ingredients 450 | VALUES (7, 4, 7, 17, 1); 451 | INSERT INTO Recipe_Ingredients 452 | VALUES (7, 5, 53, 14, 1); 453 | INSERT INTO Recipe_Ingredients 454 | VALUES (7, 6, 54, 4, 3); 455 | INSERT INTO Recipe_Ingredients 456 | VALUES (8, 1, 55, 20, 1); 457 | INSERT INTO Recipe_Ingredients 458 | VALUES (8, 2, 56, 20, 1); 459 | INSERT INTO Recipe_Ingredients 460 | VALUES (8, 3, 57, 20, 1); 461 | INSERT INTO Recipe_Ingredients 462 | VALUES (8, 4, 59, 21, 1); 463 | INSERT INTO Recipe_Ingredients 464 | VALUES (8, 5, 58, 3, 1); 465 | INSERT INTO Recipe_Ingredients 466 | VALUES (9, 1, 1, 5, 4); 467 | INSERT INTO Recipe_Ingredients 468 | VALUES (9, 2, 9, 16, 6); 469 | INSERT INTO Recipe_Ingredients 470 | VALUES (9, 3, 11, 3, 1); 471 | INSERT INTO Recipe_Ingredients 472 | VALUES (9, 4, 10, 3, 1); 473 | INSERT INTO Recipe_Ingredients 474 | VALUES (10, 1, 61, 2, 1.5); 475 | INSERT INTO Recipe_Ingredients 476 | VALUES (10, 2, 3, 2, 1); 477 | INSERT INTO Recipe_Ingredients 478 | VALUES (10, 3, 62, 7, 2); 479 | INSERT INTO Recipe_Ingredients 480 | VALUES (10, 4, 11, 3, 0.5); 481 | INSERT INTO Recipe_Ingredients 482 | VALUES (10, 5, 26, 2, 0.5); 483 | INSERT INTO Recipe_Ingredients 484 | VALUES (10, 6, 63, 3, 4); 485 | INSERT INTO Recipe_Ingredients 486 | VALUES (11, 1, 64, 5, 2); 487 | INSERT INTO Recipe_Ingredients 488 | VALUES (11, 2, 21, 1, 4); 489 | INSERT INTO Recipe_Ingredients 490 | VALUES (11, 3, 2, 17, 1); 491 | INSERT INTO Recipe_Ingredients 492 | VALUES (11, 4, 65, 10, 2); 493 | INSERT INTO Recipe_Ingredients 494 | VALUES (11, 5, 11, 3, 1); 495 | INSERT INTO Recipe_Ingredients 496 | VALUES (11, 6, 10, 3, 0.5); 497 | INSERT INTO Recipe_Ingredients 498 | VALUES (11, 7, 66, 3, 0.25); 499 | INSERT INTO Recipe_Ingredients 500 | VALUES (11, 8, 67, 3, 0.5); 501 | INSERT INTO Recipe_Ingredients 502 | VALUES (11, 9, 68, 3, 2); 503 | INSERT INTO Recipe_Ingredients 504 | VALUES (11, 10, 49, 7, 8); 505 | INSERT INTO Recipe_Ingredients 506 | VALUES (11, 11, 61, 2, 0.5); 507 | INSERT INTO Recipe_Ingredients 508 | VALUES (11, 12, 70, 22, 2); 509 | INSERT INTO Recipe_Ingredients 510 | VALUES (12, 1, 69, 5, 1); 511 | INSERT INTO Recipe_Ingredients 512 | VALUES (12, 2, 47, 4, 4); 513 | INSERT INTO Recipe_Ingredients 514 | VALUES (12, 3, 9, 16, 4); 515 | INSERT INTO Recipe_Ingredients 516 | VALUES (13, 1, 71, 24, 2); 517 | INSERT INTO Recipe_Ingredients 518 | VALUES (13, 2, 72, 5, 2); 519 | INSERT INTO Recipe_Ingredients 520 | VALUES (13, 3, 2, 2, 0.333); 521 | INSERT INTO Recipe_Ingredients 522 | VALUES (13, 4, 67, 4, 1); 523 | INSERT INTO Recipe_Ingredients 524 | VALUES (13, 5, 73, 3, 0.5); 525 | INSERT INTO Recipe_Ingredients 526 | VALUES (13, 6, 11, 3, 0.25); 527 | INSERT INTO Recipe_Ingredients 528 | VALUES (13, 7, 10, 3, 0.5); 529 | INSERT INTO Recipe_Ingredients 530 | VALUES (13, 8, 74, 2, 0.5); 531 | INSERT INTO Recipe_Ingredients 532 | VALUES (14, 1, 19, 25, 4); 533 | INSERT INTO Recipe_Ingredients 534 | VALUES (14, 2, 6, 7, 2); 535 | INSERT INTO Recipe_Ingredients 536 | VALUES (14, 3, 75, 7, 1); 537 | INSERT INTO Recipe_Ingredients 538 | VALUES (14, 4, 76, 7, 1); 539 | INSERT INTO Recipe_Ingredients 540 | VALUES (14, 5, 47, 4, 4); 541 | INSERT INTO Recipe_Ingredients 542 | VALUES (14, 6, 25, 4, 2); 543 | INSERT INTO Recipe_Ingredients 544 | VALUES (14, 7, 77, 4, 2); 545 | INSERT INTO Recipe_Ingredients 546 | VALUES (14, 8, 78, 7, 1); 547 | INSERT INTO Recipe_Ingredients 548 | VALUES (14, 9, 11, 3, 0.5); 549 | INSERT INTO Recipe_Ingredients 550 | VALUES (14, 10, 10, 3, 0.5); 551 | INSERT INTO Recipe_Ingredients 552 | VALUES (15, 1, 79, 26, 2); 553 | INSERT INTO Recipe_Ingredients 554 | VALUES (15, 2, 80, 4, 2); -------------------------------------------------------------------------------- /Week 1/sql/ddl/02 RecipesViewsPG.sql: -------------------------------------------------------------------------------- 1 | -- This requires that the tables from 00 RecipiesStructurePG.sql be created first. 2 | 3 | SET search_path TO RecipesExample; 4 | 5 | CREATE VIEW CH04_Complete_Ingredient_List 6 | AS 7 | SELECT IngredientName 8 | FROM Ingredients; 9 | 10 | 11 | CREATE VIEW CH04_Main_Recipe_Information 12 | AS 13 | SELECT Recipes.* 14 | FROM Recipes 15 | ORDER BY RecipeTitle; 16 | 17 | 18 | CREATE VIEW CH04_Recipe_Class_IDs 19 | AS 20 | SELECT DISTINCT RecipeClassID 21 | FROM Recipes; 22 | 23 | 24 | CREATE VIEW CH04_Recipe_Classes_And_Titles 25 | AS 26 | SELECT RecipeClassID, RecipeTitle 27 | FROM Recipes 28 | ORDER BY RecipeClassID, RecipeTitle; 29 | 30 | 31 | CREATE VIEW CH06_First_5_Recipes 32 | AS 33 | SELECT RecipeID, RecipeTitle, Preparation, Notes 34 | FROM Recipes 35 | WHERE (RecipeID BETWEEN 1 AND 5); 36 | 37 | 38 | CREATE VIEW CH06_Main_Courses_With_Notes 39 | AS 40 | SELECT RecipeTitle, Preparation, Notes 41 | FROM Recipes 42 | WHERE (RecipeClassID = 1) AND (Notes IS NOT NULL); 43 | 44 | 45 | CREATE VIEW CH06_Meats_That_Are_Not_Chicken 46 | AS 47 | SELECT IngredientName 48 | FROM Ingredients 49 | WHERE (IngredientName NOT LIKE '%Chicken%') AND (IngredientClassID = 2); 50 | 51 | 52 | CREATE VIEW CH06_Recipes_With_No_Notes 53 | AS 54 | SELECT RecipeTitle 55 | FROM Recipes 56 | WHERE (Notes IS NULL); 57 | 58 | 59 | CREATE VIEW CH08_Beef_And_Garlic_Recipes 60 | AS 61 | SELECT BeefRecipes.RecipeTitle 62 | FROM (SELECT Recipes.RecipeID, Recipes.RecipeTitle 63 | FROM (Recipes INNER JOIN 64 | Recipe_Ingredients ON Recipes.RecipeID = Recipe_Ingredients.RecipeID) INNER JOIN 65 | Ingredients ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 66 | WHERE Ingredients.IngredientName = 'Beef') BeefRecipes INNER JOIN 67 | (SELECT Recipe_Ingredients.RecipeID 68 | FROM Recipe_Ingredients INNER JOIN 69 | Ingredients ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 70 | WHERE Ingredients.IngredientName = 'Garlic') GarlicRecipes ON BeefRecipes.RecipeID = GarlicRecipes.RecipeID; 71 | 72 | 73 | CREATE VIEW CH08_Beef_Or_Garlic_Recipes 74 | AS 75 | SELECT DISTINCT Recipes.RecipeTitle 76 | FROM Recipes INNER JOIN 77 | Recipe_Ingredients ON Recipes.RecipeID = Recipe_Ingredients.RecipeID 78 | WHERE (Recipe_Ingredients.IngredientID IN (1, 9)); 79 | 80 | 81 | CREATE VIEW CH08_Ingredients_Same_Measure 82 | AS 83 | SELECT First_Ingredient.FirstIngredientName, First_Ingredient.MeasurementDescription, Second_Ingredient.SecondIngredientName 84 | FROM (SELECT Ingredients.IngredientName AS FirstIngredientName, Measurements.MeasurementDescription 85 | FROM Ingredients INNER JOIN 86 | Measurements ON Ingredients.MeasureAmountID = Measurements.MeasureAmountID) First_Ingredient INNER JOIN 87 | (SELECT Ingredients.IngredientName AS SecondIngredientName, Measurements.MeasurementDescription 88 | FROM Ingredients INNER JOIN 89 | Measurements ON Ingredients.MeasureAmountID = Measurements.MeasureAmountID) Second_Ingredient ON 90 | First_Ingredient.FirstIngredientName <> Second_Ingredient.SecondIngredientName AND 91 | First_Ingredient.MeasurementDescription = Second_Ingredient.MeasurementDescription; 92 | 93 | 94 | CREATE VIEW CH08_Main_Course_Ingredients 95 | AS 96 | SELECT Recipes.RecipeTitle, Ingredients.IngredientName, Measurements.MeasurementDescription, Recipe_Ingredients.Amount 97 | FROM Recipe_Classes INNER JOIN 98 | Recipes ON Recipes.RecipeClassID = Recipe_Classes.RecipeClassID INNER JOIN 99 | Recipe_Ingredients ON Recipes.RecipeID = Recipe_Ingredients.RecipeID INNER JOIN 100 | Ingredients ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID INNER JOIN 101 | Measurements ON Measurements.MeasureAmountID = Recipe_Ingredients.MeasureAmountID 102 | WHERE (Recipe_Classes.RecipeClassDescription = 'Main course'); 103 | 104 | 105 | CREATE VIEW CH08_Recipes_Containing_Carrots 106 | AS 107 | SELECT Recipes.RecipeID, Recipes.RecipeTitle, Ingredients.IngredientName 108 | FROM Recipes INNER JOIN 109 | Recipe_Ingredients ON Recipes.RecipeID = Recipe_Ingredients.RecipeID INNER JOIN 110 | Ingredients ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID INNER JOIN 111 | (SELECT Recipe_Ingredients.RecipeID 112 | FROM Ingredients INNER JOIN 113 | Recipe_Ingredients ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 114 | WHERE Ingredients.IngredientName = 'Carrot') Carrots ON Recipes.RecipeID = Carrots.RecipeID; 115 | 116 | 117 | CREATE VIEW CH08_Recipes_Containing_Dairy 118 | AS 119 | SELECT DISTINCT Recipes.RecipeTitle 120 | FROM Recipes INNER JOIN 121 | Recipe_Ingredients ON Recipes.RecipeID = Recipe_Ingredients.RecipeID INNER JOIN 122 | Ingredients ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID INNER JOIN 123 | Ingredient_Classes ON Ingredient_Classes.IngredientClassID = Ingredients.IngredientClassID 124 | WHERE (Ingredient_Classes.IngredientClassDescription = 'Dairy'); 125 | 126 | 127 | CREATE VIEW CH08_Salads 128 | AS 129 | SELECT Recipes.RecipeTitle 130 | FROM Recipes INNER JOIN 131 | Recipe_Classes ON Recipes.RecipeClassID = Recipe_Classes.RecipeClassID 132 | WHERE (Recipe_Classes.RecipeClassDescription = 'Salad'); 133 | 134 | 135 | CREATE VIEW CH09_All_Ingredients_Any_Recipes 136 | AS 137 | SELECT Ingredients.IngredientName, R.RecipeID, R.RecipeTitle 138 | FROM Ingredients LEFT OUTER JOIN 139 | (SELECT DISTINCT Recipes.RecipeID, Recipes.RecipeTitle, Recipe_Ingredients.IngredientID 140 | FROM Recipes INNER JOIN 141 | Recipe_Ingredients ON Recipes.RecipeID = Recipe_Ingredients.RecipeID) R ON Ingredients.IngredientID = R.IngredientID; 142 | 143 | 144 | CREATE VIEW CH09_All_Recipe_Classes_All_Recipes 145 | AS 146 | SELECT Recipe_Classes.RecipeClassDescription, Recipes.RecipeTitle, Ingredients.IngredientName, Recipe_Ingredients.RecipeSeqNo, 147 | Recipe_Ingredients.Amount, Measurements.MeasurementDescription 148 | FROM Recipes LEFT OUTER JOIN Recipe_Ingredients 149 | ON Recipes.RecipeID = Recipe_Ingredients.RecipeID 150 | INNER JOIN Measurements 151 | ON Measurements.MeasureAmountID = Recipe_Ingredients.MeasureAmountID 152 | FULL OUTER JOIN Ingredients 153 | ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 154 | FULL OUTER JOIN Recipe_Classes 155 | ON Recipes.RecipeClassID = Recipe_Classes.RecipeClassID 156 | ORDER BY Recipe_Classes.RecipeClassDescription DESC, Recipes.RecipeTitle, Recipe_Ingredients.RecipeSeqNo; 157 | 158 | 159 | CREATE VIEW CH09_All_RecipeClasses_And_Matching_Recipes 160 | AS 161 | SELECT Recipe_Classes.RecipeClassDescription, Recipes.RecipeTitle 162 | FROM Recipe_Classes LEFT OUTER JOIN 163 | Recipes ON Recipe_Classes.RecipeClassID = Recipes.RecipeClassID; 164 | 165 | CREATE VIEW CH09_Ingredients_Not_Used 166 | AS 167 | SELECT Ingredients.IngredientName 168 | FROM Ingredients LEFT OUTER JOIN 169 | Recipe_Ingredients ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 170 | WHERE (Recipe_Ingredients.RecipeID IS NULL); 171 | 172 | CREATE VIEW CH09_Recipe_Classes_No_Recipes 173 | AS 174 | SELECT Recipe_Classes.RecipeClassDescription 175 | FROM Recipe_Classes LEFT OUTER JOIN 176 | Recipes ON Recipe_Classes.RecipeClassID = Recipes.RecipeClassID 177 | WHERE (Recipes.RecipeID IS NULL); 178 | 179 | 180 | CREATE VIEW CH09_Salad_Soup_Main_Courses 181 | AS 182 | SELECT RCFiltered.ClassName, R.RecipeTitle 183 | FROM (SELECT RC.RecipeClassID, RC.RecipeClassDescription AS ClassName 184 | FROM Recipe_Classes AS RC 185 | WHERE RC.RecipeClassDescription = 'Salad' OR 186 | RC.RecipeClassDescription = 'Soup' OR 187 | RC.RecipeClassDescription = 'Main course') RCFiltered LEFT OUTER JOIN 188 | Recipes R ON RCFiltered.RecipeClassID = R.RecipeClassID; 189 | 190 | 191 | CREATE VIEW CH10_Classes_Recipes_Ingredients 192 | AS 193 | SELECT Recipe_Classes.RecipeClassDescription AS IndexName, 'Recipe Class' AS Type 194 | FROM Recipe_Classes 195 | UNION 196 | SELECT Recipes.RecipeTitle, 'Recipe' AS Type 197 | FROM Recipes 198 | UNION 199 | SELECT Ingredients.IngredientName, 'Ingredient' AS Type 200 | FROM Ingredients; 201 | 202 | CREATE VIEW CH10_Ingredient_Recipe_Measurements 203 | AS 204 | SELECT Ingredients.IngredientName, Measurements.MeasurementDescription, 'Ingredient' AS Type 205 | FROM Measurements INNER JOIN 206 | Ingredients ON (Measurements.MeasureAmountID = Ingredients.MeasureAmountID) 207 | UNION 208 | SELECT Ingredients.IngredientName, Measurements.MeasurementDescription, 'Recipe' AS Type 209 | FROM (Measurements INNER JOIN 210 | Recipe_Ingredients ON (Measurements.MeasureAmountID = Recipe_Ingredients.MeasureAmountID)) INNER JOIN 211 | Ingredients ON (Ingredients.IngredientID = Recipe_Ingredients.IngredientID); 212 | 213 | CREATE VIEW CH11_Count_Of_Recipe_Types 214 | AS 215 | SELECT RecipeClassID, RecipeClassDescription, 216 | (SELECT COUNT(*) 217 | FROM Recipes 218 | WHERE Recipes.RecipeClassID = Recipe_Classes.RecipeClassID) AS RecipeCount 219 | FROM Recipe_Classes; 220 | 221 | CREATE VIEW CH11_Ingredients_Using_NonStandard_Measure 222 | AS 223 | SELECT IngredientID, IngredientName, MeasureAmountID 224 | FROM Ingredients 225 | WHERE (MeasureAmountID <> ANY 226 | (SELECT Recipe_Ingredients.MeasureAmountID 227 | FROM Recipe_Ingredients 228 | WHERE Recipe_Ingredients.IngredientID = Ingredients.IngredientID)); 229 | 230 | CREATE VIEW CH11_Meat_Ingredient_Recipe_Count 231 | AS 232 | SELECT Ingredient_Classes.IngredientClassDescription, Ingredients.IngredientName, 233 | (SELECT COUNT(*) 234 | FROM Recipe_Ingredients 235 | WHERE Recipe_Ingredients.IngredientID = Ingredients.IngredientID) AS RecipeCount 236 | FROM Ingredient_Classes INNER JOIN 237 | Ingredients ON Ingredient_Classes.IngredientClassID = Ingredients.IngredientClassID 238 | WHERE (Ingredient_Classes.IngredientClassDescription = 'Meat'); 239 | 240 | CREATE VIEW CH11_Recipes_Ingredients_With_Carrots 241 | AS 242 | SELECT Recipes.RecipeTitle, Ingredients.IngredientName 243 | FROM Recipes INNER JOIN 244 | Recipe_Ingredients ON Recipes.RecipeID = Recipe_Ingredients.RecipeID INNER JOIN 245 | Ingredients ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 246 | WHERE (Recipes.RecipeID IN 247 | (SELECT Recipe_Ingredients.RecipeID 248 | FROM Ingredients INNER JOIN 249 | Recipe_Ingredients ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 250 | WHERE Ingredients.IngredientName = 'Carrot')); 251 | 252 | CREATE VIEW CH12_Number_of_Ingredients_Measured_by_the_Cup 253 | AS 254 | SELECT COUNT(*) AS NumberOfIngredients 255 | FROM Ingredients INNER JOIN 256 | Measurements ON Ingredients.MeasureAmountID = Measurements.MeasureAmountID 257 | WHERE (Measurements.MeasurementDescription = 'Cup'); 258 | 259 | CREATE VIEW CH12_Number_Of_Main_Course_Recipes 260 | AS 261 | SELECT DISTINCT COUNT(*) AS NumberOfRecipes 262 | FROM Recipes INNER JOIN 263 | Recipe_Classes ON Recipes.RecipeClassID = Recipe_Classes.RecipeClassID 264 | WHERE (Recipe_Classes.RecipeClassDescription = 'Main course'); 265 | 266 | CREATE VIEW CH12_Recipe_With_Most_Cloves_of_Garlic 267 | AS 268 | SELECT DISTINCT Recipes.RecipeTitle 269 | FROM Recipes INNER JOIN 270 | Recipe_Ingredients ON Recipes.RecipeID = Recipe_Ingredients.RecipeID INNER JOIN 271 | Ingredients ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID 272 | WHERE (Ingredients.IngredientName = 'Garlic') AND (Recipe_Ingredients.Amount = 273 | (SELECT MAX(Amount) 274 | FROM Recipe_Ingredients INNER JOIN 275 | Ingredients ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID 276 | WHERE IngredientName = 'Garlic')); 277 | 278 | CREATE VIEW CH12_Recipes_With_Beef_Ingredient 279 | AS 280 | SELECT COUNT(*) AS NumberOfRecipes 281 | FROM Recipes 282 | WHERE (RecipeID IN 283 | (SELECT RecipeID 284 | FROM Recipe_Ingredients INNER JOIN 285 | Ingredients ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID 286 | WHERE IngredientName LIKE '%Beef%')); 287 | 288 | CREATE VIEW CH12_Total_Salt_Used 289 | AS 290 | SELECT SUM(Recipe_Ingredients.Amount) AS TotalTeaspoons 291 | FROM Recipe_Ingredients INNER JOIN 292 | Ingredients ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID 293 | WHERE (Ingredients.IngredientName = 'Salt'); 294 | 295 | CREATE VIEW CH13_IngredientClass_Distinct_Recipe_Count 296 | AS 297 | SELECT Ingredient_Classes.IngredientClassDescription, COUNT(DISTINCT Recipe_Ingredients.RecipeID) AS CountOfRecipeID 298 | FROM Ingredient_Classes INNER JOIN 299 | Ingredients ON Ingredient_Classes.IngredientClassID = Ingredients.IngredientClassID INNER JOIN 300 | Recipe_Ingredients ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 301 | GROUP BY Ingredient_Classes.IngredientClassDescription; 302 | 303 | CREATE VIEW CH13_Meat_Ingredient_Recipe_Count_Group 304 | AS 305 | SELECT Ingredient_Classes.IngredientClassDescription, Ingredients.IngredientName, COUNT(*) AS RecipeCount 306 | FROM Ingredient_Classes INNER JOIN 307 | Ingredients ON Ingredient_Classes.IngredientClassID = Ingredients.IngredientClassID INNER JOIN 308 | Recipe_Ingredients ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 309 | WHERE (Ingredient_Classes.IngredientClassDescription = 'Meat') 310 | GROUP BY Ingredient_Classes.IngredientClassDescription, Ingredients.IngredientName; 311 | 312 | CREATE VIEW CH13_Meat_Ingredient_Recipe_Count_Subquery 313 | AS 314 | SELECT Ingredient_Classes.IngredientClassDescription, Ingredients.IngredientName, 315 | (SELECT COUNT(*) 316 | FROM Recipe_Ingredients 317 | WHERE Recipe_Ingredients.IngredientID = Ingredients.IngredientID) AS RecipeCount 318 | FROM Ingredient_Classes INNER JOIN 319 | Ingredients ON Ingredient_Classes.IngredientClassID = Ingredients.IngredientClassID 320 | WHERE (Ingredient_Classes.IngredientClassDescription = 'Meat'); 321 | 322 | CREATE VIEW CH13_Total_Ingredients_Needed 323 | AS 324 | SELECT Ingredients.IngredientName, Measurements.MeasurementDescription, SUM(Recipe_Ingredients.Amount) AS SumOfAmount 325 | FROM Ingredients INNER JOIN 326 | (Measurements INNER JOIN 327 | Recipe_Ingredients ON Measurements.MeasureAmountID = Recipe_Ingredients.MeasureAmountID) 328 | ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 329 | GROUP BY Ingredients.IngredientName, Measurements.MeasurementDescription; 330 | 331 | CREATE VIEW CH14_Recipe_Classes_Lots_Of_Salt 332 | AS 333 | SELECT Recipe_Classes.RecipeClassDescription, Ingredients.IngredientName, Sum(Recipe_Ingredients.Amount) AS SumOfSaltTeaspoons 334 | FROM (((Recipe_Classes INNER JOIN Recipes 335 | ON Recipe_Classes.RecipeClassID=Recipes.RecipeClassID) 336 | INNER JOIN Recipe_Ingredients 337 | ON Recipes.RecipeID=Recipe_Ingredients.RecipeID) 338 | INNER JOIN Ingredients 339 | ON Ingredients.IngredientID=Recipe_Ingredients.IngredientID) 340 | INNER JOIN Measurements 341 | ON Measurements.MeasureAmountID=Recipe_Ingredients.MeasureAmountID 342 | WHERE Ingredients.IngredientName='Salt' 343 | And Measurements.MeasurementDescription='Teaspoon' 344 | GROUP BY Recipe_Classes.RecipeClassDescription, Ingredients.IngredientName 345 | HAVING Sum(Recipe_Ingredients.Amount)>3; 346 | 347 | CREATE VIEW CH14_Recipe_Classes_Two_Or_More 348 | AS 349 | SELECT Recipe_Classes.RecipeClassDescription, COUNT(Recipes.RecipeID) AS CountOfRecipeID 350 | FROM Recipe_Classes INNER JOIN 351 | Recipes ON Recipe_Classes.RecipeClassID = Recipes.RecipeClassID 352 | GROUP BY Recipe_Classes.RecipeClassDescription 353 | HAVING (COUNT(Recipes.RecipeID) >= 2); 354 | 355 | CREATE VIEW CH14_Recipes_Beef_And_Garlic 356 | AS 357 | SELECT RecipeTitle 358 | FROM Recipes 359 | WHERE (RecipeID IN 360 | (SELECT Recipe_Ingredients.RecipeID 361 | FROM Ingredients INNER JOIN 362 | Recipe_Ingredients ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 363 | WHERE Ingredients.IngredientName = 'Beef' OR 364 | Ingredients.IngredientName = 'Garlic' 365 | GROUP BY Recipe_Ingredients.RecipeID 366 | HAVING COUNT(Recipe_Ingredients.RecipeID) = 2)); 367 | 368 | CREATE VIEW CH18_Ingredients_No_Recipe 369 | AS 370 | SELECT Ingredients.IngredientName 371 | FROM Ingredients 372 | WHERE Ingredients.IngredientID NOT IN 373 | (SELECT Recipe_Ingredients.IngredientID FROM Recipe_Ingredients); 374 | 375 | 376 | CREATE VIEW CH18_Ingredients_NOTIN_IrishStew_PolloPicoso_RoastBeef 377 | AS 378 | SELECT Ingredients.IngredientID, Ingredients.IngredientName 379 | FROM Ingredients 380 | WHERE Ingredients.IngredientID NOT IN 381 | (SELECT Recipe_Ingredients.IngredientID 382 | FROM Recipe_Ingredients 383 | INNER JOIN Recipes 384 | ON Recipe_Ingredients.RecipeID = Recipes.RecipeID 385 | WHERE RecipeTitle IN 386 | ('Irish Stew', 'Pollo Picoso', 'Roast Beef')); 387 | 388 | 389 | CREATE VIEW CH18_Recipe_Classes_No_Recipes_NOT_IN 390 | AS 391 | SELECT Recipe_Classes.RecipeClassDescription 392 | FROM Recipe_Classes 393 | WHERE Recipe_Classes.RecipeClassID NOT IN 394 | (SELECT Recipes.RecipeClassID 395 | FROM Recipes); 396 | 397 | 398 | CREATE VIEW CH18_Recipes_AtLeast_3_Same_Ingredients 399 | AS 400 | SELECT Recipes.RecipeID, Recipes.RecipeTitle, R2.RecipeID AS R2ID, R2.RecipeTitle AS R2Title, Count(Recipe_Ingredients.RecipeID) AS CountOfRecipeID 401 | FROM ((Recipes 402 | INNER JOIN Recipe_Ingredients 403 | ON Recipes.RecipeID=Recipe_Ingredients.RecipeID) 404 | INNER JOIN Recipe_Ingredients AS RI2 405 | ON Recipe_Ingredients.IngredientID=RI2.IngredientID) 406 | INNER JOIN Recipes AS R2 407 | ON R2.RecipeID=RI2.RecipeID 408 | WHERE RI2.RecipeID>Recipes.RecipeID 409 | GROUP BY Recipes.RecipeID, Recipes.RecipeTitle, R2.RecipeID, R2.RecipeTitle 410 | HAVING Count(Recipe_Ingredients.RecipeID)>=3; 411 | 412 | 413 | CREATE VIEW CH18_Recipes_Beef_And_Garlic 414 | AS 415 | SELECT Recipes.RecipeTitle, Recipes.Preparation 416 | FROM Recipes 417 | WHERE EXISTS 418 | (SELECT Recipe_Ingredients.* 419 | FROM Ingredients 420 | INNER JOIN Recipe_Ingredients 421 | ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 422 | WHERE Ingredients.IngredientName='Beef' 423 | AND Recipe_Ingredients.RecipeID = Recipes.RecipeID) 424 | AND EXISTS 425 | (SELECT Recipe_Ingredients.* 426 | FROM Ingredients 427 | INNER JOIN Recipe_Ingredients 428 | ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 429 | WHERE Ingredients.IngredientName='Garlic' 430 | AND Recipe_Ingredients.RecipeID = Recipes.RecipeID); 431 | 432 | 433 | CREATE VIEW CH18_Recipes_Beef_Onion_Carrot 434 | AS 435 | SELECT Recipes.RecipeID, Recipes.RecipeTitle 436 | FROM Recipes 437 | WHERE Recipes.RecipeID IN 438 | (SELECT Recipe_Ingredients.RecipeID 439 | FROM Recipe_Ingredients 440 | INNER JOIN Ingredients 441 | ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID 442 | WHERE Ingredients.IngredientName = 'Beef') 443 | AND Recipes.RecipeID IN 444 | (SELECT Recipe_Ingredients.RecipeID 445 | FROM Recipe_Ingredients 446 | INNER JOIN Ingredients 447 | ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID 448 | WHERE Ingredients.IngredientName = 'Onion') 449 | AND Recipes.RecipeID IN 450 | (SELECT Recipe_Ingredients.RecipeID 451 | FROM Recipe_Ingredients 452 | INNER JOIN Ingredients 453 | ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID 454 | WHERE Ingredients.IngredientName = 'Carrot'); 455 | 456 | 457 | CREATE VIEW CH18_Recipes_Butter_NOT_Beef_Onion_Carrot_GROUPBY 458 | AS 459 | SELECT Recipes.RecipeID, Recipes.RecipeTitle 460 | FROM ((Recipes 461 | INNER JOIN Recipe_Ingredients 462 | ON Recipes.RecipeID=Recipe_Ingredients.RecipeID) 463 | INNER JOIN Ingredients 464 | ON Ingredients.IngredientID=Recipe_Ingredients.IngredientID) 465 | LEFT JOIN 466 | (SELECT Recipe_Ingredients.RecipeID 467 | FROM Recipe_Ingredients INNER JOIN Ingredients 468 | ON Ingredients.IngredientID=Recipe_Ingredients.IngredientID 469 | WHERE Ingredients.IngredientName In ('Beef','Onion','Carrot')) AS RIBOC 470 | ON Recipes.RecipeID=RIBOC.RecipeID 471 | WHERE Ingredients.IngredientName='Butter' 472 | AND RIBOC.RecipeID Is Null 473 | GROUP BY Recipes.RecipeID, Recipes.RecipeTitle 474 | HAVING COUNT(RIBOC.RecipeID)=0; 475 | 476 | 477 | CREATE VIEW CH18_Recipes_No_Dairy_RIGHT 478 | AS 479 | SELECT Recipes.RecipeID, Recipes.RecipeTitle 480 | FROM Recipes 481 | WHERE Recipes.RecipeID NOT IN 482 | (SELECT Recipe_Ingredients.RecipeID 483 | FROM (Recipe_Ingredients 484 | INNER JOIN Ingredients 485 | ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID) 486 | INNER JOIN Ingredient_Classes 487 | ON Ingredient_Classes.IngredientClassID = Ingredients.IngredientClassID 488 | WHERE Ingredient_Classes.IngredientClassDescription = 'Butter') 489 | AND Recipes.RecipeID NOT IN 490 | (SELECT Recipe_Ingredients.RecipeID 491 | FROM (Recipe_Ingredients 492 | INNER JOIN Ingredients 493 | ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID) 494 | INNER JOIN Ingredient_Classes 495 | ON Ingredient_Classes.IngredientClassID = Ingredients.IngredientClassID 496 | WHERE Ingredient_Classes.IngredientClassDescription = 'Cheese') 497 | AND Recipes.RecipeID NOT IN 498 | (SELECT Recipe_Ingredients.RecipeID 499 | FROM (Recipe_Ingredients 500 | INNER JOIN Ingredients 501 | ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID) 502 | INNER JOIN Ingredient_Classes 503 | ON Ingredient_Classes.IngredientClassID = Ingredients.IngredientClassID 504 | WHERE Ingredient_Classes.IngredientClassDescription = 'Dairy'); 505 | 506 | 507 | CREATE VIEW CH18_Recipes_No_Dairy_WRONG 508 | AS 509 | SELECT DISTINCT Recipes.RecipeID, Recipes.RecipeTitle 510 | FROM ((Recipes 511 | INNER JOIN Recipe_Ingredients 512 | ON Recipes.RecipeID=Recipe_Ingredients.RecipeID) 513 | INNER JOIN Ingredients 514 | ON Ingredients.IngredientID=Recipe_Ingredients.IngredientID) 515 | INNER JOIN Ingredient_Classes 516 | ON Ingredient_Classes.IngredientClassID=Ingredients.IngredientClassID 517 | WHERE Ingredient_Classes.IngredientClassDescription Not In ('Butter','Cheese','Dairy'); 518 | 519 | 520 | CREATE VIEW CH18_Recipes_NOT_Beef_Onion_Carrot_GROUPBY 521 | AS 522 | SELECT Recipes.RecipeID, Recipes.RecipeTitle 523 | FROM Recipes LEFT JOIN (SELECT Recipe_Ingredients.RecipeID 524 | FROM Recipe_Ingredients 525 | INNER JOIN Ingredients 526 | ON Ingredients.IngredientID=Recipe_Ingredients.IngredientID 527 | WHERE Ingredients.IngredientName In ('Beef','Onion','Carrot')) AS RIBOC 528 | ON Recipes.RecipeID=RIBOC.RecipeID 529 | WHERE RIBOC.RecipeID IS NULL 530 | GROUP BY Recipes.RecipeID, Recipes.RecipeTitle 531 | HAVING COUNT(RIBOC.RecipeID)=0; 532 | 533 | 534 | CREATE VIEW CH18_Recipes_NOT_Beef_Onion_Carrot_NOTEXISTS 535 | AS 536 | SELECT Recipes.RecipeID, Recipes.RecipeTitle 537 | FROM Recipes 538 | WHERE NOT EXISTS 539 | (SELECT * FROM Recipe_Ingredients 540 | INNER JOIN Ingredients 541 | ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID 542 | WHERE Ingredients.IngredientName IN ('Beef', 'Onion', 'Carrot') 543 | AND Recipe_Ingredients.RecipeID = Recipes.RecipeID); 544 | 545 | 546 | CREATE VIEW CH18_Recipes_NOT_Beef_Onion_Carrot_NOTIN_1 547 | AS 548 | SELECT Recipes.RecipeID, Recipes.RecipeTitle 549 | FROM Recipes 550 | WHERE Recipes.RecipeID NOT IN 551 | (SELECT Recipe_Ingredients.RecipeID 552 | FROM Recipe_Ingredients 553 | INNER JOIN Ingredients 554 | ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID 555 | WHERE Ingredients.IngredientName = 'Beef') 556 | AND Recipes.RecipeID NOT IN 557 | (SELECT Recipe_Ingredients.RecipeID 558 | FROM Recipe_Ingredients 559 | INNER JOIN Ingredients 560 | ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID 561 | WHERE Ingredients.IngredientName = 'Onion') 562 | AND Recipes.RecipeID NOT IN 563 | (SELECT Recipe_Ingredients.RecipeID 564 | FROM Recipe_Ingredients 565 | INNER JOIN Ingredients 566 | ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID 567 | WHERE Ingredients.IngredientName = 'Carrot'); 568 | 569 | 570 | CREATE VIEW CH18_Recipes_NOT_Beef_Onion_Carrot_NOTIN_2 571 | AS 572 | SELECT Recipes.RecipeID, Recipes.RecipeTitle 573 | FROM Recipes 574 | WHERE Recipes.RecipeID NOT IN 575 | (SELECT Recipe_Ingredients.RecipeID 576 | FROM Recipe_Ingredients 577 | INNER JOIN Ingredients 578 | ON Recipe_Ingredients.IngredientID = Ingredients.IngredientID 579 | WHERE Ingredients.IngredientName IN ('Beef', 'Onion', 'Carrot')); 580 | 581 | 582 | CREATE VIEW CH18_Recipes_NOT_Beef_Onion_Carrot_OUTERJOIN 583 | AS 584 | SELECT Recipes.RecipeID, Recipes.RecipeTitle 585 | FROM Recipes LEFT JOIN 586 | (SELECT Recipe_Ingredients.RecipeID 587 | FROM Recipe_Ingredients INNER JOIN Ingredients 588 | ON Recipe_Ingredients.IngredientID=Ingredients.IngredientID 589 | WHERE Ingredients.IngredientName In ('Beef','Onion','Carrot')) AS RBeefCarrotOnion 590 | ON Recipes.RecipeID=RBeefCarrotOnion.RecipeID 591 | WHERE RBeefCarrotOnion.RecipeID Is Null; 592 | 593 | 594 | CREATE VIEW CH21_RecipeClass_IngredientClass_Counts 595 | AS 596 | SELECT CASE WHEN GROUPING(RecipeClassDescription) = 0 597 | THEN RecipeClassDescription 598 | ELSE 'Total Recipes All Classes' END AS RecipeClass, 599 | CASE WHEN GROUPING (IngredientClassDescription) = 0 600 | THEN IngredientClassDescription 601 | ELSE 'All Ingredient Classes' END AS IngredientClass, 602 | Count(Recipes.RecipeID) AS TotalRecipes 603 | FROM Recipe_Classes 604 | INNER JOIN Recipes 605 | ON Recipes.RecipeClassID = Recipe_Classes.RecipeClassID 606 | INNER JOIN Recipe_Ingredients 607 | ON Recipe_Ingredients.RecipeID = Recipes.RecipeID 608 | INNER JOIN Ingredients 609 | ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 610 | INNER JOIN Ingredient_Classes 611 | ON Ingredient_Classes.IngredientClassID = Ingredients.IngredientClassID 612 | GROUP BY GROUPING SETS (RecipeClassDescription, IngredientClassDescription); 613 | 614 | 615 | CREATE VIEW CH21_RecipeClass_IngredientClass_CUBE 616 | AS 617 | SELECT CASE WHEN GROUPING(RecipeClassDescription) = 0 618 | THEN RecipeClassDescription 619 | ELSE 'Total Recipes All Classes' END AS RecipeClass, 620 | CASE WHEN GROUPING (IngredientClassDescription) = 0 621 | THEN IngredientClassDescription 622 | ELSE 'All Ingredient Classes' END AS IngredientClass, 623 | Count(Recipes.RecipeID) AS TotalRecipes 624 | FROM Recipe_Classes 625 | INNER JOIN Recipes 626 | ON Recipes.RecipeClassID = Recipe_Classes.RecipeClassID 627 | INNER JOIN Recipe_Ingredients 628 | ON Recipe_Ingredients.RecipeID = Recipes.RecipeID 629 | INNER JOIN Ingredients 630 | ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID 631 | INNER JOIN Ingredient_Classes 632 | ON Ingredient_Classes.IngredientClassID = Ingredients.IngredientClassID 633 | GROUP BY CUBE (RecipeClassDescription, IngredientClassDescription); 634 | 635 | 636 | CREATE VIEW CH21_RecipeClass_Recipe_Counts_ROLLUP 637 | AS 638 | SELECT CASE WHEN GROUPING(RecipeClassDescription) = 0 639 | THEN RecipeClassDescription 640 | ELSE 'Total Recipes All Classes' END AS RecipeClass, 641 | Count(Recipes.RecipeID) AS TotalRecipes 642 | FROM Recipe_Classes 643 | LEFT JOIN Recipes 644 | ON Recipes.RecipeClassID = Recipe_Classes.RecipeClassID 645 | GROUP BY ROLLUP (RecipeClassDescription); 646 | 647 | CREATE VIEW CH22_Ingredient_Recipe_Counts 648 | AS 649 | SELECT I.IngredientName, R.RecipeTitle, 650 | COUNT(*) OVER ( 651 | PARTITION BY I.IngredientName 652 | ) AS RecipeCount 653 | FROM Recipes AS R 654 | INNER JOIN Recipe_Ingredients AS RI 655 | ON RI.RecipeID = R.RecipeID 656 | INNER JOIN Ingredients AS I 657 | ON I.IngredientID = RI.IngredientID; 658 | 659 | 660 | CREATE VIEW CH22_Ingredients_By_Ingredient_Class 661 | AS 662 | SELECT ROW_NUMBER() OVER ( 663 | ORDER BY IC.IngredientClassDescription, I.IngredientName 664 | ) AS OverallNumber, 665 | IC.IngredientClassDescription, 666 | ROW_NUMBER() OVER ( 667 | PARTITION BY IC.IngredientClassDescription 668 | ORDER BY I.IngredientName 669 | ) AS NumberInClass, 670 | I.IngredientName 671 | FROM Ingredient_Classes AS IC 672 | LEFT JOIN Ingredients AS I 673 | ON I.IngredientClassID = IC.IngredientClassID; 674 | 675 | 676 | CREATE VIEW CH22_Recipe_Classes_Numbered 677 | AS 678 | SELECT ROW_NUMBER() OVER ( 679 | ORDER BY RC.RecipeClassDescription, R.RecipeTitle 680 | ) AS OverallNumber, 681 | RC.RecipeClassDescription, 682 | ROW_NUMBER() OVER ( 683 | PARTITION BY RC.RecipeClassDescription 684 | ORDER BY R.RecipeTitle 685 | ) AS NumberInClass, 686 | R.RecipeTitle 687 | FROM Recipe_Classes AS RC 688 | LEFT JOIN Recipes AS R 689 | ON R.RecipeClassID = RC.RecipeClassID; 690 | 691 | 692 | CREATE VIEW CH22_Recipe_Ingredient_Counts 693 | AS 694 | SELECT R.RecipeTitle, I.IngredientName, 695 | COUNT(*) OVER ( 696 | PARTITION BY R.RecipeTitle 697 | ) AS IngredientCount 698 | FROM Recipes AS R 699 | INNER JOIN Recipe_Ingredients AS RI 700 | ON RI.RecipeID = R.RecipeID 701 | INNER JOIN Ingredients AS I 702 | ON I.IngredientID = RI.IngredientID; 703 | 704 | --------------------------------------------------------------------------------