RDBMS Lab Manual
RDBMS Lab Manual
RDBMS Lab Manual
Manual
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:
2
DBMS Lab Manual PCC CS 501
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>));
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);
Syntax:
Rename <oldtable> to <new table>;
3
DBMS Lab Manual PCC CS 501
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));
Table created.
SQL> insert into std values(101,'AAA',16,'2020-01-03',80,90,0);
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
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:
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);
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
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
ON table1.column = table2.column;
7
DBMS Lab Manual PCC CS 501
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;
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
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;
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