0% found this document useful (0 votes)
19 views39 pages

Ravindra DBMS

Uploaded by

jainnjinesh2312
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)
19 views39 pages

Ravindra DBMS

Uploaded by

jainnjinesh2312
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/ 39

ENGINEERING COLLEGE BIKANER

DBMS PRACTICAL FILE

SESSION 2022-23
DATE OF SUBMISSION: 10-10-2023

SUBMITTED TO: SUBMITTED BY:


MR. DHANROOP MAL NAME: RAVINDRA
NAGAR BRANCH: CSE ‘B’
(Assistant professor) ROLLNo:21EEBCS090
Sr. No. List of Experiments Date Signature

1. Design a Database and create required table. For e.g. Bank, College
Database.

2. Write a SQL statement for implementing ALTER, UPDATE and


DELETE.

3. Perform the following operations for demonstrating the insertion,


updation and deletion.

4. Write the query to implement the concept of Integrity constrains.

5. Apply the constrains like Primary key, Foreign key, NOT NULL to the
tables.

6. Using the referential integrity constraints.

7. Write query for implementing the following functions: MAX( ), MIN(


), AVG( ) and COUNT( ).

8. Perform the queries for triggers.

9. Write the queries to implement the joins.

10. Write the query to create the views.


11. Write the query for creating the users and their roles.

12. Explain the entity relationship model.


Que.1. To Design a Database and create required table.
For e.g. Bank, College Database.
Database : 'Data' is originated from the word 'datum' that means 'single piece of information.' It is
plural of the word datum.A database is an organized collection of inter-related data, generally stored
and accessed electronically from a computer system.

DBMS : DBMS or Database Management System is a software application used to access, create,
and manage databases by modifying them.

Functions of DBMS :

• Data abstraction and independence.


• Data security.
• A locking mechanism for concurrent access.
• Robust data integrity capabilities.
• Logging and auditing of activity.
• Simple access using a standard API.
• Uniform administration procedures for data.

Types of DBMS :

• Hierarchical DBMS : In a Hierarchical database, model data is organized in a tree-like


structure. Data is represented using a parent-child relationship. In Hierarchical DBMS
parent may have many children, but children have only one parent.
• Network Model : The network database model allows each child to have multiple
parents. It helps you to address the need to model more complex relationships like as the
orders/parts many-to-many relationship. In this model, entities are organized in a graph
which can be accessed through several paths.
• Relational model : Relational DBMS is the most widely used DBMS model . This model
is based on normalizing data in the rows and columns of the tables. Relational model
stored in fixed structures and manipulated using SQL.
• Object-Oriented Model : In Object-oriented Model data stored in the form of objects.
The structure which is called classes which display data within it. It defines a database as
a collection of objects which stores both data members values and operations.

Installation steps for MySql:

• Download MySql from https://dev.mysql.com/downloads/mysql/

Installing MySQL on Windows: Your downloaded MySQL is neatly packaged with an installer.
Download the installer package, unzip it anywhere and run setup.exe. By default, this process will
install everything under C:\mysql.

Verify MySQL installation:

• Step- 1 : Open from start

• Step-2 : enter the password you have set during installation .

• Step-3 : if you already have created data bases then use them else create new database.
Creation of Database :
Syntax : CREATE DATABASE databasename

How to use the data base you have created :

DESC Command : desc or describe command shows the structure of table which include name of
the column, data-type of column and the nullability which means, that column can contain null values
or not.
Que.2. SQL statement for implementing ALTER,
UPDATE and DELETE.
Database Language : Database languages are used to read, update and store data in a
database. Read, Update, Manipulate, and Store data in a database using Database
Languages.there are four types of DBMS language :

1. DDL (Data Definition Language)


2. DML (Data Manipulation Language)
3. DCL (Data Control Language)
4. TCL (Transaction Control Language)

DDL (Data Definition language) - DDL is used when we need to create database and tables,
alter them , drop them and rename the table .

• CREATE: Create new database, table.

CREATE DATABASE goeduhub;


CREATE TABLE bank (id INT(16) PRIMARY KEY AUTO_INCREMENT,name
VARCHAR(255) NOT NULL );

• ALTER: Alter existing database, table.

ALTER TABLE bank ADD COLUMN lastname VARCHAR(255) NOT NULL;

• DROP: Drop the database

DROP DATABASE goeduhub;


DROP TABLE bank;

• RENAME: Set a new name for the table.


RENAME TABLE bank TO student;

• TRUNCATE : To delete tables in a database instance

TRUNCATE student;
DML (Data Manipulation Language) - DML language used to manipulate the database like
inserting data, updating table, retrieving record from a table .

• SELECT: Retrieve data from the database

SELECT * FROM student;

• INSERT: Insert data

INSERT INTO student (name, lastname) VALUES ('nams', 'Shivi');

• UPDATE: Update data

UPDATE student SET name = 'Dia' WHERE lastname = 'Shivi';

• DELETE: Delete all records

DELETE FROM student WHERE name = 'Dia';

• MERGE: It performs UPSERT (update and insert ) operation

DCL (Data Control Language) : Grant privilege to a user using the GRANT
statement.revoke the privilege using the REVOKE statement.

• GRANT: Give privilege to access the database.

CREATE USER 'dia'@'localhost' IDENTIFIED BY '123';


GRANT ALL PRIVILEGES ON student TO 'dia'@'localhost'; FLUSH PRIVILEGES;

• REVOKE: Take back the privilege to access the database.

REVOKE ALL PRIVILEGES ON student* FROM 'dia'@'localhost'; FLUSH


PRIVILEGES;
TCL (Transaction Control Language):Manage transactions in the Database using the
Transaction Control Language:

• COMMIT: Save the work.

START TRANSACTION;
INSERT INTO student (name, lastname) VALUES ('Dia', 'Shivi');
COMMIT;

• SAVEPOINT: Set a point in transaction to rollback later  ROLLBACK:


Restores since last commit

Que.3. Perform the following operations for


demonstrating the insertion, updation and deletion.
INSERTION Query

Syntax : INSERT INTO table(column1, column2, …) VALUES (value1,


value2, …);

• Inserting only values

ROLL_NO NAME DEPT SEC PHONE

101 Alia CSE B 9876540010

102 Alisha ME A 9876541110

• Inserting values in only specified columns

Example :

INSERT INTO college(ROLL_NO, NAME, SEC)VALUES(‘104,’Aditya','A');


INSERT INTO college(ROLL_NO, NAME, SEC)VALUES(‘105,’NITYA','B'); INSERT INTO
college(ROLL_NO, NAME, SEC)VALUES(‘106,’Anaya','C');

ROLL_NO NAME DEPT SEC PHONE

101 Alia CSE B 9876540010

102 Alisha ME A 9876541110

104 Aditya NULL A NULL

105 Nitya NULL B NULL

106 Anaya NULL C NULL

UPDATION Query :

syntax : UPDATE table_name


SET column1 = value1, column2 = value2, ...WHERE condition;
ROLL_NO NAME DEPT SEC PHONE

101 Alia CSE B 9876540010

102 Alisha ME A 9876541110

ROLL_NO NAME DEPT SEC PHONE

104 Amit NULL A NULL

105 Nitya NULL B NULL

106 Anaya NULL C NULL

• Updating multiple columns:

ROLL_NO NAME DEPT SEC PHONE

101 Alia CSE B 9876540010


102 Alisha ME A 9876541110

104 Amit NULL A NULL

105 Atulya ECE B NULL

106 Anaya NULL C NULL

DELETION Query :

syntax : DELETE FROM table_name WHERE condition;


• Deleting single record:

ROLL_NO NAME DEPT SEC PHONE

101 Alia CSE B 9876540010

102 Alisha ME A 9876541110

104 Amit NULL A NULL

105 Atulya ECE B NULL


ROLL_NO NAME DEPT SEC PHONE

101 Alia CSE B 9876540010

105 Atulya ECE B NULL

• Delete all of the records:

Que.4. Write the query to implement the concept of


Integrity constrains.
INTEGRITY CONSTRAINT : Integrity constraints are a set of rules. It is used to maintain
the quality of information.integrity constraint is used to guard against accidental damage to
the database.Constraints can be defined in two ways :-
1) The constraints can be specified immediately after the column definition. This is
called column-level definition.
2) The constraints can be specified after all the columns are defined. This is called
table-level definition.

DATABASE CONSTRAINTS

• Primary Key
• Foreign Key
• CHECK Constraint
• UNIQUE Constraint
• NOT NULL Constraint

PRIMARY KEY : A primary key is a column or a group of columns used to identify a row
uniquely in a table. A primary key constraint is the combination of a not-null
constraint and a UNIQUE constraint.

In case you want to specify the name of the primary key constraint, you use CONSTRAINT
clause as follows:

Define primary key when changing the existing table structure


FOREIGN KEY : Also called referential integrity . This constraint identifies any column
referencing the PRIMARY KEY in another table. For a column to be defined as a Foreign
Key, it should be a defined as a Primary Key in the table which it is referring. One or more
columns can be defined as Foreign key.

ALTER TABLE child_table


ADD CONSTRAINT constraint_name FOREIGN KEY (c1) REFERENCES parent_table (p1);

Drop existing foreign key constraint :


CHECK Constraint : This constraint defines a business rule on a column. All the rows must
satisfy this rule. The constraint can be applied for a single column or a group of columns.

UNIQUE Constraint : This constraint ensures that a column or a group of columns in each
row have a distinct value. A column(s) can have a null value but the values cannot be
duplicated.
NOT NULL Constraint : This constraint ensures all rows in the table contain a definite
value for the column which is specified as not null. Which means a null value is not
allowed.

Que.5. Apply the constrains like Primary key, Foreign


key, NOT NULL to the tables.
Constraints : SQL constraints are used to specify rules for data in a table.These are used to
limit the type of data that can go into a table. This ensures the accuracy and reliability of the
data in the database.Constraints could be either on a column level or a table level. The
column level constraints are applied only to one column, whereas the table level constraints
are applied to the whole table.Constraints can be divided into the following two types :

1. Column level constraints: Limits only column data.


2. Table level constraints: Limits whole table data.

Following are the most used constraints that can be applied to a table.

• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT
NOT NULL : NOT NULL constraint restricts a column from having a NULL value.
Once NOT NULL constraint is applied to a column, you cannot pass a null value to that
column. It enforces a column to contain a proper value.

UNIQUE : UNIQUE constraint ensures that a field or column will only have unique values.
A UNIQUE constraint field will not have duplicate data. This constraint can be applied at
column level or table level.

PRIMARY KEY : Uniquely identifies each row/record in a database table. A combination


of a NOT NULL and UNIQUE. this constraint is used to specify a field in a table as
primary key.

FOREIGN KEY : FOREIGN KEY is used to relate two tables. FOREIGN KEY constraint
is also used to restrict actions that would destroy links between tables. Foreign Key is a
field in a table which uniquely identifies each row of a another table

CREATE table bank(bank_id int PRIMARY KEY, emp_name varchar(60) NOT


NULL,stud_id int FOREIGN KEY REFERENCES college(coll_id));
Applying Foreign key constraint column wise :

ALTER table bank ADD FOREIGN KEY (coll_id) REFERENCES college(coll_id);


CHECK : CHECK constraint is used to restrict the value of a column between a range. It
performs check on the values, before storing them into the database. Its like condition
checking before saving data into a column.

CREATE table Student(stu_id int NOT NULL CHECK(stu_id >


0),Name varchar(60) NOT NULL,Age int);
Applying Check constraint column wise :

ALTER table Student ADD CHECK(stu_id > 0);


DEFAULT : Sets a default value for a column when no value is specified. This constraint
is used to provide a default value for the fields.

Que.6. Using the referential integrity constraints.


Referential Integrity Constraint : Referential Integrity is set of constraints applied to
foreign key which prevents entering a row in child table (where you have foreign key) for
which you don't have any corresponding row in parent table .Referential Integrity
prevents your table from having incorrect or incomplete relationship .

Referential Integrity example :

• Create Table command:

CREATE TABLE Dept (dept_id INT NOT NULL, dept_name


VARCHAR(256), PRIMARY KEY (dept_id)) ENGINE=INNODB;

CREATE TABLE Emp (emp_id INT NOT NULL, emp_name


VARCHAR(256), dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Dept(dept_id) )
ENGINE=INNODB;
• Insert Query :

insert into dept (dept_id,dept_name) values(101,"CSE"); insert into dept

(dept_id,dept_name) values(102,"EE"); insert into dept (dept_id,dept_name)

values(103,"ECE"); insert into dept (dept_id,dept_name) values(104,"ME"); insert into

dept (dept_id,dept_name) values(105,"IT");


insert into emp (emp_id,dept_id,emp_name) values(1,105,"amita"); insert into emp

(emp_id,dept_id,emp_name) values(2,104,"anita"); insert into emp (emp_id,dept_id,emp_name)

values(3,103,"anitya"); insert into emp (emp_id,dept_id,emp_name) values(4,102,"ananya"); insert into

emp (emp_id,dept_id,emp_name) values(5,101,"amaaya");


1.Prevents the entry of duplicate data

2.Prevents one table from pointing to a nonexistent field in another table

3.Guarantees consistency between "partnered" tables

4.Prevents the deletion of a record that contains a value referred to by a foreign key in
another table

5.Prevents the addition of a record to a table that contains a foreign key unless there is a
primary key in the linked table
Que.7. Write query for implementing the following
functions: MAX( ), MIN( ), AVG( ) and COUNT( ).
Aggregate Functions: Aggregate functions perform a calculation on a set of rows and return
a single row. You can use aggregate functions as expressions only in the following clauses
i.e., SELECT clause, HAVING clause. There are five aggregate functions:

• AVG() – returns the average value.


• COUNT() – returns the number of values.
• MAX() – returns the maximum value.
• MIN() – returns the minimum value.
• SUM() – returns the sum of all or distinct values.

AVG(): The AVG() function allows you to calculate the average value of a numeric column.

syntax : AVG(column)

COUNT(expression)
MAX() : It returns the maximum value from the specified table field.

syntax : MAX(expression)

SUM(): SUM function which returns the sum of all the values in the specified column. SUM
works on numeric fields only. Null values are excluded from the result returned.

syntax : SUM(expression)

These functions are called aggregate functions because they operate on the aggregate of
tuples. The result of an aggregate function is a single value.
Que.8. Perform the queries for triggers.
TRIGGERS
TRIGGERS : A SQL trigger is a database object just like a stored procedure, or we can say
it is a special kind of stored procedure which fires when an event occurs in a database. We
can execute a SQL query that will "do something" in a database when an event is fired.

Types of Triggers

1. DDL Trigger
2. DML Trigger

TRIGGERS PROCEDURES

They are automatically executed on occurrence of They can be executed whenever


specified event. required.

Triggers can't be called inside a procedure. But, you can call a procedure inside a
trigger.

We can not pass parameters to triggers. We can pass parameters to procedures.

Trigger never return value on execution. Procedure may return value/s on


execution.

Creating a trigger function :


[FOR [EACH] {ROW | STATEMENT}]

id first_name last_name

1 john doe
2 lily blush

WHERE ID = 2;

id first_name last_name

1 john doe

2 lily brown

Advantages of Triggers :
• Trigger generates some derived column values automatically
• Enforces referential integrity
• Event logging and storing information on table access
• Auditing
• Synchronous replication of tables
• Imposing security authorizations
• Preventing invalid transactions

Que.9. Write the queries to implement the joins.


SQL JOINS
JOINS : join is used to combine columns from one (self-join) or more tables based on the
values of the common columns between the tables. A JOIN is a means for combining fields
from two tables by using values common to each. There are four types of joins :-

• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN

sample tables :Suppose we have two tables called basket a and basket b that stores fruits:
SELECT a.id id_a, a.fruit fruit_a, b.id id_b, b.fruit fruit_b FROM basket_a a LEFT JOIN basket_b b ON
a.fruit = b.fruit;
FULL JOIN: returns rows when there is a match in one of the tables.

SELECT a.id id_a, a.fruit fruit_a, b.id id_b, b.fruit fruit_b FROM basket_a a FULL JOIN basket_b b ON
a.fruit = b.fruit WHERE a.id IS NULL OR b.id IS NULL;
Output :

Que.10. Write the query to create the views.


VIEWS
VIEW : Views are small part of sql . In SQL views are kind of virtual tables. A view also
has rows and columns as they are in a real table in the database. We can create a view by
selecting fields from one or more tables present in the database.

CREATE VIEW stu_View AS SELECT NAME, ADDRESS FROM college WHERE S_ID
syntax :
INSERT INTO view_name(column1, column2 , column3,..) VALUES(value1, value2,
value3..);

 Deleting a row from a View:

Que.11. Write the query for creating the users and their
roles.
How To Create a New User and their roles-
Create a New User :

mysql > CREATE USER 'newuser'@'localhost' IDENTIFIED BY


'password';
At this point new user has no permissions to do anything with the databases. In fact,
even if new user tries to login (with the password, password), they will not be able to
reach the MySQL shell. provide the user with access to the information :

asterisks in this command refer to the database and table that they can access .Once you
have finalized the permissions that you want to set up for your new users, always be sure to
reload all the privileges.Your changes will now be in effect.

How To Grant Different User Permissions

• ALL PRIVILEGES - as we saw previously, this would allow a MySQL user full
access to a designated database (or if no database is selected, global access across
the system)

• CREATE - allows them to create new tables or databases


• DROP - allows them to them to delete tables or databases

• DELETE - allows them to delete rows from tables

• INSERT - allows them to insert rows into tables

• SELECT - allows them to use the SELECT command to read through databases

• UPDATE - allow them to update table rows

• GRANT OPTION - allows them to grant or remove other users’ privileges

To provide a specific user with a permission, you can use this framework:

If you need to revoke a permission, the structure is almost identical to granting it:

You can review a user’s current permissions by running the following:

Just as you can delete databases with DROP, you can use DROP to delete a user altogether:

To test out your new user, log out by typing:

log back in with this command in terminal:


Q12. Explain E-R Model?
ER (Entity Relationship) Diagram in DBMS
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.
a. Weak Entity

An entity that depends on another entity called a weak entity. The weak entity
doesn't contain any key attribute of its own. The weak entity is represented by a
double rectangle.

2. Attribute
The attribute is used to describe the property of an entity. Eclipse is used to
represent an attribute.

For example, id, age, contact number, name, etc. can be attributes of a student.

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.

AD

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.

AD

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.

You might also like