Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views67 pages

Dbms Complete Notes - 152

A Database Management System (DBMS) is software that manages databases, allowing users to efficiently retrieve, insert, and delete data while ensuring data integrity and security. It supports various operations through different types of database languages, including Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL). DBMS architectures can be single-tier or multi-tier, with advantages such as reduced redundancy and easy maintenance, but also face disadvantages like high costs and complexity.

Uploaded by

Bharu Bhargavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views67 pages

Dbms Complete Notes - 152

A Database Management System (DBMS) is software that manages databases, allowing users to efficiently retrieve, insert, and delete data while ensuring data integrity and security. It supports various operations through different types of database languages, including Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL). DBMS architectures can be single-tier or multi-tier, with advantages such as reduced redundancy and easy maintenance, but also face disadvantages like high costs and complexity.

Uploaded by

Bharu Bhargavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

DBMS

1.Database System
What is Database?
The database is a collection of inter-related data which is used to retrieve, insert and
delete the data efficiently. It is also used to organize the data in the form of a table,
schema, views, and reports, etc.

For example: The college Database organizes the data about the admin, staff, students
and faculty etc.

Using the database, you can easily retrieve, insert, and delete the information.

Database Management System


o Database management system is a software which is used to manage the database. For
example: MySQL, Oracle, etc are a very popular commercial database which is used
in different applications.
o DBMS provides an interface to perform various operations like database creation,
storing data in it, updating data, creating a table in the database and a lot more.
o It provides protection and security to the database. In the case of multiple users, it also
maintains data consistency.

DBMS allows users the following tasks:

o Data Definition: It is used for creation, modification, and removal of definition


that defines the organization of data in the database.
o Data Updation: It is used for the insertion, modification, and deletion of the
actual data in the database.
o Data Retrieval: It is used to retrieve the data from the database which can be
used by applications for various purposes.
o User Administration: It is used for registering and monitoring users, maintain
data integrity, enforcing data security, dealing with concurrency control,
monitoring performance and recovering information corrupted by unexpected
failure.
Characteristics of DBMS

o It uses a digital repository established on a server to store and manage the


information.
o It can provide a clear and logical view of the process that manipulates data.
o DBMS contains automatic backup and recovery procedures.
o It contains ACID properties which maintain data in a healthy state in case of
failure.
o It can reduce the complex relationship between data.
o It is used to support manipulation and processing of data.
o It is used to provide security of data.
o It can view the database from different viewpoints according to the
requirements of the user.

Advantages of DBMS

o Controls database redundancy: It can control data redundancy because it


stores all the data in one single database file and that recorded data is placed in
the database.
o Data sharing: In DBMS, the authorized users of an organization can share the
data among multiple users.
o Easily Maintenance: It can be easily maintainable due to the centralized nature
of the database system.
o Reduce time: It reduces development time and maintenance need.
o Backup: It provides backup and recovery subsystems which create automatic
backup of data from hardware and software failures and restores the data if
required.
o multiple user interface: It provides different types of user interfaces like
graphical user interfaces, application program interfaces

Disadvantages of DBMS

o Cost of Hardware and Software: It requires a high speed of data processor and
large memory size to run DBMS software.
o Size: It occupies a large space of disks and large memory to run them
efficiently.
o Complexity: Database system creates additional complexity and requirements.
o Higher impact of failure: Failure is highly impacted the database because in
most of the organization, all the data stored in a single database and if the
database is damaged due to electric failure or database corruption then the data
may be lost forever.

Database Language(SQL Queries):


o A DBMS has appropriate languages and interfaces to express database queries
and updates.
o Database languages can be used to read, store and update the data in the
database.

Types of Database Language

1. Data Definition Language


o DDL stands for Data Definition Language. It is used to define database
structure or pattern.
o It is used to create schema, tables, indexes, constraints, etc. in the database.
o Using the DDL statements, you can create the skeleton of the database.
o Data definition language is used to store the information of metadata like the
number of tables and schemas, their names, indexes, columns in each table,
constraints, etc.

Here are some tasks that come under DDL:

o Create: It is used to create objects in the database.


Syntax: CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);

Example:

1.CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(100), DOB DATE);

Alter: It is used to alter the structure of the database.

Syntax: To add a new column in the table

ALTER TABLE table_name ADD column_name COLUMN-definition;

1. ALTER TABLE table_name MODIFY(column_definitions....);

EXAMPLE: ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));

ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));

Drop: It is used to delete objects from the database.

Syntax: DROP TABLE table_name;

Example: 1.DROP TABLE EMPLOYEE;

Truncate: It is used to remove all records from a table.

Syntax: TRUNCATE TABLE table_name;

Example: TRUNCATE TABLE EMPLOYEE;

Rename: It is used to rename an object.

Comment: It is used to comment on the data dictionary.

These commands are used to update the database schema that's why they come under
Data definition language.

2. Data Manipulation Language

DML stands for Data Manipulation Language. It is used for accessing and
manipulating data in a database. It handles user requests.

Here are some tasks that come under DML:

o Select: It is used to retrieve data from a database.


o Insert: It is used to insert data into a table.

Syntax: INSERT INTO TABLE_NAME

VALUES (value1, value2, value3, .... valueN);

For example: INSERT INTO javatpoint (Author, Subject) VALUES ("Sonoo", "DBMS");

Update: It is used to update existing data within a table.

Syntax:
UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE CONDITI
ON]

For example: UPDATE students

SET User_Name = 'Sonoo'

WHERE Student_Id = '3'

o Delete: It is used to delete all records from a table.

Syntax: DELETE FROM table_name [WHERE condition];

For example: DELETE FROM javatpoint

WHERE Author="Sonoo";

Merge: It performs UPSERT operation, i.e., insert or update operations.

o Call: It is used to call a structured query language or a Java subprogram.


o Explain Plan: It has the parameter of explaining data.
o Lock Table: It controls concurrency.

3. Data Control Language


o DCL stands for Data Control Language. It is used to retrieve the stored or
saved data.
o The DCL execution is transactional. It also has rollback parameters.

(But in Oracle database, the execution of data control language does not have
the feature of rolling back.)

Here are some tasks that come under DCL:


o Grant: It is used to give user access privileges to a database.

Example:
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;

Revoke: It is used to take back permissions from the user.

Example: REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;

There are the following operations which have the authorization of Revoke:

CONNECT, INSERT, USAGE, EXECUTE, DELETE, UPDATE and SELECT.

4. Transaction Control Language

TCL is used to run the changes made by the DML statement. TCL can be grouped into
a logical transaction.

Here are some tasks that come under TCL:

1.Commit: It is used to save the transaction on the database.

Syntax: commit;

Example: DELETE FROM CUSTOMERS


WHERE AGE = 25;
COMMIT;

2.Rollback: It is used to restore the database to original since the last Commit.

Syntax: rollback;

Example: DELETE FROM CUSTOMERS


WHERE AGE = 25;
ROLLBACK;
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.
Types of Integrity Constraint

1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an
attribute.
o The data type of domain includes string, character, integer, time, date, currency, etc.
The value of the attribute must be available in the corresponding domain.

Example:

2. Entity integrity constraints


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:
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 JOIN

As the name shows, JOIN means to combine something. In case of SQL, JOIN means "to
combine two or more tables".

In SQL, JOIN clause is used to combine the records from two or more tables in a database.

Types of SQL JOIN

1. INNER JOIN

2. LEFT JOIN

3. RIGHT JOIN

4. FULL JOIN

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: SELECT table1.column1, table1.column2, table2.column1,....

FROM table1

INNER JOIN table2


ON table1.matching_column = table2.matching_column;

Query

SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT

FROM EMPLOYEE
INNER JOIN PROJECT

ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output:
EMP_NAME DEPARTMENT

Angelina Testing

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

SELECT table1.column1, table1.column2, table2.column1,....

FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;

Query

SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT


FROM EMPLOYEE
LEFT JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
OUTPUT:
EMP_NAME DEPARTMENT

Angelina Testing

Robert Development

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

SELECT table1.column1, table1.column2, table2.column1,....


FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;

Query

SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT


FROM EMPLOYEE
RIGHT JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;

Output

EMP_NAME DEPARTMENT

Angelina Testing

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

SELECT table1.column1, table1.column2, table2.column1,....


FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;

Query

SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT


FROM EMPLOYEE
FULL JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;

Output

EMP_NAME DEPARTMENT

Angelina Testing

Robert Development

Christian Designing

Kristen Development

Russell NULL

Marry NULL

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 .

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.

Syntax:

CREATE VIEW view_name AS


SELECT column1, column2.....
FROM table_name
WHERE condition;
2. Creating View from a single table

In this example, we create a View named DetailsView from the table Student_Detail.

Query:

CREATE VIEW DetailsView AS


SELECT NAME, ADDRESS
FROM Student_Details
WHERE STU_ID < 4;

Just like table query, we can query the view to view the data.

SELECT * FROM DetailsView;

Output:

NAME ADDRESS

Stephan Delhi

Kathrin Noida

David Ghaziabad

3. Creating View from multiple tables

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

CREATE VIEW MarksView AS


SELECT Student_Detail.NAME, Student_Detail.ADDRESS, Student_Marks.MARKS
FROM Student_Detail, Student_Mark
WHERE Student_Detail.NAME = Student_Marks.NAME;

To display data of View MarksView:


SELECT * FROM MarksView;

NAME ADDRESS MARKS

Stephan Delhi 97

Kathrin Noida 86

David Ghaziabad 74

Alina Gurugram 90

4. Deleting View

A view can be deleted using the Drop View statement.

Syntax

DROP VIEW view_name;

Example:

If we want to delete the View MarksView, we can do this as:

DROP VIEW MarksView;

Trigger: A trigger is a stored procedure in database which automatically invokes


whenever a special event in the database occurs. For example, a trigger can be invoked
when a row is inserted into a specified table or when certain table columns are being
updated.
Syntax:
create trigger [trigger_name]
[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]
Explanation of syntax
1. create trigger [trigger_name]: Creates or replaces an existing trigger with the
trigger_name.
2. [before | after]: This specifies when the trigger will be executed.
3. {insert | update | delete}: This specifies the DML operation.
4. on [table_name]: This specifies the name of the table associated with the trigger.
5. [for each row]: This specifies a row-level trigger, i.e., the trigger will be
executed for each row being affected.
6. [trigger_body]: This provides the operation to be performed as trigger is fired.

DBMS Architecture
o The DBMS design depends upon its architecture. The basic client/server architecture
is used to deal with a large number of PCs, web servers, database servers and other
components that are connected with networks.
o The client/server architecture consists of many PCs and a workstation which are
connected via the network.
o DBMS architecture depends upon how users are connected to the database to get their
request done.

Types of DBMS Architecture

Database architecture can be seen as a single tier or multi-tier. But logically, database
architecture is of two types like: 2-tier architecture and 3-tier architecture.

1-Tier Architecture
o In this architecture, the database is directly available to the user. It means the
user can directly sit on the DBMS and uses it.
o Any changes done here will directly be done on the database itself. It doesn't
provide a handy tool for end users.
o The 1-Tier architecture is used for development of the local application, where
programmers can directly communicate with the database for the quick
response.

2-Tier Architecture
o The 2-Tier architecture is same as basic client-server. In the two-tier architecture,
applications on the client end can directly communicate with the database at the server
side. For this interaction, API's like: ODBC, JDBC are used.
o The user interfaces and application programs are run on the client-side.
o The server side is responsible to provide the functionalities like: query processing and
transaction management.
o To communicate with the DBMS, client-side application establishes a connection with
the server side.

2-tier Architecture

3-Tier Architecture
o The 3-Tier architecture contains another layer between the client and server. In this
architecture, client can't directly communicate with the server.
o The application on the client-end interacts with an application server which further
communicates with the database system.
o End user has no idea about the existence of the database beyond the application server.
The database also has no idea about any other user beyond the application.
o The 3-Tier architecture is used in case of large web application.

Relational Model concept

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.

Domain: It contains a set of atomic values that an attribute can take.

Attribute: It contains the name of a column in a particular table. Each attribute Ai must have
a domain, dom(Ai)

Relational instance: In the relational database system, the relational instance is represented
by a finite set of tuples. Relation instances do not have duplicate tuples.

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.

Example: STUDENT Relation

NAME ROLL_NO PHONE_NO ADDRESS AGE

Ram 14795 7305758992 Noida 24

Shyam 12839 9026288936 Delhi 35

Laxman 33289 8583287182 Gurugram 20

Mahesh 27857 7086819134 Ghaziabad 27

Ganesh 17282 9028 9i3988 Delhi 40

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>

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.

Types of Relational operation

1. Select Operation:
o The select operation selects tuples that satisfy a given predicate.
o It is denoted by sigma (σ).
o Notation: σ p(r)

Where:

σ isusedforselectionprediction
r isusedforrelation
p is used as a propositional logic formula which may use connectors like: AND OR
and NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.

For example: LOAN Relation

BRANCH_NAME LOAN_NO AMOUNT

Downtown L-17 1000

Redwood L-23 2000

Perryride L-15 1500

Downtown L-14 1500

Mianus L-13 500

Roundhill L-11 900

Perryride L-16 1300

Input:

1. σ BRANCH_NAME="perryride" (LOAN)
Output:

BRANCH_NAME LOAN_NO AMOUNT

Perryride L-15 1500

Perryride L-16 1300


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 ∏.

1. Notation: ∏ A1, A2, An (r)

Where

A1, A2, A3 is used as an attribute name of relation r.

Example: CUSTOMER RELATION

NAME STREET CITY

Jones Main Harrison

Smith North Rye

Hays Main Harrison

Curry North Rye

Johnson Alma Brooklyn

Brooks Senator Brooklyn

Input:

1. ∏ NAME, CITY (CUSTOMER)

Output:

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:

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

o R and S must have the attribute of the same number.


o Duplicate tuples are eliminated automatically.

Example:

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

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

Input: ∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR Output:

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 (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)

Output:

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:

1. ∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)

Output:

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

EMP_ID EMP_NAME EMP_DEPT

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:

EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME

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)

SQL INTRODUCTION:
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 SQL allows users to query the database in a number of ways, using English-like
statements.

Triggers:

Triggers are stored programs, which are automatically executed or fired when some events occur. A
trigger in SQL is a procedural code that is automatically executed in response to certain events on a
specified table. It is important to understand how these small codes make such a huge difference in
database performance.

Syntax and Example

Lets now look at the syntax of a trigger.

1 Create Trigger Trigger_Name


2 (Before | After) [ Insert | Update | Delete]
3 on [Table_Name]
4 [ for each row | for each column ]
[ trigger_body ]
5
Where,

• Create Trigger
These two keywords are used to specify that a trigger block is going to be declared.
• Trigger_Name
It specifies the name of the trigger. Trigger name has to be unique and shouldn’t repeat.
• ( Before | After )
This specifies when the trigger will be executed. It tells us the time at which the trigger is
initiated, i.e, either before the ongoing event or after.

• Before Triggers are used to update or validate record values before they’re saved to the
database.
• After Triggers are used to access field values that are set by the system and to effect changes in
other records. The records that activate the after trigger are read-only. We cannot use After
trigger if we want to update a record because it will lead to read-only error.
• [ Insert | Update | Delete ]
These are the DML operations and we can use either of them in a given trigger.
• on [ Table_Name ]
We need to mention the table name on which the trigger is being applied. Don’t forget to
use on keyword and also make sure the selected table is present in the database.
• [ for each row | for each column ]
• [ trigger_body]
It consists of queries that need to be executed when the trigger is called.

So this was all about a simple trigger. But we can also create a nested trigger that can do multi-process.
Also handling it and terminating it at the right time is very important. If we don’t end the trigger
properly it may lead to an infinite loop.
Example for Trigger:

In the below trigger, we are trying to calculate the percentage of the student as soon as his details are
updated to the database.

1 CREATE TRIGGER sample_trigger


2 before INSERT
3 ON student
4 FOR EACH ROW
SET new.total = new.marks/6;
5
Here the “NEW” keyword refers to the row that is getting affected.

Operations in Triggers

We can perform many operations using triggers. Some may be simple and some may be a little complex,
but once if we go through the query its easy to understand.

• DROP A Trigger

1 DROP TRIGGER trigger name;

• Display A Trigger

The below code will display all the triggers that are present.

1 SHOW TRIGGERS;
The below code will display all the triggers that are present in a particular database.

1 SHOW TRIGGERS
2 IN database_name;
Example:

1 SHOW TRIGGERS IN dbms;


In the above example, all the triggers that are present in the database named dbms will be displayed.

We also look at some major variants of the triggers that is Before insert and After insert. We have
already seen a trigger in the example. But with the help of the table lets see how exactly this works.

As we have already understood how to create a trigger, now let’s understand the two variants of the
trigger those are Before insert and After insert. in order to implement them, let’s create a student table
with various columns as shown below:

1
2 CREATE TABLE Student(
studentID INT NOT NULL AUTO_INCREMENT,
3 FName VARCHAR(20),
4 LName VARCHAR(20),
5 Address VARCHAR(30),
6 City VARCHAR(15),
Marks INT,
7 PRIMARY KEY(studentID)
8 );
9
Now if we execute this query we get the following table.
Student ID Fname Lname Address City Marks
int Varchar(20) Varchar(20) Varchar(30) Varchar(15) int
Let’s try to use the first variant i.e, Before Insert

1 CREATE TRIGGER calculate


2 before INSERT
3 ON student
4 FOR EACH ROW
5 SET new.marks = new.marks+100;
Here when we insert data into the student table automatically the trigger will be invoked. The trigger
will add 100 to the marks column into the student column.

Now let’s use the second variant i.e, After Insert

To use this variant we need one more table i.e, Percentage where the trigger will store the results. Use
the below code to create the Percentage Table.

1 create table Final_mark(


2 per int );
Now let us use the after insert trigger

1 CREATE TRIGGER total_mark


2 after insert
3 ON student
4 FOR EACH ROW
5 insert into Final_mark values(new.marks);
Here when we insert data to the table, total_mark trigger will store the result in the Final_mark table.

2.Database Design
Database design is the organization of data according to a database model. The designer determines
what data must be stored and how the data elements interrelate. With this information, they can begin
to fit the data to the database model. Database management system manages the data accordingly.

Database can be divided into four tasks

1.Conceptual design

2.DBMS software selection

3.Logical design

4.Physical design

Conceptual design is independent of software and hardware

Logical design dependent(software)

Physical design independent of both software and hardware

ER model
o ER model stands for an Entity-Relationship model. It is a high-level data model.
This model is used to define the data elements and relationship for a specified
system.
o It develops a conceptual design for the database. It also develops a very simple
and easy to design view of data.
o In ER modeling, the database structure is portrayed as a diagram called an
entity-relationship diagram.

For example, Suppose we design a school database. In this database, the


student will be an entity with attributes like address, name, id, age, etc. The
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.

Consider an organization as an example- manager, product, employee, department


etc. can be taken as an entity.

Employee

a. Weak entity:

o 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.
a. Key Attribute

The key attribute is used to represent the main characteristics of an entity. It represents
a primary key. The key attribute is represented by an ellipse with the text underlined.

b. Composite Attribute

An attribute that composed of many other attributes is known as a composite


attribute. The composite attribute is represented by an ellipse, and those ellipses are
connected with an ellipse.

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

An attribute that can be derived from other attribute is known as a derived attribute.
It can be represented by a dashed ellipse.

For example, A person's age changes over time and can be derived from another
attribute like Date of birth.

3. Relationship

A relationship is used to describe the relation between entities. Diamond or rhombus


is used to represent the relationship.

Types of relationship are as follows:

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.

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.

Notation of ER diagram

Database can be represented using the notations. In ER diagram, many notations are
used to express the cardinality. These notations are as follows:
Mapping Constraints
o A mapping constraint is a data constraint that expresses the number of entities to
which another entity can be related via a relationship set.
o It is most useful in describing the relationship sets that involve more than two entity
sets.
o For binary relationship set R on an entity set A and B, there are four possible mapping
cardinalities. These are as follows:
1. One to one (1:1)
2. One to many (1:M)
3. Many to one (M:1)
4. Many to many (M:M)

One-to-one

In one-to-one mapping, an entity in E1 is associated with at most one entity in E2, and
an entity in E2 is associated with at most one entity in E1.

One-to-many

In one-to-many mapping, an entity in E1 is associated with any number of entities in


E2, and an entity in E2 is associated with at most one entity in E1.
Many-to-one

In one-to-many mapping, an entity in E1 is associated with at most one entity in E2,


and an entity in E2 is associated with any number of entities in E1.

Many-to-many

In many-to-many mapping, an entity in E1 is associated with any number of entities


in E2, and an entity in E2 is associated with any number of entities in E1.

Keys
o Keys play an important role in the relational database.
o It is used to uniquely identify any record or row of data from the table. It is also
used to establish and identify relationships between tables.

For example: In Student table, ID is used as a key because it is unique for each student.
In PERSON table, passport_number, license_number, SSN are keys since they are
unique for each person.

Types of key:
1. Primary key
o It is the first key which is used to identify one and only one instance of an entity
uniquely. An entity can contain multiple keys as we saw in PERSON table. The key
which is most suitable from those lists become a primary key.
o In the EMPLOYEE table, ID can be primary key since it is unique for each employee.
In the EMPLOYEE table, we can even select License_Number and Passport_Number
as primary key since they are also unique.
o For each entity, selection of the primary key is based on requirement and developers.

2. Candidate key
o A candidate key is an attribute or set of an attribute which can uniquely identify
a tuple.
o The remaining attributes except for primary key are considered as a candidate
key. The candidate keys are as strong as the primary key.

For example: In the EMPLOYEE table, id is best suited for the primary key. Rest of the
attributes like SSN, Passport_Number, and License_Number, etc. are considered as a
candidate key.
3. Super Key

Super key is a set of an attribute which can uniquely identify a tuple. Super key is a
superset of a candidate key.

For example: In the above EMPLOYEE table, for(EMPLOEE_ID,


EMPLOYEE_NAME) the name of two employees can be the same, but their
EMPLYEE_ID can't be the same. Hence, this combination can also be a key.

The super key would be EMPLOYEE-ID, (EMPLOYEE_ID, EMPLOYEE-NAME), etc.

4. Foreign key
o Foreign keys are the column of the table which is used to point to the primary key of
another table.
o In a company, every employee works in a specific department, and employee and
department are two different entities. So we can't store the information of the
department in the employee table. That's why we link these two tables through the
primary key of one table.
o We add the primary key of the DEPARTMENT table, Department_Id as a new
attribute in the EMPLOYEE table.
o Now in the EMPLOYEE table, Department_Id is the foreign key, and both the tables
are related.
Three schema Architecture
o The three schema architecture is also called ANSI/SPARC architecture or three-level
architecture.
o This framework is used to describe the structure of a specific database system.
o The three schema architecture is also used to separate the user applications and
physical database.
o The three schema architecture contains three-levels. It breaks the database down into
three different categories.

The three-schema architecture is as follows:

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.
2. Conceptual Level
o The conceptual schema describes the design of a database at the conceptual level.
Conceptual level is also known as logical level.
o The conceptual schema describes the structure of the whole database.
o The conceptual level describes what data are to be stored in the database and also
describes what relationship exists among those data.
o In the conceptual level, internal details such as an implementation of the data structure
are hidden.
o Programmers and database administrators work at this level.

3. External Level
o At the external level, a database contains several schemas that sometimes called as
subschema. The subschema is used to describe the different view of the database.
o An external schema is also known as view schema.
o Each view schema describes the database part that a particular user group is interested
and hides the remaining database from that user group.
o The view schema describes the end user interaction with database systems.

Functional Dependency

The functional dependency is a relationship that exists between two attributes. It typically exists
between the primary key and non-key attribute within a table.

1.X→Y

The left side of FD is known as a determinant, the right side of the production is known as a dependent.

For example:Assume we have an employee table with attributes: Emp_Id, Emp_Name, Emp_Address .

Here Emp_Id attribute can uniquely identify the Emp_Name attribute of employee table because if we
know the Emp_Id, we can tell that employee name associated with it.

Functional dependency can be written as:

Emp_Id → Emp_Name

We can say that Emp_Name is functionally dependent on Emp_Id.


Types of Functional dependency

Trivial functional dependency

o A → B has trivial functional dependency if B is a subset of A.

o The following dependencies are also trivial like: A → A, B → B

Example:Consider a table with two columns Employee_Id and Employee_Name.

1. {Employee_id, Employee_Name} → Employee_Id is a trivial functional dependency as


2. Employee_Id is a subset of {Employee_Id, Employee_Name}.
3. Also, Employee_Id → Employee_Id and Employee_Name → Employee_Name are trivial d
ependencies too.
2. Non-trivial functional dependency

o A → B has a non-trivial functional dependency if B is not a subset of A.

o When A intersection B is NULL, then A → B is called as complete non-trivial.

Example:

ID→Name,

Name → DOB

Transitive Dependency
When an indirect relationship causes functional dependency it is called Transitive Dependency.

If P -> Q and Q -> R is true, then P-> R is a transitive dependency .

Multivalued Dependency
When existence of one or more rows in a table implies one or more other rows in the same table, then
the Multi-valued dependencies occur.

If a table has attributes P, Q and R, then Q and R are multi-valued facts of P.

It is represented by double arrow −

->->
For our example:

P->->Q
Q->->R

In the above case, Multivalued Dependency exists only if Q and R are independent attributes.

Partial Dependency
Partial Dependency occurs when a nonprime attribute is functionally dependent on part of a candidate
key.

The 2nd Normal Form (2NF) eliminates the Partial Dependency. Let us see an example −

<StudentProject>

StudentID ProjectNo StudentName ProjectName

S01 199 Katie Geo Location

S02 120 Ollie Cluster Exploration

In the above table, we have partial dependency; let us see how −

The prime key attributes are StudentID and ProjectNo.

As stated, the non-prime attributes i.e. StudentName and ProjectName should be functionally
dependent on part of a candidate key, to be Partial Dependent.

The StudentName can be determined by StudentID that makes the relation Partial Dependent.

The ProjectName can be determined by ProjectID, which that the relation Partial Dependent.

Normalization
o Normalization is the process of organizing the data in the database.

o Normalization is used to minimize the redundancy from a relation or set of relations. It is also
used to eliminate the undesirable characteristics like Insertion, Update and Deletion
Anomalies.

o Normalization divides the larger table into the smaller table and links them using relationship.

o The normal form is used to reduce redundancy from the database table.

Types of Normal Forms

There are the four types of normal forms:


First Normal Form (1NF)

o A relation will be 1NF if it contains an atomic value.

o It states that an attribute of a table cannot hold multiple values. It must hold only single-valued
attribute.

o First normal form disallows the multi-valued attribute, composite attribute, and their
combinations.

Example: Relation EMPLOYEE is not in 1NF because of multi-valued attribute EMP_PHONE.

EMPLOYEE table:

EMP_ID EMP_NAME EMP_PHONE EMP_STATE

14 John 7272826385, UP
9064738238

20 Harry 8574783832 Bihar

12 Sam 7390372389, Punjab


8589830302

The decomposition of the EMPLOYEE table into 1NF has been shown below:

EMP_ID EMP_NAME EMP_PHONE EMP_STATE

14 John 7272826385 UP

14 John 9064738238 UP

20 Harry 8574783832 Bihar

12 Sam 7390372389 Punjab

12 Sam 8589830302 Punjab


Second Normal Form (2NF)

o In the 2NF, relational must be in 1NF.

o In the second normal form, all non-key attributes are fully functional dependent on the primary
key

Example: Let's assume, a school can store the data of teachers and the subjects they teach. In a school,
a teacher can teach more than one subject.

TEACHER table

TEACHER_ID SUBJECT TEACHER_AGE

25 Chemistry 30

25 Biology 30

47 English 35

83 Math 38

83 Computer 38

In the given table, non-prime attribute TEACHER_AGE is dependent on TEACHER_ID which is a


proper subset of a candidate key. That's why it violates the rule for 2NF.

To convert the given table into 2NF, we decompose it into two tables:

TEACHER_DETAIL table:

TEACHER_ID TEACHER_AGE

25 30

47 35

83 38

TEACHER_SUBJECT table:

TEACHER_ID SUBJECT

25 Chemistry

25 Biology
47 English

83 Math

83 Computer

Third Normal Form (3NF)

o A relation will be in 3NF if it is in 2NF and not contain any transitive partial dependency.

o 3NF is used to reduce the data duplication. It is also used to achieve the data integrity.

o If there is no transitive dependency for non-prime attributes, then the relation must be in third
normal form.

A relation is in third normal form if it holds atleast one of the following conditions for every non-trivial
function dependency X → Y.

1. X is a super key.

2. Y is a prime attribute, i.e., each element of Y is part of some candidate key.

Example:

EMPLOYEE_DETAIL table:

EMP_ID EMP_NAME EMP_ZIP EMP_STATE EMP_CITY

222 Harry 201010 UP Noida

333 Stephan 02228 US Boston

444 Lan 60007 US Chicago

555 Katharine 06389 UK Norwich

666 John 462007 MP Bhopal


Super key in the table above:

1. {EMP_ID}, {EMP_ID, EMP_NAME}, {EMP_ID, EMP_NAME, EMP_ZIP}....so on

Candidate key: {EMP_ID}

Non-prime attributes: In the given table, all attributes except EMP_ID are non-prime.

Here, EMP_STATE & EMP_CITY dependent on EMP_ZIP and EMP_ZIP dependent on


EMP_ID. The non-prime attributes (EMP_STATE, EMP_CITY) transitively dependent on super
key(EMP_ID). It violates the rule of third normal form.

That's why we need to move the EMP_CITY and EMP_STATE to the new <EMPLOYEE_ZIP>
table, with EMP_ZIP as a Primary key.
EMPLOYEE table:

EMP_ID EMP_NAME EMP_ZIP

222 Harry 201010

333 Stephan 02228

444 Lan 60007

555 Katharine 06389

666 John 462007

EMPLOYEE_ZIP table:

EMP_ZIP EMP_STATE EMP_CITY

201010 UP Noida

02228 US Boston

60007 US Chicago

06389 UK Norwich

462007 MP Bhopal

Boyce Codd normal form (BCNF)

o BCNF is the advance version of 3NF. It is stricter than 3NF.

o A table is in BCNF if every functional dependency X → Y, X is the super key of the table.

o For BCNF, the table should be in 3NF, and for every FD, LHS is super key.

Example: Let's assume there is a company where employees work in more than one department.

EMP_ID EMP_COUNTRY EMP_DEPT DEPT_TYPE EMP_DEPT_NO

264 India Designing D394 283

264 India Testing D394 300

364 UK Stores D283 232

364 UK Developing D283 549

EMPLOYEE table:
In the above table Functional dependencies are as follows:

1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}

Candidate key: {EMP-ID, EMP-DEPT}

The table is not in BCNF because neither EMP_DEPT nor EMP_ID alone are keys.

To convert the given table into BCNF, we decompose it into three tables:

EMP_COUNTRY table:

EMP_ID EMP_COUNTRY

264 India

264 India

EMP_DEPT table:

EMP_DEPT DEPT_TYPE EMP_DEPT_NO

Designing D394 283

Testing D394 300

Stores D283 232

Developing D283 549

EMP_DEPT_MAPPING table:

EMP_ID EMP_DEPT

D394 283

D394 300

D283 232

D283 549

Functional dependencies:

1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}

Candidate keys:

Forthefirsttable: EMP_ID
Forthesecondtable: EMP_DEPT
For the third table: {EMP_ID, EMP_DEPT}

Fourth normal form (4NF)

o A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-valued
dependency.

o For a dependency A → B, if for a single value of A, multiple values of B exists, then the relation
will be a multi-valued dependency.

Example

STUDENT

STU_ID COURSE HOBBY

21 Computer Dancing

21 Math Singing

34 Chemistry Dancing

74 Biology Cricket

59 Physics Hockey

The given STUDENT table is in 3NF, but the COURSE and HOBBY are two independent entity. Hence,
there is no relationship between COURSE and HOBBY.

In the STUDENT relation, a student with STU_ID, 21 contains two courses, Computer and Math and
two hobbies, Dancing and Singing. So there is a Multi-valued dependency on STU_ID, which leads to
unnecessary repetition of data.

So to make the above table into 4NF, we can decompose it into two tables:

STUDENT_COURSE

STU_ID COURSE

21 Computer
21 Math

34 Chemistry

74 Biology

59 Physics
STUDENT_HOBBY

Fifth
STU_ID HOBBY fifth
fifth
21 Dancing

21 Singing

34 Dancing

74 Cricket

59 Hockey

fifthnormal form (5NF)


o A relation is in 5NF if it is in 4NF and not contains any join dependency and joining should be
lossless.

o 5NF is satisfied when all the tables are broken into as many tables as possible in order to avoid
redundancy.

o 5NF is also known as Project-join normal form (PJ/NF).

Example

SUBJECT LECTURER SEMESTER

Computer Anshika Semester 1

Computer John Semester 1

Math John Semester 1

Math Akash Semester 2

Chemistry Praveen Semester 1

In the above table, John takes both Computer and Math class for Semester 1 but he doesn't take Math
class for Semester 2. In this case, combination of all these fields required to identify a valid data.
Suppose we add a new Semester as Semester 3 but do not know about the subject and who will be
taking that subject so we leave Lecturer and Subject as NULL. But all three columns together acts as a
primary key, so we can't leave other two columns blank.

So to make the above table into 5NF, we can decompose it into three relations P1, P2 & P3:

P1:

SEMESTER SUBJECT

Semester 1 Computer

Semester 1 Math

Semester 1 Chemistry

Semester 2 Math

P2

SUBJECT LECTURER

Computer Anshika

Computer John

Math John

Math Akash

Chemistry Praveen

P3

SEMSTER LECTURER

Semester 1 Anshika

Semester 1 John

Semester 1 John

Semester 2 Akash

Semester 1 Praveen
3.Database application design and development
Embedded Sql:
SQL is known as the Structured Query Language. It is the language that we use to perform operations
and transactions on the databases.

When we talk about industry-level applications we need properly connected systems which could
draw data from the database and present to the user. In such cases, the embedded SQL comes to our
rescue.

We embed SQL queries into high-level languages such that they can easily perform the logic part of
our analysis.

Some of the prominent examples of languages with which we embed SQL are as follows:


C++

Java

Python etc.
Dynamic sql:

Dynamic SQL is the process that we follow for programming SQL queries in such a way that the
queries are built dynamically with the application operations.

It helps us to manage big industrial applications and manage the transactions without any added
overhead.

With dynamic SQL we are free to create flexible SQL queries and the names of the variables or any
other parameters are passed when the application runs.

We need to use Dynamic SQL for the following use cases:

1. When we need to run dynamic queries on our database, mainly DML queries.
2. When we need to access an object which is not in existence during the compile time.
3. Whenever we need to optimize the run time of our queries.
4. When we need to instantiate the created logic blocks.
5. When we need to perform operations on application fed data using invoker rights.

Cursors in SQL procedures

In SQL procedures, a cursor make it possible to define a result set (a set of data rows) and perform
complex logic on a row by row basis.

By using the same mechanics, an SQL procedure can also define a result set and return it directly to
the caller of the SQL procedure or to a client application.

A cursor can be viewed as a pointer to one row in a set of rows. The cursor can only reference one
row at a time, but can move to other rows of the result set as needed.

To use cursors in SQL procedures, you need to do the following:

1. Declare a cursor that defines a result set.


2. Open the cursor to establish the result set.
3. Fetch the data into local variables as needed from the cursor, one row at a time.
4. Close the cursor when done

To work with cursors you must use the following SQL statements:

• DECLARE CURSOR
• OPEN
• FETCH
• CLOSE

The following example demonstrates the basic use of a read-only cursor within an SQL procedure:

• CREATE PROCEDURE sum_salaries(OUT sum INTEGER)
• LANGUAGE SQL
• BEGIN
• DECLARE p_sum INTEGER;
• DECLARE p_sal INTEGER;
• DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
• DECLARE c CURSOR FOR SELECT SALARY FROM EMPLOYEE;

• SET p_sum = 0;

• OPEN c;

• FETCH FROM c INTO p_sal;

• WHILE(SQLSTATE = '00000') DO
• SET p_sum = p_sum + p_sal;
• FETCH FROM c INTO p_sal;
• END WHILE;

• CLOSE c;

• SET sum = p_sum;

• END

JDBC:

JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query with
the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC drivers to connect with
the database. There are four types of JDBC drivers:

o JDBC-ODBC Bridge Driver,

o Native Driver,

o Network Protocol Driver, and

o Thin Driver

We have discussed the above four drivers in the next chapter.

We can use JDBC API to access tabular data stored in any relational database. By the help of JDBC API,
we can save, update, delete and fetch data from the database. It is like Open Database Connectivity
(ODBC) provided by Microsoft.
Why Should We Use JDBC

Before JDBC, ODBC API was the database API to connect and execute the query with the database. But,
ODBC API uses ODBC driver which is written in C language (i.e. platform dependent and unsecured).
That is why Java has defined its own API (JDBC API) that uses JDBC drivers (written in Java language).

We can use JDBC API to handle database using Java program and can perform the following activities:

1. Connect to the database

2. Execute queries and update statements to the database

3. Retrieve the result received from the database.

Authorization in sql
Authorization is a privilege provided by the Database Administer. Users of the database can only view
the contents they are authorized to view. The rest of the database is out of bounds to them.

The different permissions for authorizations available are:

• Primary Permission - This is granted to users publicly and directly.


• Secondary Permission - This is granted to groups and automatically awarded to a user if he
is a member of the group.
• Public Permission - This is publicly granted to all the users.
• Context sensitive permission - This is related to sensitive content and only granted to a
select users.
he categories of authorization that can be given to users are:

• System Administrator - This is the highest administrative authorization for a user. Users
with this authorization can also execute some database administrator commands such as
restore or upgrade a database.
• System Control - This is the highest control authorization for a user. This allows
maintenance operations on the database but not direct access to data.
• System Maintenance - This is the lower level of system control authority. It also allows users
to maintain the database but within a database manager instance.
• System Monitor - Using this authority, the user can monitor the database and take snapshots
of it.

Applications of Internet

• Download programs and files


• E-mail
• Voice and Video Conferencing
• E-commerce
• File sharing
• Information browsing
• Search the web addresses for access through search engine
• Chatting and many more…

4.Query Evaluation
The query evaluation plan is also referred to as the query execution plan. A query execution
engine is responsible for generating the output of the given query. It takes the query
execution plan, executes it, and finally makes the output for the user query.
Query Processing in DBMS
Query Processing is the activity performed in extracting data from the database. In query processing,
it takes various steps for fetching the data from the database. The steps involved are:

1. Parsing and translation

2. Optimization

3. Evaluation

The query processing works in the following way:

Parsing and Translation


As query processing includes certain activities for data retrieval. Initially, the given
user queries get translated in high-level database languages such as SQL. It gets
translated into expressions that can be further used at the physical level of the file
system. After this, the actual evaluation of the queries and a variety of query -
optimizing transformations and takes place. Thus before processing a query, a
computer system needs to translate the query into a human-readable and
understandable language. Consequently, SQL or Structured Query Language is the
best suitable choice for humans. But, it is not perfectly suitable for the internal
representation of the query to the system. Relational algebra is well suited for the
internal representation of a query. The translation process in query processing is
similar to the parser of a query. When a user executes any query, for generating the
internal form of the query, the parser in the system checks the syntax of the query,
verifies the name of the relation in the database, the tuple, and finally the required
attribute value. The parser creates a tree of the query, known as 'parse-tree.' Further,
translate it into the form of relational algebra. With this, it evenly replaces all the use
of the views when used in the query.
Thus, to make the system understand the user query, it needs to be translated in the
form of relational algebra. We can bring this query in the relational algebra form as:

o σsalary>10000 (πsalary (Employee))


o πsalary (σsalary>10000 (Employee))

After translating the given query, we can execute each relational algebra operation by
using different algorithms. So, in this way, a query processing begins its working.

Evaluation
For this, with addition to the relational algebra translation, it is required to annotate the translated
relational algebra expression with the instructions used for specifying and evaluating each operation.
Thus, after translating the user query, the system executes a query evaluation plan.

Query Evaluation Plan


o In order to fully evaluate a query, the system needs to construct a query evaluation plan.

o The annotations in the evaluation plan may refer to the algorithms to be used for the particular
index or the specific operations.

o Such relational algebra with annotations is referred to as Evaluation Primitives. The evaluation
primitives carry the instructions needed for the evaluation of the operation.

o Thus, a query evaluation plan defines a sequence of primitive operations used for evaluating a
query. The query evaluation plan is also referred to as the query execution plan.

o A query execution engine is responsible for generating the output of the given query. It takes
the query execution plan, executes it, and finally makes the output for the user query.
Optimization

o The cost of the query evaluation can vary for different types of queries.
Although the system is responsible for constructing the evaluation plan, the
user does need not to write their query efficiently.
o Usually, a database system generates an efficient query evaluation plan, which
minimizes its cost. This type of task performed by the database system and is
known as Query Optimization.
o For optimizing a query, the query optimizer should have an estimated cost
analysis of each operation. It is because the overall operation cost depends on
the memory allocations to several operations, execution costs, and so on.

Performance tuning
it takes time to become a Database Expert or an expert Database Administrator. This
all comes with lot of experience in various database designs and good trainings.
But the following list may be helpful for the beginners to have a nice database
performance −
• Use 3BNF database design explained in this tutorial in RDBMS Concepts
chapter.
• Avoid number-to-character conversions because numbers and characters
compare differently and lead to performance downgrade.
• While using SELECT statement, only fetch whatever information is required
and avoid using * in your SELECT queries because it would load the system
unnecessarily.
• Create your indexes carefully on all the tables where you have frequent search
operations. Avoid index on the tables where you have less number of search
operations and more number of insert and update operations.
• A full-table scan occurs when the columns in the WHERE clause do not have
an index associated with them. You can avoid a full-table scan by creating an
index on columns that are used as conditions in the WHERE clause of an SQL
statement.
• Be very careful of equality operators with real numbers and date/time values.
Both of these can have small differences that are not obvious to the eye but
that make an exact match impossible, thus preventing your queries from ever
returning rows.
• Use pattern matching judiciously. LIKE COL% is a valid WHERE condition,
reducing the returned set to only those records with data starting with the
string COL. However, COL%Y does not further reduce the returned results
set since %Y cannot be effectively evaluated. The effort to do the evaluation is
too large to be considered. In this case, the COL% is used, but the %Y is thrown
away. For the same reason, a leading wildcard %COL effectively prevents the
entire filter from being used.
• Fine tune your SQL queries examining the structure of the queries (and
subqueries), the SQL syntax, to discover whether you have designed your
tables to support fast data manipulation and written the query in an optimum
manner, allowing your DBMS to manipulate the data efficiently.
• For queries that are executed on a regular basis, try to use procedures. A
procedure is a potentially large group of SQL statements. Procedures are
compiled by the database engine and then executed. Unlike an SQL statement,
the database engine need not optimize the procedure before it is executed.
• Avoid using the logical operator OR in a query if possible. OR inevitably slows
down nearly any query against a table of substantial size.
• You can optimize bulk data loads by dropping indexes. Imagine the history
table with many thousands of rows. That history table is also likely to have
one or more indexes. When you think of an index, you normally think of faster
table access, but in the case of batch loads, you can benefit by dropping the
index(es).
• When performing batch transactions, perform COMMIT at after a fair number
of records creation in stead of creating them after every record creation.
• Plan to defragment the database on a regular basis, even if doing so means
developing a weekly routine.

Built-In Tuning Tools


Oracle has many tools for managing SQL statement performance but among them two are very
popular. These two tools are −

• Explain plan − tool identifies the access path that will be taken when the SQL statement is
executed.

• tkprof − measures the performance by time elapsed during each phase of SQL statement
processing.
5.Database System Architectures

1.Centralized DBMSs Architecture


A centralized database is stored at a single location such as a mainframe computer. It is maintained
and modified from that location only and usually accessed using an internet connection such as a LAN
or WAN. The centralized database is used by organisations such as colleges, companies, banks etc.

Architectures for DBMSs have followed trends similar to those for general computer system architectures . The
reason was that most users accessed such systems via computer terminals that did not have processing power
and only provided display capabilities .all processing was performed remotely on the computer system,
and only display information and controls were sent from the computer to the display terminals, which
were connected to the central computer via various types of communications networks.

At first, database systems used these computers similarly to how they had used display terminals, so
that the DBMS itself was still a centralized DBMS in which all the DBMS functionality, application
program execution, and user inter-face processing were carried out on one machine.

2. Basic Client/Server Architectures


First, we discuss client/server architecture in general, then we see how it is applied to DBMSs.
The client/server architecture was developed to deal with computing environments in which a large
number of PCs, workstations, file servers, printers, database servers,
Web servers, e-mail servers, and other software and equipment are connected via a network. The idea
is to define specialized servers with specific functionalities. For example, it is possible to connect a
number of PCs or small workstations as clients to a file server that maintains the files of the client
machines. Another machine can be designated as a printer server by being connected to various
printers; all print requests by the clients are forwarded to this machine. Web servers or e-mail
servers also fall into the specialized server category. The resources provided by specialized servers can
be accessed by many client machines. The client machines provide the user with the appropriate
interfaces to utilize these servers, as well as with local processing power to run local applications.

Other machines would be dedicated servers, and others would have both client and server
functionality.

3. Two-Tier Client/Server Architectures for DBMSs


In relational database management systems (RDBMSs), many of which started as centralized systems,
the system components that were first moved to the client side were the user interface and application
programs. Because SQL (see Chapters 4 and 5) provided a standard language for RDBMSs, this created
a logical dividing point between client and server. Hence, the query and transaction functionality
related to SQL processing remained on the server side. In such an architecture, the server is often called
a query server or transaction server because it provides these two functionalities. In an RDBMS, the
server is also often called an SQL server.

The user interface programs and application programs can run on the client side. When DBMS access
is required, the program establishes a connection to the DBMS (which is on the server side); once the
connection is created, the client program can communicate with the DBMS. A standard called Open
Database Connectivity

The architectures described here are called two-tier architectures because the soft-ware components
are distributed over two systems: client and server. The advan-tages of this architecture are its
simplicity and seamless compatibility with existing systems. The emergence of the Web changed the
roles of clients and servers, leading to the three-tier architecture.
Parallel Database
In parallel database system data processing performance is improved by using multiple resources in
parallel. In this CPU, the disk is used parallel to enhance the processing performance.

Operations like data loading and query processing are performed parallel. The centralized and client
server database systems are not powerful enough to handle applications which need fast processing.

Parallel database systems have great advantages for online transaction processing and decision support
applications. Parallel processing divides a large task into multiple tasks and each task is performed
concurrently on several nodes. This gives a larger task to complete more quickly.

Architectural Models
There are several architectural models for parallel machines. The most important one are as follows −

• Shared-memory multiple CPU − Here, the computer has several simultaneously active CPUs
that are attached to an interconnection network and share a single main memory and a common
array of disk storage.

• Shared disk architecture − Here, each node has its own main memory but all nodes share mass
storage. In practice, each node also has multiple processors.

• Shared nothing architecture − Here, each node has its own mass storage as well as main
memory.
Distributed Database
It is a collection of multiple interconnected databases that are spread physically across various locations
that communicate via a computer network.
Distributed Database gives us the following −

• Modular Development − The term modular development includes that when we want to
expand the using system on different locations then we just need to add nodes to the current
network and these nodes do not interrupt the current network functionalities.
• Increases Reliability − The term increases reliability refers to, if one node on a network fails
then its work can be distributed between other nodes on the network, failing of one node
does not stop the system.
• Improves Performance − We all know that a small database is easy to handle compared to a
large database, so in a distributed database a large database is distributed into a small
database across various locations which are easy to handle with better performance.
• Increase Availability − The failure of one node will not affect data availability because data
can be obtained from various other nodes on the network.
• Faster Response − Data retrieval becomes more efficient as data is available locally.

Object -Oriented Database


An object-oriented database is a collection of object-oriented programming and relational
database. There are various items which are created using object-oriented programming languages like
C++, Java which can be stored in relational databases, but object-oriented databases are well-suited for
those items.

An object-oriented database is organized around objects rather than actions, and data rather than
logic. For example, a multimedia record in a relational database can be a definable data object, as
opposed to an alphanumeric value.

XML Database:
XML Database is used to store huge amount of information in the XML format. As the use of XML is
increasing in every field, it is required to have a secured place to store the XML documents. The data
stored in the database can be queried using XQuery, serialized, and exported into a desired format.

XML Database Types

There are two major types of XML databases −

• XML- enabled
• Native XML (NXD)
XML - Enabled Database

XML enabled database is nothing but the extension provided for the conversion of XML document.
This is a relational database, where data is stored in tables consisting of rows and columns. The tables
contain set of records, which in turn consist of fields.

Native XML Database


Native XML database is based on the container rather than table format. It can store large amount of
XML document and data. Native XML database is queried by the XPath-expressions.
Native XML database has an advantage over the XML-enabled database. It is highly capable to store,
query and maintain the XML document than XML-enabled database.
Example
Following example demonstrates XML database −

<?xml version = "1.0"?>


<contact-info>
<contact1>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</contact1>

<contact2>
<name>Manisha Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 789-4567</phone>
</contact2>
</contact-info>

Here, a table of contacts is created that holds the records of contacts (contact1 and contact2), which in
turn consists of three entities − name, company and phone.

Datatypes in database
SQL Data Type is an attribute that specifies the type of data of any object. Each column, variable and
expression has a related data type in SQL. You can use these data types while creating your tables.
You can choose a data type for a table column based on your requirement.

SQL Server offers six categories of data types for your use which are listed below −

Exact Numeric Data Types

DATA TYPE FROM TO

bigint -9,223,372,036,854,775,808 9,223,372,036,854,775,807

int -2,147,483,648 2,147,483,647

smallint -32,768 32,767


tinyint 0 255

bit 0 1

decimal -10^38 +1 10^38 -1

numeric -10^38 +1 10^38 -1

money -922,337,203,685,477.5808 +922,337,203,685,477.5807

smallmoney -214,748.3648 +214,748.3647

Approximate Numeric Data Types

DATA TYPE FROM TO

float -1.79E + 308 1.79E + 308

real -3.40E + 38 3.40E + 38

Date and Time Data Types

DATA TYPE FROM TO

Datetime Jan 1, 1753 Dec 31, 9999

Smalldatetime Jan 1, 1900 Jun 6, 2079

Date Stores a date like June 30, 1991

Time Stores a time of day like 12:30 P.M.

Note − Here, datetime has 3.33 milliseconds accuracy where as smalldatetime has 1 minute accuracy.
Character Strings Data Types

Sr.No. DATA TYPE & Description

1 char
Maximum length of 8,000 characters.( Fixed length non-Unicode characters)

varchar
2
Maximum of 8,000 characters.(Variable-length non-Unicode data).

varchar(max)
3
Maximum length of 2E + 31 characters, Variable-length non-Unicode data (SQL Server 2005
only).

text
4
Variable-length non-Unicode data with a maximum length of 2,147,483,647 characters.

Unicode Character Strings Data Types

Sr.No. DATA TYPE & Description

1 nchar
Maximum length of 4,000 characters.( Fixed length Unicode)

nvarchar
2
Maximum length of 4,000 characters.(Variable length Unicode)

nvarchar(max)
3
Maximum length of 2E + 31 characters (SQL Server 2005 only).( Variable length Unicode)

ntext
4
Maximum length of 1,073,741,823 characters. ( Variable length Unicode )

Binary Data Types


Sr.No. DATA TYPE & Description

1 binary
Maximum length of 8,000 bytes(Fixed-length binary data )

varbinary
2
Maximum length of 8,000 bytes.(Variable length binary data)

varbinary(max)
3
Maximum length of 2E + 31 bytes (SQL Server 2005 only). ( Variable length Binary data)

image
4
Maximum length of 2,147,483,647 bytes. ( Variable length Binary Data)

Misc Data Types

Sr.No. DATA TYPE & Description

sql_variant
1
Stores values of various SQL Server-supported data types, except text, ntext, and
timestamp.

timestamp
2
Stores a database-wide unique number that gets updated every time a row gets
updated

uniqueidentifier
3
Stores a globally unique identifier (GUID)

xml
4
Stores XML data. You can store xml instances in a column or a variable (SQL Server
2005 only).

cursor
5
Reference to a cursor object
table
6
Stores a result set for later processing

Cloud based data storage systems:


Cloud Storage is a service that allows to save data on offsite storage system managed by third-party
and is made accessible by a web services API.

Storage Devices

Storage devices can be broadly classified into two categories:

• Block Storage Devices


• File Storage Devices
Block Storage Devices
The block storage devices offer raw storage to the clients. These raw storage are partitioned to create
volumes.

File Storage Devices


The file Storage Devices offer storage to clients in the form of files, maintaining its own file system.
This storage is in the form of Network Attached Storage (NAS).

Cloud Storage Classes

Cloud storage can be broadly classified into two categories:

• Unmanaged Cloud Storage


• Managed Cloud Storage
Unmanaged Cloud Storage
Unmanaged cloud storage means the storage is preconfigured for the customer. The customer can
neither format, nor install his own file system or change drive properties.

Managed Cloud Storage


Managed cloud storage offers online storage space on-demand. The managed cloud storage system
appears to the user to be a raw disk that the user can partition and format.

Creating Cloud Storage System

The cloud storage system stores multiple copies of data on multiple servers, at multiple locations. If
one system fails, then it is required only to change the pointer to the location, where the object is stored.
To aggregate the storage assets into cloud storage systems, the cloud provider can use storage
virtualization software known as StorageGRID. It creates a virtualization layer that fetches storage
from different storage devices into a single management system. It can also manage data
from CIFS and NFS file systems over the Internet. The following diagram shows how StorageGRID
virtualizes the storage into storage clouds:
Virtual Storage Containers

The virtual storage containers offer high performance cloud storage systems. Logical Unit Number
(LUN) of device, files and other objects are created in virtual storage containers. Following diagram
shows a virtual storage container, defining a cloud storage domain:
00:31/06:36

10 Sec

10.1M
236

How to find Nth Highest Salary in SQL

You might also like