Download as pdf or txt
Download as pdf or txt
You are on page 1of 50

DATABASE MANAGEMENT SYSTEM

Name: DARPAN
Class: IT-
2(4thSem)
S No. : 43
Enrollment No. : 09315003119
INDEX
S No. Experiment Date Page No.
1. Introduction to DBMS and SQL. 30/03/21 1-13

2. Design a Bank Database and create 06/04/21 14-19


required tables.

3. Apply the constraints like primary 12/05/21 20-22


key, foreign key, NOT NULL to the
tables.

4. Write a SQL statement for ALTER, 12/05/21 23-26


UPDATE AND DELETE.
5. Write the queries for implementing 19/05/21 27-30
the following functions: MAX (),
MIN (), AVG (), COUNT ().

6. Write the queries to implement the 01/06/21 31-33


joins

7. Write queries to implement the 08/06/21 34-36


concept of Integrity constraints

8. Write queries to create the views 08/06/21 37-38

9. Perform the queries for triggers 15/06/21 39-47

10. Perform the following operation for 15/06/21 48-50


demonstrating the insertion,
updation and deletion using the
referential integrity constants.
EXPERIMENT-1
AIM: Introduction to DBMS and SQL.

THEORY

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: 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.
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.
It provides protection and security to the database. In the case of multiple users,
it also maintains data consistency.

Characteristics:

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


information.

● It can provide a clear and logical view of the process that manipulates
data.

● DBMS contains automatic backup and recovery procedures.

● It contains ACID properties which maintain data in a healthy state in case


of failure.
● It can reduce the complex relationship between data.

● It is used to support manipulation and processing of data.


● It is used to provide security of data.

● It can view the database from different viewpoints according to the


requirements of the user.
Advantages:
● 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.
● Data sharing: In DBMS, the authorized users of an organization can share
the data among multiple users.
● Easily Maintenance:
● It can be easily maintainable due to the centralized nature of the database
system.
● Reduce time: It reduces development time and maintenance need.
● Backup: It provides backup and recovery subsystems which create
automatic backup of data from hardware and software failures and
restores the data if required.
● Multiple user interface: It provides different types of user interfaces like
graphical user interfaces, application program interfaces
Disadvantages:
● Cost of Hardware and Software:
● It requires a high speed of data processor and large memory size to run
DBMS software.
● Size: It occupies a large space of disks and large memory to run them
efficiently.
● Complexity: Database system creates additional complexity and
requirements.
● 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.

Applications:

Sector Use of DBMS

Banking
For customer information,
account activities, payments,
deposits, loans, etc.

Airlines
For reservations and
schedule information.

Universities
For student information,
course registrations, colleges
and grades.

Telecommunication
It helps to keep call records,
monthly bills, maintaining
balances, etc.

Finance
For storing information
about stock, sales, and
purchases of financial
instruments like stocks and
bonds.
Database languages:
DATA DEFINITION LANGUAGE (DDL):
These statements are used to classify the database structure or schema. It is a
type of language that allows the DBA or user to depict and name those entities,
attributes, and relationships that are required for the application along with any
associated integrity and security constraints. Here are the lists of tasks that come
under DDL-

• CREATE - used to create objects in the database.

• ALTER - used to alters the structure of the database.

• DROP - used to delete objects from the database.

• TRUNCATE - used to remove all records from a table, including all


spaces allocated for the records are removed.

• COMMENT - used to add comments to the data dictionary • RENAME -


used to rename an object.

DATA MANIPULATION LANGUAGE (DML):


A language that offers a set of operations to support the fundamental data
manipulation operations on the data held in the database. Data Manipulation
Language (DML) statements are used to manage data within schema objects.
• SELECT - It retrieves data from a database.
• INSERT - It inserts data into a table.
• UPDATE - It updates existing data within a table.
• DELETE - It deletes all records from a table, the space for the records remain.
• MERGE - UPSERT operation (insert or update).
• CALL - It calls a PL/SQL or Java subprogram.
• EXPLAIN PLAN - It explains access path to data.
• LOCK TABLE - It controls concurrency.

DATA CONTROL LANGUAGE (DCL):


There are another two forms of database sublanguages. The Data Control
Language (DCL) is used to control privilege in Database. To perform any
operation in the database, such as for creating tables, sequences or views we
need privileges. Privileges are of two types,
• System - creating a session, table, etc. are all types of system privilege.
• Object - any command or query to work on tables comes under object
privilege. DCL is used to define two commands. These are:
• Grant - It gives user access privileges to a database.
• Revoke - It takes back permissions from the user.
TRANSACTION CONTROL LANGUAGE:
Transaction Control statements are used to run the changes made by DML
statements. It allows statements to be grouped into logical transactions.

• COMMIT - It saves the work done.


• SAVEPOINT - It identifies a point in a transaction to which you can later roll
back.

• ROLLBACK - It restores the database to original since the last COMMIT.


• SET TRANSACTION - It changes the transaction options like isolation level
and what rollback segment to use.

Data Models:

A Database model defines the logical design and structure of a database and
defines how data will be stored, accessed and updated in a database
management system. While the Relational Model is the most widely used
database model, there are other models too: -Hierarchical Model
- Network Model
- Entity-relationship Model
- Relational Model
HIERARCHIAL MODEL
This database model organises data into a tree-like structure, with a single
root, to which all the other data is linked. The hierarchy starts from the Root
data, and expands like a tree, adding child nodes to the parent nodes.
In this model, a child node will only have a single parent node.
This model efficiently describes many real-world relationships like index of a
book, recipes etc.
In hierarchical model, data is organised into treelike structure with one one-
to-many relationship between two different types of data, for example, one
department can have many courses, many professors and of-course many
students.

NETWORK MODEL

This is an extension of the Hierarchical model. In this model data is organised


more like a graph, and are allowed to have more than one parent node.
In this database model data is more related as more relationships are
established in this database model. Also, as the data is more related, hence
accessing the data is also easier and fast.
This database model was used to map many-to-many data relationships. This
was the most widely used database model, before Relational Model was
introduced.

ENTITY RELATIONSHIP MODEL


In this database model, relationships are created by dividing object of interest
into entity and its characteristics into attributes.
Different entities are related using relationships.
E-R Models are defined to represent the relationships into pictorial form to
make it easier for different stakeholders to understand.
This model is good to design a database, which can
then be turned into tables in relational model(explained below).
Let's take an example, If we have to design a School Database,
then Student will be an entity with attributes name, age, address etc. As Address
is generally complex, it can be another entity with attributes street name,
pincode, city etc, and there will be a relationship between them.
RELATIONAL MODEL
In this model, data is organised in two-dimensional tables and the relationship is
maintained by storing a common field.
This model was introduced by E.F Codd in 1970, and since then it has been the
most widely used database model, infact, we can say the only database model
used around the world.
The basic structure of data in the relational model is tables. All the information
related to a particular type is stored in rows of that table.
Hence, tables are also known as relations in relational model.
In the coming tutorials we will learn how to design tables, normalize them to
reduce data redundancy and how to use Structured Query language to access
data from tables.
The main highlights of this model are −
*Data is stored in tables called relations.

* Relations can be normalized.


*In normalized relations, values saved are atomic values.

* Each row in a relation contains a unique value.

* Each column in a relation contains values from a same domain.

Concepts
Tables − In relational data model, relations are saved in the format of
Tables. This format stores the relation among entities. A table has rows
and columns, where rows represents records and columns represent the
attributes.
Tuple − A single row of a table, which contains a single record for that
relation is called a tuple.
Relation instance − A finite set of tuples in the relational database
system represents relation instance. Relation instances do not have
duplicate tuples.
Relation schema − A relation schema describes the relation name
tablename, attributes, and their names.
Relation key − Each row has one or more attributes, known as relation
key, which can identify the row in the relation table uniquely.
Attribute domain − Every attribute has some pre-defined value scope,
known as attribute domain.
SQL
SQL is a language to operate databases; it includes database creation,
deletion, fetching rows, modifying rows, etc. SQL is an ANSI
(American National Standards Institute) standard language, but there
are many different versions of the SQL language.
SQL is widely popular because it offers the following advantages −

• Allows users to access data in the relational database management


systems.

• Allows users to describe the data.

• Allows users to define the data in a database and manipulate that data.

• Allows to embed within other languages using SQL modules, libraries &
pre-compilers.

• Allows users to create and drop databases and tables.

• Allows users to create view, stored procedure, functions in a database.

• Allows users to set permissions on tables, procedures and views.


HISTORY OF SQL

• 1970 − Dr. Edgar F. "Ted" Codd of IBM is known as the father of


relational databases. He described a relational model for databases.

• 1974 − Structured Query Language appeared.

• 1978 − IBM worked to develop Codd's ideas and released a product


named System/R.
• 1986 − IBM developed the first prototype of relational database and
standardized by ANSI. The first relational database was released by
Relational Software which later came to be known as Oracle.
EXPERIMENT-2
AIM:- Design a Bank Database and create required tables.
1) CREATE DATABASE command:- The CREATE DATABASE statement is
used to create a new SQL database.
SYNTAX:-
CREATE DATABASE databasename;

EXAMPLE:-

2) SHOW DATABASE command:- Once a Database is created ,we can check


the list of databases using SHOW DATABASES statement.

SYNTAX:- SHOW DATABASES;


EXAMPLE:-

3) CREATE TABLE statement:- The CREATE TABLE statement is used to


create a new table in a database.
SYNTAX:- CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);

EXAMPLE:-

4) DESCRIBE statement:- As the name suggests, DESCRIBE is used to


describe something.
SYNTAX:- DESCRIBE table_name;
EXAMPLE:-
5) INSERT INTO statement:- The INSERT INTO statement is used to insert
new records in a table.

SYNTAX:- INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);
OR
SYNTAX:- INSERT INTO table_name
VALUES (value1, value2, value3, ...);

EXAMPLE:-
• INSERT INTO Deposit Table:-
• INSERT INTO Branch:-

• INSERT INTO Customers;


6) SELECT statement:- The SELECT statement is used to select data from a
database.
SYNTAX:- SELECT column1, column2, ...
FROM table_name;
OR
SYNTAX:- SELECT * FROM table_name;

EXAMPLE:-
When to select particular column from a table.
SYNTAX:- SELECT column1, column2, ...
FROM table_name;
EXAMPLE:-
EXPERIMENT-3
AIM:- Apply the constraints like primary key, foreign key, NOT NULL to the
tables.
1) Primary Key:- A primary key is a column or a set of columns that uniquely
identifies each row in the table.
SYNTAX:-
CREATE TABLE tablename(
Primary_key_column datatype PRIMARY KEY,


);
EXAMPLE:-

2) NOT NULL:- The NOT NULL constraint is a column constraint that ensures
values stored in a column are not NULL.
SYNTAX:-
column_name data_type NOT NULL;
3) FOREIGN KEY:- A foreign key is a column or group of columns in a table
that links to a column or group of columns in another table. The foreign key
places constraints on data in the related tables, which allows MySQL to
maintain referential integrity.

SYNTAX:-
[CONSTRAINT constraint_name]
FOREIGN KEY [foreign_key_name] (column_name, ...)
REFERENCES parent_table(colunm_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]

EXAMPLE:-

FUNCTIONING:-
INSERTION
DISPLAYING THE CONTENT

INSERTING INTO SECOND TABLE

DISPLAYING

ERROR CASE AS FOREIGN KEY IS APPLIED WE CAN’T ADD SUCH


VALUES
EXPERIMENT-4
AIM:- Write a SQL statement for ALTER,UPDATE AND DELETE.
1) ALTER TABLE statement:- The SQL ALTER TABLE command is used to
add, delete or modify columns in an existing table.

The basic syntax of an ALTER TABLE command to add a New Column in an


existing table is as follows.
SYNTAX:- ALTER TABLE table_name ADD column_name datatype;

EXAMPLE:-

The basic syntax of an ALTER TABLE command to DROP COLUMN in an


existing table is as follows.
SYNTAX:- ALTER TABLE table_name DROP COLUMN column_name;

EXAMPLE:-
2) UPDATE statement:- The SQL UPDATE Query is used to modify the
existing records in a table.

SYNTAX:- UPDATE table_name


SET column1 = value1, column2 = value2 ...., columnN = valueN
WHERE [condition];

EXAMPLE:-
3) DELETE statement:- The SQL DELETE Query is used to delete the existing
records from a table.

SYNTAX:- DELETE FROM table_name


WHERE [condition];

EXAMPLE:-
EXPERIMENT-5
AIM:- Write the queries for implementing the following functions: MAX (),
MIN (),AVG (),COUNT ().
1) MAX():-The MAX() function returns the largest value of the selected
column.

SYNTAX:- SELECT MAX(column_name)


FROM table_name
WHERE condition;

EXAMPLE:-
2) MIN():-The MIN() function returns the smallest value of the selected
column.

SYNTAX:- SELECT MIN(column_name)


FROM table_name
WHERE condition;

EXAMPLE:-
3) AVG():-The AVG() function returns the average value of a numeric column.

SYNTAX:- SELECT AVG(column_name)


FROM table_name
WHERE condition;

EXAMPLE:-
4) COUNT():-The COUNT() function returns the number of rows that matches
a specified criterion.

SYNTAX:- SELECT COUNT(column_name)


FROM table_name
WHERE condition;

EXAMPLE:-
EXPERIMENT-6
AIM:- Write the queries to implement the joins.
A relational database consists of multiple related tables linking together using
common columns which are known as foreign key columns. Because of this,
data in each table is incomplete from the business perspective.
1) INNER JOIN
SYNTAX:
SELECT
select_list
FROM t1
INNER JOIN t2 ON join_condition1
INNER JOIN t3 ON join_condition2
...;
EXAMPLE:

2) LEFT JOIN
SYNTAX:
SELECT
select_list
FROM
t1
LEFT JOIN t2 ON
join_condition;
EXAMPLE:

3) RIGHT JOIN
SYNTAX:
SELECT
select_list
FROM t1
RIGHT JOIN t2 USING (column_name);

EXAMPLE:
4) CROSS JOIN
SYNTAX:
SELECT * FROM t1
CROSS JOIN t2
WHERE t1.id = t2.id;
EXAMPLE:
EXPERIMENT-7
AIM:- Write queries to implement the concept of Integrity constraints.
MySQL constraints
NOT NULL constraint – introduce you to the NOT NULL constraint and show
you how to declare a NOT NULL column or add a NOT NULL constraint to an
existing column.
Primary key constraint – guide you on how to use the primary key constraint to
create the primary key for a table.
Foreign key constraint – introduce you to the foreign key and show you step by
step how to create and drop foreign keys.
UNIQUE constraint – show you how to use UNIQUE constraint to enforce the
uniqueness of values in a column or a group of columns in a table.
CHECK constraint – learn how to create CHECK constraints to ensure data
integrity.

1) NOT NULL
2) PRIMARY KEY

3) FOREIGN KEY
4) UNIQUE

5) CHECK CONSTRAINT
EXPERIMENT-8
AIM 3:- Write queries to create the views
Table for reference:-

Creating the view

Displaying the view


node1 | r o lo
bu11et |
bu11et 500 |

s1000 n n
EXPERIMENT-9
AIM:- Perform the queries for triggers
MySQL triggers
Create triggers – describe steps of how to create a trigger in MySQL.
Drop triggers – show you how to drop a trigger.
Create a BEFORE INSERT trigger – show you how to create a BEFORE
INSERT trigger to maintain a summary table from another table.
Create an AFTER INSERT trigger – describe how to create an AFTER INSERT
trigger to insert data into a table after inserting data into another table.
Create a BEFORE UPDATE trigger – learn how to create a BEFORE UPDATE
trigger that validates data before it is updated to the table.
Create an AFTER UPDATE trigger – show you how to create an AFTER
UPDATE trigger to log the changes of data in a table.
Create a BEFORE DELETE trigger – show how to create a BEFORE DELETE
trigger.
Create an AFTER DELETE trigger – describe how to create an AFTER
DELETE trigger.
1) CREATE TRIGGERS
The CREATE TRIGGER statement creates a new trigger.
2) DROP TRIGGERS

3) Create a BEFORE INSERT trigger


DE L If'\ITE R $$

'r.'orkCentens

ro'.•‹count

mysql> insert into workcenters(name,capacity)


-> values('mold machine',100);

my‘ sqI› In se rt Into t‹o r kc ente r s (name, c apac 1ty‘)


- › va lue s ( pac k1ng , 200) ;
3) Create a BEFORE INSERT trigger.
4) Create an AFTER INSERT
5) Create a BEFORE UPDATE
6) Create an AFTER UPDATE trigger
8) Create a BEFORE DELETE trigger
ny’sq1› c reate tr 1gger after sa tar 1es

› on sa tar 1es f-or each rot«


› update Sa1ary’Budget s

ny'sql› select* from Salary'Budgets,

20000
EXPERIMENT-10
AIM:- Perform the following operation for demonstrating the insertion ,
updation and deletion using the referential integrity constants.

1) Insertion
2) Updation

3) Deletion

You might also like