EXAMPLE-SQL statements
EXAMPLE-SQL statements
4. create table employee (emp_id int not null, emp_name varchar(50) not null,
emp_email varchar(50), emp_cc_info int);
5. create index employeeIndex on emoloyee(emp_name);
6. insert into employee(emp_id, emp_name, emp_email, emp_cc_info)
values(123, “Malik Ahmed”, [email protected], 1223123)
7. create or replace trigger EmployeeTrigger
before delete of emp_id, emp_name, emp_email, emp_cc_info on employee
1
for each row
begin
insert into employeelog (emp_id, emp_name, emp_email, emp_cc_info)
values (:old.emp_id, :old.emp_name, :old.emp_email, :old.emp_cc_info);
end;
/
8. drop trigger EmployeeTrigger;
EXAMPLE2:
Consider an Airline System. Make use of an SQL statements to perform the
following tasks:
viii. Create a trigger named VATTrigger that will add the VAT value
which accounts for 10% to the TicketPrice column before a new
record is inserted in the Flights table.
ix. Delete the trigger created in the above statement.
Answer:
i. CREATE TABLESPACE Airline_Tablespace DATAFILE 'C:\APP\
ADMIN\ORADATA\ORCL\ Airline_Tablespace.dbf' SIZE 100M
AUTOEXTEND ON NEXT 100K MAXSIZE UNLIMITED LOGGING
EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT
AUTO
ii. CREATE USER "Pilot" PROFILE "DEFAULT" IDENTIFIED BY
"MyPassword" DEFAULT TABLESPACE "Airline_Tablespace "
2
TEMPORARY TABLESPACE "TEMP" QUOTA 45MB ON "
Airline_Tablespace " ACCOUNT UNLOCK
EXAMPLE3:
3
“Teacher_Id”), (Enrollment – “Student_id”, “Course_Id”, “Term”,
“Year”) with at least one primary and one foreign key constraints.
e) Insert few records for each table.
f) Create a trigger before insert or update of Teacher records that
lowercase the email.
g) Drop the trigger created in the above statement.
h) Create an index on the Course_Id, Course_Code columns of the Courses
table.
i) Drop the above created index for the courses table
j) Delete few records from any table created in the above statement and
use shrink command for the same table.
k) Create a procedure to insert 500 records in the Courses table.
l) Create an explicit cursor named “C_Courses” that fetch all the courses
information in the following variables:
(C_Course_Id, C_Course_Code, C_Teacher_Id)
Answer:
a) CREATE TABLESPACE CCIS DATAFILE 'C:\APP\ADMIN\ORADATA\
ORCL\ccis.dbf' SIZE 100M AUTOEXTEND ON NEXT 100K MAXSIZE
UNLIMITED LOGGING EXTENT MANAGEMENT LOCAL SEGMENT SPACE
MANAGEMENT AUTO
4
Insert into Enrollment values (20001, 005, 1, “2017”);
EXAMPLE4:
5
NEXT 100K MAXSIZE UNLIMITED LOGGING EXTENT MANAGEMENT
LOCAL SEGMENT SPACE MANAGEMENT AUTO
b) CREATE USER "khalid" IDENTIFIED BY "Password" DEFAULT
TABLESPACE "ccis" TEMPORARY TABLESPACE "TEMP" QUOTA
UNLIMITED ON " ccis ";
c) Create role RoleName;
Grant insert, update to RoleName;
d) Grant RoleName to khalid
e) Create table student (stu_id number, stu_fname varchar(20) not null,
stu_lname varchar(20) not null, stu_email varchar(50), constraint
pk_student primary key (stu_id);
Create table teacher (tea_fname, varchar(20) not null, tea_lname
varchar(20) not null, tea_mobile number, tea_dept varchar(20) not null,
constraint pk_teacher primary key(tea_mobile);
f) begin
for i in 1 .. 500
loop
insert into student values (‘stu_fname’, ‘stu_lname’, ‘stu_email’);
end loop;
commit;
end;
/
g) create or replace function student_records return number is x number (3);
begin
select count(*) into x from student;
return(x);
end;
h) create or replace trigger email_lowercase
before insert or update of stu_email on student for each row
begin
:new.stu_email := lower(:new.stu_email);
end;
/
i) drop trigger email_lowercase;
j) create index Student_index on student (stu_id, stu_email);
k) drop index Student_index