Khawar assigment 3 dbms
Khawar assigment 3 dbms
Example:
Consider a Product table where each product is identified by a unique
ProductID:
2. Foreign Key
Example
Consider an Orders table that records orders placed by customers:
These foreign keys help establish relationships between the Orders table and
the Customers/Product tables.
3. Unique Key
Definition: A Unique Key ensures that all values in the column (or combination of
columns) are unique. It can accept NULL values.
Example:
Consider a Users table where each user has a unique Username, but
PhoneNumber can be optional:
o Username is a Unique Key because it must be unique for each user, but it can
allow NULL values for cases where the user doesn't have a username.
o PhoneNumber is also a Unique Key (if enforced), ensuring that no two users have
the same phone number.
4. Candidate Key
Example:
Consider a Student table:
Student
Email Name
ID
[email protected]
1 Alice
m
2 [email protected] Bob
charlie@example. Charli
3
com e
o Both StudentID and Email can uniquely identify a student, so they are both
Candidate Keys.
o StudentID may be chosen as the Primary Key, and Email will be an Alternate
Key.
5. Super Key
Definition: A Super Key is any set of columns that can uniquely identify
records in a table. It may include extra columns that are not necessary for
uniqueness.
Example:
Consider the same Student table:
Student
Email Name
ID
[email protected]
1 Alice
m
2 [email protected] Bob
charlie@example. Charli
3
com e
Example:
Consider a Library system with a BookLoans table:
Here, StudentID and BookID together form a Composite Key, ensuring that a
student cannot borrow the same book multiple times on the same day.
Individually, neither column would be unique.
7. Alternate Key
Definition: An Alternate Key is a candidate key that was not chosen as the
primary key.
Example:
Consider a Product table:
Product ProductNa SK
ID me U
A12
101 Laptop
3
Mobile B45
102
Phone 6
Headphone C78
103
s 9
8. Null Key
Definition: A Null Key is a column that can accept NULL values, typically
used for optional information.
Example:
Consider an Employees table:
Employe PhoneNu
Name
eID mber
123-456-
1 Alice
7890
2 Bob NULL
Charli
3 NULL
e
o PhoneNumber is a Null Key because it allows NULL values for employees who
don't have a phone number listed.
9. Secondary Key
Example:
Consider a Customers table:
Custome Customer
City
rID Name
1 Alice New York
Los
2 Bob
Angeles
3 Charlie Chicago
If you frequently search customers by their City, you might create an index on
the City column, making City a Secondary Key. This index would speed up
queries like "Find all customers in New York".