Q1: Describe all the relations among Person, Company, and Investment Table.
And also
what data can we retrieve from those tables?
Ans:
We have declared the three tables Person, Company and Investment as follows:
Person
CREATE TABLE Person_07 (
person_id INT,
person_name VARCHAR(100) NOT NULL,
dob DATE NOT NULL,
gender ENUM('MALE','FEMALE') NOT NULL,
father INT,
mother INT,
PRIMARY KEY(person_id),
FOREIGN KEY(father) references Person_07(person_id),
FOREIGN KEY(mother) references Person_07(person_id)
);
Company
CREATE TABLE Company_07 (
company_id INT,
company_name VARCHAR(100) NOT NULL,
capital DECIMAL(20,2),
PRIMARY KEY(company_id)
);
Investment
CREATE TABLE Investment_07 (
company_id INT,
person INT,
share DECIMAL(5,2) NOT NULL,
FOREIGN KEY(company_id) references Company_07(company_id),
FOREIGN KEY(person) references Person_07(person_id)
);
Here the relation among the tables are:
1. Person and Investment: A person can have none or multiple investments in the
investment table. He/She can invest in multiple companies as well.
This is achieved by assigning person_id as a foreign key to the person field of
investment table.
2. Investment and Person: The total share of a country hold by multiple person must add
up to 100%. Each investor must belong to the person table.
3. Person and Family: Father and mother of a person must be present in the person table.
This is achived by assigning father and mother fields with a foreign key of person_id
4. Company and Investment: Each company in investment table must belong to the
company table.
This is achieved by assigning company_id as foreign key tocompany field of investment
table
Q2:
1. Retrieve all people and their investments:
SELECT person_07.person_name as person_name,
company_07.company_name as company_name, investment_07.share as share
FROM person_07
LEFT JOIN investment_07 ON person_07.person_id= investment_07.person
LEFT JOIN company_07 ON investment_07.company =
company_07.company_id;
2. Retrieve total investments per company:
SELECT company_07.company_name as company_name,
SUM(investment_07.share) as totalInvestment
FROM company_07
LEFT JOIN investment_07 ON company_07.company_id =
investment_07.company
GROUP BY company_07.company_id, company_07.company_name;
3. Retrieve family information of a person:
SELECT person_07.person_id, person_07.person_name as person_name,
person_07.gender, person_07.dob, person_07.father as fatherName,
person_07.mother as motherName
FROM person_07
LEFT JOIN person_07 ON person_07.father = person_07.person_id
LEFT JOIN person_07 ON person_07.mother = person_07.person_id;
4. Retrieve investments for a specific person:
SELECT company_07.company_name, investment_07.share
FROM investment_07
JOIN company_07 ON investment_07.company = company_07.company_id
WHERE investment_07.person = 1;
5. Retrieve people who have invested in a specific company:
SELECT person_07.person_id, person_07.person_name as person_name,
person_07.gender, investment_07.share
FROM person_07
JOIN investment_07 ON person_07.person_id = investment_07.person
WHERE investment_07.company_id = 1;