Dbms Lab Manual
Dbms Lab Manual
AIM:
To implement database creation and do the operations in database using Data Definition
Commands
ALGORITHM:
Step 1: Create table by using create table command with column name, data type and size.
Step 2: Display the table by using desc command.
Step 3: Add any new column with the existing table by alter table command.
Step 4: Modify the existing table with modify command.
Step 5: Delete the records in files by truncate command.
Step 6: Delete the Entire table by drop command.
1) CREATE TABLE
Syntax:
Example:
Syntax:
desc table_name;
Example:
desc employee;
3) ALTER TABLE
Syntax:
alter table table_name add column_name datatype;
Example:
alter table employee add dept varchar(10);
table employee altered
Example:
alter table employee add (state varchar(100),weight float);
table employee altered.
4) RENAME
To rename column name
Syntax:
alter table table_name rename column existing _name to new_name;
Example:
alter table employee rename column address to addr;
table employee altered.
Example:
alter table employee rename to empl;
table employee altered.
5) TRUNCATE
Syntax:
truncate table table_name;
Example:
truncate table emp;
select * from empl;
no data found
6) DROP TABLE
Syntax:
drop table table_name;
Example:
PERFORMANCE 25
VIVA 10
RECORD 15
TOTAL 50
RESULT:
Thus the queries to implement data definition commands are executed successfully.
EX.NO:1b DATA MANIPULATION COMMANDS
DATE:
AIM:
To implement and execute a query for manipulating & storing data items in a Oracle
database using Structured Query Language commands
ALGORITHM:
Step 1: Create table by using create table command.
Step 2: Insert values into the table
Step 3: Delete any records from the table
Step 4: Update any values in the table.
Step 5: Display the values from the table by using select command.
1) CREATE A TABLE
desc student;
2) INSERT
Syntax:
insert into table_name values(column1,column2,….);
Example:
insert into student values(1,'ABC','CSE');
insert into student values(2,'DEF','CSE');
select * from student;
Example:
insert into student values(3,’alex’,default);
1 rows inserted.
select * from student;
Example:
create table stud(id int,Name varchar(30),Department varchar(10));
insert into stud select * from student;
3) UPDATE
Syntax:
update table_name set column1 = value1, column2 = value2. .. , columnn = valuen
where [condition];
Example: (single column)
4) DELETE
Example:
delete from stud where id=2;
Syntax:
delete from table_name;
Example:
delete from stud;
select * from stud;
no data found
PERFORMANCE 25
VIVA 10
RECORD 15
TOTAL 50
RESULT:
Thus, a query for manipulating & storing data items in an Oracle database using
Structured Query Language commands were implemented and executed successfully.
EX.NO:2 CREATE SET OF TABLES ADD FOREIGN KEY CONSTRAINT AND
DATE: INCORPORATE REFERENTIAL INTEGRITY
Aim:
To create set of tables add foreign key constraint and incorporate referential integrity using SQL.
Algorithm:
Create two tables with a link that will not work.
Create two tables with a link that will work.
Add a foreign key to enforce referential integrity between the two tables.
Delete data from the tables.
Update and remove data to show how the foreign key protects the data.
Use the cascade option of the foreign key.
Companies table: List of Companies
Employees table: List of Employees working at the Company
Rule: Employees can only work at one Company, and a Company can employ multiple
Employees
Program:
Step 1:
--Create the HRDatabase
USE master
GO
DROP DATABASE IF EXISTS HRDatabase
GO
CREATE DATABASE HRDatabase
GO
USE HRDatabase
GO
Step 2:
-1st Try: Create a Companies and an Employees table and link them together
DROP TABLE IF EXISTS Companies;
GO
DROP TABLE IF EXISTS Employees;
GO
-- SQL CREATE TABLE Statement
CREATE TABLE Companies (
ID INT CONSTRAINT PK_Companies PRIMARY KEY IDENTITY,
CompanyName VARCHAR(80) NOT NULL, -- column name, data types and null value
CompAddress VARCHAR(80) NOT NULL,
CompContactNo VARCHAR(20) NOT NULL,
EmpID INT NOT NULL,
CreateDate DATETIME NOT NULL constraint DF_Companies_CreateDate DEFAULT
getdate()
)CREATE TABLE Employees (
ID INT CONSTRAINT PK_Employees PRIMARY KEY IDENTITY,
EmployeeName VARCHAR(80) NOT NULL,
ContactNo VARCHAR(20) NOT NULL,
Email VARCHAR(80) NOT NULL,
CreateDate DATETIME NOT NULL constraint DF_Employees_CreateDate DEFAULT
getdate()
)
INSERT INTO Companies (CompanyName, CompAddress, CompContactNo, EmpID)
VALUES
('Alpha Company', '123 North Street, Garsfontein, Pretoria', '091 523 6987' , 1),
('Bravo Company', '456 South Street, Brooklyn, Pretoria', '091 523 4789' , 1),
('Charlie Company' , '987 West Street, Lynnwood, Pretoria', '091 523 1235' , 1),
('Delta Company', '258 East Street, The Meadows, Pretoria', '091 523 7414' , 1),
('Echo Company', '100 Amber Street, Hatfield, Pretoria', '091 523 9685' , 1)
INSERT INTO Employees (EmployeeName, ContactNo, Email) VALUES
('Joe Blogs' , '012 365 4789', '[email protected]') ,
('Jane Doe' , '012 365 4789', '[email protected]') ,
('John Smit' , '012 365 4789', '[email protected]') ,
('Eddy Jones' , '012 365 4789', '[email protected]'),
('Steve Dobson', '012 365 4789', '[email protected]')
OUTPUT :
3
PERFORMANCE 25
VIVA 10
RECORD 15
TOTAL 50
RESULT:
Thus, to create a set of tables add foreign key constraint and incorporate referential integrity were
implemented and executed successfully.
Ex: no: 3 DATABASE TABLES WITH DIFFERENT
Date: 'WHERE CLAUSE' CONDITIONS
Aim:
To using query the database tables with different 'where clause' conditions also implement aggregate
Function with algorithm.
Algorithm:
Step 1:
There are tables country and city. We’ll use the following statements:
SELECT *
FROM country;
SELECT *
FROM city;
You can see the result in the picture below:
Step 2:
SELECT *
FROM country
INNER JOIN city ON city.country_id = country.id;
SELECT *
FROM country
LEFT JOIN city ON city.country_id = country.id;
PERFORMANCE 25
VIVA 10
RECORD 15
TOTAL 50
RESULT:
Thus, to create the database tables with different 'where clause' conditions and also implement
aggregate Function with algorithm were implemented and executed successfully.
EX.NO:4 SIMPLE QUERIES, SUB QUERIES and JOINS
DATE:
AIM:
To implement Simple queries, Nested queries, Sub queries using structured query
language
ALGORITHM:
QUERIES:
create table salary(id int, Name varchar(20),Age int,Salary float);
insert into salary values(1,'Ajay',25,20000);
insert into salary values(2,'Archana',28,30000);
insert into salary values(3,'Archana',23,25000);
insert into salary values(4,'Sara',24,20000);
insert into salary values(5,'Ajay',25,40000);
insert into salary values(6,'Srinithi',26,35000);
select * from salary;
Sum function
Syntax:
select column_name1,sum(column_name2)from table_name group by column_name3;
Example:
select name, sum(salary) from salary group by name;
Min function
Syntax:
select column_name1,min(column_name2)from table_name group by column_name3;
Example:
select name, min(salary) from salary group by name;
Count function
Syntax:
select column_name1,count(column_name2)from table_name group by column_name3;
Example:
select name, count(salary) from salary group by name;
Mod function
Syntax:
select column_name1,mod(column_name2)from table_name;
Example:
select name,mod(salary,5) from salary;
Syntax:
select distinct column1, column2,. ... columnn from table_name;
Example:
select salary from salary order by salary;
HAVING clause
Example:
AND
Example:
select * from salary where age >= 25 and salary >= 25000;
OR
Example:
select * from salary where age >= 25 or salary >= 25000;
LIKE
select * from salary where name like 'Aj%';
BETWEEN
IS NOT NULL
IS NULL
(4,'Kumaran',25,'CBE',24000);
UPDATE CUSTOMERS
SET SALARY = SALARY * 0.25
WHERE AGE IN
(SELECT AGE FROM customers_backup
WHERE AGE >= 27 );
Select * from customers;
JOINS
1. INNER JOIN
Syntax:
SELECT table1.column1, table2.column2...
FROM table1
INNER JOIN table2
ON table1.common_filed = table2.common_field;
Example:
select id,name,oid,amount
2. LEFT JOIN:
Syntax:
Example:
select id,name,oid,amount
from cust
left join order1
on cust.id=order1.custid;
3. RIGHT JOIN:
Syntax:
select table1.column1, table2.column2...
from table1
right join table2
on table1.common_filed = table2.common_field;
Example:
select id,name,oid,amount
from cust
right join order1
on cust.id=order1.custid;
Example:
select id,name,oid,amount
from cust
full join order1
on cust.id=order1.custid;
PERFORMANCE 25
VIVA 10
RECORD 15
TOTAL 50
RESULT:
Thus, the SQL commands to execute simple queries, sub queries and joins are
executed successfully.
Aim
To write DQL Commands to join, Restrict and Retrieve information from one or more
tables execute it and verify the same.
SQL> select
employee.empid,employee.name,employee.depid,department.deptname,depa
rtment.location from employee,department where
employee.depid=department.depid;
6 rows selected.
2. Write a query to display the “dinesh” depid and deptname and verify it.
SQL> select e.empid,e.name,e.depid,d.deptname from employee
e,department d wheree.depid=d.depid and e.name='dinesh';
7 rows selected.
2. Write a query to display the name, salary,depid and deptname of
all employees. Make sure that departments without employees are
included as well and verify.
SQL> select e.name,e.salary,e.depid,d.deptname from employee
e,department d where e.depid(+)=d.depid;
PERFORMANCE 25
VIVA 10
RECORD 15
TOTAL 50
RESULT:
Thus the SQL commands to execute natural, equi and outer joins are executed successfully.
AIM:
To implement and execute Procedures in Oracle Database using Procedural Language
concepts.
PROCEDURES:
1) Procedure is a sub program used to perform an action.
2) Replace-recreates the procedure if it already exists.
3 MODES:
1) IN – Means you must specify a value for the argument at the time execution of the procedure.
3) INOUT – When calling the procedure, yu must specify the value and that procedures passes value back
to the calling procedure
CREATING A PROCEDURE
SYNTAX:
BEGIN
greetings;
END;
DELETING A PROCEDURE
SYNTAX:
EXAMPLE:
Procedure dropped
BEGIN
INSERTUSER(101,'Rahul');
dbms_output.put_line('record inserted successfully');
END;
PL/SQL FUNCTIONS
SYNTAX:
DECLARE
n3 number(2);
BEGIN
n3 := adder(11,22);
dbms_output.put_line('Addition is: ' || n3);
END;
Addition is: 33
Statement processed.
PERFORMANCE 25
VIVA 10
RECORD 15
TOTAL 50
RESULT:
Thus the PL/SQL procedures and functions are executed successfully
AIM:
To implement Transaction Control statements using structured Query Language
ALGORITHM:
1) CREATE A TABLE
create table employeepersonaldetails(id int primary key,name varchar(30) not null,age int not
null,mobilenumber int not null unique);
commit;
Statement executed
savepoint a;
savepoint created
savepoint b;
rollback to a;
PERFORMANCE 25
VIVA 10
RECORD 15
TOTAL 50
RESULT:
Thus Transaction Control and Data Control statements using structured Query Language is
executed successfully.
AIM:
To implement PL/SQL triggers and do the operations in database automatically.
ALGORITHM:
create or replace trigger trgrr15 after delete or update on emply101 for each row
begin
if deleting then
insert into emply102 values(:old.eid,:old.ename,:old.age,:old.salary);
elsif updating then
insert into emply102 values(:old.eid,:old.ename,:old.age,:old.salary);
end if;
end;
PERFORMANCE 25
VIVA 10
RECORD 15
TOTAL 50
RESULT:
Thus the SQL triggers are executed successfully and the output was verified successfully.
To write a DDL command to create views to restrict data access from one or more tables,execute
it and verify the same.
Creating views:
OUTPUT:
View created.
ID NAME SALARY
2. To create a view to set the salary Rs.15000/- to the employee id ‘a102’ in the
vw_dept_salary and verify the result in vw_emp12_sal and employees table.
SQL> update vw_emp12 set salary=15000 where
Thus the PL/SQL program for implementing views and index has been successfully executed.
AIM:
PROGRAM :
CREATE TABLE t1
(id NUMBER,
xml XMLTYPE;
COMMIT;
SELECT id,
XMLISVALID(xml, 'my_schema.xsd') AS is_valid
FROM t1;
ID IS_VALID
1 1
2 0
PERFORMANCE 25
VIVA 10
RECORD 15
TOTAL 50
RESULT:
Thus the XML Database and validate it using XML Schema is executed successfully.
Aim:
To Create a Document, column and graph based data using NOSQL database tools.
Algorithm:
Step 1:
There are tables country and city. We’ll use the following statements:
SELECT * FROM country;
SELECT *FROM city;
You can see the result in the picture below:
Step 2:
SELECT *FROM country
INNER JOIN city ON city.country_id = country.id;
SELECT COUNT(*) AS number_of_rows
FROM country
INNER JOIN city ON city.country_id = country.id;
Step 3:
package com.java21days;
import javax.swing.*;
Creating a Component
JButton play = new JButton("Play");
JButton stop = new JButton("Stop");
JButton rewind = new JButton("Rewind");
Adding Components to a Container
J Button quit = new JButton("Quit");
import javax.swing.*;
public class
ButtonFrame extends
new JButton("Load");
JButton("Save");
public ButtonFrame() {
super("Button Frame");
setSize(340, 170);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.add(load);
pane.add(save);
pane.add(unsubscribe);
add(pane);
setVisible(true);
}
PERFORMANCE 25
VIVA 10
RECORD 15
TOTAL 50
RESULT
Thus the real life database application for exam registration has been successfully executed.
AIM:
To implement an online exam registration page with database connectivity using php
ALGORITHM:
STEP 1: Develop a webpage which includes all the fields for registering for an online exam
STEP 2: Develop a PHP page which receives the values that are given as an input in the registration
pageSTEP 3: Connect the registration page with the PHP page
STEP 4: Receive the values from registration
pageSTEP 5: Establish a connection with the
database STEP 6: Store the received values in
the database
PROGRAM:
Registration.html
<html>
<body bgcolor="lightblue">
<p><center>REGISTRATION FORM</center></p>
<form action="Registration.php" method="post">
<center><pre><b> Student name: <input type="text" name="n" value="
"> Register Number: <input type="text" name="reg"
value=" ">
CGPA: <input type="text" name="cgpa"
value=" ">YEAR: <input type="text" name="y"
value=" ">
Branch: <input type="text" name="branch" value=" ">
<input type="submit" name="button" value="SUBMIT">
</b></center></pre>
</body>
</html>
Registration.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Online_exam";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
/*else
echo "Connection established";*/
$Name=$_POST['n'];
$RegNo=$_POST['reg'];
CS3481 DATABASE MANAGEMENT SYSTEMS LABORATORY/ II YEAR / IV SEM /
$CGPA=$_POST['cgpa'];
$Year=$_POST['y'];
$Branch=$_POST['branch'];
/*echo
$Name;
echo
$RegNo;
echo
$CGPA;
echo $Year;
echo
$Branch;*/
$sql = "CREATE TABLE regDetails(Name varchar(30) not null, RegNo int(15) not null,CGPA
floatnot null, Year varchar(5) not null, Branch varchar(5) not null)";
VIVA 10
RECORD 15
TOTAL 50
RESULT:
Thus the real life database application for exam registration has been successfully executed.
PERFORMANCE 25
VIVA 10
RECORD 15
TOTAL 50
Result:
Thus, the program was executed and output was verified successfully.