0% found this document useful (0 votes)
17 views5 pages

Phase 2 Report

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Phase 2 Report

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS306 Database project phase 2 report

This report describes the steps that are specified in the instructions for the phase 2 of our
magnificient journey to creating a database application!

Step 1) Atıf Aydın Turanlı 31034

Step 2) I have selected 2 tables: “predators” and predator_prey_relationship”. Here are the SQL
statements for constructing this table:

for the “predators” table:

create table predators (Habitat VARCHAR(50), _Name VARCHAR(50) PRIMARY KEY, Nutrition
VARCHAR(50));

INSERT INTO predators (_Name, Habitat, Nutrition)


VALUES
('Lion', 'Savanna', 'Herbivores, Carnivores'),
('Tiger', 'Rainforest', 'Herbivores, Carnivores'),
('Eagle', 'Mountains', 'Small mammals, Fish'),
('Shark', 'Ocean', 'Fish, Seals'),
('Python', 'Jungle', 'Small mammals, Birds'),
('Crocodile', 'River', 'Fish, Birds'),
('Polar Bear', 'Arctic', 'Seals, Fish'),
('Wolf', 'Forest', 'Herbivores, Carnivores'),
('Cheetah', 'Grassland', 'Herbivores, Carnivores'),
('Peregrine Falcon', 'Various', 'Birds, Insects’)

for the “predators_preys_relationship” table:

CREATE TABLE predator_prey_relationship (


predator_name VARCHAR(50),
prey_name VARCHAR(50),
FOREIGN KEY (predator_name) REFERENCES predators(_Name),
FOREIGN KEY (prey_name) REFERENCES preys(_Name)
);
INSERT INTO predator_prey_relationship (prey_name, predator_name)
VALUES
("Deer", "Wolf")
Step 3) Each table is in BCNF. Let me explain why:

The “predator_prey_relationship” is in Boyce-Codd Normal Form because it satisfies the


condition where there are no non-trivial functional dependencies other than the trivial dependency
(relation → relation), neither attribute ("predator_name" nor "prey_name") functionally determines
the other, and neither attribute alone can uniquely identify a tuple, hence neither attribute is a
superkey. Any predator can prey on multiple preys and multiple preys can be preyed on by multiple
predators.

The “predators” table also strictly adheres to the Boyce-Codd Normal Form due to the absence of
non-trivial functional dependencies beyond the trivial relation dependency (relation → relation).
None of the attributes ('Habitat', '_Name', and 'Nutrition') uniquely determine any other attribute
within the table. Additionally, no single attribute or combination thereof serves as a primary
attribute or superkey. As a result, the table inherently satisfies the criteria for BCNF.

Step 4) More data is inserted to each table as requested!

Step 5) Here is the “predators” table after 10 more insertions:


And the “predator_prey_relationship” table (only consisted of 1 row originally):

Note: Due to participation constraints, I also had to add rows to the “prey” table, since both
attributes are foreign keys.

Step 6) I will join the tables this way:

“Join these two tables such that each row in the joined table has the name of a prey that the predator
preys on.”

Relational Algebra equivalent would be:

predators ⨝ (predator_name=_Name) predators_prey_relationship


Step 7) Here is the SQL version:

SELECT predators.*, predator_prey_relationship.prey_name


FROM predators
INNER JOIN predator_prey_relationship ON predators._Name =
predator_prey_relationship.predator_name;

And the resulting table:

Step 8) I will query the database such that the result will be the relation instance that has 2
attributes: predator names and the amount of preys they prey on.

The SQL query would be:

SELECT p._Name AS Predator, COUNT(pp.prey_name) AS Prey_Count


FROM predators p
LEFT JOIN predator_prey_relationship pp ON p._Name = pp.predator_name
GROUP BY p._Name;

The resulting table:


Step 9) I don’t want the description of the habitat of a predator, in the “predators” table, to be too
long; let’s say 20 characters. I will implement this using the following SQL query:

ALTER TABLE predators


ADD CONSTRAINT CHK_HabitatLength CHECK (LENGTH(Habitat) <= 20);

As you can see when I try to insert a description longer than 20 characters, SQL does not allow me
to do so:

END OF THE REPORT

You might also like