0% found this document useful (0 votes)
51 views

JATIN Dbms (1) 1

The document contains a report submitted by Jatin Kumar Sharma to his professor, Mr. Yashwant Soni, for the Database Management System Laboratory course. It includes implementations of DDL, DML, and DCL statements as well as group functions like average, count, max, min and sum. Various clauses of the SELECT statement are also executed, including WHERE, GROUP BY, HAVING and ORDER BY clauses.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

JATIN Dbms (1) 1

The document contains a report submitted by Jatin Kumar Sharma to his professor, Mr. Yashwant Soni, for the Database Management System Laboratory course. It includes implementations of DDL, DML, and DCL statements as well as group functions like average, count, max, min and sum. Various clauses of the SELECT statement are also executed, including WHERE, GROUP BY, HAVING and ORDER BY clauses.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Jatin Kumar Sharma

22SCSE1012203

DATABASE MANAGEMENT SYSTEM LABORATORY


COURSE CODE: BCSE2073
Report file
for
BACHELOR OF
Engineering & Technology

SCHOOL OF COMPUTING SCIENCE AND ENGINEERING


GALGOTIAS UNIVERSITY, GREATER NOIDA
UTTAR PRADESH

Submitted to: Mr. Yashwant Soni Sir

Submitted By: Jatin Kumar Sharma


Admission No: 22SCSE1012203
Section: 24/P2
Jatin Kumar Sharma
22SCSE1012203

INDEX
Exp.no Programs Date Signature
1. Design Er Diagrams for various or based on
given projects.
2. Implement DDL Statements and DML
statements.
3. Execute the SELECT command with
different Clauses.
4. Implement GROUP functions(avg, count,
max, min, sum).
5. Execute the concept of Data Control
Language (DCL).
6. Implement Transaction Control Language
(TCL).
7. Execute various types of Integrity
Constraints on database.
8. Analysis and design of the normalized
tables.
9. Write a PL/SQL block to satisfy some
conditions by accepting input from the user.
10. Implement the concept of grouping of Data
and sub- queries.
11. Write a PL/SQL block to implementation of
factorial using function.
12. Write a PL/SQL Procedure for GCD
Numbers.
13. Write a PL/SQL block for greatest of three
numbers using IF AND ELSE IF.
14. Write a PL/SQL block for summation of
odd numbers using for LOOP.
Jatin Kumar Sharma
22SCSE1012203

1. Design ER diagrams for Hospital Management System.


Jatin Kumar Sharma
22SCSE1012203

2. Implement DDL statements and DML statements.


DDL (Data Definition Language):

DDL or Data Definition Language actually consists of the SQL commands that can
be used to define the database schema.

The SQL DDL allows specification of not only a set of relations but also information
about each relation, including-
 Schema for each relation
 The domain of values associated with each attribute.
 The integrity constraints.
 The set of indices to be maintained for each relation.
 The security and authorization information for each relation.
The physical storage structure of each relation on disk.

Syntax:-
CREATE TABLE
Emp1 (EID int, EName Char, Edept char, EDOB date,Salary int)
Create Table Emp1(EID int, EName varchar(20), Edept varchar(10), EDOB Date,
Salary int);
CREATE TABLE TABLENAME (COLUMN_NAME1
DATA_TYPE1(SIZE1),……. COLUMN_NAMEN DATA_TYPEN(SIZEN));

Output:-

ALTER TABLE:
Jatin Kumar Sharma
22SCSE1012203

This command is used to modify the present database and related


tables.

Syntax:-

ALTER TABLE table_name ADD column_name datatype;


ALTER TABLE table_name MODIFY column_name datatype;
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE table_name RENAME COLUMN old_column_name TO
new_column_name;

Output:-

DROP Command:
This command is used to remove the definition of a table. When you drop a table
database loses all the data in the table and all indexes associated with it.

Syntax:-
DROP object object_name.

Output:-
Jatin Kumar Sharma
22SCSE1012203

RENAME Command:
It is used to rename table.

Syntax:
Rename old name to new name;

Output:-

TRUNCATE Command
This command is used to remove all rows from the table and to release the storage
space used by that table.

Syntax:
Truncate table <tablename>;

Output:-
Jatin Kumar Sharma
22SCSE1012203

DML Commands:-

The SQL commands that deals with the manipulation of data present in the
database belong to DML or Data Manipulation Language and this includes most of
the SQL statements. It is the component of the SQL statement that controls access
to data and to the database. Basically, DCL statements are grouped with DML
statements.

List of DML commands:

 INSERT : It is used to insert data into a table.


 UPDATE: It is used to update existing data within a table.
 DELETE : It is used to delete records from a database table.
 SELECT:- The SELECT command shows the records of the specified table.

SELECT Command:
This command is used to show table data.

Syntax:
Select * from <tablename>;

Output:-

INSERT Command:
Jatin Kumar Sharma
22SCSE1012203

Records can be added to table using this command. Only one row can be inserted at
a time.
Syntax:
insert into<tablename>(col1,col2…coln)values(List of values)
Output:-

UPDATE Command:-
The UPDATE statement is used to update the data of an existing table in database.
Syntax:-
UPDATE table_name SET column1 = value1, column2 = value2,... WHERE
condition;
OUTPUT:-

DELETE Command:
Jatin Kumar Sharma
22SCSE1012203

It is used to remove rows from a table. The entire row can be deleted using this
command.

Syntax:
Delete from<table name> where condition;

OUTPUT:-
Jatin Kumar Sharma
22SCSE1012203

3. Execute the SELECT command with different clauses.


Syntax of SQL SELECT Statement:
SELECT column_list FROM table-name
[WHERE Clause]
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause];

WHERE CLAUSE:-
The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.

Syntax:-

SELECT column1, column2, ...


FROM table_name
WHERE condition;

Output:-
Jatin Kumar Sharma
22SCSE1012203

GROUP BY CLAUSE:-
A GROUP BY clause, part of a Select Expression, groups a result into subsets that
have matching values for one or more columns. In each group, no two rows have
the same value for the grouping column or columns. NULLs are considered
equivalent for grouping purposes.

Syntax:-

SELECT expression1, expression2, ... expression_n, aggregate_function


(aggregate_expression)
FROM tables
GROUP BY expression1, expression2, ... expression_n;

Output:-

HAVING CLAUSE:-
A HAVING clause restricts the results of a GROUP BY in a Select
Expression. The HAVING clause is applied to each group of the grouped table,
much as a WHERE clause is applied to a select list.

Syntax:-

SELECT expression1, expression2, ... expression_n, aggregate_function (aggregat


e_expression)FROM tables
HAVING having_condition;

Output:-
Jatin Kumar Sharma
22SCSE1012203

ORDER BY CLAUSE:-
In Oracle, ORDER BY Clause is used to sort or re-arrange the records in the result
set. The ORDER BY clause is only used with SELECT statement.

Syntax:-
SELECT expressions FROM tables ORDER BY expression;\

Output:-
Jatin Kumar Sharma
22SCSE1012203

4. Implement GROUP functions (avg, count, max, min,


sum).

I. Avg functions:-
The Oracle AVG() function accepts a list of values and returns the average.

Syntax:-

AVG([DISTINCT | ALL ] expression)

Output:-

II. Count functions:-


The Oracle COUNT() function is an aggregate function that returns the number of
items in a group.

Syntax:-
COUNT( [ALL | DISTINCT | * ] expression)

Output:-
Jatin Kumar Sharma
22SCSE1012203

III. MAX functions:-


MAX is an aggregate function that evaluates the maximum of an expression
over a set of rows MAX is allowed only on expressions that evaluate to built-in
data types (including CHAR, VARCHAR, DATE, TIME, CHAR FOR BIT
DATA, etc.).

Syntax:
MAX ( [ DISTINCT | ALL ] Expression )

Output:-

IV. MIN functions:-


The aggregate function SQL MIN() is used to find the minimum value or lowest
value of a column or expression. This function is useful to determine the smallest
of all selected values of a column.

Syntax:
MIN ([ALL | DISTINCT] expression )

Output:-
Jatin Kumar Sharma
22SCSE1012203

V. Sum functions:-
The Oracle SUM() function is an aggregate function that returns the sum of all or
distinct values in a set of values.
Syntax:-
SUM( [ALL | DISTINCT] expression)

Output:-
Jatin Kumar Sharma
22SCSE1012203

5. Execute the concept of Data Control Language.


DCL (Data Control Language):
DCL includes commands such as GRANT and REVOKE which mainly deal with
the rights, permissions, and other controls of the database system.

List of DCL commands:


GRANT: This command gives users access privileges to the database.
REVOKE: This command withdraws the user’s access privileges given by using
the GRANT command.

(i) GRANT Command:-


To grant privileges of a user account this command is used because using create
command, it creates only new user but it doesn’t grant privileges to a user account.
That is why Grant command is used.

Syntax:-
GRANT privileges_names ON object TO user;

(ii) REVOKE:-
This command withdraws the user’s access privileges given by using the GRANT
command.

Syntax :
revoke privilege_name on object_name from {user_name | public | role_name}
Jatin Kumar Sharma
22SCSE1012203

6. Implement Transaction Control (TCL)


TCL (Transaction Control Language):
Transactions group a set of tasks into a single execution unit. Each transaction
begins with a specific task and ends when all the tasks in the group successfully
complete. If any of the tasks fail, the transaction fails. Therefore, a transaction has
only two results: success or failure. You can explore more about transactions.

Hence, the following TCL commands are used to control the execution of a
transaction:
COMMIT: Commits a Transaction.
ROLLBACK: Rollbacks a transaction in case of any error occurs.
SAVEPOINT: Sets a save point within a transaction.

(i) COMMIT:-
If everything is in order with all statements within a single transaction, all changes
are recorded together in the database is called committed. The COMMIT command
saves all the transactions to the database since the last COMMIT or ROLLBACK
command.

Syntax:
COMMIT;

Output:-
Jatin Kumar Sharma
22SCSE1012203

(ii) SAVEPOINT:-
creates points within the groups of transactions in which to ROLLBACK.
A SAVEPOINT is a point in a transaction in which you can roll the transaction
back to a certain point without rolling back the entire transaction.

Syntax:-

SAVEPOINT SAVEPOINT_NAME;

(iii) ROLLBACK:-
If any error occurs with any of the SQL grouped statements, all changes need to be
aborted. The process of reversing changes is called rollback. This command can
only be used to undo transactions since the last COMMIT or ROLLBACK
command was issued.

Syntax:
ROLLBACK;

OUTPUT:-
Jatin Kumar Sharma
22SCSE1012203

7. Execute various types on Integrity Constraints on database.


SQL constraints are used to specify rules for the data in a table.
If there is any violation between the constraint and the data action, the action is
aborted by the constraint.
Constraints can be specified when the table is created (inside the CREATE
TABLE statement) or after the table is created (inside the ALTER TABLE
statement).
In SQL, we have the following constraints:
 NOT NULL - Indicates that a column cannot store NULL value
 UNIQUE - Ensures that each row for a column must have a unique value
 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures
that a column (or combination of two or more columns) have a unique
identity which helps to find a particular record in a table more easily and
quickly
 FOREIGN KEY - Ensure the referential integrity of the data in one table to
match values in another table
 CHECK - Ensures that the value in a column meets a specific condition
 DEFAULT - Specifies a default value for a column
SQL PRIMARY KEY Constraint
The PRIMARY KEY constraint uniquely identifies each record in a database table.
Primary keys must contain UNIQUE values.
A primary key column cannot contain NULL values.
Most tables should have a primary key, and each table can have only ONE primary
key.
SQL FOREIGN KEY Constraint
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.

(i) NOT NULL:-


Jatin Kumar Sharma
22SCSE1012203

The NOT NULL is a constraint in SQL which does not allow you to insert NULL
values in the specified column.
If any column is defined as a NOT NULL constraint in the table, we cannot insert a
new record in the table without adding the value to the column.

Output:-

(ii) UNIQUE:-
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.

Output:-

(iii) PRIMARY KEY:-


The primary key in SQL is a single, or a group of fields or columns that can
uniquely identify a row in a table. Putting it simply, it is a column that accepts
unique values for each row.

Output:-
Jatin Kumar Sharma
22SCSE1012203

(iv) FOREIGN KEY:-


A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the
PRIMARY KEY in another table.

(v) CHECK:-
The CHECK constraint is used to limit the value range that can be placed in a
column.
If you define a CHECK constraint on a column it will allow only certain values for
this column.

Output:-

(vi) DEFAULT:-
The DEFAULT Constraint is used to fill a column with a default and fixed value.
The value will be added to all new records when no other value is provided.

Output:-
Jatin Kumar Sharma
22SCSE1012203

8. Analysis and design of the normalized tables.


Normalized:-
Normalization is the process of organizing the data in the database.
Normalization is used to minimize the redundancy from a relation or set of
relations. It is also used to eliminate undesirable characteristics like Insertion,
Update, and Deletion Anomalies.
Normalization divides the larger table into smaller and links them using
relationships.

1. First Normal Form:-


A relation will be 1NF if it contains an atomic value.
It states that an attribute of a table cannot hold multiple values. It must hold only
single-valued attribute.
First normal form disallows the multi-valued attribute, composite attribute, and
their combinations.

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

2. Second Normal Form:-


In the 2NF, relational must be in 1NF.
Jatin Kumar Sharma
22SCSE1012203

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

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

3. Third Normal Form:-


 A relation will be in 3NF if it is in 2NF and not contain any transitive partial
dependency.
 3NF is used to reduce the data duplication. It is also used to achieve the data
integrity.
 If there is no transitive dependency for non-prime attributes, then the relation
must be in third normal form.

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

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

Example:-
Employee details tables

4. Boyce-Code Normal Form:-


 BCNF is the advance version of 3NF. It is stricter than 3NF.
 A table is in BCNF if every functional dependency X → Y, X is the super
key of the table.
 For BCNF, the table should be in 3NF, and for every FD, LHS is super key.

Example:
Let's assume there is a company where employees work in more than one
department.
Jatin Kumar Sharma
22SCSE1012203
Jatin Kumar Sharma
22SCSE1012203

9. Write a PL/SQL block to satisfy some conditions by


accepting input from the user.
declare

a number;

b number;

c number;

begin

a: =&a;

b: =&b;

c: =a+b;

dbms_output.put_line ('sum of'||a||'and'||b||'is'||c);

end;

Output:-
Jatin Kumar Sharma
22SCSE1012203

10. Implement the concept of grouping of Data and


Sub-queries.

A subquery is used to return data that will be used in the main query as a condition
to further restrict the data to be retrieved.

Rules:-
There are a few rules that subqueries must follow −
 Subqueries must be enclosed within parentheses.
 A subquery can have only one column in the SELECT clause, unless
multiple columns are in the main query for the subquery to compare its
selected columns.
 An ORDER BY command cannot be used in a subquery, although the main
query can use an ORDER BY. The GROUP BY command can be used to
perform the same function as the ORDER BY in a subquery.
 Subqueries that return more than one row can only be used with multiple
value operators such as the IN operator.
 The SELECT list cannot include any references to values that evaluate to a
BLOB, ARRAY, CLOB, or NCLOB.
 A subquery cannot be immediately enclosed in a set function.

(i) Subqueries with the SELECT statement:-

Subqueries are most frequently used with the SELECT statement. The basic syntax
is as follows

SYNTAX:-
SELECT column_name FROM table WHERE column_name OPERATOR
(SELECT column_name FROM table , WHERE);

Example:-
Jatin Kumar Sharma
22SCSE1012203

(ii) Subqueries with the UPDATE statement:-


The subquery can be used in conjunction with the UPDATE statement. Either
single or multiple columns in a table can be updated when using a subquery with
the UPDATE statement.
UPDATE table

Syntax:-
SET column_name = new_value WHERE OPERATOR VALUE (SELECT
COLUMN_NAME FROM TABLE_NAME condition);

Example:-

(iii) Subqueries with the DELETE statement:-


The subquery can be used in conjunction with the DELETE statement like with
any other statements mentioned above.

Syntax:-
DELETE FROM TABLE_NAME WHERE OPERATOR VALUE (SELECT
COLUMN_NAME FROM TABLE_NAME condition);
Jatin Kumar Sharma
22SCSE1012203

Example:-

11. Write a PL/SQL block to implementation of


factorial using functions.

CODE:-

DECLARE

fact number := 1;

n number := 7;

BEGIN

while n > 0 loop

fact:=n*fact;

n:=n-1;

End loop;

dbms_output.put_line(fact);
Jatin Kumar Sharma
22SCSE1012203

END;

Output:-

12. Write a PL/SQL Procedure for GCD numbers.

DECLARE
num1 INTEGER;
num2 INTEGER;
t INTEGER;
BEGIN
num1 := 8;

num2 := 56;

WHILE MOD(num2, num1) != 0 LOOPt := MOD(num2, num1);


num2 := num1;
num1 := t;
END LOOP;
dbms_output.Put_line('GCD of '||num1||' and '||num2||' is '||num1);
END;

Output-
Jatin Kumar Sharma
22SCSE1012203

13. Write a PL/SQL block for greatest of three


numbers using IF AND ELSEIF.
DECLARE
a NUMBER := 46;
b NUMBER := 67;
c NUMBER := 21;
BEGIN
IF a > b
AND a > c THEN
dbms_output.Put_line('Greatest number is '||a);
ELSIF b > a AND b > c THEN
dbms_output.Put_line('Greatest number is '||b);
ELSE
dbms_output.Put_line('Greatest number is '||c);
END IF;
END;
Jatin Kumar Sharma
22SCSE1012203

Output:-

14. Write a PL/SQL block for summation of odd


numbers using for LOOP.

Code:-

declare

n number;

sum1 number default 0;

endvalue number;

begin

endvalue:=500;

n:=1;

for n in 1..endvalue loop

sum1:=sum1+n;

end loop;
Jatin Kumar Sharma
22SCSE1012203

dbms_output.put_line('sum ='||sum1);

end;

Output:-

You might also like