Lab Course File: Galgotias University

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

GALGOTIAS UNIVERSITY

SCHOOL OF COMPUTING SCIENCE & ENGINEERING

LAB COURSE FILE

BCA

DATABASE MANAGEMENT SYSTEM LAB

B070303P
LIST OF EXPERIMENTS
Sr.
Title of Lab Experiments
No.
1. 1) Implement Data Definition language Statements.

2. 2) Implement Data Manipulation Statements.

3. 3)Implement SELECT command with different clauses.

4. 4)Implement various type of Integrity Constraints on database.

5)Implement SINGLE ROW functions (Character, Numeric, Date functions) and GROUP functions
5. (avg, count, max, min, sum).

6. 6)Implement various type of SET OPERATORS (Union, Intersect, Minus)

7. 7)Implement the concept of grouping of Data and Subqueries.

8. 8)Implement the concept of Data Control Language (DCL), Transaction Control Language(TCL).

9. 9)Implement Simple and Complex View. Value Added Experiments

Value Added Experiments

10. 10Create a Database for Banking Sector and implement various queries on it.

11. 11 Create a Database for Customer Sale/purchase and implement various queries on it
EXPERIMENT DETAILS
Experiment 1
Title Data Definition Language

Study of Data Definition language commands. - Create table, Alter Table, Drop Table,
Objective
Rename Table.

Pre-
Knowledge of Basic Database
requisite

The SQL DDL allows specification of not only a set of relations but also information
about each relation, including-

 Schema for each relation

Algorithm  The domain of values associated with each attribute.

/Theory  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.

CREATE TABLE

CREATE TABLE TABLENAME (COLUMN_NAME1 DATA_TYPE1(SIZE1),…….


COLUMN_NAMEN DATA_TYPEN(SIZEN));

ALTER TABLE

ALTER TABLE table_name


Syntax
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 OLDcolumn_name TO NEWcolumn_name;

DROP TABLE

DROP TABLE table_name;

RENAME TABLE

RENAME OLDtable_name TO NEWtable_name;

TRUNCATE

TRUNCATE TABLE table_name;

Post Lab
Assignment
(If Any)

OUTPUT:
CREATE TABLE

create table HIMANSHU(Name char(30),Roll number,Course char(20))

desc HIMANSHU

ALTER A NEW FIELD

alter table HIMANSHU add mobile number(10)

desc HIMANSHU
ALTER MODIFY FIELD DATA SIZE

alter table HIMANSHU modify course char(30)

desc HIMANSHU

ALTER DROP COLUMN

alter table HIMANSHU drop column mobile

desc HIMANSHU

ALTER RENAME COLUMN

alter table HIMANSHU rename column Course to Sub

desc HIMANSHU
TRUNCATE

truncate table HIMANSHU

desc HIMANSHU

RENAME TABLE

rename HIMANSHU to RISHABH

desc RISHABH

DROP TABLE

drop table RISHABH

desc RISHABH
Experiment 2
Title Data Manipulation Language Statements.

Objective Study of Data Manipulation Statements.

Pre- Knowledge of
requisite  ORACLE Queries

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:
Algorithm
o SELECT - retrieve data from the a database
/Theory
o INSERT - insert data into a table

o UPDATE - updates existing data within a table

o DELETE - deletes all records from a table, the space for the records remain

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);

UPDATE table_name SET column1 = value1, column2 = value2, ...


Syntax
WHERE condition;

DELETE FROM table_name WHERE condition;

SELECT column1, column2, ...FROM table_name;

Post Lab
Assignment
(If Any)
OUTPUT:
INSERT

insert into HIMANSHU values('A',1,'B.Tech',9451268565)

select * from HIMANSHU

insert into HIMANSHU values('B',2,'B.Tech',9560108177)

select * from HIMANSHU

UPDATE

update HIMANSHU set mobile=9451268565 where roll=2

select * from HIMANSHU

DELETE

delete from HIMANSHU where roll=2

select * from HIMANSHU


SELECT
Experiment 3
Title SELECT Command

Objective Study of SELECT command with different clauses.

Pre- Knowledge of
requisite  ORACLE

SQL SELECT Statement

The most commonly used SQL command is SELECT statement. SQL SELECT
Algorithm statement is used to query or retrieve data from a table in the database. A query may
/Theory 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.

Syntax of SQL SELECT Statement:

SELECT column_list FROM table-name


[WHERE Clause]
Syntax
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause];

Post Lab
Assignment
(If Any)
OUTPUT:
Experiment 4
Title Keys

Objective Study of various type of Integrity Constraints.

Pre- Knowledge of
requisite  ORACLE COMMANDS

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
Algorithm  PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures
/Theory 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.

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,
....
);

CREATE TABLE PersonsNotNull


(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
Syntax FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

CREATE TABLE Persons


(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
)

Post Lab
Assignment
(If Any)

OUTPUT
Experiment 5
Title Single Row functions and Group functions

Study of single row functions (Character, Numeric, Date functions) and group functions
Objective
(avg, count, max, min, sum).

Pre- Knowledge of

requisite  ORACLE

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.

Aggregate functions perform a variety of actions such as counting all the rows in a table,
Algorithm
averaging a column's data, and summing numeric data.
/Theory
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.

The SELECT query below demonstrates the use of NVL function.

SELECT first_name, last_name, salary, NVL (commission_pct,0)

FROM employees

Syntax WHERE rownum < 5;

SUM( [ALL | DISTINCT] expression )

AVG( [ALL | DISTINCT] expression )

COUNT( [ALL | DISTINCT] expression )


COUNT(*)

MAX(expression)

MIN(expression)

Post Lab
Assignment
(If Any)
OUTPUT

SINGLE ROW FUNCTIONS


CHARACTER

NUMERIC

DATE FUNCTIONS

GROUP FUNCTIONS
SUM

AVG
MAX
MIN
COUNT
Experiment 6(a)
Title SET Operators.

Objective Study of various type of SET OPERATORS (Union, Intersect, Minus).

Pre- Knowledge of
requisite  Concept of SET Operators.

Set Operation in SQL


Algorithm
SQL supports few Set operations to be performed on table data. These are used to get
/Theory
meaningful results from data, under different special conditions.

select * from First

Syntax UNION

select * from second


Post Lab
Assignment
(If Any)

OUTPUT:
Experiment 6(b)
Title Joins

Objective Study of Various type of JOINS.

Pre- Knowledge of
requisite  ORACLE COMMANDS

SQL JOIN

An SQL JOIN clause is used to combine rows from two or more tables, based on a
common field between them.

The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER
JOIN return all rows from multiple tables where the join condition is me. SQL INNER
JOIN Keyword

The INNER JOIN keyword selects all rows from both tables as long as there is a match
between the columns in both table

SQL LEFT JOIN Keyword


Algorithm The LEFT JOIN keyword returns all rows from the left table (table1), with the matching
/Theory rows in the right table (table2). The result is NULL in the right side when there is no
match.

SQL 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 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 INNER JOIN Syntax

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;

SQL LEFT JOIN Syntax

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
Syntax
SQL RIGHT JOIN Syntax

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;

SQL FULL OUTER JOIN Syntax

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;

Post Lab
Assignment
(If Any)
OUTPUT:
INNER JOIN

LEFT JOIN
RIGHT JOIN

FULL OUTER JOIN


Experiment 7
Title Subqueries

Objective Study and implement the concept of sub queries.

Pre- Knowledge of
requisite  ORACAL COMMANDS

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 but the subquery is use by the following statement.

It can be used by the following commands:

1. To insert records in the target table.

2. To create tables and insert records in this table.

Algorithm 3. To update records in the target table.

/Theory 4. To create view.

5. To provide values for the condition in the WHERE, HAVING IN, SELECT,
UPDATE, and DELETE statements.

Exam:-

Creating clientmaster table from oldclient_master, table

Create table client_master

AS SELECT * FROM oldclient_master;

Union Clause:

The user can put together multiple queries and combine their output using the union
Syntax clause. The union clause merges the output of two or more queries into a single set of
rows and column. The final output of union clause will be
Output: = Records only in query one + records only in query two + A single set of records
with is common in the both queries.

Syntax:

SELECT columnname, columname

FROM tablename 1

UNION

SELECT columnname, columnname

From tablename2;

Intersect Clause:

The use can put together multiple queries and their output using the interest clause. The
final output of the interest clause will be:

Output =A single set of records which are common in both queries

Syntax:

SELECT columnname, columnname

FROM tablename 1

INTERSECT

SELECT columnname, columnname

FROM tablename 2;

Minus Clause:-

The user can put together multiple queries and combine their output = records only in
query one

Syntax:

SELECT columnname, columnname

FROM tablename ;

MINUS
SELECT columnname, columnname

FROM tablename ;

Post Lab
Assignment
(If Any)

OUTPUT:
CREATING TABLE
UNION

INTERSECT
MINUS
Experiment 8
Title Control languages

Study and implement the concept of Data Control Language (DCL), Transaction Control
Objective
Language (TCL).

Pre- Knowledge of
requisite  ORACAL COMMANDS

TCL command

Transaction Control Language (TCL) commands are used to manage transactions in


database. These are used to manage the changes made by DML statements. It also allows
statements to be grouped together into logical transactions.

Commit command

Algorithm Commit command is used to permanently save any transaction into database.

/Theory Rollback command

This command restores the database to last commited state. It is also use with savepoint
command to jump to a savepoint in a transaction.

Savepoint command

savepoint command is used to temporarily save a transaction so that you can rollback to
that point whenever necessary.

commit;

Syntax rollback to savepoint-name;

savepoint savepoint-name;

Post Lab
Assignment
(If Any)
OUTPUT:
SAVEPOINT
ROLLBACK
Experiment 9
Title Views

Objective Study of Simple and Complex View.

Pre- Knowledge of
requisite  ORACLE COMMANDS

CREATE VIEW Statement

In SQL, a view is a virtual table based on the result-set of an SQL statement.

A view contains rows and columns, just like a real table. The fields in a view are fields
Algorithm from one or more real tables in the database.
/Theory You can add SQL functions, WHERE, and JOIN statements to a view and present the
data as if the data were coming from one single table.

Note: A view always shows up-to-date data! The database engine recreates the data,
using the view's SQL statement, every time a user queries a view.

SQL CREATE VIEW Syntax

CREATE VIEW view_name AS


SELECT column_name(s)
FROM table_name
WHERE condition

Renaming the columns of a view:-


Syntax 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:-

DROP VIEW viewname;

Post Lab
Assignment
(If Any)
OUTPUT:
CREATE TABLE

CREATING VIEW
RENAMING VIEW

SELECTING DATA FROM VIEW

DROP VIEW

You might also like