Unit 2
Unit 2
Candidate Key
The minimal set of attributes that can uniquely identify a tuple is
known as a candidate key. For Example, STUD_NO in STUDENT
relation.
It is a minimal super key.
It is a super key with no repeated data is called a candidate key.
The minimal set of attributes that can uniquely identify a record.
It must contain unique values.
It can contain NULL values.
Every table must have at least a single candidate key.
A table can have multiple candidate keys but only one primary key.
The value of the Candidate Key is unique and may be null for a
tuple.
There can be more than one candidate key in a relationship.
Example:
STUD_NO is the candidate key for relation STUDENT.
Table STUDENT
STUD_NO SNAME ADDRESS PHONE
Example:
Consider the table shown above.
STUD_NO+PHONE is a super key.
Super key attributes can contain NULL Candidate key attributes can also
values. contain NULL values.
Primary Key
There can be more than one candidate key in relation out of which one
can be chosen as the primary key.
It is a unique key.
It can identify only one tuple (a record) at a time.
It has no duplicate values, it has unique values.
It cannot be NULL.
Primary keys are not necessarily to be a single column; more than
one column can also be a primary key for a table.
Example:
Table STUDENT
STUD_NO SNAME ADDRESS PHONE
S.
Primary Key Candidate Key
No.
Primary key is a unique and non−null
Candidate key is also a unique key to
1. key which identify a record uniquely in
identify a record uniquely in a table.
table.
Primary key column value cannot be Candidate key column can have
2.
NULL. NULL value.
Primary key is most important part of Candidate key signifies as which key
3.
any relation or table. can be used as Primary Key.
Alternate Key
The candidate key other than the primary key is called an alternate
key .
All the keys which are not primary keys are called alternate keys.
It is a secondary key.
It contains two or more fields to identify two or more records.
These values are repeated.
Eg:- SNAME, and ADDRESS is Alternate keys
Example:
UNIQUE KEY
The syntax of the SQL UNIQUE is:
CREATE TABLE table_name (
column_name data_type UNIQUE
);
Here,
UNIQUE
It enforces entity
Purpose It enforces unique data.
integrity.
Each table supports only A table can have more than one unique
Number of Key
one primary key. key.
We can create a primary
key column in the table
using the below syntax: We can create a unique key column in
the table using the below syntax:
Foreign Key
If an attribute can only take the values which are present as values of
some other attribute, it will be a foreign key to the attribute to which it
refers.
Surrogate Key
A surrogate key is an artificial key that is assigned to a record in a database table to
uniquely identify the row data in a table, and is not based on any natural primary
key. A surrogate key column holds usually an integer and the value is generated
before any record is inserted into the table.
Example: In the following table, the Date of Joining is a column that holds a
timestamp that inserts at the time any employee joins, and it is uniquely identified
for each employee. In this example, the Date of Joining is a surrogate key.
Employee_ID Employee_Name Email Mobile Date of Joining
1. Domain constraints
3. Check constraints
Here,
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>=18)
);
it is used to fill a column with a default and fixed value. The value will
be added to all new records when no other value is provided.
Query:
CREATE TABLE Geeks (
ID int NOT NULL,Name varchar(255),
Age int, Location varchar(255) DEFAULT 'Noida');
5. Key constraints
o Keys are the entity set that is used to identify an entity within its
entity set uniquely.
o An entity set can have multiple keys, but out of which one key will
be the primary key. A primary key can contain a unique and null
value in the relational table.
Example:
Example
o The entity integrity constraint states that primary key value can't
be null.
o This is because the primary key value is used to identify individual
rows in relation and if the primary key has a null value, then we
can't identify those rows.
o A table can contain a null value other than the primary key field.
Example:
https://www.scaler.com/topics/dbms/relational-algebra-in-dbms/
Fundamental Operations
Select (σ): This operation is used to select rows from a relation that
satisfy a given predicate.
Project (π): It is used to select columns from a relation, thereby
reducing the relation to only those columns that are required.
Union (⋃): This operation combines the tuples of two relations
and removes duplicate tuples.
Set Difference (-): It yields the tuples that are present in one
relation but not in another.
Cartesian Product (×): This operation returns a relation that is a
combination of every tuple of one relation with every tuple of
another.
Additional Operations
Syntax
σcondition(table name)
Example
1 CSE A
2 ECE B
3 CIVIL B
4 IT A
Now, to display all the records of student table, we will use the
following command −
σ(student)
In addition to this, when we have to display all the records of CSE
branch in student table, we will use the following command −
σbranch=cse(student)
Hence, the result will be as follows −
1 CSE A
To display all the records in student tables whose regno>2, we will use
the below mentioned command −
σRegNo>2(student)
The output will be as follows −
3 CIVIL B
4 IT A
To display the record of ECE branch section B students, use the given
command −
σbranch=ECE ^ section=B(student)
To display the records of section B CSE and IT branch, use the
following command −
B C
2 4
2 3
3 4
Example:
FRENCH
Student_Name Roll_Number
Ram 01
Mohan 02
Vivek 13
Geeta 17
GERMAN
Student_Name Roll_Number
Vivek 13
Geeta 17
Shyam 21
Rohan 25
Ram
Mohan
Vivek
Geeta
Shyam
Rohan
Note: The only constraint in the union of two relations is that both relations
must have the same set of Attributes.
Example: From the above table of FRENCH and GERMAN, Set Difference
is used as follows
π(Student_Name)FRENCH - π(Student_Name)GERMAN
Student_Name
Ram
Mohan
Note: The only constraint in the Set Difference between two relations is that
both relations must have the same set of Attributes.
Example: From the above table of FRENCH and GERMAN, the Set
Intersection is used as follows
π(Student_Name)FRENCH ∩ π(Student_Name)GERMAN
Student_Name
Vivek
Geeta
Note: The only constraint in the Set Difference between two relations is that
both relations must have the same set of Attributes.
6. Rename(ρ): Rename is a unary operation used for renaming attributes of
a relation.
A
Name Age Sex
Ram 14 M
Sona 15 F
Kim 20 M
B
ID Course
1 DS
2 DBMS
AXB
Name Age Sex ID Course
Ram 14 M 1 DS
Ram 14 M 2 DBMS
Sona 15 F 1 DS
Name Age Sex ID Course
Sona 15 F 2 DBMS
Kim 20 M 1 DS
Kim 20 M 2 DBMS
Note: If A has ‘n’ tuples and B has ‘m’ tuples then A X B will have ‘ n*m ‘
tuples.
Relational Division
Let's assume that table A has two sets of columns, X and Y, and
table B has a set of columns Y. In other words, X is the set of columns
in A that are not present in B. We'll assume an ordered relation between
the Y columns in A and B so that we know which column
in B corresponds to which column in A.
Figure 12.27. Now consider the query: Which countries speak all the
languages spoken in Canada?
To answer this, we first find all the languages spoken in Canada, using
the expression Speaks where country = ‘Canada’ [language].
This returns the table {English, French}. We then divide the Speaks
table by this result, as shown, to obtain the final answer {Canada,
Dominica}.
Derived Operators
These are some of the derived operators, which are derived from the
fundamental operators.
1. Natural Join(⋈)
2. Conditional Join
Join Operations:
A Join operation combines related tuples from different relations, if and only
if a given join.
EMP
Name ID Dept_Name
A 120 IT
B 125 HR
C 110 Sales
D 111 IT
DEPT
Dept_Name Manager
Sales Y
Production Z
IT A
EMP ⋈ DEPT
Name ID Dept_Name Manager
A 120 IT A
C 110 Sales Y
D 111 IT A
Theta Join is used to join two tables based on some conditions. The
condition can be on any attributes of the tables performing Theta join.
Any comparison operator can be used in the condition.
A ⋈θ B where θ is the condition for join.
R1 and R2 are relations having attributes (A1, A2, .., An) and (B1, B2,..
,Bn) such that the attributes don’t have anything in common, that is R1
∩ R2 = Φ.
Student Table:
Name Std
SID
101 Alex 10
102 Maria 11
\
Subjects Table:
Subject
Class
10 Math
10 English
11 Music
11 Sports
STUDENT ⋈Student.Std = Subject.Class SUBJECT
Student_detail
SID Name Std Class Subject
101 Alex 10 10 Math
101 Alex 10 10 English
102 Maria 11 11 Music
102 Maria 11 11 Sports
Self Join :
SQL self join where a table joins with itself, allowing for comparisons
within the same table. It's particularly useful for querying hierarchical
data or comparing rows within the same table. When performing a self
join, SQL treats the single table as if it were two separate tables to
perform the join.
Syntax
SELECTA.column_name, B.column_name
FROM table_name AS A JOIN table_name AS B
ON A.common_field=B.common_field
WHERE condition;
table_name: The name of the table you are performing the self
join on.
A and B: Aliases for the table to distinguish the two instances.
common_field: The column based on which the join is performed.
condition: Additional conditions to filter the results.Example
CREATETABLE Employees (
EmployeeID INT,
Name VARCHAR(50),
ManagerID INT
);
1 John NULL
2 Jane 1
3 Doe 1
4 Smith 2
To find each employee and their manager's name, you would use the
following SQL query:
John NULL
Jane John
Doe John
Smith Jane
In this example, the self join allows us to link each employee to their
manager within the same Employees table by using
the ManagerID column to relate employees to each other.
CROSS JOIN is the best choice when we need to match each row of one
table to every other row of another table. It is helpful in many
applications where we need to obtain paired combinations of records.
Cross Join B/W Two Sets
Syntax
SELECT * FROM table1
CROSS JOIN table2;
Table 1- Customer
ID NAME AGE PHONE
To create both these tables on your system, you can write the following code:
In this example, we will use the CROSS JOIN command to match the data of the
Customer and Orders table.
Query
SELECT *
FROM CUSTOMER
CROSS JOIN ORDERS;
Output
Cross Join
As we can see, whether the other table matches or not, the CROSS JOIN keyword
returns all similar records from both tables
4. Outer Join:
EMPLOYEE
FACT_WORKERS
EMPLOYEE ⟕ FACT_WORKERS
Nehru
Hari Hyderabad TCS 50000
street
M.G.
Ravi Delhi NULL NULL
Street
b. Right outer join:
1. EMPLOYEE ⟖ FACT_WORKERS
Output:
Park
Shyam Kolkata Wipro 20000
street
Nehru
Hari Hyderabad TCS 50000
street
o Full outer join is like a left or right join except that it contains all
rows from both tables.
o In full outer join, tuples in R that have no matching tuples in S and
tuples in S that have no matching tuples in R in their common
attribute name.
o It is denoted by ⟗.
1. EMPLOYEE ⟗ FACT_WORKERS
EMP_NAME STREET CITY BRANCH SALARY
Park
Shyam Kolkata Wipro 20000
street
Nehru
Hari Hyderabad TCS 50000
street
M.G.
Ravi Delhi NULL NULL
Street