INSTRUCTOR:
NUSRAT JAHAN
SENIOR LECTURER, SWE, DIU.
SE223
LECTURE 4
09/16/2023Database
1
Key Constrain
There are five types of keys in database which is as follows -
• Candidate key
• Primary Key
• Foreign Key
• Alternate Key
• Composite Key
09/16/2023Database
2
• Example:
• STUDENT {SID, FNAME, LNAME, COURSEID}
• Here in STUDENT table keys are:
• Candidate keys are SID or FNAME+LAME
• Primary Key: SID
• Foreign Key: COURSEID
• Alternate Key: FNAME+LAME
• Composite Key: FNAME+LAME
09/16/2023Database
3
Candidate Key
Candidate keys are those keys which is candidate for primary key of a table. In simple words we can understand that such type of keys which full fill all the requirements of primary key which is not null and have unique records is a candidate for primary key. So thus type of key is known as candidate
key. Every table must have at least one candidate key.
09/16/2023Database
4
Primary Key
•
Such type of candidate key which is chosen as a primary key for table is known as primary key.
Primary keys are used to identify tables. There is only one primary key per table. In SQL Server
when we create primary key to any table then a clustered index is automatically created to that column.
09/16/2023Database
5
Foreign Key
• Foreign key are those keys which is used to define relationship between two tables. When we want to implement relationship between two tables then we use concept of foreign key. It is also known as referential integrity. We can create more than one foreign key per table. foreign key is generally a primary key from one table that appears as a field in another where the first table has a relationship to the second. In other words, if we had a table A with a primary key X that linked to a table B
where X was a field in B, then X would be a foreign key in B.
09/16/2023Database
6
Alternate Key
• If any table have more than one candidate key, then after choosing primary key from those
candidate key, rest of candidate keys are known as an alternate key of that table. Like here we
can take a very simple example to understand the concept of alternate key. Suppose we have a table named Employee which has two columns EmpID and EmpMail, both have not null attributes and unique value. So both columns are treated as
candidate key. Now we make EmpID as a primary key to that table then EmpMail is known as
alternate key.
09/16/2023Database
7
Composite Key
When we create keys on more than one column then that key is known as composite key. Like here we can take an example to understand this feature.
I have a table Student which has two columns Sid and SrefNo and we make primary key on these two column. Then this key is known as composite key.
09/16/2023Database
8
Joined Relations
• Join operations take two relations and return as a result another relation.
• A join operation is a Cartesian product which requires that tuples in the two relations match (under some condition). It also specifies the
attributes that are present in the result of the join
• The join operations are typically used as subquery expressions in the from clause
Join operations – Example
• Relation course
Relation prereq
Observe that
prereq information is missing for CS-315 and course information is missing for CS-437
Outer Join
• An extension of the join operation that avoids loss of information.
• Computes the join and then adds tuples form one relation that does not match tuples in the other relation to the result of the join.
• Uses null values.
Left Outer Join
course natural left outer join prereq
Right Outer Join
course natural right outer join prereq
Joined Relations
• Join operations take two relations and return as a result another relation.
• These additional operations are typically used as subquery expressions in the from clause
• Join condition – defines which tuples in the two
relations match, and what attributes are present in the result of the join.
• Join type – defines how tuples in each relation that do not match any tuple in the other relation (based on the join condition) are treated.
Full Outer Join
course natural full outer join prereq
Joined Relations – Examples
• course inner join prereq on
course.course_id = prereq.course_id
What is the difference between the above, and a natural join?
course left outer join prereq on course.course_id = prereq.course_id
Joined Relations – Examples
course natural right outer join prereq
course full outer join prereq using (course_id)
Joined Relations – Datasets for Examples
• Relation loan
Relation borrower
Note: borrower information missing for L-260 and loan information missing for L-155
Joined Relations – Examples
• loan inner join borrower on
loan.loan_number = borrower.loan_number
loan left outer join borrower on
loan.loan_number = borrower.loan_number
Joined Relations – Examples
• loan natural inner join borrower
loan natural right outer join borrower
Find all customers who have either an account or a loan (but not both) at the bank.
select customer_name
from (depositor natural full outer join borrower )
where account_number is null or loan_number is null
Joined Relations – Examples
• Natural join can get into trouble if two relations have an attribute with
same name that should not affect the join condition
• e.g. an attribute such as remarks may be present in many tables
• Solution:
• loan full outer join borrower using (loan_number)
Thanks
09/16/2023Database
22