A relational data model is where we use Tables to describe how our data is stored in a Relational Databases. Rows are called Tuples and Columns are called Attributes.
Student ID | Name | Age | Field |
---|---|---|---|
1 | John Smith | 20 | Computer Science |
2 | Jane Doe | 21 | Biology |
3 | Bob Johnson | 19 | Mathematics |
Here, Student ID, Name, Age and Field come under Attributes(columns) and the Tuples(rows) hold student data corresponding to the attributes.
A. An attribute is a property that describes the entity. For example, in a customer database, the attributes might be name, address, and phone number.
In a relational data model, it is depicted as a column.
A table is a collection of data organized in a table-like format within a database. It contains rows(called tuples), and columns(called attributes).
The data type defined for a column in a database is called a database domain. This data type can either be a built-in type (such as an integer or a string) or a custom type that defines data constraints.
In a database table, domain constraints are guidelines that specify the acceptable values for a certain property or field. These restrictions guarantee data consistency and aid in preventing the entry of inaccurate or inconsistent data into the database
In Relational Database Model , constraints are guidelines or limitations imposed on database tables to maintain the integrity, correctness, and consistency of the data. Constraints can be used to enforce data linkages across tables, verify that data is unique, and stop the insertion of erroneous data. A database needs constraints to be reliable and of high quality.
Types of Constraints in Relational Database Model
Relational algebra is a procedural query language, which takes instances of relations as input and yields instances of relations as output. It uses operators to perform queries.
An operator can be either unary or binary. They accept relations as their input and yield relations as their output.
Relational algebra is performed recursively on a relation and intermediate results are also considered relations.
The fundamental operations of relational algebra are
Will be explained in Following Questions
Select operation chooses the subset of tuples from the relation that satisfies the given condition mentioned in the syntax of selection.
The selection operation is also known as horizontal partitioning since it partitions the table or relation horizontally. Its Notation is σ c(R). where ‘c’ is the selection condition which is a boolean expression(condition).
Roll | Name | Department | Fees | Team |
---|---|---|---|---|
2 | Josh | CSE | 34000 | A |
3 | Kevin | ECE | 36000 | C |
4 | Ben | ECE | 56000 | D |
Query: Select all the students of Team A: σ Team = ‘A’ (Student)
SELECT *
FROM Students
WHERE Team = 'A';
Result:
Roll | Name | Department | Fees | Team |
---|---|---|---|---|
2 | Josh | CSE | 34000 | A |
Project operation selects (or chooses) certain attributes discarding other attributes. The Project operation is also known as vertical partitioning since it partitions the relation or table vertically discarding other columns or attributes.
Notation: πA(R) where ‘A’ is the attribute list, it is the desired set of attributes from the attributes of relation(R),symbol ‘π(pi)’ is used to denote the Project operator,R is generally a relational algebra expression, which results in a relation.
Class | Dept | Position |
---|---|---|
5 | CSE | Assistant Professor |
6 | EE | Assistant Professor |
6 | EE | Assistant Professor |
Query: Project Class and Dept from Faculty: π Class, Dept (Faculty)
SELECT DISTINCT Class, Dept
FROM Faculty;
Result: | Class | Dept | |——-|——| | 5 | CSE | | 6 | EE |
UNION Operation: A ∪ S where, A and S are the relations, symbol ‘∪’ is used to denote the Union operator. The result of Union operation, which is denoted by A ∪ S, is a relation that basically includes all the tuples that are present in A or in S, or in both, eliminating the duplicate tuples.
EMPID | NAME |
---|---|
1001 | Charles |
1002 | Abdul |
1003 | Rohan |
EMPID | NAME |
---|---|
1004 | Shermi |
1005 | Jhansi |
Query: Union operation on EMPID and NAME from two tables
SELECT EMPID, NAME
FROM Table1
UNION
SELECT EMPID, NAME
FROM Table2;
Result:
EMPID | NAME |
---|---|
1001 | Charles |
1002 | Abdul |
1003 | Rohan |
1004 | Shermi |
1005 | Jhansi |
This is a binary relation operation. It combines the tuples of two relations into one relation. It is denoted by ‘X’ symbol. A X S. this operator will simply create a pair between the tuples of each table.
EMPID | NAME |
---|---|
1001 | Charles |
1002 | Abdul |
1003 | Rohan |
DEPTID | DNAME |
---|---|
10 | Accounts |
20 | Design |
30 | Testing |
Query: Cartesian product of EMPID and NAME from two tables
SELECT A.EMPID, A.NAME, B.EMPID, B.NAME
FROM Table1 A
CROSS JOIN Table2 B;
Result: | EMPID | NAME | DEPTID | DNAME | |——-|———|——–|———-| | 1001 | Charles | 10 | Design | | 1001 | Charles | 20 | Accounts | | 1001 | Charles | 30 | Testing | | 1002 | Abdul | 10 | Design | | 1002 | Abdul | 20 | Accounts | | 1002 | Abdul | 30 | Testing | | 1003 | Rohan | 10 | Accounts | | 1003 | Rohan | 20 | Design | | 1003 | Rohan | 30 | Testing |
Consider two tables: employees
and departments
.
employees
id | name | dept_id |
---|---|---|
1 | Alice | 1 |
2 | Bob | 2 |
3 | Charlie | NULL |
4 | David | 1 |
departments
id | dept_name |
---|---|
1 | HR |
2 | IT |
3 | Marketing |
dept_id
column, as it’s present in both tables.Notation: R ⨝ S
Example:
SELECT employees.name, departments.dept_name FROM employees NATURAL JOIN departments;
Result:
id | name | dept_name |
---|---|---|
1 | Alice | HR |
2 | Bob | IT |
4 | David | HR |
NULL
is returned for columns from the right table.Notation: R ⟕ S
Example:
SELECT employees.name, departments.dept_name FROM employees LEFT JOIN departments ON employees.dept_id = departments.id;
Result:
name | dept_name |
---|---|
Alice | HR |
Bob | IT |
Charlie | NULL |
David | HR |
NULL
is returned for columns from the left table.Notation: R ⟖ S
Example:
SELECT employees.name, departments.dept_name FROM employees RIGHT JOIN departments ON employees.dept_id = departments.id;
Result:
name | dept_name |
---|---|
Alice | HR |
Bob | IT |
NULL | Marketing |
NULL
is returned for the unmatched rows.Notation: R ⟗ S
MySQL does not directly support full outer join, but you can achieve it using a combination of left join and right join with a UNION
.
Example:
SELECT employees.name, departments.dept_name FROM employees LEFT JOIN departments ON employees.dept_id = departments.id
UNION
SELECT employees.name, departments.dept_name FROM employees RIGHT JOIN departments ON employees.dept_id = departments.id;
Result:
name | dept_name |
---|---|
Alice | HR |
Bob | IT |
Charlie | NULL |
David | HR |
NULL | Marketing |
Example:
SELECT employees.name, departments.dept_name FROM employees INNER JOIN departments ON employees.dept_id = departments.id;
Result:
name | dept_name |
---|---|
Alice | HR |
Bob | IT |
David | HR |