Unit-4 Relational Model and SQL Commands - Image.Marked
Unit-4 Relational Model and SQL Commands - Image.Marked
Relational model can represent as a table with columns and rows. Each row is known as a
tuple. Each table of the column has a name or attribute.
Attribute: It contains the name of a column in a particular table. Each attribute Ai must
have a domain, dom(Ai)
Relational schema: A relational schema contains the name of the relation and name of all
columns or attributes.
Relational key: In the relational key, each row has one or more attributes. It can identify
the row in the relation uniquely.
o In the given table, NAME, ROLL_NO, PHONE_NO, ADDRESS, and AGE are the
attributes.
o The instance of schema STUDENT has 5 tuples.
o t3 = <Laxman, 33289, 8583287182, Gurugram, 20>
Properties of Relations
o Name of the relation is distinct from all other relations.
o Each relation cell contains exactly one atomic (single) value
o Each attribute contains a distinct name
o Attribute domain has no significance
o tuple has no duplicate value
o Order of tuple can have a different sequence
Relational Algebra
Relational algebra is a procedural query language. It gives a step by step process to obtain
the result of the query. It uses operators to perform queries.
1. Select Operation:
o The select operation selects tuples that satisfy a given predicate.
o It is denoted by sigma (σ).
1. Notation: σ p(r)
Where:
Input:
1. σ BRANCH_NAME="perryride" (LOAN)
Output:
2. Project Operation:
o This operation shows the list of those attributes that we wish to appear in the result.
Rest of the attributes are eliminated from the table.
o It is denoted by ∏.
Input:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
3. Union Operation:
o Suppose there are two tuples R and S. The union operation contains all the tuples
that are either in R or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by ∪.
1. Notation: R ∪ S
A union operation must hold the following condition:
Example:
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
4. Set Intersection:
o Suppose there are two tuples R and S. The set intersection operation contains all
tuples that are in both R & S.
o It is denoted by intersection ∩.
1. Notation: R ∩ S
Example: Using the above DEPOSITOR table and BORROW table
Input:
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
o Suppose there are two tuples R and S. The set intersection operation contains all
tuples that are in R but not in S.
o It is denoted by intersection minus (-).
1. Notation: R - S
Example: Using the above DEPOSITOR table and BORROW table
Input:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
6. Cartesian product
o The Cartesian product is used to combine each row in one table with each row in the
other table. It is also known as a cross product.
o It is denoted by X.
1. Notation: E X D
Example:
EMPLOYEE
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
1. EMPLOYEE X DEPARTMENT
Output:
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
7. Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).
Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
1. ρ(STUDENT1, STUDENT)
Note: Apart from these common operations Relational algebra can be used in Join
operations.
Join Operations:
A Join operation combines related tuples from different relations, if and only if a given join
condition is satisfied. It is denoted by ⋈.
Example:
EMPLOYEE
EMP_CODE EMP_NAME
101 Stephan
102 Jack
103 Harry
SALARY
EMP_CODE SALARY
101 50000
102 30000
103 25000
Example: Let's use the above EMPLOYEE table and SALARY table:
Input:
EMP_NAME SALARY
Stephan 50000
Jack 30000
Harry 25000
2. Outer Join:
The outer join operation is an extension of the join operation. It is used to deal with missing
information.
Example:
EMPLOYEE
FACT_WORKERS
Input:
1. (EMPLOYEE ⋈ FACT_WORKERS)
Output:
EMP_NAME STREET CITY BRANCH SALARY
Input:
1. EMPLOYEE ⟕ FACT_WORKERS
Input:
1. EMPLOYEE ⟖ FACT_WORKERS
Output:
Input:
1. EMPLOYEE ⟗ FACT_WORKERS
Output:
3. Equi join:
It is also known as an inner join. It is the most common join. It is based on matched data
as per the equality condition. The equi join uses the comparison operator(=).
Example:
CUSTOMER RELATION
CLASS_ID NAME
1 John
2 Harry
3 Jackson
PRODUCT
PRODUCT_ID CITY
1 Delhi
2 Mumbai
3 Noida
Input:
1. CUSTOMER ⋈ PRODUCT
Output:
1 John 1 Delhi
2 Harry 2 Mumbai
3 Harry 3 Noida
Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the quality of
information.
o Integrity constraints ensure that the data insertion, updating, and other processes
have to be performed in such a way that data integrity is not affected.
o Thus, integrity constraint is used to guard against accidental damage to the
database.
Example:
Example:
3. Referential Integrity Constraints
o A referential integrity constraint is specified between two tables.
o In the Referential integrity constraints, if a foreign key in Table 1 refers to the
Primary Key of Table 2, then every value of the Foreign Key in Table 1 must be null
or be available in Table 2.
Example:
4. 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:
SQL SET OPERATIONS
SQL supports few Set operations which can be performed on the table data. These are used to get
meaningful results from data stored in the table, under different special conditions.
In this tutorial, we will cover 4 different types of SET operations, along with example:
UNION
UNION ALL
INTERSECT
MINUS
UNION Operation
UNION is used to combine the results of two or more SELECT statements. However it will eliminate
duplicate rows from its resultset. In case of union, number of columns and datatype must be same in
both the tables, on which UNION operation is being applied.
Example of UNION
The First table,
ID Name
1 abhi
2 adam
ID Name
2 adam
3 Chester
UNION
ID NAME
1 abhi
2 adam
3 Chester
UNION ALL
This operation is similar to Union. But it also shows the duplicate rows.
Example of Union All
The First table,
ID NAME
1 abhi
2 adam
ID NAME
2 adam
3 Chester
UNION ALL
ID NAME
1 abhi
2 adam
2 adam
3 Chester
INTERSECT
Intersect operation is used to combine two SELECT statements, but it only retuns the
records which are common from both SELECT statements. In case of Intersect the
number of columns and datatype must be same.
NOTE: MySQL does not support INTERSECT operator.
Example of Intersect
The First table,
ID NAME
1 abhi
2 adam
ID NAME
2 adam
3 Chester
INTERSECT
2 adam
MINUS
The Minus operation combines results of two SELECT statements and return only those
in the final result, which belongs to the first set of the result.
Example of Minus
The First table,
ID NAME
1 abhi
2 adam
ID NAME
2 adam
3 Chester
ID NAME
1 abhi
S SQL COMMANDS
Q
L
C
o
m
m
a
n
d
s
⮚ What is SQL?
• Structured Query Language and it helps to make practice on SQL commands which provides
immediate results.
• SQL is Structured Query Language, which is a computer language for storing, manipulating and
retrieving data stored in relational database.
• SQL is the standard language for Relation Database System.
• All relational database management systems like MySQL, MS Access, and Oracle, Sybase,
Informix, and SQL Server use SQL as standard database language.
⮚ Why SQL?
• Allows users to create and drop databases and tables.
• Allows users to describe the data.
• Allows users to define the data in database and manipulate that data.
• Allows users to access data in relational database management systems.
• Allows embedding within other languages using SQL modules, libraries & pre-compilers.
Allows users to set permissions on tables, procedures, and views
⮚ SQL Architecture:
• When you are executing an SQL command for any RDBMS, the system determines the best way
to carry out your request and SQL engine figures out
how to interpret the task.
⮚ SQL Commands:
• The standard SQL commands to interact with relational databases are CREATE, SELECT,
INSERT, UPDATE, DELETE and DROP.
• These commands can be classified into groups based on their nature:
Comma Descripti
nd on
DROP Deletes an entire table, a view of a table or other object in the database.
Comman Descripti
d on
Comma Descripti
nd on
Command Description
COMMIT Make all the changes made by the statements issued permanent.
ROLLBACK Undoes all changes since the beginning of transaction or since a save
point.
S
DATA TYPE DESCRIPTIO
L
N
N
o
1. NUMBER:
Used to store a numeric value in a field column.
It may be decimal, integer or real value.
General syntax: NUMBER(n, d)
Where n specifies the number of digits and d specifies the number of digits to right of the
decimal point.
Example: marks NUMBER(3), average NUMBER(2, 3)
2. CHAR:
Used to store a character type data in a column.
General syntax: CHAR(size) o Where size represents the maximum (255 Characters)
number of characters in a column.
Example: name CHAR(15)
3. VARCHAR/VARCHAR2:
It is used to store variable length alphanumeric data.
General syntax: VARCHAR(size) / VARCHAR2(size)
Where size represents the maximum (2000 Characters) number of characters in a column.
Example: address VARCHAR2(50)
4. DATE:
It is used to store date in columns.
SQL supports the various date formats other than the standard D-MON-YY.
Example: dob DATE
5. TIME:
It is used to store time in columns.
SQL supports the various time formats other than the standard hh-mm-ss.
Every DATE and TIME can be added, subtracted or compared as it can be done with other data
types.
6. LONG:
1. It is used to store variable length strings of up to 2GB size.
2. Example: description LONG
ALL The ALL operator is used to compare a value to all values in another value set.
AND The AND operator allows the existence of multiple conditions in an SQL statement's
WHERE clause.
ANY The ANY operator is used to compare a value to any applicable value in the list
according to the condition.
BETWEE The BETWEEN operator is used to search for values that are within a set of values,
N given the minimum value and the maximum value.
EXISTS The EXISTS operator is used to search for the presence of a row in a specified table that
meets certain criteria.
IN The IN operator is used to compare a value to a list of literal values that have been
specified.
LIKE The LIKE operator is used to compare a value to similar values using wildcard operators.
NOT The NOT operator reverses the meaning of the logical operator with which it is used. Eg:
NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator.
IS NULL The NULL operator is used to compare a value with a NULL value.
UNIQUE The UNIQUE operator searches every row of a specified table for uniqueness
(no duplicates).
);
o The DESCRIBE or DESC command displays name of the columns, their data type and size
ALTER Statement:
• The table can be modified or changed by using the ALTER command.
• Syntax: Basic syntax of ALTER TABLE statement is as follows:
Example:
• Using the ALTER TABLE command the following tasks cannot be performed
o Changing a table name.
o Changing the column name.
o Decreasing the size of a column if table data exists.
o Changing a column’s data type.
DROP TABLE:
• The SQL DROP TABLE statement is used to remove a table definition and all data,
indexes, triggers, constraints, and permission specifications for that table.
• Example:
INSERT:
• The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.
• Syntax:
• There are two basic syntaxes of INSERT INTO statement as follows:
• Here, column1, column2,...columnN are the names of the columns in the table into which you
want to insert data.
• You may not need to specify the column(s) name in the SQL query if you are adding values for all
the columns of the table. But make sure the order of the values is in the same order as the columns
in the table.
UPDATE:
• SQL provides the ability to change data through UPDATE command.
• The UPDATE command used to modify or update an already existing row or rows of a table.
• The basic syntax of UPDATE command is given below.
SELECT:
• SQL SELECT statement is used to fetch the data from a database table which returns data in the
form of result table. These result tables are called result-sets.
• Syntax: The basic syntax of SELECT statement is as follows:
SELECT column1, column2, columnN Compulsor
FROM Table_name; y Part
[HAVING condition(s)]
[ORDER BY column-name(s)];
• Here, column1, column2...are the fields of a table whose values you want to fetch. If you want to
fetch all the fields available in the field, then you can use the following syntax: