0% found this document useful (0 votes)
8 views12 pages

Lab Manual-DBMS

The document outlines a series of experiments focused on database design and SQL operations, including creating Entity-Relationship (ER) models, converting them to relational models, and implementing various SQL commands such as DDL and DML. It covers theoretical concepts like attributes, relationships, and constraints, as well as practical procedures for executing SQL queries and performing join operations. Each experiment concludes with a requirement for screenshots and personal conclusions on the outcomes.
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)
8 views12 pages

Lab Manual-DBMS

The document outlines a series of experiments focused on database design and SQL operations, including creating Entity-Relationship (ER) models, converting them to relational models, and implementing various SQL commands such as DDL and DML. It covers theoretical concepts like attributes, relationships, and constraints, as well as practical procedures for executing SQL queries and performing join operations. Each experiment concludes with a requirement for screenshots and personal conclusions on the outcomes.
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/ 12

Experiment No.

01

Aim:
Identify the case study and detail statement of problem. Design an Entity-Relationship (ER) / Extended
Entity- Relationship (EER) Model.

Theory:
An Entity Relationship Diagram (ERD) is a visual representation of different data using conventions that
describe how these data are related to each other. For example, the elements writer, novel, and
consumer may be described using ER diagrams this way:

There are three basic elements in an ER Diagram: entity, attribute, relationship. There are more
elements which are based on the main elements. They are weak entity, multivalued attribute, derived
attribute, weak relationship and recursive relationship. Cardinality and ordinality are two other
notations used in ER diagrams to further define relationships.
Entity
An entity can be a person, place, event, or object that is relevant to a given system. For example, a
school system may include students, teachers, major courses, subjects, fees, and other items. Entities
are represented in ER diagrams by a rectangle and named using singular nouns.
Weak Entity
A weak entity is an entity that depends on the existence of another entity. In more technical terms it
can defined as an entity that cannot be identified by its own attributes. It uses a foreign key combined
with its attributed to form the primary key. An entity like order item is a good example for this. The
order item will be meaningless without an order so it depends on the existence of order.

Attribute
An attribute is a property, trait, or characteristic of an entity, relationship, or another attribute. For
example, the attribute Inventory Item Name is an attribute of the entity Inventory Item. An entity can
have as many attributes as necessary. Meanwhile, attributes can also have their own specific
attributes. For example, the attribute “customer address” can have the attributes number, street, city,
and state. These are called composite attributes. Note that some top level ER diagrams do not show
attributes for the sake of simplicity. In those that do, however, attributes are represented by oval
shapes.

Multivalued Attribute
If an attribute can have more than one value it is called a multivalued attribute. It is important to note
that this is different to an attribute having its own attributes. For example a teacher entity can have
multiple subject values.
Derived Attribute
An attribute based on another attribute. This is found rarely in ER diagrams. For example for a circle
the area can be derived from the radius.

Relationship
A relationship describes how entities interact. For example, the entity “carpenter” may be related to
the entity “table” by the relationship “builds” or “makes”. Relationships are represented by diamond
shapes and are labeled using verbs.

Generalization
A generalization hierarchy is a form of abstraction that specifies that two or more entities that share
common attributes can be generalized into a higher-level entity type called a super type or generic
entity. The lower level of entities becomes the subtypes, or categories, to the super type. Subtypes are
dependent entities.It makes ER diagram simpler because shared attributes are not repeated.
Generalization is denoted through a triangular component labeled ‘IS A’ as shown in fig. In fig, Account
is the higher-level entity set and saving account and current account are lower-level entity sets.

Specialization:
Specialization is the process of taking subsets of the higher-level entity set to form lower-level entity
sets. It is a process of defining a set of subclasses of an entity type, which is called the super class of
the specialization. The process of defining subclass is based on the basis of some distinguish
characteristics of the entities in the super class.

Aggregation:
Aggregation is the process of compiling information on an object, thereby abstracting a higher-level
object. One limitation of the E-R model is that it cannot express relationships among relationships.
To illustrate the need for such a construct, consider the ternary relationship works-on, between
employee, branch and job as shown in fig. The best way to model a situation like this is by the use of
aggregation. Thus the relationship set works-on relating the entity sets employee, branch and job is a
higher-level entity set. Such an entity set is treated in the same manner, as is any other entity set. We
can then create a binary relationship manages between works-on and manager to represent who
manages what task.

Procedure:
1.​ Identify any real world problem statement.
2.​ Draw EER diagram for the topic selected using any software tool.

Output:
Paste the screenshot of the output screen.

Conclusion:
Insert your own conclusion for the experiment.

Experiment No. 02
Aim:
Design a Relational Model based on Entity-Relationship (ER) / Extended Entity-Relationship (EER)
Model.

Theory:
What is Relational Model?
Relational Model represents how data is stored in Relational Databases. A relational database stores
data in the form of relations (tables). Consider a relation STUDENT with attributes ROLL_NO, NAME,
ADDRESS, PHONE and AGE shown in Table.
STUDENT
ROLL_NO NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18
2 RAMESH GURGAON 9652431543 18
3 SUJIT ROHTAK 9156253131 20
4 SURESH DELHI 18
Attribute: Attributes are the properties that define a relation. e.g.; ROLL_NO, NAME
Relation Schema: A relation schema represents name of the relation with its attributes. e.g.;
STUDENT (ROLL_NO, NAME, ADDRESS, PHONE and AGE) is relation schema for STUDENT. If a schema
has more than 1 relation, it is called Relational Schema.
Tuple: Each row in the relation is known as tuple.
Relation Instance: The set of tuples of a relation at a particular instance of time is called as relation
instance. Table 1 shows the relation instance of STUDENT at a particular time. It can change whenever
there is insertion, deletion or updation in the database.
Degree: The number of attributes in the relation is known as degree of the relation. The STUDENT
relation defined above has degree 5.
Cardinality: The number of tuples in a relation is known as cardinality. The STUDENT relation defined
above has cardinality 4.
Column: Column represents the set of values for a particular attribute. The column ROLL_NO is
extracted from relation STUDENT.
Constraints in Relational Model
While designing Relational Model, we define some conditions which must hold for data present in
database are called Constraints. These constraints are checked before performing any operation in
database. If there is a violation in any of constrains, operation will fail.
Domain Constraints: These are attribute level constraints. An attribute can only take values which lie
inside the domain range. e.g. If a constraint AGE>0 is applied on STUDENT relation, inserting negative
value of AGE will result in failure.
Key Integrity: Every relation in the database should have at least one set of attributes which defines
a tuple uniquely. Those set of attributes is called key. e.g.; ROLL_NO in STUDENT is a key. No two
students can have same roll number. So a key has two properties: It should be unique for all tuples. It
can’t have NULL values.
Referential Integrity: When one attribute of a relation can only take values from other attribute of
same relation or any other relation, it is called referential integrity.

Procedure:
1.​ Convert the EER model obtained in experiment 1 into Relational model using the 8 steps of
conversion.
2.​ Draw this relational model in any software tool.

Output:
Paste the screenshot of the output screen.

Conclusion:
Insert your own conclusion for the experiment.

Experiment No. 03
Aim:
To implement DDL commands.

Theory:
DDL is used to:
Create an object
Alter the structure of an object
To drop the object created.
The commands used are: Create, Alter, Drop, Truncate
CREATE TABLE
It is used to create a table.
Syntax: Create table tablename (column_name1 data_ type constraints,column_name2 data_ type
constraints …)
Example: create table student (name varchar(10),roll_no number(10),
branch varchar(10),address varchar(10));
ALTER TABLE
Alter command is used to:
1. Add a new column.
2. Modify the existing column definition.
3. To include or drop integrity constraint.
Syntax: alter table tablename add/modify (attribute datatype(size));
Example: alter table student add ph_no number(10);
DROP TABLE
It will delete the table structure provided the table should be empty.
Example: Drop table student;
TRUNCATE TABLE
If there is no further use of records stored in a table and the structure has to be retained then the
records alone can be deleted.
Syntax: TRUNCATE TABLE <TABLE NAME>;
Example: Truncate table student;

Procedure:
1.​ Open the Postgresql software tool.
2.​ Create the following tables in a Database named ‘Company’.

Output:
Paste the screenshot of the output screen.

Conclusion:
Insert your own conclusion for the experiment.

Experiment No. 04
Aim:
To implement DML commands.

Theory:
DML commands are the most frequently used SQL commands and is used to query and manipulate the
existing database objects. Some of the commands are Insert, Select, Update, Delete
INSERT COMMAND
This is used to add one or more rows to a table. The values are separated by commas and the data
types char and date are enclosed in apostrophes. The values must br entered in the same order as
they are defined.
Syntax: insert into <table name> values (value list)
Example: insert into emp values(‘s3’,’sup3’,’blore’,10)
SELECT COMMANDS
It is used to retrieve information from the table.it is generally refered to as querying the table.We can
either display all columns in a table or only specify column from the table.
Syntax: Select * from tablename;
Example: Select * from emp;
The retrieval of specific columns from a table:
Syntax: Select column_name1, …..,column_namen from table name;
Example: Select empno, empname from emp;
UPDATE COMMAND
It is used to alter the column values in a table. A single column may be updated or more than one
column could be updated.
Syntax: updatetablename set field=values where condition;
Example: update emp set sal = 10000 where empno=135;
DELETE COMMAND
After inserting row in a table we can also delete them if required. The delete command consists of a
from clause followed by an optional where clause.
Syntax: Delete from table where conditions;
Example: delete from emp where empno=135;

Procedure:
1.​ Open the Postgresql software tool.
2.​ Insert data of about 5 rows in each table of the ‘Company’ database.
3.​ Update some rows of the data in the database.
4.​ Delete the updated rows from the database.

Output:
Paste the screenshot of the output screen.

Conclusion:
Insert your own conclusion for the experiment.

Experiment No. 05
Aim:
To perform simple queries and aggregate functions

Theory:
Where Clause:
The WHERE clause allows you to filter the results from an SQL statement - select, insert, update, or
delete statement.
Select column_name1,column_name2 From table_name Where condition
ORDER BY:
SQL uses ORDER BY clause to display the tuples in the result of the query to appear in sorted order.
Select column_name1,column_name2
From table_name
Where condition
Order by column name [ASC,DESEC]
GROUP BY:
GROUP BY clause is used to group the rows based on certain criteria. GROUP BY clause is usually used
in conjunction with aggregate function like sum, avg, min, and max.
Select column_name1, SUM [column_name2] from table name Group by column_name1
Having Clause:
The HAVING clause is used in combination with the GROUP BY clause. It can be used in a SELECT
statement to filter the records that a GROUP BY returns.
The syntax for the HAVING clause is:
SELECT column1, column2, ... column_n, aggregate_function (expression)
FROM tables
WHERE predicates
GROUP BY column1, column2, ... column_n
HAVING condition1 ... condition_n;
Aggregate_function:
The aggregate_function can be a function such as SUM, COUNT, AVG, MIN, or MAX.
A group function returns a result based on group of rows.
1. AVG
Example: select avg (total) from student;
2 .MAX
Example:select max (percentagel) from student;
3 .MIN
Example: select min (marksl) from student;
4 .SUM
Example: select sum(price) from product;
5. COUNT
In order to count the number of rows, count function is used.
1. count(*) – It counts all, inclusive of duplicates and nulls.
Example: select count(*) from student;
2. count(col_name)– It avoids null value.
Example: select count(total) from order;
3. count(distinct col_name) – It avoids the repeated and null values.
Example: select count(distinct ordid) from order;

Procedure:
1.​ Open the Postgresql software tool.
2.​ Execute the following queries on the inserted data:
i.​ Retrieve all the details of the departments associated with the company.
ii.​ Retrieve the date of Birth and address of the employee(s) whose name is ‘John’.
iii.​ Retrieve the Name and SSN of the employee(s) who stays in Palghar.
iv.​ Write a query to fetch total working hours of each employee.
v.​ Find the sum of salaries of all employees of the ‘Research’ department, as well as the maximum
salary, minimum salary, and average salary in this department.
vi.​ Write a query to fetch Dept. No. whose average salary is greater than 1,00,000.

Output:
Paste the screenshot of the output screen.

Conclusion:
Insert your own conclusion for the experiment.

Experiment No. 06
Aim:
To Implement various Join operations.

Theory:
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field
between them. The different SQL joins you can use:
INNER JOIN: Returns all rows when there is at least one match in BOTH tables
LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
FULL JOIN: Return all rows when there is a match in ONE of the tables
INNER JOIN
The INNER JOIN keyword selects all rows from both tables as long as there is a match between the
columns in both tables.
SQL INNER JOIN Syntax
SELECT column_name(s)​
FROM table1​
INNER JOIN table2​
ON table1.column_name=table2.column_name;
LEFT JOIN Keyword
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the
right table (table2). The result is NULL in the right side when there is no match.
SQL LEFT JOIN Syntax
SELECT column_name(s)​
FROM table1​
LEFT JOIN table2​
ON table1.column_name=table2.column_name;
RIGHT JOIN Keyword
The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the
left table (table1). The result is NULL in the left side when there is no match.
SQL RIGHT JOIN Syntax
SELECT column_name(s)​
FROM table1​
RIGHT JOIN table2​
ON table1.column_name=table2.column_name;
FULL OUTER JOIN Keyword
The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table
(table2).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.
SQL FULL OUTER JOIN Syntax
SELECT column_name(s)​
FROM table1​
FULL OUTER JOIN table2​
ON table1.column_name=table2.column_name;

Procedure:
1.​ Open the Postgresql software tool.
2.​ Execute the following queries on the inserted data:
i.​ Write a query to fetch Employee Name and Dept. Name they are associated with.
ii.​ Retrieve all employees in department 5 whose salary is between 30,000 and 40,000.
iii.​ Retrieve a list of employees and projects they are working on, ordered by department and
within each department, ordered alphabetically by last name, first name.
iv.​ Demonstrate Full Outer Join, Left Outer Join and Right Outer Join on tables Employee and
Project.

Output:
Paste the screenshot of the output screen.

Conclusion:
Insert your own conclusion for the experiment.

Experiment No. 07

Aim:
To Perform Nested and Complex queries
Theory:
IN:
The IN connective tests for set membership, where the set is a collection of values produced by a
SELECT clause
​ Select * from table_name where condition is IN
NOTIN:
The NOTIN connective tests for the absence of set membership.
​ Select * from table_name where condition is NOTIN
AND/OR:
Sometimes the criteria for including a row are more complicated than a simple condition can express.
Fortunately, we can combine two or more condition using AND and OR. Two conditions joined by AND
must both be true for the entire condition to evaluate to true.
Select column name from table_name where condition [AND/OR] CONDITION.
BETWEEN:
The BETWEEN operator allows us to specify a range of values (inclusive) for matching.
Select column_name1,column_name2 from table name
Where column_name1 between value 1 and value 2
CREATE TABLE AS:
the SQL CREATE TABLE AS statement to create a table from an existing table by copying the existing
table's columns.
It is important to note that when creating a table in this way, the new table will be populated with the
records from the existing table (based on the SELECT Statement).
Create Table - By copying all columns from another table
The syntax for the CREATE TABLE AS statement when copying all of the columns in SQL is:
CREATE TABLE new_table AS (SELECT * FROM old_table);

Procedure:
1.​ Open the Postgresql software tool.
2.​ Execute the following tasks:
a.​ Create table Employee with emp_id,emp_name,salary,city and insert 5 rows.
b.​ Create table Dependents with dep_id,emp_id,no_of_dependents and insert 3-4 rows.
c.​ Perform the following queries:
i.​ To Display Last 3 Records of Employee table
ii.​ Display Employees Who stays in ‘miraroad’ And ‘nallasopara’
iii.​ Display Employees Who stays elsewhere other than palghar
iv.​ Employees with less than 3 dependents
v.​ To find count of duplicate salary
vi.​ Second Highest Salary
vii.​ Create Table Employee1 With Same Structure and values Of Employee Table
viii.​ Create table Emp20000 with employees having salary less than equal to 20000

Output:
Paste the screenshot of the output screen.

Conclusion:
Insert your own conclusion for the experiment.

Experiment No. 08
Aim:
To Implement DCL commands

Theory:
Data Control Language
DCL commands are used to grant and take back authority from any database user.
Here are some commands that come under DCL:
Grant
Revoke
a. Grant: It is used to give user access privileges to a database.
Example
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
b. Revoke: It is used to take back permissions from the user.
Example
REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;

Procedure:
1.​ Open the Postgresql software tool.
2.​ Create user ‘abc’.
3.​ Grant insert and select operations on employee table to user abc.
4.​ Demonstrate its use.
5.​ Revoke back the privileges from abc user.

Output:
Paste the screenshot of the output screen.

Conclusion:
Insert your own conclusion for the experiment.

Experiment No. 09
Aim:
To implement procedures and functions.

Theory:
Procedures:
A procedure is a named PL/SQL block which performs one or more specific task. This is similar to a
procedure in other programming languages. A procedure has a header and a body.​
The header consists of the name of the procedure and the parameters or variables passed to the
procedure. The body consists or declaration section, execution section and exception section similar to
a general PL/SQL Block. A procedure is similar to an anonymous PL/SQL Block but it is named for
repeated usage.​
We can pass parameters to procedures in three ways:
Parameters Description
IN type These types of parameters are used to send values to stored procedures.
These types of parameters are used to get values from stored procedures. This is
OUT type
similar to a return type in functions.
These types of parameters are used to send values and get values from stored
IN OUT type
procedures.
A procedure may or may not return any value.
Syntax:
CREATE [OR REPLACE] PROCEDURE procedure_name (<Argument> {IN, OUT, IN OUT}
<Datatype>,…)
IS
Declaration section<variable, constant> ;
BEGIN
Execution section
EXCEPTION
Exception section
END
IS - marks the beginning of the body of the procedure and is similar to DECLARE in anonymous PL/SQL
Blocks. The code between IS and BEGIN forms the Declaration section.​
The syntax within the brackets [ ] indicate they are optional. By using CREATE OR REPLACE together
the procedure is created if no other procedure with the same name exists or the existing procedure is
replaced with the current code.
How to execute a Procedure?
There are two ways to execute a procedure :
From the SQL prompt : EXECUTE [or EXEC] procedure_name;
Within another procedure – simply use the procedure name : procedure_name;
Example:
create table named emp have two column id and salary with number datatype.
CREATE OR REPLACE PROCEDURE p1(id IN NUMBER, sal IN NUMBER)
AS
BEGIN
INSERT INTO emp VALUES(id, sal);
DBMD_OUTPUT.PUT_LINE('VALUE INSERTED.');
END;
/

Functions:
A function is a named PL/SQL Block which is similar to a procedure. The major difference between a
procedure and a function is, a function must always return a value, but a procedure may or may not
return a value.
Syntax:
CREATE [OR REPLACE] FUNCTION function_name [parameters]
RETURN return_datatype; {IS, AS}
Declaration_section <variable,constant> ;
BEGIN
Execution_section
Return return_variable;
EXCEPTION
exception section
Return return_variable;
END;
RETURN TYPE: The header section defines the return type of the function. The return datatype can be
any of the oracle datatype like varchar, number etc.​
The execution and exception section both should return a value which is of the datatype defined in the
header section.
A function can be executed in the following ways.
As a part of a SELECT statement : SELECT emp_details_func FROM dual;
In a PL/SQL Statements like, : dbms_output.put_line(emp_details_func);
This line displays the value returned by the function .
Example:
create or replace function getsal (no IN number) return number
is
sal number(5);
begin
select salary into sal from emp where id=no;
return sal;
end;
/

Procedure:
1.​ Open the Postgresql software tool.
2.​ Develop a procedure to transfer 1000 INR from one account to another. Create the required
tables and execute the necessary queries.
3.​ Develop a function to count the number of cars ranging from 10000 to 50000. Create the
required tables and execute the necessary queries.

Output:
Paste the screenshot of the output screen.

Conclusion:
Insert your own conclusion for the experiment.

Experiment No. 10
Aim:
To implement Triggers in SQL.

Theory:
A trigger is a PL/SQL block or a PL/SQL procedure that executes implicitly whenever a particular event
takes place. It can either be:
Application trigger: Fires whenever an event occurs with a particular application.
Database Trigger: Fires whenever a data event (such as DML) occurs on a schema or database.
Guidelines to Designing Triggers:
Use the following guidelines when designing triggers.
Use triggers to guarantee that when a specific operation is performed, related actions are performed.
Only use database triggers for centralized, global operations that should be fired for the triggering
statement, regardless of which user or application issues the statement.
Do not define triggers to duplicate or replace the functionality already built into the oracle database.
For example do not define trigger to implement integrity rules that can be done by using declarative
constraints.
The excessive use to triggers can result in complex interdependencies, which may be difficult to
maintain in large applications. Only use triggers when necessary, and beware of recursive and
cascading effects.
Elements in a Trigger:
Trigger timing
For table: BEFORE, AFTER
For view: INSTEAD OF
Trigger event: INSERT, UPDATE, OR DELETE
Table name: On table, view
Trigger Type: Row or statement
When clause: Restricting condition
Trigger body: PL/SQL block
“Before triggers” execute the trigger body before the triggering DML event on a table. These are
frequently used to determine whether that triggering statement should be allowed to complete. This
situation enables you to eliminate unnecessary processing of the triggering statement and it eventual
rollback in cases where an exception is raised in the triggering action.
“After triggers” are used when the triggering statement is to be completed before the triggering action
and to perform a different action on the same triggering statement if a BEFORE trigger is already
present.
“Instead of Triggers” are used to provide a transparent way of modifying views that cannot be modified
directly through SQL DML statements because the view is not inherently modifiable. You can write
INSERT, UPDATE, and DELETE statements against the view. The INSTEAD OF trigger works invisibly in
the background performing the action coded in the trigger body directly on the underlying tables.
Triggering user events:
INSERT
UPDATE
DELETE
Trigger Components:
Statement: The trigger body executes once for the triggering event. This is the default. A statement
trigger fires once, even if no rows are affected at all.
Row: The trigger body executes once for each row affected by the triggering event. A row trigger is not
executed if the triggering event affects no rows.
Trigger Body:

The trigger body is a PL/SQL block or a call to a procedure.


Syntax:
CREATE [OR REPLACE] TRIGGER trigger_name
Timing
Event1 [OR event2 OR event3]
ON table_name
Trigger_body
Example: CREATE [OR REPLACE] TRIGGER secure_empBefore insert on emp
BEGIN
IF(to_char(sysdate,’dy’) IN (‘SAT’,’SUN’)) OR To_char(sysdate,’HH24’) NOT BETWEEN ‘08’ AND ‘18’)
THEN RAISE_APPLICATION_ERROR(-20500,‘you may only insert into EMP during normal hours.’); END
IF;
END;

Procedure:
1.​ Open the Postgresql software tool.
2.​ Develop a trigger to store the old employee name in audits table when the new name is
updated. Create the required tables and execute the necessary queries.

Output:
Paste the screenshot of the output screen.

Conclusion:
Insert your own conclusion for the experiment.

You might also like