9
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 ClientService s ClientCity ClientState ClientRevenue EmpID (FK) EMPLOYEE *EmpID EmpFirstname EmpLastname EmpStatus EmpRate

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

Embed Size (px)

Citation preview

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

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

*ClientIDClientName

ClientServicesClientCity

ClientStateClientRevenue

EmpID (FK)

EMPLOYEE

*EmpIDEmpFirstnameEmpLastname

EmpStatusEmpRate

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

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

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

Employee and Client

Client

ClientIDClientName

ClientServices

ClientCity

ClientState

ClientRevenue

EmpID

1 BK Associates Commercial Portland Oregon $210,000.00 2

2 Blaloc Industrial Kansas City Missouri $330,000.00 2

3 Bankton Electric Government New York New York $210,000.00 3

4 Bick Industrial Raleigh North Carolina $550,000.00 1

5 TX Electric Government Houston Texas $160,000.00 3

6 Crow Commercial Dallas Texas $270,000.00 2

7 GRB Industrial Atlanta Georgia $180,000.00 1

8 H&P Industrial Denver Colorado $90,000.00 2

9 LB&B Industrial Boston Massachusetts $211,000.00 3

10 Congro Industrial Atlanta Georgia $122,000.00 3

11 Moss Enterprises Commercial Phoenix Arizona $33,000.00 1

12 Ruby Industrial San Antonio Texas $344,000.00 3

13 Silver Industries Industrial Omaha Nebraska $218,000.00 2

14 TPH Commercial Annaheim California $166,000.00 1

Employee

EmpIDEmp

FirstnameEmp

LastnameEmp

StatusEmpRate

1 Dan Pierce full time $120.00

2 Sean Smith full time $85.00

3 Laura Johnson full time $85.00

4 Mike Lucas full time $55.00

5 Larry Wilson full time $55.00

6 Keith Ethridge part time $25.00

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

Querying both tablesDisplay 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

Empfirstname

Emplastname

Empstatus

EmpRate ClientID Client

nameClientservices

Clientcity

Clientstate

ClientRevenue

Client.EmpID

1 Dan Pierce full time $120.00 4 Bick Industrial Raleigh North Carolina $550,000.00 1

1 Dan Pierce full time $120.00 7 GRB Industrial Atlanta Georgia $180,000.00 1

1 Dan Pierce full time $120.00 11 Moss Enterprises Commercial Phoenix Arizona $33,000.00 1

1 Dan Pierce full time $120.00 14 TPH Commercial Annaheim California $166,000.00 1

2 Sean Smith full time $85.00 1 BK Associates Commercial Portland Oregon $210,000.00 2

2 Sean Smith full time $85.00 2 Blaloc Industrial Kansas City Missouri $330,000.00 2

2 Sean Smith full time $85.00 6 Crow Commercial Dallas Texas $270,000.00 2

2 Sean Smith full time $85.00 8 H&P Industrial Denver Colorado $90,000.00 2

2 Sean Smith full time $85.00 13 Silver Industries Industrial Omaha Nebraska $218,000.00 2

3 Laura Johnson full time $85.00 3 Bankton Electric Government New York New York $210,000.00 3

3 Laura Johnson full time $85.00 5 TX Electric Government Houston Texas $160,000.00 3

3 Laura Johnson full time $85.00 9 LB&B Industrial Boston Massachusetts $211,000.00 3

3 Laura Johnson full time $85.00 10 Congro Industrial Atlanta Georgia $122,000.00 3

3 Laura Johnson full time $85.00 12 Ruby Industrial San Antonio Texas $344,000.00 3

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

More QueriesList 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

EmpLastName ClientState ClientRevenue

Johnson Georgia $122,000.00

Johnson Texas $160,000.00

Pierce Georgia $180,000.00

Smith Texas $270,000.00

Johnson Texas $344,000.00

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

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

EmpLastname TotalRevManaged

Johnson $1,047,000.00

Pierce $929,000.00

Smith $1,118,000.00

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

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 EmpLastnameHaving Count (*)>=5;

7

EmpLastname TotalRevManaged

Johnson $1,047,000.00

Smith $1,118,000.00

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

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

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

Correlated subquery

Solves the inner query via a loopList 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

EmpLastname ClientName ClientRevenue

Pierce Bick $550,000.00

Smith Blaloc $330,000.00

Smith Crow $270,000.00

Johnson Ruby $344,000.00