Data Base Final
Data Base Final
Table of Contents
Basic Database Concepts ...................................................................................................................... 6
Evolution of the database .................................................................................................................. 8
Types of databases ............................................................................................................................. 9
DBMS - Architecture .................................................................................................................................. 9
3-tier Architecture .............................................................................................................................. 10
Database Life Cycle ................................................................................................................................. 11
Analysis ................................................................................................................................................ 11
Database Design ................................................................................................................................ 11
Implementation ................................................................................................................................... 11
Operation.............................................................................................................................................. 12
Maintenance ........................................................................................................................................ 12
Three schema Architecture..................................................................................................................... 12
Objectives of Three Schema Architecture .................................................................................. 13
1. Internal Level .............................................................................................................................. 14
2. Conceptual Level ....................................................................................................................... 15
3. External Level ............................................................................................................................. 15
Mapping between Views................................................................................................................... 16
What is Data Modelling?................................................................................................................... 16
Data Models in DBMS ....................................................................................................................... 17
Types of Data Models in DBMS ...................................................................................................... 17
Entity Relationship Diagram (ERD) .......................................................................................................... 18
Component of ER Diagram .............................................................................................................. 19
1. Entity: ............................................................................................................................................ 19
2. Attribute ....................................................................................................................................... 20
3. Relationship ................................................................................................................................ 23
Participation Constraints ............................................................................................................. 25
Generalization Aggregation .................................................................................................................... 25
Generalization ..................................................................................................................................... 25
Specialization ...................................................................................................................................... 26
Inheritance ........................................................................................................................................... 26
Enhanced ER Model ................................................................................................................................ 27
Relation Data Model ................................................................................................................................ 31
2
Concepts .............................................................................................................................................. 31
Constraints .......................................................................................................................................... 32
Key Constraints .............................................................................................................................. 32
Domain Constraints....................................................................................................................... 32
Referential integrity Constraints ................................................................................................ 32
Mapping ER Model to Relational Model ............................................................................................... 33
Mapping Entity .................................................................................................................................... 33
Mapping Process (Algorithm) ..................................................................................................... 33
Mapping Relationship ....................................................................................................................... 33
Mapping Process ........................................................................................................................... 34
Mapping Weak Entity Sets ............................................................................................................... 34
Mapping Process ........................................................................................................................... 34
Mapping Hierarchical Entities......................................................................................................... 35
Mapping Process ........................................................................................................................... 35
Functional Dependency........................................................................................................................... 35
Types of Functional dependency .................................................................................................. 36
1. Trivial functional dependency ................................................................................................ 36
2. Non-trivial functional dependency ........................................................................................ 37
normalization .............................................................................................................................................. 37
Data normalization rules.................................................................................................................. 38
Data normalization example ........................................................................................................... 38
1. First Normal Form – ................................................................................................................. 38
2. Second Normal Form –............................................................................................................ 40
3. Third Normal Form – ................................................................................................................ 42
4. Boyce-Codd Normal Form (BCNF) – ................................................................................... 43
Database normalization tools ......................................................................................................... 44
Relational Algebra .................................................................................................................................... 44
Relational Algebra ............................................................................................................................. 44
Select Operation (σ) .......................................................................................................................... 44
Project Operation (∏) ........................................................................................................................ 45
Union Operation (∪) ........................................................................................................................... 45
Set Difference (−)................................................................................................................................ 45
Cartesian Product (Χ) ........................................................................................................................ 46
3
the data. At this level, databases are described logically in terms of simple
data structures. Users at this level are not concerned with how these logical
data structures will be implemented at the physical level.
3. External Level (View Level): It is the level closest to users and is related to
the way the data is viewed by individual users.
Since a database can be viewed through three levels of abstraction, any change
at one level can affect plans at other levels. As databases continue to grow, there
may be frequent changes to it at times. This should not lead to redesign and re-
implementation of the database. In such a context the concept of data
independence proves beneficial.
Concept of Database
To store and manage data efficiently in the database let us understand some key
terms:
1. Database Schema: It is a design of the database. Or we can say that it is a
skeleton of the database that is used to represent the structure, types of data will
be stored in the rows and columns, constraints, relationships between the tables.
2. Data Constraints: In a database, sometimes we put some restrictions on the
table that what type of data can be stored in one or more columns of the table, it
can be done by using constraints. Constraints are defined while we are creating
a table.
3. Data dictionary or Metadata: Metadata is known as the data about the data.
Or we can say that the database schema along with different types of constraints
on the data is stored by DBMS in the dictionary is known as metadata.
4. Database instance: In a database, a database instance is used to define the
complete database environment and its components. Or we can say that it is a
8
set of memory structures and background processes that are used to access the
database files.
5. Query: In a database, a query is used to access data from the database. So
users have to write queries to retrieve or manipulate data from the database.
6. Data manipulation: In a database, we can easily manipulate data using the
three main operations that is Insertion, Deletion, and updation.
7. Data Engine: It is an underlying component that is used to create and manage
various database queries.
Advantages of Database
Let us consider some of the benefits provided by a database system and see how
a database system overcomes the above-mentioned problems: -
1. Reduces database data redundancy to a great extent
2. The database can control data inconsistency to a great extent
3. The database facilitates sharing of data.
4. Database enforce standards.
5. The database can ensure data security.
6. Integrity can be maintained through databases.
Therefore, for systems with better performance and efficiency, database systems
are preferred.
Disadvantages of Database
With the complex tasks to be performed by the database system, some things
may come up which can be termed as the disadvantages of using the database
system. These are: -
1. Security may be compromised without good controls.
2. Integrity may be compromised without good controls.
3. Extra hardware may be required
4. Performance overhead may be significant.
5. The system is likely to be complex.
growth of the internet and the need for faster speed and processing of
unstructured data. Today, cloud databases and self-driving databases are
breaking new ground when it comes to how data is collected, stored,
managed, and utilized.
Types of databases
There are many different types of databases. The best database for a
specific organization depends on how the organization intends to use the
data.
Relational databases
Relational databases became dominant in the 1980s. Items in a
relational database are organized as a set of tables with columns and
rows. Relational database technology provides the most efficient and
flexible way to access structured information.
Object-oriented databases
Information in an object-oriented database is represented in the form of
objects, as in object-oriented programming.
Distributed databases
A distributed database consists of two or more files located in different
sites. The database may be stored on multiple computers, located in the
same physical location, or scattered over different networks.
Data warehouses
A central repository for data, a data warehouse is a type of database
specifically designed for fast query and analysis.
DBMS - Architecture
The design of a DBMS depends on its architecture. It can be centralized or
decentralized or hierarchical. The architecture of a DBMS can be seen as
either single tier or multi-tier. An n-tier architecture divides the whole system
10
Database (Data) Tier − At this tier, the database resides along with its
query processing languages. We also have the relations that define the
data and their constraints at this level.
Application (Middle) Tier − At this tier reside the application server
and the programs that access the database. For a user, this application
tier presents an abstracted view of the database. End-users are
unaware of any existence of the database beyond the application. At
the other end, the database tier is not aware of any other user beyond
the application tier. Hence, the application layer sits in the middle and
acts as a mediator between the end-user and the database.
User (Presentation) Tier − End-users operate on this tier and they
know nothing about any existence of the database beyond this layer.
11
Operation
Now the database is operational.
Maintenance
DBA performs maintenance that includes backup and recovery.
1. Internal Level
o The internal level has an internal schema which describes the physical
storage structure of the database.
o The internal schema is also known as a physical schema.
o It uses the physical data model. It is used to define that how the data
will be stored in a block.
o The physical level is used to describe complex low-level data structures
in detail.
o Storage-space-allocations.
For Example: B-Trees, Hashing etc.
o Access-paths.
For Example: Specification of primary and secondary keys, indexes,
pointers and sequencing.
o Data compression and encryption techniques.
o Optimization of internal structures.
o Representation of stored fields.
15
2. Conceptual Level
3. External Level
Data modeling (data modelling) is the process of creating a data model for
the data to be stored in a database. This data model is a conceptual
representation of Data objects, the associations between different data
objects, and the rules.
17
Types of Data Models: There are mainly three different types of data
models: conceptual data models, logical data models, and physical data
models, and each one has a specific purpose. The data models are used
to represent the data and how it is stored in the database and to set the
relationship between data items.
1. Conceptual Data Model: This Data Model defines WHAT the system
contains. This model is typically created by Business stakeholders and Data
Architects. The purpose is to organize, scope and define business concepts
and rules.
2. Logical Data Model: Defines HOW the system should be implemented
regardless of the DBMS. This model is typically created by Data Architects
and Business Analysts. The purpose is to developed technical map of rules
and data structures.
3. Physical Data Model: This Data Model describes HOW the system will be
implemented using a specific DBMS system. This model is typically created
18
address can be another entity with attributes like city, street name, pin code,
etc and there will be a relationship between them.
Component of ER Diagram
1. Entity:
An entity may be any object, class, person or place. In the ER diagram, an
entity can be represented as rectangles.
20
a. Weak Entity
An entity that depends on another entity called a weak entity. The weak entity
doesn't contain any key attribute of its own. The weak entity is represented
by a double rectangle.
2. Attribute
The attribute is used to describe the property of an entity. Eclipse is used to
represent an attribute.
For example, id, age, contact number, name, etc. can be attributes of a
student.
21
a. Key Attribute
b. Composite Attribute
c. Multivalued Attribute
An attribute can have more than one value. These attributes are known as a
multivalued attribute. The double oval is used to represent multivalued
attribute.
For example, a student can have more than one phone number.
d. Derived Attribute
For example, A person's age changes over time and can be derived from
another attribute like Date of birth.
23
3. Relationship
A relationship is used to describe the relation between entities. Diamond or
rhombus is used to represent the relationship.
a. One-to-One Relationship
When only one instance of an entity is associated with the relationship, then
it is known as one to one relationship.
For example, A female can marry to one male, and a male can marry to one
female.
b. One-to-many relationship
When only one instance of the entity on the left, and more than one instance
of an entity on the right associates with the relationship then this is known
as a one-to-many relationship.
For example, Scientist can invent many inventions, but the invention is done
by the only specific scientist.
24
c. Many-to-one relationship
When more than one instance of the entity on the left, and only one instance
of an entity on the right associates with the relationship then it is known as
a many-to-one relationship.
For example, Student enrolls for only one course, but a course can have
many students.
d. Many-to-many relationship
When more than one instance of the entity on the left, and more than one
instance of an entity on the right associates with the relationship then it is
known as a many-to-many relationship.
For example, Employee can assign by many projects and project can have
many employees.
25
Participation Constraints
Total Participation − Each entity is involved in the relationship. Total
participation is represented by double lines.
Partial participation − Not all entities are involved in the relationship.
Partial participation is represented by single lines.
Generalization Aggregation
The ER Model has the power of expressing database entities in a conceptual
hierarchical manner. As the hierarchy goes up, it generalizes the view of
entities, and as we go deep in the hierarchy, it gives us the detail of every
entity included.
Going up in this structure is called generalization, where entities are
clubbed together to represent a more generalized view. For example, a
particular student named Mira can be generalized along with all the students.
The entity shall be a student, and further, the student is a person. The
reverse is called specialization where a person is a student, and that
student is Mira.
Generalization
As mentioned above, the process of generalizing entities, where the
generalized entities contain the properties of all the generalized entities, is
called generalization. In generalization, a number of entities are brought
together into one generalized entity based on their similar characteristics.
For example, pigeon, house sparrow, crow and dove can all be generalized
as Birds.
26
Specialization
Inheritance
We use all the above features of ER-Model in order to create classes of
objects in object-oriented programming. The details of entities are generally
hidden from the user; this process known as abstraction.
27
For example, the attributes of a Person class such as name, age, and gender
can be inherited by lower-level entities such as Student or Teacher.
Enhanced ER Model
Enhanced entity-relationship diagrams are advanced database diagrams
very similar to regular ER diagrams which represent requirements and
complexities of complex databases.
It is a diagrammatic technique for displaying the Sub Class and Super
Class; Specialization and Generalization; Union or Category; Aggregation
etc.
Generalization and Specialization –
These are very common relationships found in real entities. However, this
kind of relationship was added later as an enhanced extension to the
classical ER model. Specialized classes are often called subclass while
a generalized class is called a superclass, probably inspired by object-
oriented programming. A sub-class is best understood by “IS-A
28
Mapping Process
Create table for a relationship.
Add the primary keys of all participating Entities as fields of table with
their respective data types.
If relationship has any attribute, add each attribute as field of table.
Declare a primary key composing all the primary keys of participating
entities.
Declare all foreign key constraints.
Mapping Weak Entity Sets
A weak entity set is one which does not have any primary key associated
with it.
Mapping Process
Create table for weak entity set.
Add all its attributes to table as field.
Add the primary key of identifying entity set.
Declare all foreign key constraints.
35
Mapping Process
Create tables for all higher-level entities.
Create tables for lower-level entities.
Add primary keys of higher-level entities in the table of lower-level
entities.
In lower-level tables, add all other attributes of lower-level entities.
Declare primary key of higher-level table and the primary key for lower-
level table.
Declare foreign key constraints.
Functional Dependency
1. X → Y
36
The left side of FD is known as a determinant, the right side of the production
is known as a dependent.
For example:
1. Emp_Id → Emp_Name
Example:
Example:
1. ID → Name,
2. Name → DOB
3.
normalization
If this table is used for the purpose of keeping track of the price of items and the
user want to delete one of the customers, he or she will also delete the price.
Normalizing the data would mean understanding this and solving the problem by
dividing this table into two tables, one with information about each customer and
the product they bought and the second with each product and its price. Making
additions or deletions to either table would not affect the other.
Example 2 –
ID Name Courses
------------------
1 A c1, c2
2 E c3
3 M C2, c3
In the above table Course is a multi-valued attribute so it is not in 1NF.
Below Table is in 1NF as there is no multi-valued attribute
ID Name Course
------------------
1 A c1
1 A c2
2 E c3
40
3 M c2
3 M c3
Transitive dependency – If A->B and B->C are two FDs then A->C is
called transitive dependency.
Example 1 – In relation STUDENT given in Table 4,
FD set: {STUD_NO -> STUD_NAME, STUD_NO ->
STUD_STATE, STUD_STATE -> STUD_COUNTRY, STUD_NO -
> STUD_AGE}
Candidate Key: {STUD_NO}
For this relation in table 4, STUD_NO -> STUD_STATE and
STUD_STATE -> STUD_COUNTRY are true. So
STUD_COUNTRY is transitively dependent on STUD_NO. It
violates the third normal form. To convert it in third normal form,
we will decompose the relation STUDENT (STUD_NO,
STUD_NAME, STUD_PHONE, STUD_STATE,
STUD_COUNTRY_STUD_AGE) as:
STUDENT (STUD_NO, STUD_NAME, STUD_PHONE,
STUD_STATE, STUD_AGE)
STATE_COUNTRY (STATE, COUNTRY)
Example 2 – Consider relation R(A, B, C, D, E)
A -> BC,
CD -> E,
B -> D,
E -> A
43
All possible candidate keys in above relation are {A, E, CD, BC}
All attributes are on right sides of all functional dependencies are
prime.
Relational Algebra
Relational Algebra
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 as follows −
Select
Project
Union
Set different
Cartesian product
Rename
We will discuss all these operations in the following sections.
Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)
Where σ stands for selection predicate and r stands for relation. p is
prepositional logic formula which may use connectors like and, or, and not.
These terms may use relational operators like − =, ≠, ≥, < , >, ≤.
For example −
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450"(Books)
45
Output − Selects tuples from books where subject is 'database' and 'price'
is 450.
σsubject = "database" and price = "450" or year > "2010"(Books)
Output − Selects tuples from books where subject is 'database' and 'price'
is 450 or those books published after 2010.
Project Operation (∏)
It projects column(s) that satisfy a given predicate.
Notation − ∏A1, A2, An (r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
For example −
∏subject, author (Books)
Selects and projects columns named as subject and author from the relation
Books.
Union Operation (∪)
It performs binary union between two given relations and is defined as −
r ∪ s = { t | t ∈ r or t ∈ s}
Notation − r U s
Where r and s are either database relations or relation result set (temporary
relation).
For a union operation to be valid, the following conditions must hold −
r, and s must have the same number of attributes.
Attribute domains must be compatible.
Duplicate tuples are automatically eliminated.
∏ author (Books) ∪ ∏ author (Articles)
Output − Projects the names of the authors who have either written a book
or an article or both.
Set Difference (−)
The result of set difference operation is tuples, which are present in one
relation but are not in the second relation.
46
Notation − r − s
Finds all the tuples that are present in r but not in s.
∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not
articles.
Cartesian Product (Χ)
Combines information of two different relations into one.
Notation − r Χ s
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}
σauthor = 'tutorialspoint'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by
tutorialspoint.
Rename Operation (ρ)
The results of relational algebra are also relations but without any name. The
rename operation allows us to rename the output relation. 'rename' operation
is denoted with small Greek letter rho ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.
Additional operations are −
Set intersection
Assignment
Natural join
Relational Calculus
In contrast to Relational Algebra, Relational Calculus is a non-procedural
query language, that is, it tells what to do but never explains how to do it.
Relational calculus exists in two forms −
Notation − {T | Condition}
Returns all tuples T that satisfies a condition.
For example −
{ T.name | Author(T) AND T.article = 'database' }
Output − Returns tuples with 'name' from Author who has written article on
'database'.
TRC can be quantified. We can use Existential (∃) and Universal Quantifiers
(∀).
For example −
{ R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}
Output − The above query will yield the same result as the previous one.
Domain Relational Calculus (DRC)
In DRC, the filtering variable uses the domain of attributes instead of entire
tuple values (as done in TRC, mentioned above).
Notation −
{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where a1, a2 are attributes and P stands for formulae built by inner
attributes.
For example −
{< article, page, subject > | ∈ TutorialsPoint ∧ subject = 'database'}
Output − Yields Article, Page, and Subject from the relation TutorialsPoint,
where subject is database.
Just like TRC, DRC can also be written using existential and universal
quantifiers. DRC also involves relational operators.
The expression power of Tuple Relation Calculus and Domain Relation
Calculus is equivalent to Relational Algebra.
SQL
48
o SQL stands for Structured Query Language. It is used for storing and
managing data in relational database management system (RDMS).
o It is a standard language for Relational Database System. It enables a
user to create, read, update and delete relational databases and tables.
o All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server
use SQL as their standard database language.
o SQL allows users to query the database in a number of ways, using
English-like statements.
Rules:
SQL process:
o When an SQL command is executing for any RDBMS, then the system
figure out the best way to carry out the request and the SQL engine
determines that how to interpret the task.
o In the process, various components are included. These components
can be optimization Engine, Query engine, Query dispatcher, classic,
etc.
o All the non-SQL queries are handled by the classic query engine, but
SQL query engine won't handle logical files.
49
Characteristics of SQL
Advantages of SQL
High speed
Using the SQL queries, the user can quickly and efficiently retrieve a large
amount of records from a database.
50
No coding needed
In the standard SQL, it is very easy to manage the database system. It doesn't
require a substantial amount of code to manage the database system.
Long established are used by the SQL databases that are being used by ISO
and ANSI.
Portability
SQL can be used in laptop, PCs, server and even some mobile phones.
Interactive language
Using the SQL language, the users can make different views of the database
structure.
SQL Datatype
o SQL Datatype is used to define the values that a column can contain.
o Every column is required to have a name and data type in the database
table.
51
Datatype of SQL:
1. Binary Datatypes
There are Three types of binary Datatypes which are given below:
Data Description
Type
Data Description
type
Datatype Description
timestamp It stores the year, month, day, hour, minute, and the second value.
SQL Commands
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
54
o CREATE
o ALTER
o DROP
o TRUNCATE
Syntax:
Example:
b. DROP: It is used to delete both the structure and record stored in the
table.
Syntax
Example
c. ALTER: It is used to alter the structure of the database. This change could
be either to modify the characteristics of an existing attribute or probably to
add a new attribute.
Syntax:
EXAMPLE
d. TRUNCATE: It is used to delete all the rows from the table and free the
space containing the table.
Syntax:
Example:
56
o INSERT
o UPDATE
o DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into
the row of a table.
Syntax:
Or
For example:
Syntax:
57
For example:
1. UPDATE students
2. SET User_Name = 'Sonoo'
3. WHERE Student_Id = '3'
Syntax:
For example:
DCL commands are used to grant and take back authority from any database
user.
o Grant
o Revoke
Example
Example
58
TCL commands can only use with DML commands like INSERT, DELETE and
UPDATE only.
o COMMIT
o ROLLBACK
o SAVEPOINT
Syntax:
1. COMMIT;
Example:
Syntax:
1. ROLLBACK;
Example:
59
Syntax:
1. SAVEPOINT SAVEPOINT_NAME;
5. Data Query Language
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 Operator
Let's assume 'variable a' and 'variable b'. Here, 'a' contains 20 and 'b' contains
10.
- It is used to subtract the right-hand operand from the a-b will give
left-hand operand. 10
/ It is used to divide the left-hand operand by the right- a/b will give
hand operand. 2
% It is used to divide the left-hand operand by the right- a%b will give
hand operand and returns reminder. 0
61
Let's assume 'variable a' and 'variable b'. Here, 'a' contains 20 and 'b' contains
10.
= It checks if two operands values are equal or not, if the (a=b) is not
values are queal then condition becomes true. true
> It checks if the left operand value is greater than right (a>b) is not
operand value, if yes then condition becomes true. true
< It checks if the left operand value is less than right (a<b) is true
operand value, if yes then condition becomes true.
<= It checks if the left operand value is less than or equal (a<=b) is
to the right operand value, if yes then condition true
becomes true.
62
!< It checks if the left operand value is not less than the (a!=b) is not
right operand value, if yes then condition becomes true
true.
!> It checks if the left operand value is not greater than (a!>b) is
the right operand value, if yes then condition true
becomes true.
Operator Description
BETWEEN It is used to search for values that are within a set of values.
SQL Table
Operation on Table
1. Create table
2. Drop table
3. Delete table
64
4. Rename table
SQL create table is used to create a table in the database. To define the table,
you should define the name of the table and also define its columns and
column's data type.
Syntax
Example
If you create the table successfully, you can verify the table by looking at the
message by the SQL server. Else you can use DESC command as follows:
Now you have an EMPLOYEE table in the database, and you can use the
stored information related to the employees.
Drop table
A SQL drop table is used to delete a table definition and all the data from a
table. When this command is executed, all the information available in the
table is lost forever, so you have to very careful while using this command.
Syntax
Firstly, you need to verify the EMPLOYEE table using the following
command:
This table shows that EMPLOYEE table is available in the database, so we can
drop it as follows:
Now, we can check whether the table exists or not using the following
command:
In SQL, DELETE statement is used to delete rows from a table. We can use
WHERE condition to delete a specific row from a table. If you want to delete
all the records from the table, then you don't need to use the WHERE clause.
Syntax
Example
If you don't specify the WHERE condition, it will remove all the rows from the
table.
68
Views in SQL
o Views in SQL are considered as a virtual table. A view also contains rows
and columns.
o To create the view, we can select the fields from one or more tables
present in the database.
o A view can either have specific rows based on certain condition or all
the rows of a table.
Sample table:
Student_Detail
1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram
Student_Marks
1 Stephan 97 19
69
2 Kathrin 86 21
3 David 74 18
4 Alina 90 20
5 John 96 18
1. Creating view
A view can be created using the CREATE VIEW statement. We can create a
view from a single table or multiple tables.
Query:
Just like table query, we can query the view to view the data.
Output:
70
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
View from multiple tables can be created by simply include multiple tables
in the SELECT statement.
In the given example, a view is created named MarksView from two tables
Student_Detail and Student_Marks.
Query:
Stephan Delhi 97
Kathrin Noida 86
71
David Ghaziabad 74
Alina Gurugram 90
4. Deleting View
Syntax
Example:
SQL Index
o Indexes are special lookup tables. It is used to retrieve data from the
database very fast.
o An Index is used to speed up select queries and where clauses. But it
shows down the data input with insert and update statements. Indexes
can be created or dropped without affecting the data.
o An index in a database is just like an index in the back of a book.
o For example: When you reference all pages in a book that discusses a
certain topic, you first have to refer to the index, which alphabetically
lists all the topics and then referred to one or more specific page
numbers.
Syntax
72
Example
Syntax
Example
Syntax
Example
A Subquery is a query within another SQL query and embedded within the
WHERE clause.
Important Rule:
73
SQL subqueries are most frequently used with the Select statement.
Syntax
1. SELECT column_name
2. FROM table_name
3. WHERE column_name expression operator
4. ( SELECT column_name from table_name WHERE ... );
Example
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
1. SELECT *
2. FROM EMPLOYEE
3. WHERE ID IN (SELECT ID
4. FROM EMPLOYEE
5. WHERE SALARY > 4500);
4 Alina 29 UK 6500.00
o In the subquery, the selected data can be modified with any of the
character, date functions.
Syntax:
Example
Now use the following syntax to copy the complete EMPLOYEE table into the
EMPLOYEE_BKP table.
The subquery of SQL can be used in conjunction with the Update statement.
When a subquery is used with the Update statement, then either single or
multiple columns in a table can be updated.
Syntax
1. UPDATE table
2. SET column_name = new_value
3. WHERE VALUE OPERATOR
4. (SELECT COLUMN_NAME
5. FROM TABLE_NAME
6. WHERE condition);
Example
76
1. UPDATE EMPLOYEE
2. SET SALARY = SALARY * 0.25
3. WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
4. WHERE AGE >= 29);
This would impact three rows, and finally, the EMPLOYEE table would have
the following records.
1 John 20 US 2000.00
4 Alina 29 UK 1625.00
The subquery of SQL can be used in conjunction with the Delete statement
just like any other statements mentioned above.
Syntax
77
Example
This would impact three rows, and finally, the EMPLOYEE table would have
the following records.
1 John 20 US 2000.00
1. COUNT FUNCTION
o COUNT function is used to Count the number of rows in a database
table. It can work on both numeric and non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all the
rows in a specified table. COUNT(*) considers duplicate and Null.
Syntax
1. COUNT(*)
2. or
3. COUNT( [ALL|DISTINCT] expression )
Sample table:
PRODUCT_MAST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Example: COUNT()
1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
Output:
10
1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
80
3. WHERE RATE>=20;
Output:
Output:
Output:
Com1 5
Com2 3
Com3 2
Output:
Com1 5
Com2 3
81
2. SUM Function
Sum function is used to calculate the sum of all selected columns. It works
on numeric fields only.
Syntax
1. SUM()
2. or
3. SUM( [ALL|DISTINCT] expression )
Example: SUM()
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST;
Output:
670
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST
3. WHERE QTY>3;
Output:
320
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST
3. WHERE QTY>3
4. GROUP BY COMPANY;
Output:
82
Com1 150
Com2 170
Output:
Com1 335
Com3 170
3. AVG function
The AVG function is used to calculate the average value of the numeric type.
AVG function returns the average of all non-Null values.
Syntax
1. AVG()
2. or
3. AVG( [ALL|DISTINCT] expression )
Example:
1. SELECT AVG(COST)
2. FROM PRODUCT_MAST;
Output:
67.00
4. MAX Function
MAX function is used to find the maximum value of a certain column. This
function determines the largest value of all selected values of a column.
83
Syntax
1. MAX()
2. or
3. MAX( [ALL|DISTINCT] expression )
Example:
1. SELECT MAX(RATE)
2. FROM PRODUCT_MAST;
30
5. MIN Function
MIN function is used to find the minimum value of a certain column. This
function determines the smallest value of all selected values of a column.
Syntax
1. MIN()
2. or
3. MIN( [ALL|DISTINCT] expression )
Example:
1. SELECT MIN(RATE)
2. FROM PRODUCT_MAST;
Output:
10
SQL JOIN
As the name shows, JOIN means to combine something. In case of SQL, JOIN
means "to combine two or more tables".
84
In SQL, JOIN clause is used to combine the records from two or more tables
in a database.
Sample Table
EMPLOYEE
PROJECT
101 1 Testing
102 2 Development
103 3 Designing
104 4 Development
1. INNER JOIN
In SQL, INNER JOIN selects records that have matching values in both tables
as long as the condition is satisfied. It returns the combination of all rows
from both the tables where the condition satisfies.
Syntax
Query
Output
EMP_NAME DEPARTMENT
Angelina Testing
86
Robert Development
Christian Designing
Kristen Development
2. LEFT JOIN
The SQL left join returns all the values from left table and the matching values
from the right table. If there is no matching join value, it will return NULL.
Syntax
Query
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
87
Christian Designing
Kristen Development
Russell NULL
Marry NULL
3. RIGHT JOIN
In SQL, RIGHT JOIN returns all the values from the values from the rows of
right table and the matched values from the left table. If there is no matching
in both tables, it will return NULL.
Syntax
Query
Output
EMP_NAME DEPARTMENT
Angelina Testing
88
Robert Development
Christian Designing
Kristen Development
4. FULL JOIN
In SQL, FULL JOIN is the result of a combination of both left and right outer
join. Join tables have all the records from both tables. It puts NULL on the
place of matches not found.
Syntax
Query
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
89
Christian Designing
Kristen Development
Russell NULL
Marry NULL
Conflicts in Schedules
In a schedule comprising of multiple transactions, a conflict occurs when
two active transactions perform non-compatible operations. Two operations
are said to be in conflict, when all of the following three conditions exists
simultaneously −
The two operations are parts of different transactions.
Both the operations access the same data item.
At least one of the operations is a write_item() operation, i.e. it tries to
modify the data item.
Serializability
A serializable schedule of ‘n’ transactions is a parallel schedule which is
equivalent to a serial schedule comprising of the same ‘n’ transactions. A
serializable schedule contains the correctness of serial schedule while
ascertaining better CPU utilization of parallel schedule.
Equivalence of Schedules
Equivalence of two schedules can be of the following types −
Result equivalence − Two schedules producing identical results are
said to be result equivalent.
View equivalence − Two schedules that perform similar action in a
similar manner are said to be view equivalent.
Conflict equivalence − Two schedules are said to be conflict
equivalent if both contain the same set of transactions and has the
same order of conflicting pairs of operations.
Concurrency
93
concurrency control
Concurrency control concept comes under the Transaction in database
management system (DBMS). It is a procedure in DBMS which helps us for
the management of two simultaneous processes to execute without conflicts
between each other, these conflicts occur in multi user systems.
Concurrency can simply be said to be executing multiple transactions at a
time. It is required to increase time efficiency. If many transactions try to
access the same data, then inconsistency arises. Concurrency control
required to maintain consistency data.
For example, if we take ATM machines and do not use concurrency, multiple
persons cannot draw money at a time in different places. This is where we
need concurrency.
Advantages
The advantages of concurrency control are as follows −
Waiting time will be decreased.
Response time will decrease.
Resource utilization will increase.
System performance & Efficiency is increased.
Control concurrency
The simultaneous execution of transactions over shared databases can
create several data integrity and consistency problems.
For example, if too many people are logging in the ATM machines, serial
updates and synchronization in the bank servers should happen whenever
the transaction is done, if not it gives wrong information and wrong data in
the database.
94
Optimistic
It is based on the assumption that conflict is rare and it is more efficient to
allow transactions to proceed without imposing delays to ensure
serializability.
memory buffers called the DBMS cache is kept under the control of
DBMS for holding these buffers. A directory is used to keep track of
which database items are in the buffer. A dirty bit is associated with
each buffer, which is 0 if the buffer is not modified else 1 if modified.
Shadow paging – It provides atomicity and durability. A directory with
n entries is constructed, where the ith entry points to the ith database
page on the link. When a transaction began executing the current
directory is copied into a shadow directory. When a page is to be
modified, a shadow page is allocated in which changes are made and
when it is ready to become durable, all pages that refer to the original
are updated to refer new replacement page.
Backward Recovery – The term “Rollback ” and “UNDO” can also
refer to backward recovery. When a backup of the data is not available
and previous modifications need to be undone, this technique can be
helpful. With the backward recovery method, unused modifications are
removed and the database is returned to its prior condition. All
adjustments made during the previous traction are reversed during the
backward recovery. In another word, it reprocesses valid transactions
and undoes the erroneous database updates.
Forward Recovery – “Roll forward “and “REDO” refers to forwarding
recovery. When a database needs to be updated with all changes
verified, this forward recovery technique is helpful.
Some failed transactions in this database are applied to the database
to roll those modifications forward. In another word, the database is
restored using preserved data and valid transactions counted by their
past saves.
Some of the backup techniques are as follows :
Full database backup – In this full database including data and
database, Meta information needed to restore the whole database,
including full-text catalogs are backed up in a predefined time series.
Differential backup – It stores only the data changes that have
occurred since the last full database backup. When some data has
changed many times since last full database backup, a differential
backup stores the most recent version of the changed data. For this
first, we need to restore a full database backup.
Transaction log backup – In this, all events that have occurred in the
database, like a record of every single statement executed is backed
up. It is the backup of transaction log entries and contains all
transactions that had happened to the database. Through this, the
98
Encryption in database:
While the study of encryption is about trying to explain encryption logic
systematically through generalizations and propositions, encryption technology is,
based on encryption theories, a product of necessity aimed at creating the most
cost-efficient, profitable outcomes in line with economic principles. It is the
outcome of a process of transforming, refining, and amalgamating theories for
practical application.
“If you purchase this encryption appliance, all your security concerns
will be resolved.”
But this is undoubtedly the wrong attitude: a specialized encryption vendor
will always the first talk about the need for security governance and
improving the security consciousness across an organization.
Kerberos
Kerberos provides a centralized authentication server whose function is
to authenticate users to servers and servers to users. In Kerberos
Authentication server and database is used for client authentication.
Kerberos runs as a third-party trusted server known as the Key
Distribution Center (KDC). Each user and service on the network is a
principal.
The main components of Kerberos are:
Database:
The Authentication Server verifies the access rights of users in the
database.
Kerberos Overview:
103
Step-1:
User login and request services on the host. Thus user requests for
ticket-granting service.
Step-2:
Authentication Server verifies user’s access right using database and
then gives ticket-granting-ticket and session key. Results are encrypted
using the Password of the user.
Step-3:
The decryption of the message is done using the password then send
the ticket to Ticket Granting Server. The Ticket contains authenticators
like user names and network addresses.
Step-4:
Ticket Granting Server decrypts the ticket sent by User and
authenticator verifies the request then creates the ticket for requesting
services from the Server.
104
Step-5:
The user sends the Ticket and Authenticator to the Server.
Step-6:
The server verifies the Ticket and authenticators then generate access
to the service. After this User can access the services.
Kerberos Limitations
Is Kerberos Infallible?
Despite this, Kerberos remains the best access security protocol available
today. The protocol is flexible enough to employ stronger encryption
algorithms to combat new threats, and if users employ good password-
choice guidelines, you shouldn’t have a problem!
Data integrity refers to the overall accuracy, completeness, and reliability of data.
It can be specified by the lack of variation between two instances or consecutive
updates of a record, indicating that your information is error-free. It also
corresponds to the security and integrity controls and methods of regulatory
compliance.
People often confuse data integrity with data security or data quality. However,
these three are related but different concepts.
Data security concerns measures taken to protect enterprise data from misuse. It
includes using methods and techniques that make your data inaccessible to
undesired parties or making selected data accessible to the desired parties. Data
security breaches can threaten the existence of an organization. On the other hand,
data integrity deals with the accuracy and completeness of data present in the
database.
The end goal of data security is to protect your data from external or internal
breaches. Thus, it is one of the many aspects of data integrity, but it isn’t extensive
enough to account for the numerous procedures essential for keeping your
information unaffected over time. Similarly, data quality is another facet of data
integrity, albeit a major one.
106
Data quality ensures that the data stored in your database is compliant with the
organization’s standards and requirements. In other words, it maintains integrity in
a database. In doing so, it applies a set of rules to a specific or complete dataset and
stores it in the target database. Moreover, data quality is data accuracy, which
refers explicitly to the correctness of stored values. Data integrity vs. data accuracy
can be understood by seeing data integrity as an umbrella term, whereby data
accuracy is one of the many categories.
Data integrity in a database covers all aspects of data quality and advances further
by executing several rules and procedures that oversee how information is entered,
deposited, transmitted and more.
Consider this example of data integrity. While the Salary of all employees is an
integer, one employee has a salary in alphanumeric characters. Since the Salary
table only accepts integers (INT), the value 697abc will not be accepted by the
database. This one-way data is protected by the database using domain-level data
integrity.
Let’s look at the two methods that help ensure data integrity.
Data integrity is applied in all databases can be categorized into two main types:
Physical Integrity
Protecting data against external factors, such as natural calamities, power outages,
or hackers, falls under the domain of physical integrity. Moreover, human faults,
storage attrition, and several other problems can make data operators unable to
obtain information from a database.
Logical Integrity
It concerns the rationality of data present within the relational database. Logical
integrity constraints can be categorized into four types:
Entity Integrity
107
It depends on the making of primary keys or exclusive values that classify data
items. The purpose is to ensure that data is not recorded multiple times (i.e., each
data item is unique), and the table has no null fields.
Referential Integrity
It denotes a series of procedures that ensure proper and consistent data storage and
usage. Referential integrity ensures that only the required alterations, additions, or
removals happen via rules implanted into the database’s structure about how
foreign keys are used.
These rules might include conditions that remove duplicate data records, warrant
that data is precise, and prohibit unsuitable recording data.
Domain Integrity
It’s an assortment of procedures that ensures the precision of every data item is
maintained in a domain. Here, a domain is defined as a set of suitable values that a
column is permitted to enclose.
Domain integrity encompasses rules and other processes restricting the format,
type, and volume of data recorded in a database. It ensures that every column in a
relational database is in a defined domain.
User-Defined Integrity
It comprises the rules defined by the operator to fulfill their specific requirements.
Entity, referential, and domain integrity are not enough to refine and secure data.
Time in time again, particular business rules must be considered and integrated
into data integrity processes to meet enterprise standards.
Types:
1. Homogeneous Database:
In a homogeneous database, all different sites store database identically. The
operating system, database management system, and the data structures used –
all are the same at all sites. Hence, they’re easy to manage.
2. Heterogeneous Database:
In a heterogeneous distributed database, different sites can use different schema
and software that can lead to problems in query processing and transactions.
Also, a particular site might be completely unaware of the other sites. Different
computers may use a different operating system, different database application.
They may even use different data models for the database. Hence, translations
are required for different sites to communicate.
Distributed Data Storage :
109
There are 2 ways in which data can be stored on different sites. These are:
1. Replication –
In this approach, the entire relationship is stored redundantly at 2 or more sites. If
the entire database is available at all sites, it is a fully redundant database. Hence,
in replication, systems maintain copies of data.
2. Fragmentation –
In this approach, the relations are fragmented (i.e., they’re divided into smaller
parts) and each of the fragments is stored in different sites where they’re
required. It must be made sure that the fragments are such that they can be used
to reconstruct the original relation (i.e, there isn’t any loss of data).
Fragmentation is advantageous as it doesn’t create copies of data, consistency is
not a problem.
The relation is fragmented into groups of tuples so that each tuple is assigned to
at least one fragment.
Vertical fragmentation – Splitting by columns –
The schema of the relation is divided into smaller schemas. Each fragment must
contain a common candidate key so as to ensure a lossless join.
In certain cases, an approach that is hybrid of fragmentation and replication is
used.
That must be followed for any network connection. If the one client wants to send
an email over the network. It requests the server, the SMTP (the protocol that is
SMPT is standing for simple mail transfer protocol that used to transfer a mail
over the network. SMTP is a set of commands or commands that check
authentication and the transfer of email. When configuring the settings for your
e-mail program, you usually need to set the SMTP server to your local Internet
Service Provider’s SMTP settings. After all process, the server will transfer e-mail
to the desired client.
Another example of a client-server model is online gaming.in online gaming.
Structure of Client-server Database Architecture in DBMS
By using this architecture structure this software is divided into three different
tiers: Presentation tier
Logic tier
Data-tier