Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

RDBMS Lab Manual

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

RDBMS Lab

Manual

Vidya Vihar Institute of Technology

BIADA Industrial Growth Centre ,Maranga, NH 31 Purnea , Bihar, India – BCA

IV SEMESTER

LAB MANUAL

Prepared By:
Sheetal Singh
(Assistant Professor , Department of Computer Science and Engineering)

P a g e 1 | 12
DBMS Lab Manual PCC CS 501

LIST OF EXPERIMENTS:

Exp. No. Title

1 Data Definition Language (DDL) commands in RDBMS

2 Data Manipulation Language (DML) and Data Control


Language (DCL)
3 Triggers
4 Joining Tables
5 Views
6 Subqueries
7 Advanced SELECT statements
8 Database design using E-R model and Normalization
9 High level language extensions with cursors
10 Project

2
DBMS Lab Manual PCC CS 501

EX.NO:1 Data Definition Language (DDL) commands in RDBMS.

AIM:
To create a DDL to perform creation of table, alter, modify and drop column.
DDL COMMANDS
1. The Create Table Command: - it defines each column of the table uniquely. Each column
has minimum of three attributes, a name , data type and size.

Syntax:
Create table <table name> (<col1> <datatype>(<size>),<col2> <datatype><size>));

Ex:create table emp(empno int(4) primary key, ename char(10));

2. Modifying the structure of tables.


a) Add new columns
Syntax:
Alter table <tablename> add(<new col><datatype(size),<new col>datatype(size));
Ex: alter table emp add(sal float(7,2));
3. Dropping a column from a table.

Syntax:
Alter table <tablename> drop column <col>;
Ex: alter table emp drop column sal;
4. Modifying existing columns.

Syntax:
Alter table <tablename> modify (<col><newdatatype>(<newsize>));
Ex: alter table emp modify ename varchar(15);

5. Renaming the tables

Syntax:
Rename <oldtable> to <new table>;

Ex: rename table emp to emp1;

6. truncating the tables.


Syntax:

3
DBMS Lab Manual PCC CS 501

Truncate table <tablename>;


Ex: TRUNCATE TABLE emp1;

CREATION OF TABLE:
SYNTAX:
create table<tablename>(column1 datatype,column2 datatype...);
EXAMPLE:
SQL> create table std (sno int(5), sname varchar(20),age int(5),sdob date,sm1 float(4,2),sm2
float(4,2),sm3 float(4,4));

CREATE TABLE People(


id int NOT NULL AUTO_INCREMENT,

name varchar(45) NOT NULL,


occupation varchar(35) NOT NULL,
age int,
PRIMARY KEY (id)
);

Table created.
SQL> insert into std values(101,'AAA',16,'2020-01-03',80,90,0);

EX.NO:2 IMPLEMENTATION OF DML AND DCL COMMANDS


AIM:
To study the various DML commands and implement them on the database.
DML COMMANDS

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.
Q1: Insert a single record into dept table.
insert into dept values (1,'CSE','VVITPurnea');
Q2: Insert more than a record into People table using a single insert command.

4
DBMS Lab Manual PCC CS 501

INSERT INTO People VALUES


(102, 'Joseph', 'Developer', 30),
(103, 'Mike', 'Content Writer ', 28),
(104, 'Stephen', 'Scientist', 45);

Q3: Update the People.


UPDATE People

SET name = 'Mary', occupation = 'Content Writer'


WHERE id = 105;

Q4: Delete only whose name is Stephen.


DELETE FROM People WHERE name =’Stephen’;

EX.NO:3 IMPLEMENTATION OF TRIGGERS

A trigger in MySQL is a set of SQL statements that reside in a system catalog . It is a special
type of stored procedure that is invoked automatically in response to an event. Each trigger is
associated with a table, which is activated on any DML statement such as INSERT, UPDATE,
or DELETE.
A trigger is called a special procedure because it cannot be called directly like a stored
procedure. The main difference between the trigger and procedure is that a trigger is called
automatically when a data modification event is made against a table. In contrast, a stored
procedure must be called explicitly.
Syntax:
CREATE TRIGGER trigger_name
(AFTER | BEFORE) (INSERT | UPDATE | DELETE)
ON table_name FOR EACH ROW
BEGIN
--variable declarations
--trigger code
END;

5
DBMS Lab Manual PCC CS 501

Create Table:

CREATE TABLE employee(


name varchar(45) NOT NULL,
occupation varchar(35) NOT NULL,
working_date date,
working_hours varchar(10)
);

Insert values:
INSERT INTO employee VALUES
('Robin', 'Scientist', '2020-10-04', 12),
('Warner', 'Engineer', '2020-10-04', 10),
('Peter', 'Actor', '2020-10-04', 13),
('Marco', 'Doctor', '2020-10-04', 14),
('Brayden', 'Teacher', '2020-10-04', 12),
('Antonio', 'Business', '2020-10-04', 11);

Create Trigger on Table:


Next, we will create a BEFORE INSERT trigger. This trigger is invoked automatically insert
the working_hours = 0 if someone tries to insert working_hours < 0.

DELIMITER //
Create Trigger before_insert_empworkinghours
BEFORE INSERT ON employee FOR EACH ROW
BEGIN
IF NEW.working_hours < 0 THEN SET NEW.working_hours = 0;
END IF;
END //

6
DBMS Lab Manual PCC CS 501

EX.NO:4 JOINING DATABASE TABLES


Aim:
To Learn how to perform the following types of database joins
MySQL supports the following types of joins:

 Inner join
 Left join
 Right join
 Cross join

Inner Join: The MySQL INNER JOIN is used to return all rows from multiple tables where
the join condition is satisfied. It is the most common type of join.

Syntax:

SELECT columns

FROM table1

INNER JOIN table2

ON table1.column = table2.column;

CREATE TABLE members (


member_id INT AUTO_INCREMENT,
name VARCHAR(100),
PRIMARY KEY (member_id)
);

7
DBMS Lab Manual PCC CS 501

CREATE TABLE committees (


committee_id INT AUTO_INCREMENT,
name VARCHAR(100),
PRIMARY KEY (committee_id)
);

INSERT INTO members(name)


VALUES('John'),('Jane'),('Mary'),('David'),('Amelia');

INSERT INTO committees(name)


VALUES('John'),('Mary'),('Amelia'),('Joe');

SELECT
m.member_id,
m.name member,
c.committee_id,

c.name committee
FROM

members m
INNER JOIN committees c
ON c.name = m.name;

Left Outer Join:

The LEFT OUTER JOIN returns all rows from the left hand table specified in the ON
condition and only those rows from the other table where the join condition is fulfilled.

8
DBMS Lab Manual PCC CS 501

Syntax:

SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;

SELECT

m.member_id,
m.name member,
c.committee_id,
c.name committee

FROM
members m

LEFT JOIN committees c


ON c.name = m.name;

Right Outer Join:


The MySQL Right Outer Join returns all rows from the Right-hand table specified in the ON
condition and only those rows from the other table where he join condition is fulfilled.

9
DBMS Lab Manual PCC CS 501

Syntax:
SELECT columns
FROM table1
RIGHT [OUTER] JOIN table2
ON table1.column = table2.column;

SELECT

m.member_id,
m.name member,
c.committee_id,

c.name committee
FROM

members m
RIGHT JOIN committees c
ON c.name = m.name;

CROSS JOIN:

Unlike the inner join, left join, and right join, the cross join clause does not have a join
condition. The cross join makes a Cartesian product of rows from the joined tables. The cross
join combines each row from the first table with every row from the right table to make the
result set. Suppose the first table has n rows and the second table has m rows. The cross join
that joins the first with the second table will return nxm rows.

10
DBMS Lab Manual PCC CS 501

Syntax:
SELECT column-lists
FROM table1
CROSS JOIN table2;
………………………………………………………………………………………………….

SELECT

m.member_id,
m.name member,
c.committee_id,
c.name committee
FROM

members m
CROSS JOIN committees c;

EX.NO:5 Create views of the database

Views: View is a virtual table based on a SELECT query. The query can contain columns,
computed columns, aliases, and aggregate functions from one or more tables. The tables on
which the view is based are called base tables. You can create a view by using the CREATE
VIEW command:

Views syntax:
CREATE [OR REPLACE] VIEW view_name AS
SELECT columns
FROM tables
[WHERE conditions];

11
DBMS Lab Manual PCC CS 501

Create Table:
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

Create Views:
CREATE VIEW Customers AS
SELECT PersonID, Address
FROM Persons
WHERE City = 'Patna';

12

You might also like