DBMS RECORD
DBMS RECORD
AIM
To create a table called "emp" with necessary columns and then perform the DDL
Operations without Constraints.
Algorithm
Queries
1.1 Create table
(
empno NUMBER,
empname VARCHAR2(25),
dob DATE,
salary NUMBER,
designation VARCHAR2(20)
);
The EmpNo column and Salary is of type number and will hold a Employee number and
Salary of an Employee.
The EmpName, Designation columns are of type varchar with a maximum length of 25
characters each.
Page |1
The empty "Emp" table will now look like this:
DESC emp;
Alter table
a. ADD
b. MODIFY
// To alter the table emp by modifying the size of the attribute department
ALTER TABLE emp MODIFY department VARCHAR2(100);
DESC emp;
c. DROP
d. RENAME
DESC emp1;
3. DROP
Page |2
OUTPUT
Page |3
Page |4
RESULT
The DDL queries are used successfully to create and delete the table in SQL.
Page |5
EXP: NO: 02
DDL Operations with Constraints
DATE: 24/08/23
AIM
To create a table called "emp" with necessary columns and then perform the DDL
Operations with Constraints.
Constraints Types
✓ NOT NULL
✓ UNIQUE
✓ PRIMARY KEY
✓ FOREIGN KEY
✓ CHECK
✓ DEFAULT
Algorithm
Queries
CREATE THE TABLE
// To create a table student
);
Page |6
DESC student;
);
//Describe the table exam
Desc Exam;
A. ADD
DESC student;
//To alter the table student by adding new constraint to the examID attribute
ALTER TABLE exam CONSTRAINT pr PRIMARY KEY (examID);
B. MODIFY
//To alter the table student by modifying the size of the attribute address
DESC student;
C.DROP
Page |7
//To alter the table student by deleting the attribute address
DESC student;
D.RENAME
Table altered
DESC student1;
• Table altered
3. DROP
DESC exam;
Page |8
OUTPUT
Page |9
P a g e | 10
Result:
Thus all the above DDL SQL commands has been executed successfully and the output
was verified.
P a g e | 11
EXP: NO: 03
DML Operations
DATE: 31/08/23
AIM
To create a table named "BOOKS” with necessary columns and then perform the DML
Operations.
Algorithm
DML Statements
➢ Insert into
➢ Update
➢ Delete
1. Insert into
c. Another form of insertion is Macro Substitution, this query is used to receive values at
runtime.
P a g e | 12
Insert into table_name (Columnname1, columnname2,…..)
values(&columnname1,&columnname2, ….);
2. Update
Update new data into an existing table
Syntax
UPDATE table_name SET column1=value, column2=value2,... WHERE
some_column=some_value;
3. Delete Query
Syntax
Queries
1. Create table
SQL>Create table Books (
BookNo Number(5),
BookName varchar2(25),
AuthName varchar2(25),
Publisher varchar2(25),
Year Number(3) );
2. Insert
SQL>Insert into books values (101,‘Professional_C#’,‘Christian_Nagel’,
‘Tata’,2008);
- 1 row created
P a g e | 13
Enter BookName : OOPs
Enter AuthName : Balagurusamy
Enter Publisher : Tata
Enter Year : 2009
- 1 row created
- 1 row updated
- 1 row created
No rows selected
P a g e | 14
OUTPUT
P a g e | 15
RESULT
The DML queries are used successfully to insert a new row, update existing value and
deleting a row in the table.
P a g e | 16
EXP: NO: 04
Data Query Projection SQL commands
DATE: 31/08/23
AIM
Algorithm
Queries
(
studentID NUMBER PRIMARY KEY, sname VARCHAR2(30) NOT NULL,
department CHAR(5), sem NUMBER, email_id VARCHAR2(20) UNIQUE, college
VARCHAR2(20) DEFAULT 'MEC'
);
// Describe the table student
DESC student;
1. SELECT ALL COLUMNS
P a g e | 17
5.DISTINCT RECORD SELECTION
// To display the college of the student from the table student by avoiding repeated
values.
// To display the records from the table student who belongs to mec college.
SELECT * FROM student WHERE college='SKCET';
P a g e | 18
OUTPUT
P a g e | 19
P a g e | 20
Result:
Thus all the above Data Query Projection SQL commands has been executed
successfully and the output was verified.
P a g e | 21
EXP: NO: 05
Selection commands Operations
DATE: 07/09/23
AIM
To create a table named "STUDENT" with necessary columns and then perform the
Selection commands Operations.
Selection Commands
✓ Between…and
✓ In
✓ Not in
✓ Like
✓ Relational Operators
✓ Logical Operators
Queries
(
studentID NUMBER PRIMARY KEY, sname VARCHAR2(30) NOT NULL,
department CHAR(5), sem NUMBER, email_id VARCHAR2(20) UNIQUE, college
VARCHAR2(20) DEFAULT 'MEC'
);
// Describe the table student
DESC student;
1. SELECT ALL COLUMNS
// To display the student id, student name and department of the student
whose the semester in between 4 and 8
P a g e | 22
SELECT studentid,sname,department FROM student WHERE sem BETWEEN 4 AND 8;
// To display the student id, student name and department of the student
whose in CSE and IT department
// To display the student id, student name and department of the student whose
not in CSE department
// To display the student id and student name of the student whose name starts letters
'U' from the table
student
// To display the student id and student name of the student whose name has
letters 'el' from the table student
SELECT studentid, sname FROM student WHERE sname LIKE '%EL%';
// To display the student id, student name of the student whose the student id
greater than 2
P a g e | 23
OUTPUT
P a g e | 24
P a g e | 25
Result:
Thus all the above Data Query Selection SQL commands has been executed
successfully and the output was verified
P a g e | 26
EXP: NO: 06
DATE: 07/09/23 PL/SQL
PL/SQL
Advantages of PL/SQL
P a g e | 27
Example
Declare
salary number(5);
Conditional Statement
Iterative Statement
Simple Loop
LOOP
statements;
EXIT;
{or EXIT WHEN condition;}
END LOOP;
LOOP statements;
P a g e | 28
END LOOP;
FOR Loop
FOR counter IN val1..val2
LOOP statements; END
LOOP;
P a g e | 29
3 begin
4 loop
5 a:=a+25;
6 exit when a=250;
7 end loop;
8 dbms_output.put_line(to_char(a));
9 end;
10 /
P a g e | 30
OUTPUT-1:
OUTPUT-2:
P a g e | 31
OUTPUT-3:
Result:
Thus all the above Data Query Selection SQL commands has been executed
successfully and the output was verified
P a g e | 32
EXP: NO: 07
DATE: 09/09/23 PL/SQL find prime number
AIM
To write and execute a program in PL/SQL to find the prime number.
SQL> declare
2 no number(3):=&no;
3 a number(4);
4 b number(2);
5 begin
6 for i in 2..no-1
7 loop
8 a:=no MOD i;
9 if a=0
10 then
11 GOTO out;
12 end if;
13 end loop;
14 <<out>>
15 if a=1
16 then
17 dbms_output.put_line(no||'is a prime');
18 else
19 dbms_output.put_line(no||'is not a prime');
20 end if;
21* end;
22 /
P a g e | 33
OUTPUT:
RESULT
Thus the program to find the given number is prime or not was executed successfully.
P a g e | 34
EXP: NO: 08
DATE: 09/09/23 PL/SQL to Add two numbers
AIM
To write and execute a program in PL/SQL to Add two numbers.
P a g e | 35
OUTPUT:
RESULT
P a g e | 36
EXP: NO: 09
DATE: 14/09/23 PL/SQL to Find greatest among two numbers
AIM
To write and execute a program in PL/SQL to Find greatest among two numbers.
P a g e | 37
OUTPUT:
RESULT
Thus the program to find greatest among three numbers was executed successfully.
P a g e | 38
EXP: NO: 10
Reverse a given number
DATE: 14/09/23
AIM
To write a program to reverse a given number.
P a g e | 39
OUTPUT:
RESULT
Thus the program to Reverse a Number was executed Successfully
P a g e | 40
EXP: NO: 11
DATE: 16/09/23 Sum of odd numbers
AIM
To write a program to calculate the sum of odd numbers.
P a g e | 41
OUTPUT:
RESULT
Thus the program to calculate the sum of odd number was executed successfully.
P a g e | 42
EXP: NO: 12
DATE: 16/09/23 Net pay
AIM
To write a program to calculate the Net pay.
P a g e | 43
30 /
OUTPUT:
RESULT
Thus the program to calculate the Net Pay was executed successfully
P a g e | 44
EXP: NO: 13
DATE: 21/09/23 Calculate area and record values
AIM
To write a program to calculate the area and to record the values.
P a g e | 45
OUTPUT:
RESULT
Thus the program to Calculate the Area and Recording the values was executed
successfully.
P a g e | 46
EXP: NO: 14
DATE: 21/09/23 Cursor program
AIM
To access the tables using the Cursor program.
Table created.
P a g e | 47
7 is
8 select cno from ttt where cname=cnam;
9 begin
10 open c1;
11 fetch c1 into cid;
12 if c1%notfound then
13 cid:=9999;
14 end if;
15 close c1;
16 return cid;
17 end; 18 /
Function created.
P a g e | 48
OUTPUT:
P a g e | 49
RESULT
Thus the program for cursor was created in PL/SQL and executed successfully.
P a g e | 50
EXP: NO: 15
DATE: 05/10/23 Package in PL/SQL
AIM
PACKAGE
Creating a Package:
SQL> create or replace package emp_objects
2 as
3 cursor emp_list return employee1%rowtype;
4 function seniors(dept in varchar2) return varchar2;
5 procedure high_sal(dept in number, eno out empl.eno%type, ename out
empl.name%type);
6 end; 7 /
Package created.
Creating a Package Body
SQL> create or replace package body emp_objects as
2 cursor emp_list return employee1%rowtype is select * from employee1;
3 function seniors(dept in varchar2) return varchar2 is
4 cursor a is select * from employee1 where dt_join=(select min(dt_join) from
employee1 where deptid=dept); 5 a_var a%rowtype;
6 begin
7 open a;
8 fetch a into a_var;
9 if a%notfound then
10 return' No employees in the dept.';
11 else
12 return a_var.empno||','||a_var.name;
13 end if;
14 end;
15 procedure high_sal(dept in number, eno out empl.eno%type, ename out
empl.name%type) is
16 cursor a is select eno, name from empl where basic=(select max(basic) from empl
where deptno=dept);
17 begin
18 open a;
19 fetch a into eno, ename;
P a g e | 51
20 close a;
21 end;
22 end emp_objects;
23 /
Package body created.
Calling the Package
declare
eno employee1.empno%type;
ename
employee1.name%type; a_var
employee1%rowtype; name
varchar2(30); begin
for c in emp_objects.emp_list loop
emp_objects.high_sal(a_var.deptid, eno, ename);
dbms_output.put_line(a_var.deptid||' '||eno||'
'||ename); end loop;
for a1 in (select dept from departments)
loop
name:=senior(a1.dept);
dbms_output.put_line(a1.dept||' '||name);
end loop;
end;
/
SQL> @ packfunc;
d1 102,kenneth d2
101,erich d3
105,clive
d4 106,robin
RESULT
Thus the package was created in PL/SQL and executed successfully.
P a g e | 52
EXP: NO: 16
DATE: 12/10/23 PL/SQL to implement the Trigger
AIM
To write a program in PL/SQL to implement the Trigger.
TRIGGER
TRIGGER WITH BEFORE UPDATE
Create Table
SQL> create table orders(order_id number(5),quantity number(4),cost_per_item
number(6,2),total_cost number(8,2),updated_date date,updated_by varchar2(10));
Table created.
Inserting Records
SQL> insert into
orders(order_id,quantity,cost_per_item)values(&order_id,&quantity,&cost_per_item);
Enter value for order_id: 1
Enter value for quantity: 4
Enter value for cost_per_item: 20
old 1: insert into orders(order_id,quantity,cost_per_item)
values(&order_id,&quantity,&cost_per_it
new 1: insert into orders(order_id,quantity,cost_per_item) values(1,4,20)
1 row created.
SQL> /
Enter value for order_id: 2
Enter value for quantity: 5
Enter value for cost_per_item: 30
old 1: insert into orders(order_id,quantity,cost_per_item)
values(&order_id,&quantity,&cost_per_it
new 1: insert into orders(order_id,quantity,cost_per_item) values(2,5,30)
1 row created.
SQL> /
Enter value for order_id: 3
P a g e | 53
Enter value for quantity: 6
Enter value for cost_per_item: 25
old 1: insert into orders(order_id,quantity,cost_per_item)
values(&order_id,&quantity,&cost_per_it
new 1: insert into orders(order_id,quantity,cost_per_item) values(3,6,25)
1 row created.
SQL> select * from orders;
TRIGGER SCRIPT
P a g e | 54
ORDER_ID QUANTITY COST_PER_ITEM TOTAL_COST UPDATED_D
UPDATED_BY
---------- ---------- ------------- ---------- --------- ----------
1 4 20
2 5 30 3000 19-SEP-07 CSE3101
3 6 25
Table created.
SQL> create table orders_audit(order_id number,quantity_before
number,quantity_after number,username varchar2(20));
Table created.
Inserting Records
SQL> insert into orders30(order_id,quantity,cost_per_item)
values(&order_id,&quantity,&cost_per_item);
Enter value for order_id: 100
Enter value for quantity: 5
Enter value for cost_per_item: 10
old 1: insert into orders30(order_id,quantity,cost_per_item)
values(&order_id,&quantity,&cost_per_
new 1: insert into orders30(order_id,quantity,cost_per_item) values(100,5,10)
1 row created.
SQL> /
Enter value for order_id: 101
P a g e | 55
Enter value for quantity: 4
Enter value for cost_per_item: 20
old 1: insert into orders30(order_id,quantity,cost_per_item)
values(&order_id,&quantity,&cost_per_
new 1: insert into orders30(order_id,quantity,cost_per_item) values(101,4,20)
1 row created.
SQL> /
Enter value for order_id: 102
Enter value for quantity: 5
Enter value for cost_per_item: 30
old 1: insert into orders30(order_id,quantity,cost_per_item)
values(&order_id,&quantity,&cost_per_
new 1: insert into orders30(order_id,quantity,cost_per_item) values(102,5,30)
1 row created.
P a g e | 56
20 end;
21 /
Trigger created.
1 row updated.
RESULT
P a g e | 57
EXP: NO: 17
DATE: 14/10/23 PL/SQL to implement the Exception concept
AIM
To write the program in PL/SQL to implement the Exception concept.
EXCEPTIONS
Pre-defined Exception:
declare
ename employee.ename%type;
begin select ename into ename from
employee;
dbms_output.put_line('Employee
Name'); exception
when too_many_rows then
dbms_output.put_line('Multiple rows assigned to a single
variable'); end;
/
SQL> @ build_excep;
Multiple rows assigned to a single variable
User-Define Exception :
Checking the Integrity Constraing Violation:
declare
not_null_vio exception;
pragma exception_init(not_null_vio,-
1400); ename varchar2(15); dept
varchar2(5); designation varchar2(10);
basic number(8,2); begin
P a g e | 58
ename:='&emp_name';
dept:='&department';
designation:='&designation';
basic:=&basic; insert into
employee
(ename,dept,designation,basic)
values(ename,dept,designation,basic);
exception
when not_null_vio then dbms_output.put_line('Violation of Integrity constraint-not
null value needed!!!');
end;
/
SQL> @ excep;
P a g e | 59
end if; exception when incorrect_desig then
dbms_output.put_line('You have entered the wrong
designation'); dbms_output.put_line('Valid values are
analyst, leader'); dbms_output.put_line(to_char(sqlcode));
dbms_output.put_line(sqlerrm); end;
/
Enter value for new_desig: manager
old 5: new_desig:= '&new_desig';
new 5: new_desig:= 'manager'; You
have entered the wrong designation
Valid values are analyst, leader
1
User-Defined Exception
RESULT
P a g e | 60
EXP: NO: 18
DATE: 19/10/23 Aggregate Functions
AIM
To create a table named "Employee" with necessary columns and then perform
the Aggregate Functions.
Types
Order By
Group By
Aggregate Functions
P a g e | 61
OUTPUT
P a g e | 62
RESULT
Thus the queries for Aggregate functions are executed successfully.
P a g e | 63
EXP: NO: 19
DATE: 26/10/23 Extract Data from Multiple Tables using SQL Joins
AIM
Self Join
A self join is a join of a table to itself. The same table appear twice after the
FROM clause.
Cartesian Product
If in the joins statement you forget to put the WHERE condition or intentionally
don’t write the WHERE clause at all, the result will be Cartesian product
Equi-Join
Equi comes because of the use of Equal sign (=) in the join condition.
The query containing Equijoin will give you total number of records equal to
or less than the number of records of the one table among all the tables in the query,
having least number of records in it.
P a g e | 64
// Table Creation - placement
CREATE TABLE placement
(
PlacementID NUMBER PRIMARY KEY, StudentID NUMBER,
department CHAR(5), Company VARCHAR2(30), salary NUMBER
);
// INNER JOIN
SELECT *
FROM cseitstudent
RIGHT OUTER JOIN placement
ON cseitstudent.studentID=placement.studentID;
SELECT cseitstudent.sname,placement.placementID, placement.company
FROM cseitstudent RIGHT OUTER JOIN placement
ON cseitstudent.studentID = placement.studentID;
//FULL OUTER JOIN
P a g e | 65
SELECT * FROM cseitstudent
FULL OUTER JOIN placement
ON cseitstudent.studentID=placement.studentID;
SELECT cseitstudent.sname,placement.placementID, placement.company FROM
cseitstudent
//TABLE CREATION
//EQUI JOIN
P a g e | 66
OUTPUT
P a g e | 67
P a g e | 68
Result:
Thus all the above Joins SQL commands has been executed successfully and
the output was verified.
P a g e | 69
EXP: NO: 20
Subquery and Nested Subqueries
DATE: 02/11/23
AIM
To create table and to perform subquery and nested subqueries.
// EXISTS
P a g e | 70
OUTPUT
P a g e | 71
Result:
Thus all the above SQL nested queries has been executed successfully and the
output was verified.
P a g e | 72
EXP: NO: 21
DATE: 04/11/23 Set Operations
AIM
To create tables named "Author" and “Publisher” with necessary columns and
then to perform Set operations.
Set Operations
The set operations union, intersect, and except operate on relations and
correspond to the relational algebra operations È, Ç, -.
Each of the above operations automatically eliminates duplicates.
);
P a g e | 73
Select * from exam;
// To display student id using union - displays all values without duplicates from
student and exam
SELECT studentid FROM student UNION ALL SELECT studentid FROM exam;
INTERSECT
SELECT studentid FROM exam;
// To display student id using minus - displays the values that minus the student
id of student from exam
STUDENTID
102
P a g e | 74
OUTPUT
P a g e | 75
Result:
Thus all the above SQL Set Operation queries has been executed successfully and
the output was verified.
P a g e | 76
EXP: NO: 22
DATE: 09/11/23 View table with necessary columns
AIM
1)View Creation:
View created.
SQL> select * from bookview;
SQL> create view bookv (title, author, price) as select title, author, price from book
where price between 100 and 300; View created.
SQL> select * from bookv;
2)Record Insertion:
SQL> insert into bookv (title,author,price) values('&title','&author',&price);
SQL> select * from bookv;
SQL> select * from bookview;
3)Record Deletion:
SQL> delete from bookview where
author='kumar'; 1 row deleted.
SQL> delete from bookview where publisher='TATA';
1 rows deleted.
SQL> select * from bookview;
4)View Deletion:
SQL> drop view bookv; View
dropped.
SQL> select * from bookv;
select * from bookv
P a g e | 77
OUTPUT
P a g e | 78
RESULT
Thus a view was created for a table and some operations are done with the help of
view on the table.
P a g e | 79
EXP: NO: 23
DATE: 16/11/23 STUDENT INFORMATION SYSTEM
AIM
P a g e | 80
{
dept.Items.Add("IT");
dept.Items.Add("CSE");
dept.Items.Add("ECE");
dept.Items.Add("EEE");
dept.Items.Add("MECH");
sem.Items.Add("I");
sem.Items.Add("II");
sem.Items.Add("III");
sem.Items.Add("IV");
sem.Items.Add("V");
sem.Items.Add("VI");
sem.Items.Add("VII");
sem.Items.Add("VIII");
year.Items.Add("1");
year.Items.Add("2");
year.Items.Add("3");
year.Items.Add("4");
}
private void save_Click(object sender, EventArgs e)
{ try
{
String g;
if (male.Checked ==
true) g = "male";
else
g="femal
e";
conn.Open();
cmd = new OracleCommand("Insert into
P a g e | 81
exam(id,name,gen,dep,yr,sems,sub1,sub2,sub3,tot,cg,rest) values('" +
regno.Text + "','" + name.Text + "','"+dept.Text+"','"+g+"','"+year.Text+"',
'"+sem.Text+"','"+m1.Text+"','"+m2.Text+"','"+m3.Text+"',
'"+total.Text+"','"+cgpa.Text+"','"+res.Text+"')", conn);
MessageBox.Show("Records Inserted
Successfully");
cmd.ExecuteNonQuery();
conn.Close();
}
P a g e | 82
string ssem = dr["sems"].ToString(); string sm1 =
dr["sub1"].ToString(); string sm2=
dr["sub2"].ToString(); string sm3 =
dr["sub3"].ToString(); string stot= dr["tot"].ToString();
string scgpa = dr["cg"].ToString(); string sres=
dr["rest"].ToString(); name.Text = sname;
dept.Text=sdept;
year.Text=syear;
sem.Text=ssem;
m1.Text=sm1; m2.Text=sm2;
m3.Text=sm3; total.Text=stot;
cgpa.Text=scgpa; res.Text = sres;
}
} dr.Close();
cmd.Dispose();
conn.Dispose();
}
{
MessageBox.Show(ex.Message);
}
}
P a g e | 83
c = (t)/29 ;
if (mar1>=50 && mar2>=50 && mar3 >= 50)
rest = "Pass";
else
rest = "Fail"; total.Text =
t.ToString(); cgpa.Text =
c.ToString(); res.Text = rest;
}
P a g e | 84
}
//SELECT
//INSERT
//Select
//Delete
//Select
//Report
P a g e | 85
OUTPUT:
P a g e | 86
P a g e | 87
Result:
Thus the above Student Information System Application has been created
successfully using SQL queries and the output was verified.
P a g e | 88