Beach Cruisers Solution (Week 4) for
-
Upload
phillip-norman -
Category
Documents
-
view
218 -
download
0
Transcript of Beach Cruisers Solution (Week 4) for
-
7/30/2019 Beach Cruisers Solution (Week 4) for
1/6
I will attempt to describe the process and logic behind completing the Week 3 / Week 4
Homework.
First, the question:
1. Your company has decided to add a new line of bikes. These will be beach cruisers. Youwill need to update /insert information the following tables:select * from Production.Product
select * from Production.ProductCategory
select * from Production.ProductSubcategory
select * from Production.ProductModel
The beach cruisers come in two sizes: 44 and 48 inch, and each size has three color choices (you
can decide on these colors).
Before I start with an insert I always try to build a query that shows the relationships between
the tables. It helps to know which columns are important.
SELECT*From Production.Product
innerjoin Production.ProductModel
on ProductModel.ProductModelID = Product.ProductModelID
innerjoin Production.ProductSubcategory
on ProductSubcategory.ProductSubcategoryID = Product.ProductSubcategoryID
innerjoin Production.ProductCategoryon ProductCategory.ProductCategoryID = ProductSubcategory.ProductCategoryID
-
7/30/2019 Beach Cruisers Solution (Week 4) for
2/6
At this point, I like to see everything in Query Designer:
Although you dont have to with this exercise, it is a good idea to build inserts inwards toensure that the Primary Keys are populated before the Foreign Keys are created.
The ProductCategory is already available, so there is nothing to insert in
Production.ProductCategory (but I make a note of the ProductCategoryID:
The two ProductModels need to be created in Production.ProductModel. Because the
ProductModelID is a PK, with an IDENTITY constraint, I dont have to worry about creating a
ProductModelID number. Two of the columns allow nulls and two have default constraints
which makes populating the table easy:
INSERTINTO Production.ProductModel(Name, CatalogDescription, Instructions)
-
7/30/2019 Beach Cruisers Solution (Week 4) for
3/6
VALUES ('Beach Cruiser 44',NULL,NULL);
INSERTINTO Production.ProductModel(Name, CatalogDescription, Instructions)VALUES ('Beach Cruiser 48',NULL,NULL);
Which produces the following:
Next I have to populate the ProductSubCategory table with a new value. This is an IDENTITY as
well, so all I have to describe is the ProductCategoryID to use as a Foreign Key, and the Name:
SELECT*FROM Production.ProductSubcategory;INSERTINTO Production.ProductSubcategory
(ProductCategoryID, Name)
VALUES ('1','Beach Cruisers')
The result shows the inserted line:
I now have all of the necessary values to populate the Product Table. To summarize:
ProductSubcategoryID = 38
ProductModelID = 129, 130
These values have to be part of the INSERT, or otherwise the joins will not work after the insert.
It is important to note the nonclemature of fields like MakeFlag to retain consistency.
INSERT INTO Production.Product (
Name
,ProductNumber
,MakeFlag
,FinishedGoodsFlag
,Color
-
7/30/2019 Beach Cruisers Solution (Week 4) for
4/6
,SafetyStockLevel
,ReorderPoint
,StandardCost
,ListPrice
,DaysToManufacture
,ProductSubcategoryID
,ProductModelID
,SellStartDate
,ModifiedDate
)
VALUES (
Beach Cruiser 440 Red, 44
, BC-R50R 44
, 1
, 1
, Red
, 100
, 75
, 486.71
, 782.99
, 4
, 38
, 129
, GETDATE()
, GETDATE()
)
The most important step then is to use my original query to see if the row I added created the
correct PK/FK values:
-
7/30/2019 Beach Cruisers Solution (Week 4) for
5/6
SELECTProductID
, Product.Name, ProductModel.Name
, ProductCategory.Name, ProductNumber
, Color, Product.ProductSubcategoryID, ProductSubcategory.ProductSubcategoryID
, ProductSubCategory.ProductCategoryID, ProductCategory.ProductCategoryID, Product.ProductModelID, ProductModel.ProductModelID
From Production.Product
innerjoin Production.ProductModelon ProductModel.ProductModelID = Product.ProductModelID
innerjoin Production.ProductSubcategoryon ProductSubcategory.ProductSubcategoryID =
Product.ProductSubcategoryID
innerjoin Production.ProductCategoryon ProductCategory.ProductCategoryID =
ProductSubcategory.ProductCategoryID
where Product.ProductID >'1000'
I then check the results to ensure all of my joins produce the same results. This may seem like apointless step, but if you are creating joins with rowguids or complex ID fields it is a necessity.
ProductID 1005
Name Beach Cruiser 440 Red, 44
Name Beach Cruiser 44
Name Bikes
ProductNumber BC-R50R 44
Color Red
ProductSubcategoryID 38
ProductSubcategoryID 38
-
7/30/2019 Beach Cruisers Solution (Week 4) for
6/6
ProductCategoryID 1
ProductCategoryID 1
ProductModelID 129
ProductModelID 129
Once this step is completed correctly, I can then modify the INSERT statement to insert the
other products.