'How to implement an Aggregation in SQL? (This is not about GroupBy)
In the scope of a university project I am supposed to implement an aggregation of my database.
I'm given a Entity-Relationship model which looks similar to this one:
Now I'm supposed to implement a SQL-Script which creates a database like this, but I cannot find anything about this topic on google or anywhere else. In the slides of my Professor it says
- For example, to represent aggregation manages between relationship works_on and entity set manager, create a schema
manages(employee_id, branch_name, title,manager_name)- Schema works_on is redundant provided we are willing to store null values for attribute manager_name in relation on schema manages
So I tried to just put two tables into my SQL-Script one called works-on and one called manages. In works-on I put all the Primary keys of job, branch and employee and defined them as foreign keys. In manages I put all of these Primary keys and additionally I put manager. Now the problem is that when I use the Reverse-Engineer of MySQL-workbench to create the EER-Model of the database, I don't get anything out of it which has to do with this aggregation. So what am I doing wrong here?
As requested by @Barmar I just wrote the CREATE TABLE-statements that I would've used for this:
CREATE TABLE job
(jobid INT,
PRIMARY KEY(jobid));
CREATE TABLE employee
(employeeid INT,
PRIMARY KEY(employeeid));
CREATE TABLE branch
(branchid INT,
PRIMARY KEY(branchid));
CREATE TABLE manager
(managerid INT,
PRIMARY KEY(managerid));
CREATE TABLE works_on
(jobid INT, KEY(jobid),
branchid INT, KEY(branchid),
employeeid INT, KEY(employeeid));
CREATE TABLE manages
(jobid INT, KEY(jobid),
branchid INT, KEY(branchid),
employeeid INT, KEY(employeeid),
managerid INT, KEY(managerid));
ALTER TABLE works_on
ADD CONSTRAINT FK_workson_employee FOREIGN KEY(employeeid) REFERENCES employee(employeeid);
ALTER TABLE works_on
ADD CONSTRAINT FK_workson_branch FOREIGN KEY(branchid) REFERENCES branch(branchid);
ALTER TABLE works_on
ADD CONSTRAINT FK_workson_job FOREIGN KEY(jobid) REFERENCES job(jobid);
ALTER TABLE manages
ADD CONSTRAINT FK_manages_employee FOREIGN KEY(employeeid) REFERENCES employee(employeeid);
ALTER TABLE manages
ADD CONSTRAINT FK_manages_branch FOREIGN KEY(branchid) REFERENCES branch(branchid);
ALTER TABLE manages
ADD CONSTRAINT FK_manages_job FOREIGN KEY(jobid) REFERENCES job(jobid);
ALTER TABLE manages
ADD CONSTRAINT FK_manages_manager FOREIGN KEY(managerid) REFERENCES job(managerid);
Solution 1:[1]
You need to give a primary key to the works_on table, and then reference that in the manages table, rather than referencing employee, job, and branch directly.
CREATE TABLE works_on (
works_on_id INT PRIMARY KEY,
jobid INT,
branchid INT,
employeeid INT,
CONSTRAINT jobid FOREIGN KEY REFERENCES job(jobid),
CONSTRAINT branchid FOREIGN KEY REFERENCES brahc(branchid),
CONSTRAINT employeeid FOREIGN KEY REFERENCES employee(employeeid)
);
CREATE TABLE manages (
managerid INT,
works_on_id INT,
CONSTRAINT managerid FOREIGN KEY REFERENCES manager(id),
CONSTRAINT works_on_id FOREIGN KEY REFERENCES works_on(id)
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Barmar |
