DBMS Lab Manual 2023-24
DBMS Lab Manual 2023-24
System Lab
Credit
Teaching Scheme: Examination Scheme:
Scheme:
Practical (PR): 04 PR: 25 TW: 25
02 Credits
hrs/week Marks Marks
2023-2024
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 1 | 77
VISION
MISSION
1) To transform the students into innovative, competent and high
quality IT professionals to meet the growing global challenges.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 2 | 77
PROGRAM EDUCATIONAL OBJECTIVES
The students of the Information Technology course after passing out will:
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 3 | 77
PROGRAM OUTCOMES
The students in the Information Technology course are expected to know and be
able to:
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 4 | 77
PROGRAM SPECIFIC OUTCOMES
A graduate of the Information Technology Program will demonstrate-
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 5 | 77
DOCUMENT CONTROL
Document History
Revision
Revision Date Reason For Change
No.
1 01-07-2021 Update
2 01-07-2022 Update
3 01-01-2024 Updating the theory content of a few assignments
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 6 | 77
SYLLABUS
Savitribai Phule Pune University, Pune
Second Year Information Technology (2019 Course)
214456: Database Management System Lab
Prerequisite Courses:
Data structures and Software engineering principles and practices.
Course Objectives:
1. Understand the fundamental concepts of database management. These
concepts include aspects of database design, database languages, and
database-system implementation.
2. To provide a strong formal foundation in database concepts, recent
technologies and best industry practices.
3. To give systematic database design approaches covering conceptual design,
logical design and an overview of physical design.
4. To learn the SQL database system.
5. To learn and understand various Database Architectures and its use for
application development.
6. To program PL/SQL including stored procedures, stored functions, cursors
and packages.
Course Outcomes:
On completion of the course, students will be able to --
CO1: Install and configure database systems.
CO2: Analyze database models & entity relationship models.
CO3: Design and implement a database schema for a given problem-domain
CO4: Implement relational database systems.
CO5: Populate and query a database using SQL DDL / DML / DCL commands.
CO6: Design a backend database of any one organization: CASE STUDY
Guidelines for Instructor’s Manual
The faculty member should prepare the laboratory manual for all the
experiments and it should be made available to students and laboratory
instructor/Assistant.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 7 | 77
Guidelines for Student's Lab Journal
1. Student should submit term work in the form of journal with write-ups
based on specified list of assignments.
2. Practical and Oral Examination will be based on all the assignments in the
lab manual.
3. Candidate is expected to know the theory involved in the experiment.
4. The practical examination should be conducted only if the journal of the
candidate is complete in all respects
Guidelines for Oral/ Practical Assessment
1. Examiners will assess the student based on performance of students
considering the parameters such as timely conduction of practical
assignment, methodology adopted for implementation of practical
assignment, timely submission of assignment in the form of handwritten
write-up along with results of implemented assignment, attendance etc.
2. Examiners will judge the understanding of the practical performed in the
examination by asking some questions related to theory & implementation of
experiments he/she has carried out.
3. Appropriate knowledge of usage of software and hardware related to
respective laboratory should be checked by the concerned faculty member.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 9 | 77
GROUP A Study of MySQL Open source software. Discuss the
ASSIGNMENT NO.1 characteristics like efficiency, scalability, performance
and transactional properties.
GIVEN DATE:
SUBMISSION DATE:
SIGN. OF FACULTY:
ASSIGNMENT NO.: 01
AIM:
Study of MySQL Open source software. Discuss the characteristics like
efficiency, scalability, performance and transactional properties.
PREREQUISITE:
1. Data Structures
2. Software Engineering Principles
OBJECTIVES:
1. To study of different type of SQL Databases.
2. To study of advantages of various SQL Databases.
3. To compare the different database systems based on points like
efficiency, scalability, characteristics and performance.
THEORY:
What is Database?
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 10 | 77
Typically, a DBMS provides the following facilities:
Advantages of DBMS:
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 11 | 77
Disadvantages of DBMS:
1. Complexity
2. Size
3. Additional hardware costs
4. Cost of DBMS
5. Performance
6. Higher impact of failure
RDBMS Terminology:
MySQL Database:
Types of Databases:
1. SQL:
SQL stands for Structured Query Language. It is used for storing and
managing data in relational database management system (RDMS). It is a
standard language for Relational Database System. It enables a user to
create, read, update and delete relational databases and tables. All the
RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use
SQL as their standard database language. SQL allows users to query the
database in a number of ways, using English-like statements.
2. PostgreSQL:
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 13 | 77
PostgreSQL is ACID-compliant and transactional. PostgreSQL has
updatable views and materialized views, triggers, foreign keys; supports
functions and stored procedures, and other expandability.
3. MySQL:
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
QUESTIONS:
Write the answers of following questions.
OUTPUT:
(Attach Screenshots of your output in sequence)
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 14 | 77
GROUP A Install and configure client and server of
ASSIGNMENT NO.2 MySQL.(Show all commands and necessary steps for
installation and configuration)
GIVEN DATE:
SUBMISSION DATE:
SIGN. OF FACULTY:
ASSIGNMENT NO.: 02
AIM:
Install and configure client and server of MySQL. (Show all commands
and necessary steps for installation and configuration)
PREREQUISITE:
1. Knowledge of MySQL Databases.
OBJECTIVES:
1. To study of installation of client and server of MySQL.
THEORY:
What is MySQL client?
MySQL client is a common name for tools that are designed to connect to
MySQL Server. Client programs are used to send commands or queries to
the server and allow managing data in the databases stored on the server.
You can use the MySQL client to send commands to any MySQL server;
on a remote computer or your own. The MySQL clients purpose is to allow
you to use that query interface.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 15 | 77
other applications, such as FTP, email and web servers. MySQL server is
configured to use an appropriate proportion of system resources.
The MySQL server package will install the MySQL database server which
you can interact with using a MySQL client. The MySQL server is used to
persist the data and provide a query interface for it (SQL).
1. To install it, update the package index on your server with apt
$ sudo mysql_secure_installation
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 16 | 77
QUESTIONS:
Write the answers of following questions.
1. What are the steps for installation of MySQL Client and server?
2. What is MySQL Client and MySQL Server?
OUTPUT:
(Attach Screenshots of your output in sequence)
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 17 | 77
GROUP A Study of SQLite: What is SQLite? Uses of Sqlite.
ASSIGNMENT NO.3 Building and installing SQLite.
GIVEN DATE:
SUBMISSION DATE:
SIGN. OF FACULTY:
ASSIGNMENT NO.: 03
AIM:
Study of SQLite: What is SQLite? Uses of Sqlite. Building and installing
SQLite.
PREREQUISITE:
1. Knowledge of MySQL Databases.
OBJECTIVES:
1. To study of installation of SQLite.
THEORY:
What is SQLite?
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 18 | 77
Steps for installation of SQLite on Linux:
2. Ensure that the server is running using the systemctl start command
$ sqlite3 --version
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 19 | 77
QUESTIONS:
Write the answers of following questions.
1. What is SQLite?
2. What is Difference between SQL and SQLite?
3. What are four difference features of SQLite?
4. What are features of SQLite.
OUTPUT:
(Attach Screenshots of your output in sequence)
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 20 | 77
GROUP B Design any database with at least 3 entities and
ASSIGNMENT NO.1 relationships between them. Draw suitable ER/EER
diagram for the system.
GIVEN DATE:
SUBMISSION DATE:
SIGN. OF FACULTY:
ASSIGNMENT NO.: 01
AIM:
Design any database with at least 3 entities and relationships between
them. Draw suitable ER/EER diagram for the system.
PREREQUISITE:
1. Knowledge of MySQL Databases.
OBJECTIVES:
1. To understand the ER diagram & EER diagram.
THEORY:
What is ER diagram?
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 21 | 77
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.
Components of ER Diagram:
ER Model
Entity Attribute Relation
Notations of ER Model:
Sr.
Figures Symbols Description
No.
Relationships among
3 Diamond
Entities
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 22 | 77
Attributes to Entities and
5 Line Entity Sets with other
Relationship types
Double
6 Multi-valued Attributes
Ellipse
Double
7 Weak Entity
Rectangle
Entity:
An Entity may be an object with a physical existence – a particular
person, car, house, or employee – or it may be an object with a conceptual
existence – a company, a job, or a university course
Entity Set:
An Entity is an object of Entity Type and a set of all entities is called an
entity set. For Example, E1 is an entity having Entity Type Student and
the set of all students is called Entity Set.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 23 | 77
1. Strong Entity:
A Strong Entity is a type of entity that has a key Attribute. Strong Entity
does not depend on other Entity in the Schema. It has a primary key, that
helps in identifying it uniquely, and it is represented by a rectangle. These
are called Strong Entity Types.
2. Weak Entity:
An Entity type has a key attribute that uniquely identifies each entity in
the entity set. But some entity type exists for which key attributes can’t
be defined. These are called Weak Entity types.
For Example:
A company may store the information of dependents (Parents, Children,
Spouse) of an Employee. But the dependents don’t have existed without
the employee. So Dependent will be a Weak Entity Type and Employee
will be Identifying Entity type for Dependent, which means it is Strong
Entity Type.
A weak entity type is represented by a Double Rectangle. The
participation of weak entity types is always total. The relationship
between the weak entity type and its identifying strong entity type is
called identifying relationship and it is represented by a double diamond.
Attributes:
Attributes are the properties that define the entity type. For example,
Roll_No, Name, DOB, Age, Address, and Mobile_No are the attributes that
define entity type Student. In ER diagram, the attribute is represented by
an oval.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 24 | 77
1. Key Attribute:
The attribute which uniquely identifies each entity in the entity set is
called the key attribute. For example, Roll_No will be unique for each
student. In ER diagram, the key attribute is represented by an oval with
underlying lines.
2. Composite Attribute:
An attribute composed of many other attributes is called a composite
attribute. For example, the Address attribute of the student Entity type
consists of Street, City, State, and Country. In ER diagram, the composite
attribute is represented by an oval comprising of ovals.
3. Multivalued Attribute:
An attribute consisting of more than one value for a given entity. For
example, Phone_No (can be more than one for a given student). In ER
diagram, a multivalued attribute is represented by a double oval.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 25 | 77
4. Derived Attribute:
An attribute that can be derived from other attributes of the entity type is
known as a derived attribute. e.g.; Age (can be derived from DOB). In ER
diagram, the derived attribute is represented by a dashed oval.
The Complete Entity Type Student with its Attributes can be represented
as:
Cardinality:
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 26 | 77
Sr. Cardinality
Description Sets
No. Type
In one-to-many mapping as
well where each entity can
be related to more than one
relationship and the total
2 One-to-Many number of tables that can
be used in this is 2. total
number of tables that can
used is 3.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 27 | 77
How to Draw ER Diagram?
• The very first step is Identifying all the Entities, and place them in a
Rectangle, and labeling them accordingly.
• The next step is to identify the relationship between them and pace
them accordingly using the Diamond, and make sure that,
Relationships are not connected to each other.
• Attach attributes to the entities properly.
• Remove redundant entities and relationships.
• Add proper colors to highlight the data present in the database.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 28 | 77
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
QUESTIONS:
Write the answers of following questions.
OUTPUT:
(Attach Screenshots of your output in sequence)
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 29 | 77
GROUP B Design and implement a database (for assignment no
ASSIGNMENT NO.2 1) using DDL statements and apply normalization on
them.
START DATE:
SUBMISSION DATE:
SIGN. OF FACULTY:
ASSIGNMENT NO.: 02
AIM:
Design and implement a database (for assignment no 1) using DDL
statements and apply normalization on them.
PREREQUISITE:
1. Data Structures
2. Software Engineering Principles
OBJECTIVES:
1. To study Data Definition Language with SQL Commands.
2. To understand concept of normalization.
THEORY:
What is SQL?
SQL stands for Structured Query Language. It is used for storing and
managing data in relational database management system (RDMS). It is a
standard language for Relational Database System. It enables a user to
create, read, update and delete relational databases and tables. All the
RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use
SQL as their standard database language.
SQL allows users to query the database in a number of ways, using
English-like statements.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 30 | 77
like creating tables, adding data to tables, modifying data, and dropping
tables.
SQL Rules:
What is DDL:
1. CREATE Command
2. DROP Command
3. ALTER Command
4. TRUNCATE Command
5. RENAME Command
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 31 | 77
1. CREATE command:
CREATE command is used to create databases, tables,
Description:
triggers and other database objects.
❖ Syntax to Create a Database:
CREATE Database Database_Name;
❖ Syntax to create a new table:
CREATE TABLE table_name
Syntax: (
column_Name1 data_type ( size of the column ) ,
column_Name2 data_type ( size of the column) ,
...
column_NameN data_type ( size of the column )
);
2. DROP command:
DROP command is used to delete/remove the database
Description: objects from the SQL database.
❖ Syntax to remove a Database:
DROP Database Database_Name;
❖ Syntax to remove a table:
Syntax: DROP TABLE Table_Name;
❖ Syntax to remove an index:
DROP INDEX Index_Name;
3. ALTER command:
ALTER command changes or modifies the existing
structure of the database, and it also changes the
Description: schema of database objects.
We can also add and drop constraints of the table using
the ALTER command.
❖ Syntax to add new field in table :
ALTER TABLE table_Name ADD col_name col_definition;
❖ Syntax to remove existing column from table:
Syntax: ALTER TABLE table_name DROP col_name_1, col_name_2;
❖ Syntax to modify the column of the table:
ALTER TABLE table_name MODIFY(col_name col_datatype
(size));
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 32 | 77
4. TRUNCATE command:
TRUNCATE is another DDL command which deletes or
removes all the records from the table.
Description:
This command also removes the space allocated for
storing the table records.
❖ Syntax:
Syntax:
TRUNCATE Table_Name;
5. RENAME command:
❖ Syntax:
Syntax:
RENAME TABLE Old_Table_Name To New_Table_Name;
What is Normalization?
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 33 | 77
Advantages of Normalization:
Disadvantages of Normalization:
1. You cannot start building the database before knowing what the
user needs.
2. The performance degrades when normalizing the relations to higher
normal forms, i.e., 4NF, 5NF.
3. It is very time-consuming and difficult to normalize relations of a
higher degree.
4. Careless decomposition may lead to a bad database design, leading
to serious problems.
Data modification anomalies:
Sr. Normal
Description
No Form
1 1NF • A relation is in 1NF if it contains an atomic value.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 34 | 77
• Eliminating repeating groups.
• A relation will be in 2NF if it is in 1NF and all non-
key attributes are fully functional dependent on
2 2NF
the primary key.
• Eliminate partial functional dependency
• A relation will be in 3NF if it is in 2NF and no
3 3NF transition dependency exists.
• Eliminate transitive dependency.
• A stronger definition of 3NF is known as Boyce
4 BCNF
Codd's normal form.
• A relation will be in 4NF if it is in Boyce Codd's
5 4NF normal form and has no multi-valued dependency.
• Eliminate multi-valued dependency
• A relation is in 5NF. If it is in 4NF and does not
contain any join dependency, joining should be
6 5NF
lossless.
• Eliminate join dependency
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
QUESTIONS:
Write the answers of following questions.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 35 | 77
7. Explain Third Normal(3NF) form with example.
8. Explain BCNF with example.
9. Explain use of DDL commands and show it practically.
10. Explain fifth Normal form with example.
OUTPUT:
(Attach Screenshots of your output in sequence)
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 36 | 77
GROUP B Create Table with primary key and foreign key
ASSIGNMENT NO.3 constraints.
a. Alter table with add n modify b. Drop table
START DATE:
SUBMISSION DATE:
SIGN. OF FACULTY:
ASSIGNMENT NO.: 03
AIM:
Create Table with primary key and foreign key constraints.
a. Alter table with add n modify
b. Drop table
PREREQUISITE:
1. Data Structures
2. Software Engineering Principles
OBJECTIVES:
1. To study Primary key and foreign key constraints
2. To study alter and drop commands.
OUTCOMES:
1. Analyze database models & entity relationship models.
2. Design and implement a database schema for a given problem-
domain
3. Implement relational database systems.
4. Populate and query a database using SQL DDL / DML / DCL
commands.
THEORY:
What is Constraints?
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 37 | 77
there is any violation between the constraint and the data action, the
action is aborted. Constraints can be column level or table level.
Constraints can be column level or table level. Column level constraints
apply to a column, and table level constraints apply to the whole table.
The following constraints are commonly used in SQL:
Sr.
Constraint Description
No
1 NOT NULL Ensures that a column cannot have a NULL value
By default, a column can hold NULL values. The NOT NULL constraint
enforces a column to NOT accept NULL values. This enforces a field to
always contain a value, which means that you cannot insert a new record,
or update a record without adding a value to this field.
The following SQL ensures that the "ID", "LastName", and "FirstName"
columns will NOT accept NULL values when the "Students" table is
created:
Example:
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 38 | 77
SQL NOT NULL on ALTER TABLE:
To create a NOT NULL constraint on the "Age" column when the "Persons"
table is already created, use the following SQL:
Example:
The UNIQUE constraint ensures that all values in a column are different.
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns. A PRIMARY KEY constraint
automatically has a UNIQUE constraint. However, you can have
many UNIQUE constraints per table, but only one PRIMARY KEY constraint
per table.
The following SQL creates a UNIQUE constraint on the "ID" column when
the "Students" table is created:
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 39 | 77
CONSTRAINT UC_Student UNIQUE (ID,LastName)
);
The following SQL creates a PRIMARY KEY on the "ID" column when the
"Students" table is created:
To create a PRIMARY KEY constraint on the "ID" column when the table is
already created, use the following SQL:
If you use ALTER TABLE to add a primary key, the primary key column(s)
must have been declared to not contain NULL values (when the table was
first created).
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 41 | 77
A FOREIGN KEY is a field (or collection of fields) in one table, that refers
to the PRIMARY KEY in another table.
The table with the foreign key is called the child table, and the table with
the primary key is called the referenced or parent table.
Orders Table:
OrderID OrderNumber PersonID
1 77895 3
2 44678 3
3 22456 2
Notice that the "PersonID" column in the "Orders" table points to the
"PersonID" column in the "Persons" table.
The "PersonID" column in the "Persons" table is the PRIMARY KEY in the
"Persons" table.
The FOREIGN KEY constraint prevents invalid data from being inserted
into the foreign key column, because it has to be one of the values
contained in the parent table.
The following SQL creates a FOREIGN KEY on the "PersonID" column when
the "Orders" table is created:
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 43 | 77
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
QUESTIONS:
Write the answers of following questions.
OUTPUT:
(Attach Screenshots of your output in sequence)
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 44 | 77
Perform following SQL queries on the database
created in assignment 1.
1. Implementation of relational operators in SQL
GROUP B 2. Boolean operators and pattern matching
ASSIGNMENT NO.4 3. Arithmetic operations and built in functions
4. Group functions
5. Processing Date and Time functions
6. Complex queries and set operators
START DATE:
SUBMISSION DATE:
SIGN. OF FACULTY:
ASSIGNMENT NO.: 04
AIM:
Perform following SQL queries on the database created in assignment 1.
1. Implementation of relational operators in SQL
2. Boolean operators and pattern matching
3. Arithmetic operations and built in functions
4. Group functions
5. Processing Date and Time functions
6. Complex queries and set operators
PREREQUISITE:
1. Data Structures
2. Operators
OBJECTIVES:
1. To understand the relational operators, Boolean operators and
pattern matching & Arithmetic operations.
2. To Understand the built in functions, Group functions, Date and
Time functions, Complex queries and set operators.
THEORY:
SQL commands are instructions. It is used to communicate
with the database. It is also used to perform specific tasks, functions, and
queries of data.
BOOLEAN OPERATOR
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 45 | 77
A Boolean is a universal data type which stores true or false values. It is
used when we define a variable in a column of the table.
For instance, a customer wants a list of all the red cars. So, we can find
this using the BOOLEAN operator as given in the below table −
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 46 | 77
Comparision
Description
Operators
For example:
to display all the names with 'a' second character,
SELECT first_name, last_name
FROM student_details
WHERE first_name LIKE '_a%';
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 47 | 77
SQL IN Operator:
The IN operator is used when you want to compare a column with more
than one value. It is similar to an OR condition.
For example: I
f you want to find the names of students who are studying either Maths
or Science, the query would be like,
SELECT first_name, last_name, subject
FROM student_details
WHERE subject IN ('Maths', 'Science');
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 48 | 77
Sr. Function & Description
No.
APPROX_COUNT_DISTINCT()
1 Returns the approximate number of rows with distinct
expression values.
AVG()
2 Returns the average of the fields in a particular column that
has only numeric values.
CHECKSUM_AGG()
3
Returns the checksum value.
COUNT_BIG()
4 Returns the count of the number of items or rows selected by
the select statement.
COUNT()
5 Returns the number of non-NULL values in a particular
column.
GROUPING()
6 Returns whether the specified column expression in a group
by list is aggregate or not.
GROUPING_ID()
7
Returns the level of grouping.
MAX()
8 Returns the maximum value among the fields in a particular
column.
MIN()
9
Returns the minimum value among the fields in a particular
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 49 | 77
column.
STDEV()
10 Returns the statistical standard deviation of the fields
(numerical values) in a particular column.
STDEVP()
11 Returns the population standard deviation for the fields
(numerical values) in a particular column.
SUM()
12 Returns the sum of all the fields (numeric) in a particular
column.
VAR()
13 Returns the statistical standard variance of the fields
(numerical values) in a particular column.
VARP()
14 Returns the population standard variance of the fields
(numerical values) in a particular column.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 50 | 77
• UTC_TIME() – Return the current UTC time.
Converting Functions
The extraction functions allow you to extract date and time components
from a date and time.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 51 | 77
• MONTH() – Return the month component of a date.
• WEEK() – Return the week component of a date.
• WEEKDAY() – Return the weekday index of a date.
• WEEKOFYEAR() – Return the calendar week of the date (1-53) –
equivalent to WEEK(date, 3).
• DAY() – Return the day of the month for a specific date (1-31).
DAYOFMONTH is the synonym for DAY.
• DAYOFYEAR() – Return the day of the year (1-366).
• DAYOFWEEK() – Return the day of the week (1-7).
• HOUR() – Return the hour for a time.
• MINUTE() – Return the minute for a time.
• SECOND() – Return the second for a time.
• LAST_DAY() – Return an integer that represents the last day of the month
for a specific date
This section shows you how to use functions to get the month and day
names.
This section covers the function that manipulates the periods in the
format YYMM or YYMMMM.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 52 | 77
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
QUESTIONS:
Write the answers of following questions.
OUTPUT:
(Attach Screenshots of your output in sequence
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 53 | 77
Execute DDL/DML statements which demonstrate
the use of views. Update the base table using its
GROUP B
corresponding view. Also consider restrictions on
ASSIGNMENT NO.5 updatable views and perform view creation from
multiple tables.
START DATE:
SUBMISSION DATE:
SIGN. OF FACULTY:
ASSIGNMENT NO.: 05
AIM:
Execute DDL/DML statements which demonstrate the use of views.
Update the base table using its corresponding view. Also consider
restrictions on updatable views and perform view creation from multiple
tables.
PREREQUISITE:
1. Data Structures
2. Software Engineering Principles
OBJECTIVES:
1. To understand DDL and DML commands.
2. To understand views and its use.
THEORY:
What is View?
Disadvantages of View:
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 55 | 77
Syntax:
Where
view_name: Name for the View
table_name: Name of the table
condition: Condition to select rows
Sample table:
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 56 | 77
A. Creating View:
Example:
To see the data in the View, we can query the view in the same manner
as we query a table.
Query:
Output:
NAME ADDRESS
Harsh Kolkata
Suresh Durgapur
Pratik Delhi
Pratik Delhi
Dhanraj Bihar
Query:
To see the data in the View, we can query the view in the same manner
as we query a table.
Query:
SELECT * FROM StudentNames;
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 57 | 77
S_ID NAME
2 Ashish
4 Dhanraj
1 Harsh
3 Pratik
5 Ram
Query:
Query:
SELECT * FROM MarksView;
Output:
We can list View using the SHOW FULL TABLES statement or using the
information_schema table. A View can be created from a single table or
multiple tables.
Syntax (Using SHOW FULL TABLES):
use "database_name";
show full tables where table_type like "%VIEW";
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 58 | 77
Syntax (Using information_schema) :
C. Deleting Views:
For example:
If we want to delete the View MarksView, we can do this as: DROP VIEW
MarksView;
D. Updating Views:
There are certain conditions needed to be satisfied to update a view. If
any one of these conditions is not met, then we will not be allowed to
update the view.
1. The SELECT statement which is used to create the view should not
include GROUP BY clause or ORDER BY clause.
2. The SELECT statement should not have the DISTINCT keyword.
3. The View should have all NOT NULL values.
4. The view should not be created using nested queries or complex
queries.
5. The view should be created from a single table. If the view is
created using multiple tables then we will not be allowed to update
the view.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 59 | 77
For example:
If we want to update the view MarksView and add the field AGE to this
View from StudentMarks Table, we can do this as
CREATE OR REPLACE VIEW MarksView AS SELECT
StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS,
StudentMarks.AGE FROM StudentDetails, StudentMarks WHERE
StudentDetails.NAME = StudentMarks.NAME;
Output:
Uses of a View:
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 60 | 77
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
QUESTIONS:
Write the answers of following questions.
OUTPUT:
(Attach Screenshots of your output in sequence)
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 61 | 77
Write and execute PL/SQL stored procedure and
GROUP C
function to perform a suitable task on the
ASSIGNMENT NO.1 database. Demonstrate its use.
START DATE:
SUBMISSION DATE:
SIGN. OF FACULTY:
ASSIGNMENT NO.: 01
AIM:
Write and execute PL/SQL stored procedure and function to perform a
suitable task on the database. Demonstrate its use.
PREREQUISITE:
1. Basic Knowledge of procedure and functions.
2. Knowledge of SQL queries.
OBJECTIVES:
1. To understand Concept of PL/SQL stored procedure and function
and its use.
THEORY:
What is PL/SQL?
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 62 | 77
Basics of PL/SQL:
Syntax of PL/SQL:
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 63 | 77
Sr.
Section Description
No.
This section starts with the keyword DECLARE. It is
an optional section and defines all variables, cursors,
1 Declarations
subprograms, and other elements to be used in the
program.
This section is enclosed between the
keywords BEGIN and END and it is a mandatory
section. It consists of the executable PL/SQL
Executable
2 statements of the program. It should have at least
Commands
one executable line of code, which may be just
a NULL command to indicate that nothing should be
executed.
This section starts with the keyword EXCEPTION.
Exception
3 This optional section contains exception(s) that
Handling
handle errors in the program.
Example:
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
The end; line signals the end of the PL/SQL block. To run the code from
the SQL command line, you may need to type / at the beginning of the
first blank line after the last line of the code. When the above code is
executed at the SQL prompt, it produces the following result
Hello World
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 64 | 77
The PL/SQL Delimiters:
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 65 | 77
QUESTIONS:
Write the answers of following questions.
OUTPUT:
(Attach Screenshots of your output in sequence)
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 66 | 77
GROUP C Write and execute suitable database triggers.
ASSIGNMENT NO.2 Consider row level and statement level triggers.
START DATE:
SUBMISSION DATE:
SIGN. OF FACULTY:
ASSIGNMENT NO.: 02
AIM:
Write and execute suitable database triggers. Consider row level and
statement level triggers.
PREREQUISITE:
OBJECTIVES:
1. To understand the concept of database MySQL Trigger
THEORY:
Introduction to MYSQL Trigger
What is a Trigger?
Types of triggers:
There are two types of triggers based on the which level it is triggered.
1. Row level trigger: An event is triggered for each row updated,
inserted or deleted.
2. Statement level trigger: An event is triggered for each sql statement
executed.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 67 | 77
Trigger Execution Hierarchy:
Syntax of Trigger
• [OF col_name] - This clause is used with update triggers. This clause
is used when you want to trigger an event only when a specific
column is updated.
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 68 | 77
• CREATE [OR REPLACE ] TRIGGER trigger_name - This clause
creates a trigger with the given name or overwrites an existing
trigger with the same name.
• WHEN (condition) - This clause is valid only for row level triggers.
The trigger is fired only for rows that satisfy the condition specified.
Trigger Examples:
Example 1:
create a trigger that may insert a tuple into T5 when a tuple is inserted
into T4. inserts the reverse tuple into T5:
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 69 | 77
Example 2:
The price of a product changes constantly. It is important to maintain
the history of the prices of the products. Create a trigger to update the
'product_price_history' table when the price of the product is updated in
the 'product' table.
Create the 'product' table and 'product_price_history' table
Once the above update query is executed, the trigger fires and updates
the 'product_price_history' table.
Example 3:
create table account(accno int, amount int);
Create a trigger on account table before update in new inserted amount
is less than “0” then set amount “0” else if amount is greater than 100
then set amount 100
CREATE TRIGGER upd_check BEFORE UPDATE ON account
FOR EACH ROW
BEGIN
IF NEW.amount < 0 THEN
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 70 | 77
SET NEW.amount = 0;
ELSEIF NEW.amount > 100 THEN
SET NEW.amount = 100;
END IF;
END
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
QUESTIONS:
Write the answers of following questions.
OUTPUT:
(Attach Screenshots of your output in sequence)
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 71 | 77
GROUP C
Write a PL/SQL block to implement cursors.
ASSIGNMENT NO. 3
START DATE:
SUBMISSION DATE:
SIGN. OF FACULTY:
ASSIGNMENT NO.: 03
AIM:
Write a PL/SQL block to implement cursors.
PREREQUISITE:
OBJECTIVES:
1. To understand the basic concept of cursors used in PL/SQL.
THEORY:
Introduction to Cursor
What is a Cursor?
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 72 | 77
Properties of Cursors:
Types of cursors:
1. Implicit cursors:
Attribute Description
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 73 | 77
Returns the number of rows affected by an INSERT, UPDATE,
%ROWCOUNT or DELETE statement, or returned by a SELECT INT O
statement.
2. Explicit cursors:
Explicit cursors are programmer defined cursors for gaining more control
over the context area. An explicit cursor should be defined in the
declaration section of the PL/SQL Block.
Syntax:
Then, we can use the FETCH statement to retrieve the current row
pointed by the cursor, and with each FETCH, the cursor moves to the
next row in the result set. This allows us to process each row one by one.
Syntax:
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 74 | 77
Close Cursor Statement:
Once all the rows are fetched, we must close the cursor to release the
memory associated with it. We can do this using the CLOSE statement.
Syntax:
CLOSE cursor_name;
Example:
Now, let us insert some records into the CUSTOMERS table using
the INSERT statement as follows:
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 75 | 77
Here, we are creating a stored procedure named FetchCustomers to fetch
customer names from the CUSTOMERS table and inserting them one by
one into the BACKUP table. We are using a cursor to iterate through the
rows and a handler to detect the end of the result-set, ensuring all names
are processed –
DELIMITER //
CREATE PROCEDURE FetchCustomers()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE customer_id INT;
DECLARE customer_name VARCHAR(255);
DECLARE auto_id INT;
-- Declare cursor
DECLARE MY_CURSOR CURSOR FOR
SELECT id, name FROM CUSTOMERS;
-- Open cursor
OPEN MY_CURSOR;
-- Close cursor
CLOSE MY_CURSOR;
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 76 | 77
END //
DELIMITER ;
You can verify the contents of the CUSTOMERS_BACKUP table using the
SELECT statement as shown below −
ID NAME
1 Ramesh
2 Khilan
3 Kaushik
4 Chaitali
CONCLUSION:
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
__________________________________________________________________________________
QUESTIONS:
Write the answers of following questions.
OUTPUT:
(Attach Screenshots of your output in sequence)
D e p a r t m e n t o f I n f o r m a t i o n T e c h n o l o g y P a g e 77 | 77