Foreign Keys - The SQL
As we continue to explore databases and ideas, this project will help you practice model databases and relationships.
We will be expanding on our CompanyDatabase from the sql-intro assignment. You will be adding new tables and be writing queries for these new tables.
Reuse the same repository from that assignment for this assignment. Add the new queries below the queries from that assignment such that the one repository will have both sets of queries. Use the same repository URL to turn in tonight's homework.
Objectives
- Work with foreign keys
- Craft queries that join tables
Explorer Mode
- In your
CompanyDatabase, add a table namedDepartmentswith the following columns:Idas a primary keyDepartmentNameas textBuildingas text
- Add a Foreign key
DepartmentIdto yourEmployeesTable. If you have trouble, remove the existing employees by runningtruncate table "Employees". - Add tables named
ProductsandOrders.Productsshould have the columnsIdas a primary keyPriceas a data type that can store a number with decimal places (e.g. 45.12)Nameas textual dataDescriptionas textual dataQuantityInStockas a data type that can store a number WITHOUT decimal places (e.g. 42)
Ordersshould have the columnsIdas a primary keyOrderNumberas textual dataDatePlacedas a data type that can store a date (with Year, Month, and Day) -- or one with hours, minutes, and seconds. Student's choice.Emailas textual data
- In our company, one
Ordercan have manyProductsand oneProductcan have manyOrders. This will be a Many-to-Many relationship. Create the necessary tableProductOrders, foreign keys, and theOrderQuantityfield needed for this to happen.
Create queries that can do the following:
- Insert the following
Departments
Department Name Building Development Main Marketing North - Insert the following
Employees
FullName Salary JobPosition PhoneExtension IsPartTime Department Id Tim Smith 40000 Programmer 123 false 1 Barbara Ramsey 80000 Manager 234 false 1 Tom Jones 32000 Admin 456 true 2 - Insert the following
Insert the following
ProductsPrice Name Description QuantityInStock 12.45 Widget The Original Widget 100 99.99 Flowbee Perfect for haircuts 3
- Insert a new order with order number
X529, placed on Jan 1st, 2020 at 4:55PM, by someone with the email address "person@example.com" - Add an order quantity of
3for the product namedWidgetto the orderX529 - Add an order quantity of
2for the product namedFlowbeeto the orderX529 - Given a building, return all employees that work in that building. Show this query for buildings named
North Side,East Side, and finally a building that you actually have in your data -- even if your data doesn't have any departments in those buildings. NOTE this means you can't manually look upIds and use them, you have to let SQL do that work. (Hint: JOIN) - Find all orders that contain the product id of
2. - Find the quantity of the
Flowbeeproduct from order with order numberX529. You may not write the value of anIdin your query, let SQL do the work (HINT: JOIN)
Adventure Mode
None
Epic Mode
- Give these SQL Koans a try.