Anda di halaman 1dari 34

RDBMS lab Roll no-17040

Practical-1
Data types and Table creation

Query 1- create table department

Command- Create table department1704064


(d_no integer primary key,
d_name varchar(10) not null ,
d_head varchar(15) Default(‘prince_verma’));
Output-

Query 2-create table student

Command- Create table student1704064


(rollno integer primary key,
varchar(15) not null,
deptno integer,
DOB integer,
gender varchar(6) check(gender in(‘male’,’female’));
Output-

Page no - 1
RDBMS lab Roll no-17040

Query 3-describe department table

Command-describe department1704064
Output-

Query 4-describe student table

Command-describe student1704064
Output-

Query 5-Create a copy of student table

Command-create table student17040641 as(select * from student_1704064)


Output-

Page no - 2
RDBMS lab Roll no-17040

Practical-2
Alter, Drop & Rename commands

Query 1-Add a column

Command- Alter table student_1704064 add semester varchar(10)


Output-

Query 2-Modify a column

Command- Alter table student_1704064 modify semester varchar(15);


Output-

Query 3-Rename a column

Command- Alter table student_1704064 rename column semester to sem

Page no - 3
RDBMS lab Roll no-17040

Output-

Query 4-Drop a column

Command- Alter table student_1704064 drop column semester


Output-

Query 5-Add constraint to a column

Command- Alter table student_1704064 add constraint semuk unique(semester);


Output-

Page no - 4
RDBMS lab Roll no-17040

Query 6-Drop constraint from a column

Command- Alter table student_1704064 drop constraint semuk;


Output-

Query 7-Drop the copied table

Command-drop table student17040681


Output-

Query 8-Rename a table

Command-rename table student1704064 to student_1704064


Output-

Page no - 5
RDBMS lab Roll no-17040

Practical-3
Insert commands

Query 1-Insert details into department table without using column names

Command- INSERT INTO department1704064 values (103,'EE','prince verma');


INSERT INTO department1704064 values (105,'AUE','prince verma');
INSERT INTO department1704064 values (106,'MBA','prince verma');
Output-

Query 2-Insert details into department table with using column name

Command- INSERT INTO department1704064 values (&d_no,'&d_name','&d_head')


Output-

Page no - 6
RDBMS lab Roll no-17040

Query 3-Insert details into student table

Command-insert into student_1704064 values


(&rollno, ‘&name’, &deptno, ‘&DOB’, ‘&gender’, &mobile_no);
Commit;
Output-

Page no - 7
RDBMS lab Roll no-17040

Practical-4
Delete, Update, Commit & Rollback commands

Query 1-Delete a row from student table

Command- Delete From student_1704064 where rollno=1011;


Commit;
Output-

Query 2-Update student details in student table

Command- update student_1704064 set d_no=102 where rollno=1004;


Commit;
Output-

Page no - 8
RDBMS lab Roll no-17040

Query 3-Save student details in student table by using commit

Command-commit;
Output-

Query 4-save department details in department table using commit

Command-commit;
Output-

Page no - 9
RDBMS lab Roll no-17040

Practical-5
Select commands

Query 1-Select all details of student from student table

Command-select * from student_1704064


Output-

Query 2-Select all details of department from department table

Command-select * from department1704064


Output-

Page no - 10
RDBMS lab Roll no-17040

Query 3-Select name, roll no, age of student from student table

Command-select name, rollno, (sysdate-DOB)/365 from student_1704064


Output-

Query 4-Select name, roll no of student from student table

Command-select name,rollno from student_1704064


Output-

Query 5-Select all constraints

Command-select * from user_constraints


Output-

Page no - 11
RDBMS lab Roll no-17040

Query 6-Select all constraints used in student table

Command-select constraint_name, constraint_type, search_condition, r_constraint_name


from user_constraints where table_name=‘STUDENT_1704064’

Output-

Page no - 12
RDBMS lab Roll no-17040

Practical-6
Operators: conditional retrieval of rows matching a pattern from a table

Query 1-Select student details using where condition

Command-select name, d_no from student_1704064 where (sysdate-DOB)/365>8


Output-

Query 2-Select student details using OR logical operator

Command-select name, d_no from student_1704064 where (d_no=101 or d_no=102)


Output-

Query 3-Select student details using AND logical operator

Command-select rollno, name, d_no from student_1704064 where (sysdate-DOB)/365>7


and (sysdate-DOB)/365<9
Output-

Page no - 13
RDBMS lab Roll no-17040

Query 4-Select student details using AND logical operator with two different conditions

Command- select name, d_no from student_1704064 where (sysdate-DOB)/365>8


and d_no=101
Output-

Query 5-Select student details using NOT logical operator

Command- select * from student_1704064 where not d_no=101


Output-

Query 6-Select student details using in clause

Command-select name, d_no from student_1704064 where d_no in (101,102)


Output-

Page no - 14
RDBMS lab Roll no-17040

Query 7-Select student details between two intervals (conditions)

Command-select rollno, name, d_no from student_1704064 where (sysdate-DOB)/365


between 7 and 9
Output-

Query 8-Select student details whose name starts with ‘A’

Command-select name, rollno, d_no from student_1704064 where name like ‘%a’
Output-

Query 9-Select student details whose name ends with ‘A’

Command- select name, rollno, d_no from student_1704064 where name like ‘a%’
Output-

Page no - 15
RDBMS lab Roll no-17040

Query 10-Select student details whose name containing char ‘A’

Command- select name, rollno, d_no from student_1704064 where name like ‘%a%’
Output-

Query 11-Select student details using NOT assignment operator

Command-select * from student_1704064 where d_no! = 101


Output-

Query 12-Display student details in the following format


‘Roll no has name student name’

Command-select rollno||‘has name’||name information from student_1704064


Output-

Page no - 16
RDBMS lab Roll no-17040

Practical-7
Single row functions: aggregate functions and ordering the result of a
query

Query 1-Select different date commands

Command-select months_between (sysdate, DOB), add_months (DOB, 7),


next_day (sysdate, ‘Monday’), last_day (DOB) from student_1704064
Output-

Query 2-Select student details from student table order by deptno

Command-select * from student_1704064 order by d_no


Output-

Query 3- Select student details from student table order by age in decreasing order

Command-select rollno, name, d_no, DOB from student_1704064


order by (sysdate-DOB)/365 desc

Page no - 17
RDBMS lab Roll no-17040

Output-

Query 4-Use trunc and round commands on date

Command-select round (to_date(20-aug-19), ‘year’), round(to_date(20-aug-19), ‘month’),


trunc (to_date(20-aug-19), ‘year’), trunc(to_date(20-aug-19), ‘month’) from
student_1704064
Output-

Query 5-Select student details having name in upper case

Command-select * from student_1704064 where upper(NAME)=‘GOURAV’


Output-

Query 6-Select student details with name in upper case, lower case and initcap

Command-select upper(NAME) A, lower(name) B, initcap(name) C from


student_1704064

Page no - 18
RDBMS lab Roll no-17040

Output-

Query 7-Select length of string in students name from student table

Command-select length(name) from student_1704064


Output-

Query 8-Select substring from student table

Command-select substr(name, 2, 3) from student_1704064


Output-

Page no - 19
RDBMS lab Roll no-17040

Query 9-Select index of char in student records

Command-select instr(name, ‘a’) from student_1704064


Output-

Query 10-Replace a char from student table

Command-select replace(name, ‘a’, ‘b’) from student_1704064


Output-

Query 11-Add left padding to name column

Command-select lpad(name, 10, ‘$’) from student_1704064


Output-

Page no - 20
RDBMS lab Roll no-17040

Query 12-Add right padding to name column

Command-select rpad(name, 10, ‘*’) from student_1704064


Output-

Query 13-Use trim command

Command- select trim(‘ raj ’) from dual;


Output-

Query 14-Use left trim command

Command-select ltrim(‘raj ’,‘r’) from dual;


Output-

Query 15-Use right trim command

Command-select rtrim(‘raj ’,‘r’) from dual


Output-

Page no - 21
RDBMS lab Roll no-17040

Query 16-Select name, roll no and round (age) from student table

Command-select name, rollno, round(sysdate-DOB)/365 from student_1704064


Output-

Page no - 22
RDBMS lab Roll no-17040

Practical-8
Group functions: Grouping the result of a query

Query 1- select max, min, sum, avg marks and total no of enteries in student table

Command- select count(marks),min(marks),max(marks),avg(marks),sum(marks) from


student_1704064
Output-

Query 2- select max, min, sum, avg marks and total no of enteries in student table grouped by
deparment no

Command- select count(marks),min(marks),max(marks),avg(marks),sum(marks) from


student_1704064 group by d_no
Output-

Query 3- select max, min, sum, avg marks and total no of enteries in student table having
max(marks)<85

Command- select count(marks),min(marks),max(marks),avg(marks),sum(marks) from


student_1704064 having max(marks)<85
Output-

Query 4- select max, min, sum, avg marks and total no of enteries in student table grouped by
department no and having max(marks)<85

Command- select count(marks),min(marks),max(marks),avg(marks),sum(marks) from


student_1704064 group by d_no having max(marks)<85
Output-

Page no - 23
RDBMS lab Roll no-17040

Practical-9
Set Operators, Nested Queries, Joins, Sequences

Query 1- Find student name from student table using subqueries

Command- select name from student_1704064 where d_no= (select d_no from
department_1704064 where d_name='cse');
Output-

Query 2- Find student details from student table using subqueries

Command- select * from student_1704064 where d_no= (select d_no from


department_1704064 where d_name='cse');
Output-

Query 3- Find student name, dept name of students having marks >75 using join

Command- select s.name,d.d_name from student_1704064 s join department_1704064 d


using (d_no) where marks>=75;
(OR)
select s.name,d.d_name from student_1704064 s join department_1704064 d
on s.d_no=d.d_no where marks>=78;
Output-

Page no - 24
RDBMS lab Roll no-17040

Query 4- Find student details from table using natural join

Command- select s.name,d.d_name from student_1704064 s natural join


department_1704064 d where marks>=78;
Output-

Query 5- Find student details from table using inner join

Command- select s.name,d.d_name from student_1704064 s inner join department_1704064


d using (d_no) where marks>=75;
(OR)
select s.name,d.d_name from student_1704064 s inner join department_1704064
d on s.d_no=d.d_no where marks>=75;
Output-

Query 6- Find student details from table using outer join

Command- select s.name,d.d_name from student_1704064 s full outer join


department_1704064 d using(d_no) where marks>=85;
(OR)
select s.name,d.d_name from student_1704064 s full outer join
department_1704064 d on s.d_no=d.d_no where marks>=85;
Output-

Page no - 25
RDBMS lab Roll no-17040

Query 7- Find student details from table using left outer join

Command- select s.name,d.d_name from student_1704064 s left outer join


department_1704064 d using(d_no) where marks>=85;
(OR)
select s.name,d.d_name from student_1704064 s left outer join
department_1704064 d on s.d_no=d.d_no where marks>=85;
Output-

Query 8- Find student details from table using right outer join

Command- select s.name,d.d_name from student_1704064 s right outer join


department_1704064 d on s.d_no=d.d_no where marks>=90;
(OR)
select s.name,d.d_name from student_1704064 s right outer join
department_1704064 using(d_no) where marks>=90;
Output-

Query 9- Find student details from table using (+) notation in the join

Command- select s.name, d.d_name from student_1704064 s right outer join


department_1704064 d on s.d_no (+)=d.d_no where marks>=75;
Output-

Page no - 26
RDBMS lab Roll no-17040

Query 10- Find the student names that in same department as that of student ‘abc’

Command- select s1.name from student_1704064 s1 join student_1704064 s2 on


s1.d_no=s2.d_no where s2.name='Rahul'
Output-

Query 11-Combine data of two tables

Command- select rollno, name, DOB, gender, marks from student_17040 union select
rollno, name, DOB, gender, marks from student_1704064
Output-

Page no - 27
RDBMS lab Roll no-17040

Query 12-Combime data of two tables with repetition

Command- select rollno, name, DOB, gender, marks from student_17040 union all select
rollno, name, DOB, gender, marks from student_1704064
Output-

Query 13- Find common details between two tables

Command- select marks from student_17040 intersect select marks from student_1704064
Output-

Query 14-Find student details which is present in table1 but not in table2

Command- select name, marks from student_17040 minus select name, marks from
student_1704064
Output-

Page no - 28
RDBMS lab Roll no-17040

Practical-10
Views, Indexes, Database security and privileges: Grant and Revoke
commands

Query 1- Create views

Command-create view student_details as select name, rollno, marks, DOB from


student_1704064;
Output-

Query 2- Select student details using views

Command- select * from student_details


Output-

Query 3- Update view

Command- update student_details set name='sahil' where rollno=1003;


commit;
Output-

Page no - 29
RDBMS lab Roll no-17040

Query 4- Select student details after updating data using view

Command- select * from student_details


Output-

Query 5-select student details from table

Command- select name, rollno, marks, DOB from student_1704064


Output-

Query 6-Create index

Command- create index dept_index on student_1704064(d_no);


Output-

Query 7-Select student information without using index

Command- select * from student_1704064 where d_no=102;


Output-

Page no - 30
RDBMS lab Roll no-17040

Query 8-Select student information using index

Command- select * from student_1704064 where d_no=102;


Output-

Query 9-Drop index

Command- drop index dept_index;


Output-

Query 10-Grant access to student table


Command- grant select, update on student_1704064 to student1;
Output-

Query 11-Revoke access from student table


Command- revoke select, update on student_1704064 from student1;
Output-

Page no - 31
RDBMS lab Roll no-17040

Practical-11
Pl/SQL architecture, assignments and expression, writing PL/SQL code,
referencing non-SQL parameters

Query 1- Create a PL/SQL block to change d_no using if condition

Command- Declare
rno int;
deptno int;
Begin
rno:=&rollno;
select d_no into deptno from student064 where rollno=rno;
if (deptno=102) then
deptno: =101;
End if;
Update student064 set d_no=deptno where rollno=rno;
Commit;
End;
Output-

Page no - 32
RDBMS lab Roll no-17040

Query 2- Create a PL/SQL block to change d_no using if else condition

Command- Declare
rno int;
deptno int;
Begin
rno:=&rollno;
select d_no into deptno from student064 where rollno=rno;
if (deptno=104) then
deptno:=103;
elsif (deptno=103) then
deptno:=102;
end if;
update student064 set d_no=deptno where rollno=rno;
commit;
End;

Output-

Page no - 33
RDBMS lab Roll no-17040

Practical-12
Stored procedures and exception handling

Query 1-Create a stored procedure to change department number of a student

Command- create procedure change_department (roll_no integer in) as


roll_no int;
dept_no int;
begin
roll_no:=&rollno;
select d_no into dept_no from student_1704064 where rollno=roll_no;
if(dept_no=101) then dept_no:=100;
elsif(dept_no=102) then dept_no:=101;
elsif(dept_no=103) then dept_no:=102;
else dept_no:=103;
end if;
update student_1704064 set d_no=dept_no where rollno=roll_no;
commit;
end;
Output-

Query 2- Use or implement a stored procedure to change department no of student

Command-
Output-

Page no - 34

Anda mungkin juga menyukai