Presentation is loading. Please wait.

Presentation is loading. Please wait.

One-to-many relationship Relational databases link several entities based on relationships that exist (based on modeling reality) 1:m relationship example:

Similar presentations


Presentation on theme: "One-to-many relationship Relational databases link several entities based on relationships that exist (based on modeling reality) 1:m relationship example:"— Presentation transcript:

1 One-to-many relationship Relational databases link several entities based on relationships that exist (based on modeling reality) 1:m relationship example: An Employee can serve as the project manager for many Clients but a Client is only managed by one Employee 1 CLIENT *ClientID ClientName ClientServices ClientCity ClientState ClientRevenue EmpID (FK) EMPLOYEE *EmpID EmpFirstname EmpLastname EmpStatus EmpRate

2 Connecting Tables Each specific entity = a corresponding table Entity name = Table name Entity attribute = Table columns 1:m relationship The two tables must be linked through a common column (key) Primary Key in 1 side (Employee) is added as a Foreign key on the many side (Client) Add a column to the Client table at the many end of a 1:m relationship (EmpID) This key (EmpID) acts as a “bridge” between the two tables Primary and Foreign keys should never be null (empty) 2

3 Employee and Client Client ClientID Client Name Client Services Client City Client State Client Revenue EmpID 1BK AssociatesCommercialPortlandOregon$210,000.002 2BlalocIndustrialKansas CityMissouri$330,000.002 3Bankton ElectricGovernmentNew York $210,000.003 4BickIndustrialRaleighNorth Carolina$550,000.001 5TX ElectricGovernmentHoustonTexas$160,000.003 6CrowCommercialDallasTexas$270,000.002 7GRBIndustrialAtlantaGeorgia$180,000.001 8H&PIndustrialDenverColorado$90,000.002 9LB&BIndustrialBostonMassachusetts$211,000.003 10CongroIndustrialAtlantaGeorgia$122,000.003 11Moss EnterprisesCommercialPhoenixArizona$33,000.001 12RubyIndustrialSan AntonioTexas$344,000.003 13Silver IndustriesIndustrialOmahaNebraska$218,000.002 14TPHCommercialAnnaheimCalifornia$166,000.001 Employee EmpID Emp Firstname Emp Lastname Emp Status Emp Rate 1DanPiercefull time$120.00 2SeanSmithfull time$85.00 3LauraJohnsonfull time$85.00 4MikeLucasfull time$55.00 5LarryWilsonfull time$55.00 6KeithEthridgepart time$25.00

4 Querying both tables Display all information from both tables - by matching on a common column (EmpID) SELECT * FROM Employee, Client Where Employee.EmpID = Client.EmpID; SELECT * FROM Employee INNER JOIN Client ON Employee.EmpID = Client.EmpID; 4 Employee.EmpID Emp firstname Emp lastname Emps tatus Emp Rate ClientID Client name Clients ervices Client city Client state Client Revenue Client.EmpID 1DanPiercefull time$120.004BickIndustrialRaleighNorth Carolina$550,000.001 1DanPiercefull time$120.007GRBIndustrialAtlantaGeorgia$180,000.001 1DanPiercefull time$120.0011Moss EnterprisesCommercialPhoenixArizona$33,000.001 1DanPiercefull time$120.0014TPHCommercialAnnaheimCalifornia$166,000.001 2SeanSmithfull time$85.001BK AssociatesCommercialPortlandOregon$210,000.002 2SeanSmithfull time$85.002BlalocIndustrialKansas CityMissouri$330,000.002 2SeanSmithfull time$85.006CrowCommercialDallasTexas$270,000.002 2SeanSmithfull time$85.008H&PIndustrialDenverColorado$90,000.002 2SeanSmithfull time$85.0013Silver IndustriesIndustrialOmahaNebraska$218,000.002 3LauraJohnsonfull time$85.003Bankton ElectricGovernmentNew York $210,000.003 3LauraJohnsonfull time$85.005TX ElectricGovernmentHoustonTexas$160,000.003 3LauraJohnsonfull time$85.009LB&BIndustrialBostonMassachusetts$211,000.003 3LauraJohnsonfull time$85.0010CongroIndustrialAtlantaGeorgia$122,000.003 3LauraJohnsonfull time$85.0012RubyIndustrialSan AntonioTexas$344,000.003

5 More Queries List the Project Manager’s (Employee) Last Name, Client Name, Client State, and Client Revenue for those Clients who are in Texas and Georgia. Sort the findings in order of Revenue. SELECT EmpLastName, ClientState, ClientRevenue FROM Employee, Client Where Employee.EmpID = Client.EmpID and ClientState In ('Texas', 'Georgia') Order by ClientRevenue; 5 EmpLastNameClientStateClientRevenue JohnsonGeorgia$122,000.00 JohnsonTexas$160,000.00 PierceGeorgia$180,000.00 SmithTexas$270,000.00 JohnsonTexas$344,000.00

6 Reporting the results of groups List the total revenue of clients for each Project Manager. Display the Emp Lastname and total revenue in the output. SELECT EmpLastname, sum(ClientRevenue) as TotalRevManaged FROM Employee, Client Where Employee.EmpID = Client.EmpID Group By EmpLastname; 6 EmpLastnameTotalRevManaged Johnson$1,047,000.00 Pierce$929,000.00 Smith$1,118,000.00

7 Applying Having Statement to the Group (Similar to the Where statement) List the total revenue of clients for each Project Manager for those Employees that manage 5 or more Clients. Display the Emp Lastname and total revenue in the output. SELECT EmpLastname, sum(ClientRevenue) as TotalRevManaged FROM Employee, Client Where Employee.EmpID = Client.EmpID Group By EmpLastname Having Count (*)>=5; 7 EmpLastnameTotalRevManaged Johnson$1,047,000.00 Smith$1,118,000.00

8 Subqueries – Alternative to Joins Subquery = a query nested within another query List the Last Name of all Employees who manage Clients located in either Texas or Oregon. SELECT EmpLastname FROM Employee Where EmpID IN (Select EmpID from Client Where ClientState = 'Texas' or ClientState = 'Oregon'); 8 EmpLastname Smith Johnson

9 Correlated subquery Solves the inner query via a loop List the EmpLastName, ClientName and Client Revenue for Clients that have revenue that exceeds the average revenue for the 3 Project Managers on ave SELECT EmpLastname, ClientName, ClientRevenue FROM Employee, Client Where Employee.EmpId = Client.EmpID and ClientRevenue > (Select AVG(ClientRevenue) FROM Client) 9 EmpLastnameClientNameClientRevenue PierceBick$550,000.00 SmithBlaloc$330,000.00 SmithCrow$270,000.00 JohnsonRuby$344,000.00


Download ppt "One-to-many relationship Relational databases link several entities based on relationships that exist (based on modeling reality) 1:m relationship example:"

Similar presentations


Ads by Google