Dbms LAB File Ritesh
Dbms LAB File Ritesh
Experiment -1
Aim- Design ER diagrams for various scenarios or based on given projects.
Theory-
What is ER Diagram?
ER Diagram stands for Entity Relationship Diagram, also known as ERD is a diagram that displays the
relationship of entity sets stored in a database. In other words, ER diagrams help to explain the logical structure
of databases. ER diagrams are created based on three basic concepts: entities, attributes and relationships.
ER Diagrams contain different symbols that use rectangles to represent entities, ovals to define attributes and
diamond shapes to represent relationships.
Entities
Attributes
Relationships
Relationship is nothing but an association among two or more entities. E.g., Tom works in
the Chemistry department.
Ritesh kumar (21SCSE1260009) Page |3
Experiment -2
Aim- Implement DDL Statements and DML statements.
Theory-
The SQL Data Definition Language 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.
Data Manipulation Language (DML) statements are used for managing data in database. DML commands are
not auto-committed. It means changes made by DML command are not permanent to database, it can be rolled
back.
DML statements are used for managing data within schema objects. Some examples:
SELECT - retrieve data from the a database
INSERT - insert data into a table
UPDATE - updates existing data within a table
DELETE - deletes all records from a table, the space for the records remain.
Data Definition Language
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));
ALTER TABLE
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;
DROP TABLE
DROP TABLE table_name;
RENAME TABLE
Ritesh kumar (21SCSE1260009) Page |4
TRUNCATE
TRUNCATE TABLE table_name;
Data Manipulation Language
SELECT column1, column2, ...FROM table_name;
INSERT INTO table_name (column1, column2, column3, ...) VALUES
(value1, value2, value3, ...);
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE
condition;
DELETE FROM table_name WHERE condition; Delete from
Emp where EID=2;
Ritesh kumar (21SCSE1260009) Page |5
Experiment -3
Aim- Execute the SELECT command with different clauses.
Theory-
SQL SELECT Statement
The most used SQL command is SELECT statement.
SQL SELECT statement is used to query or retrieve data from a table in the database.
A query may retrieve information from specified columns or from all of the columns in the table.
To create a simple SQL SELECT Statement, you must specify the column(s) name and the table name.
The whole query is called SQL SELECT Statement.
Experiment -4
Aim- Execute various types of Integrity Constraints on database.
Theory-
SQL Constraints
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.
Syntax
SQL CREATE TABLE + CONSTRAINT Syntax
CREATE TABLE table_name( column_name1 data_type(size) constraint_name, column_name2
data_type(size) constraint_name, column_name3 data_type(size) constraint_name,);
Ritesh kumar (21SCSE1260009) Page |7
Experiment -5
Aim- Implement SINGLE ROW functions (Character, Numeric, Date functions) and
GROUP functions (avg, count, max, min, sum).
Theory-
SINGLE ROW functions
Oracle SQL supplies a rich library of in-built functions which can be employed for various tasks.
The essential capabilities of functions can be the case conversion of strings, in-string or substring operations,
mathematical computations on numeric data, and date operations on date type values.
SQL Functions optionally take arguments from the user and mandatorily return a value.
GROUP functions
Aggregate functions perform a variety of actions such as counting all the rows in a table, averaging a
column's data, and summing numeric data.
Aggregates can also search a table to find the highest "MAX" or lowest "MIN" values in a column.
As with other types of queries, you can restrict, or filter out the rows these functions act on with the WHERE
clause.
For example, if a manager needs to know how many employees work in an organization, the aggregate
function named COUNT(*) can be used to produce this information.The COUNT(*) function shown in the
below
SELECT statement counts all rows in a table.
Experiment -6
Aim- Implement the concept of grouping of Data and Sub-queries.
Theory-
Subqueries:- A subquery is a form of an SQL statement that appears inside another SQL statement.
It also termed as nested query.
The statement containing a subquery called a parent statement.
The rows returned by the subquery are use by the following statement.
Example:-
Customers
Experiment -7
Aim- Implement Simple and Complex View.
Theory-
Syntax:-
CREATE VIEW viewname AS SELECT newcolumnname…. FROM tablename
WHERE columnname=expression_list;
Selecting a data set from a view- Syntax:-
SELECT columnname, columnname FROM viewname
WHERE search condition;
Destroying a view-
Syntax:-
Experiment -8
Aim- Execute the concept of Data Control Language (DCL).
Theory-
Data Control Language (or DCL) consists of statements that control security and concurrent access to table
data.
COMMIT
Instructs the XDB Server to make permanent all data changes resulting from DML statements executed
by a transaction.
CONNECT
Connects the application process (or user) to a designated XDB Server or DB2 location. This location
becomes the current location for the application process or user.
GRANT (Database Privileges)
Extends XDB Server's automatic record and table level locking functions (in a multi-user system) by
acquiring explicit locks on a particular table.
REVOKE (Database Privileges)
Instructs the XDB Server to reverse the effect of any DML commands executed on a database by a
transaction. Information recorded in a backward log is used to restore the database to a state existing
before the transaction.
UNLOCK TABLE
Removes table locks (in a multi-user system) acquired with the LOCK TABLE command.
Grant
Revoke
Grant:
This command is use to give user access privileges to a database.
Syntax:
For example:
Syntax:
For example:
Experiment -9
Aim- Implement Transaction Control Language (TCL).
Theory-
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 here.
Hence, the following TCL commands are used to control the execution of a transaction:
Transaction control language or TCL commands deal with the transaction within the database.
Commit
This command is used to save all the transactions to the database.
Syntax:
Commit;
For example:
COMMIT;
Rollback
Rollback command allows you to undo transactions that have not already been saved to the database.
Syntax:
ROLLBACK;
Example:
Syntax:
SAVEPOINT SAVEPOINT_NAME;
Example:
SAVEPOINT RollNo;
Ritesh kumar (21SCSE1260009) P a g e | 16
Experiment -10
Aim- Write a PL/SQL block for greatest of three numbers using IF AND ELSEIF.
Algorithm-
STEP 1: Start
STEP 2: Initialize the necessary variables.
STEP 3: invoke the if else if condition.
STEP 4: Execute the statements.
STEP 5: Stop
Syntax-
SQL>set server output on
SQL> declare
2 a number;
3 b number;
4 c number;
5 begin
6 a:=&a;
7 b:=&b;
8 c:=&c;
9 if(a>b)and(a>c) then
10 dbms_output.put_line('A is maximum');
11 else if(b>a)and(b>c)then
12 dbms_output.put_line('B is maximum');
13 else
14 dbms_output.put_line('C is maximum');
15 end if;
16 end;
Ritesh kumar (21SCSE1260009) P a g e | 17