Database Management Unit 2 (Self)
Database Management Unit 2 (Self)
Relational model:
The relational model represents how data is stored in
Relational Databases. A relational database consists of a
collection of tables, each of which is assigned a unique name.
Consider a relation STUDENT with attributes ROLL_NO, NAME,
ADDRESS, PHONE, and AGE shown in the table.
Table Student
ROLL_NO NAME ADDRESS PHONE AGE
4 SURESH DELHI 18
Important Terminologies
Attribute: Attributes are the properties that define an
entity. e.g.; ROLL_NO, NAME, ADDRESS
Relation Schema: A relation schema defines the
structure of the relation and represents the name of
the relation with its attributes. e.g.; STUDENT
(ROLL_NO, NAME, ADDRESS, PHONE, and AGE)
authorized user.
constraints, namely :
Key Constraints
column, etc.
successfully applied.
key, a unique key can accept NULL values and it can be used
error.
4. Key Constraints
There are a number of key constraints in SQL that ensure
the database. There can be more than one key in the table
Relational Algebra:
Relational Calculus:
Where,
t: the set of tuples
p: is the condition which is true for the given set of tuples.
S.N Basis of
O Comparison Relational Algebra Relational Calculus
Relational Calculus is a
It is a Procedural
Language Type Declarative (non-procedural)
language.
1. language.
tuples.
Notation :
{ a1, a2, a3, ..., an | P (a1, a2, a3, ..., an) }
Where a1, a2, a3, … an are attributes of the relation and P is the
condition.
Example :
{ | < EMPLOYEE > DEPT_ID = 10 }
select EMP_ID and EMP_NAME of employees who work for
department 10.
Difference between Tuple Relational Calculus (TRC) and Domain
Relational Calculus (DRC) :
A domain is equivalent to
A tuple is a single element
column data type and any
3. Tuple/ Domain of relation. In database
constraints on the value of
terms, it is a row.
data.
14. Use case Useful for selecting tuples Useful for selecting specific
that satisfy a certain values or for constructing
condition or for retrieving a more complex queries that
S. Basis of Tuple Relational Domain Relational
No. Comparison Calculus (TRC) Calculus (DRC)
SQL
SQL is a standard database language used to access and manipulate
data in databases. SQL stands for Structured Query Language. SQL
was developed by IBM Computer Scientists in the 1970s. By
executing queries SQL can create, update, delete, and retrieve data
in databases like MySQL, Oracle, PostgreSQL, etc. Overall SQL is a
query language that communicates with databases.
SQL also provides a standardized way of communicating with
databases, ensuring that data is consistent and uniform across
different systems. Its popularity and versatility have made it a
must-have skill for data professionals and developers, as it is used
extensively in various applications such as web development, data
analytics, business intelligence, and more.
Need of SQL :
Advantages of SQL :
SQL has many advantages which makes it popular and highly
demanded. It is a reliable and efficient language used for
communicating with the database. Some advantages of SQL are as
follows:
2. No Coding Skills –
For data retrieval, large number of lines of code is not
required. All basic keywords such as SELECT, INSERT INTO,
UPDATE, etc are used and also the syntactical rules are not
complex in SQL, which makes it a user-friendly language.
3. Standardized Language –
Due to documentation and long establishment over years,
it provides a uniform platform worldwide to all its users.
4. Portable –
It can be used in programs in PCs, server, laptops
independent of any platform (Operating System, etc). Also,
it can be embedded with other applications as per
need/requirement/use.
5. Interactive Language –
Easy to learn and understand, answers to complex queries
can be received in seconds.
1. Complex Interface –
SQL has a difficult interface that makes few users
uncomfortable while dealing with the database.
2. Cost –
Some versions are costly and hence, programmers cannot
access it.
3. Partial Control –
Due to hidden business rules, complete control is not given
to the database.
4. Limited Flexibility: SQL databases are less flexible than
NoSQL databases when it comes to handling unstructured
or semi-structured data, as they require data to be
structured into tables and columns.
5. Lack of Real-Time Analytics: SQL databases are
designed for batch processing and do not support real-time
analytics, which can be a disadvantage for applications
that require real-time data processing.
6. Limited Query Performance: SQL databases may have
limited query performance when dealing with large
datasets, as queries may take longer to process than in-
memory databases.
7. Complexity: SQL databases can be complex to set up and
manage, requiring skilled database administrators to
ensure optimal performance and maintain data integrity.
Applications of SQL :
Retrieving Information
Modification/Manipulation of data and database table such
as Insertion, Deletion and Updation .
SQL Literals
There are four kinds of literal values supported in SQL. They are :
Character string, Bit string, Exact numeric, and Approximate
numeric. These are explained as following below.
1. Character string : Character strings are written as a
sequence of characters enveloped in single quotes. the only
quote character is delineate at intervals a personality string
by 2 single quotes. Some example of character strings are :
‘My String’
‘I love GeeksForGeeks’
‘16378’
2. Bit string : A bit string is written either as a sequence of 0s
and 1s enveloped in single quotes and preceded by the
letter ‘B’ or as a sequence of positional representation
system digits enveloped in single quotes and preceded by
the letter X’ some examples are given below :
B’10001011′
B’1′
B’0′
X’C 5′
X’0′
3. Exact numeric : These literals are written as a signed or
unsigned decimal variety probably with mathematical
notation. Samples of actual numeric literals are given
below :
8
80
80.00
0.8
+88.88
-88.88
4. Approximate numeric : Approximate numeric literals are
written as actual numeric literals followed by the letter ‘E’,
followed by a signed or unsigned number. Some example
are :
6E6
66.6E6
+66E-6
0.66E
-6.66E-8
SQL Commands
o SQL commands are instructions. It is used to communicate with the
database. It is also used to perform specific tasks, functions, and queries
of data.
o SQL can perform various tasks like create a table, add data to tables, drop
the table, modify the table, set permission for users.
Syntax:
b. DROP: It is used to delete both the structure and record stored in the
table.
Syntax
Syntax:
d. TRUNCATE: It is used to delete all the rows from the table and free the
space containing the table.
Syntax:
1: INSERT
2:UPDATE
3:DELETE
Syntax:
OR
For example:
Syntax:
For example:
1. UPDATE students
2. SET User_Name = 'Sonoo'
3. WHERE Student_Id = '3'
Syntax:
1. DELETE FROM table_name [WHERE condition];
For example:
o Grant
o Revoke
Example
Example
o COMMIT
o ROLLBACK
o SAVEPOINT
1. COMMIT;
Example:
Syntax:
1. ROLLBACK;
Example:
Syntax:
1. SAVEPOINT SAVEPOINT_NAME;
5. Data Query Language
DQL is used to fetch the data from the database.
o SELECT
Syntax:
1. SELECT expressions
2. FROM TABLES
3. WHERE conditions;
For example:
1. SELECT emp_name ;
2. FROM employee ;
3. WHERE age > 20;
SQL Operators
Structured Query Language is a computer language that we
use to interact with a relational database. In this article we will
see all types of SQL operators.
In simple operator can be defined as an entity used to perform
operations in a table.
Operators are the foundation of any programming language.
We can define operators as symbols that help us to perform
specific mathematical and logical computations on operands. In
other words, we can say that an operator operates the
operands. SQL operators have three different categories.
Types of SQL Operators
Arithmetic operator
Comparison operator
Logical operator
Arithmetic Operators
We can use various arithmetic operators on the data stored in the tables.
Arithmetic Operators are:
Operator Description
This operator works with the ‘ALL’ keyword and it calculates division
/
operations.
Operator Description
Example Query:
SELECT * FROM employee WHERE emp_city NOT LIKE 'A%';
Output:
Comparison Operators
Another important operator in SQL is a comparison operator, which is used to
compare one expression’s value to other expressions. SQL supports different
types of comparison operator, which is described below:
Operator Description
= Equal to.
Example Query:
SELECT * FROM MATHS WHERE MARKS=50;
Output:
Logical Operators
The Logical operators are those that are true or false. They return true or false
values to combine one or more true or false values.
Operator Description
Not takes a single Boolean as an argument and change its value from false
NOT
to true or from true to false.
Example Query:
SELECT * FROM employee WHERE emp_city =
'Allahabad' AND emp_country = 'India';
Output:
SQL Indexes
An index is a schema object. It is used by the server to speed
up the retrieval of rows by using a pointer. It can reduce disk
I/O(input/output) by using a rapid path access method to locate
data quickly.
An index helps to speed up select queries and where clauses,
but it slows down data input, with the update and the insert
statements. Indexes can be created or dropped with no effect
on the data. In this article, we will see how to create, delete,
and use the INDEX in the database.
Creating an Index
Syntax
CREATE INDEX index
ON TABLE column;
SQL | Subquery
Sum():
Avg():
Min():
SQL | Triggers
Trigger is a statement that a system executes automatically
when there is any modification to the database. In a trigger, we
first specify when the trigger is to be executed and then the
action to be performed when the trigger executes. Triggers are
used to specify certain integrity constraints and referential
constraints that cannot be specified using the constraint
mechanism of SQL.
Example –
Suppose, we are adding a tuple to the ‘Donors’ table that is
some person has donated blood. So, we can design a trigger
that will automatically add the value of donated blood to the
‘Blood_record’ table.
Types of Triggers –
We can define 6 types of triggers for each table:
1. AFTER INSERT activated after data is inserted into the
table.
AS
BEGIN
-- Query --
END
GO
Example:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Comments --
AS
BEGIN
-- Query --
END
GO
Example:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO