Question

In: Computer Science

Database Problem description:1. What is the difference between a candidate key and the primary key for...

Database

Problem description:1. What is the difference between a candidate key and the primary key for a given relation? What is a superkey?

2. What is a foreign key constraint? Why are such constraints important? What is referential integrity?

3. Answer teach of the following questions. The questions are based on the following relational schema:Emp(eid: integer, ename: string, age: integer, salary: real)Works(eid: integer, did: integer, pct_time: integer)Dept(did: integer, dname: string, budget: real, managerid: integer)

(1) Write the SQL statements required to create the preceding relations, including appropriate versions of all primary and foreign key integrity constraints.

(2) Define the Dept relation in SQL so that every department is guaranteed to have a manager

(3) Write an SQL statement to add John Doe as an employee with eid = 101, age= 32 and salary = 15,000

(4) Write an SQL statement to delete the Three tables: Emp, Works, Dept.

Solutions

Expert Solution

1) A Primary key is an attribute(column) or set of attributes that can be used to uniquely identify tuples within a relation or table. A table or a relation can only have a single Primary key. A primary key is a minimal superkey i.e , it contains only the least amount of attributes with which all tuples can be identified

A Candidate key, much like the primary key, is also an attribute or set of attributes that can uniquely identify tuples within a relation. The major difference is that, a relation may have multiple Candidate keys. Any one of them can be selected as the Primary key

A Primary Key cannot have NULL values, whereas candidate keys can have NULL values

Eg:

Student {ID, F_name, M_name, L_name, DOB} 

ID is a primary key because it is unique for every tuple

{F_name, M_name, L_name,DOB} can also be used as a primary key, but in this case, we used ID as the primary key hence {F_name, M_name, L_name, DOB} becomes a candidate key

Super Key is an attribute (or set of attributes) that is used to uniquely identifies all attributes in a relation. All super keys can’t be candidate keys but its reverse is true. Candidate keys form a proper subset of superkeys

eg: In the Student relation the three superkeys are:

  1. {ID}
  2. {F_name, M_name, L_name,DOB}
  3. {ID,F_name, M_name, L_name,DOB}

From the above three, we choose 1 and 2 as the candidate keys since 3 has redundant attributes.

2) A Foreign key is a column (or columns) that references a column (most often the primary key) of another table. Foreign keys are used to enforce referential integrity within the relations.

Eg: CUSTOMER{SID(Primary key), Last_name, First_name}

ORDER{Order_ID(Primary key), Order_date, Customer_SID(Foreign key), Amount}

In this example, Customer_SID attribute in the ORDERS table acts as a foreign key pointing to the SID column in the CUSTOMER table.

Foreign keys are important because:

  • They link tables
  • Prevent actions that would destroy link between tables
  • prevents insertion of invalid data onto the foreign key column of a table because it needs to be a value that is also present in the table that it points to

Referential integrity is specified between two relations and is used to maintain the consistency among tuples in two relations. The constraint states that a tuple in one relation that refers to another relation must refer to an existing tuple in that relation. In the above CUSTOMER and ORDER tables, Customer_SID of ORDER can only have values that are already present in the SID column of CUSTOMER table.

3) (1)

  • CREATE TABLE Works ( eid INTEGER NOT NULL , did INTEGER NOT NULL,pct_time INTEGER, PRIMARY KEY (eid, did), UNIQUE (eid), FOREIGN KEY (did) REFERENCES Dept )
  • CREATE TABLE Emp ( eid INTEGER, ename CHAR(10), age INTEGER, salary REAL, PRIMARY KEY (eid) )
  • CREATE TABLE Dept ( did INTEGER, budget REAL, managerid INTEGER ,dname CHAR(10), PRIMARY KEY (did), FOREIGN KEY (managerid) REFERENCES Emp, ON DELETE SET NULL)

(3) (2) CREATE TABLE Dept ( did INTEGER, budget REAL, managerid INTEGER NOT NULL , PRIMARY KEY (did), FOREIGN KEY (managerid) REFERENCES Emp)

(3) (3) INSERT INTO Emp (eid, ename, age, salary) VALUES (101, ’John Doe’, 32, 15000)

(3)(4) Assuming the table schema's need to be kept intact and only the values need to be deleted, the commands would be

DELETE FROM Emp;

DELETE FROM Works;

DELETE FROM Dept;

alternatively, if the table scheme does not have to be kept intact DROP TABLE command may be used,

DROP TABLE Table_name;


Related Solutions

what is super key,  candidate key, and primary key, and foreign key in terms of database? and...
what is super key,  candidate key, and primary key, and foreign key in terms of database? and plz provide some examples, thanks.
the mysql lyrics database is provided below 1.)What is a primary key used for? Can you...
the mysql lyrics database is provided below 1.)What is a primary key used for? Can you give me an example of a primary key in the Lyrics database? 2.)What is the purpose of doing a join between two tables. Ex. Why would I ever want to join the Tracks and Titles tables together? 3.)Why is isolation important in database design? DROP TABLES IF EXISTS Artists,Genre, Members, Titles, Tracks,SalesPeople,Studios,XrefArtistsMembers; DROP TABLES IF EXISTS Authors,Publishers,Titles,Title_Authors,Royalties; DROP TABLES IF EXISTS Products,Customers,Orders,Order_details; DROP TABLES...
1a) i) Explain the candidate key, primary key and foreign key with suitable examples [6] ii)...
1a) i) Explain the candidate key, primary key and foreign key with suitable examples [6] ii) Draw a suitable ER diagram to show the “IS-A” relationship between subtype and supertype [5] iii) Briefly explain the recursive relationship with a suitable example. [2]
Identify every possible primary key, candidate key, and foreign key for the following relations. Separate each...
Identify every possible primary key, candidate key, and foreign key for the following relations. Separate each key using a semicolon to avoid confusion. Assumptions: MIScompany has branches located in several states within the United States. A customer can be an individual or organization. driverId is the driving license number, ssno is the social security number and upc is the universal product code. Any equipment is rented and returned at the same branch. A customer can be a manufacturer and vice...
What are emotions? What is the difference between primary and secondary emotions? What are the primary...
What are emotions? What is the difference between primary and secondary emotions? What are the primary and secondary emotions? What is the developmental timecourse of primary and secondary emotions?
What is the primary difference between commissions and referrals?
What is the primary difference between commissions and referrals?
What is the difference between primary and secondary apnea?
What is the difference between primary and secondary apnea?
1. What is the difference between primary and secondary succession? Explain in detail.
1. What is the difference between primary and secondary succession? Explain in detail.2. How does the Biomass Accumulation Model describe succession of ecosystems following disturbances?
What is the difference between primary markets and secondary markets?Explain What is the difference between money...
What is the difference between primary markets and secondary markets?Explain What is the difference between money markets and capital markets?Explain What are three (3) of the seven (7) types of financial institutions? Include a description of the main services offered by each. Explain Why would the U.S. government, local governments, and corporations issue bonds? Explain Provide the definitions of a discount bond and a premium bond. Give examples. As owners, what rights and advantages do shareholders obtain? Explain Why might...
1. With respect to the polymerization process, what is the primary difference between a thermoplastic polymer...
1. With respect to the polymerization process, what is the primary difference between a thermoplastic polymer and a thermoset polymer? How does this affect recycling? 2. In a typical stress-strain plot of a structural steel, why is the apparent drop-off in strength after reaching the ultimate value not a realistic portrayal of steel behaviour? Why is this phenomenon ignored in steel structure design?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT