0% found this document useful (0 votes)
0 views

EXAMPLE-SQL statements

The document outlines SQL statements for creating and managing database objects in various systems, including HR, Airline, and College systems. It covers tasks such as creating tablespaces, users, roles, tables, indexes, triggers, and procedures, along with specific SQL commands for each task. The examples demonstrate the implementation of database design and data manipulation using SQL syntax.

Uploaded by

khawla2076
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

EXAMPLE-SQL statements

The document outlines SQL statements for creating and managing database objects in various systems, including HR, Airline, and College systems. It covers tasks such as creating tablespaces, users, roles, tables, indexes, triggers, and procedures, along with specific SQL commands for each task. The examples demonstrate the implementation of database design and data manipulation using SQL syntax.

Uploaded by

khawla2076
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EXAMPLE1:

Consider an HR System. Make use of an SQL statements to perform the


following tasks:
1. Build a tablespace named HR_tablespace with extent management local.
2. Create a user named “Ahmed”. Make sure the user has Unlimited Quota on
the default tablespace.
3. Create a role “Hr_manager” and grant select, insert, update privileges to this
role.
4. Create a table “employee” with the following fields (emp_id, emp_name,
emp_email, emp_cc_info).
5. Create an appropriate index for the employee table.
6. Insert a record into employee table.
7. Before an employee record is deleted from the current employees’ table,
it is necessary to transfer all his/her details from a table to archive tables.
Create a trigger for the same.
8. Drop the trigger created in the above statement (g).
Answer:
1. CREATE TABLESPACE "HR_tablespace" DATAFILE 'C:\APP\ADMIN\
ORADATA\ORCL\HR_tablespace.dbf' SIZE 100M AUTOEXTEND ON NEXT
100K MAXSIZE UNLIMITED LOGGING EXTENT MANAGEMENT LOCAL
SEGMENT SPACE MANAGEMENT AUTO

2. CREATE USER "Ahmed" PROFILE "DEFAULT" IDENTIFIED BY "Password"


DEFAULT TABLESPACE "HR_tablespace" TEMPORARY TABLESPACE "TEMP"
QUOTA UNLIMITED ON "HR_tablespace";
3. create role Hr_mnager;
grant select, insert, update to Hr_mnager;

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:

i. Build a big file tablespace named AirLine_Tablespace with SIZE


40M and has a manual segment space management.
ii. Create a user named Pilot. Make sure the user account is unlocked.
iii. Create a table called Flights with the following columns (FlightNo,
FlightDestination, FlightTime ,TicketPrice)
iv. Create an index named FlightIndex on FlightNo and FlightTime
columns.
v. Remove the index created above in part(iv).
vi. Create a role named PilotsRole that contains create session, select,
insert, update privileges to this role.
vii. Apply PilotRole role to Pilot user that was created in part(ii).

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

iii. Create table flights (Flightno varchar(5), Flightdistination


varchar(20), Flighttime varchar2(20), TicketPrice number(5),
constraint FlightsPK primary key(Flightno));

iv. Create index FlightsIndex on Terminals (FlightNo, FlightTime);

v. Drop index FlightIndex

vi. Create role PilotsRole;


Grant create, update, select, update to PilotsRole;
vii. Grant PilotRole to Pilot
viii. create or replace trigger FlightsTrigger
before insert of TicketPrice on Flights for each row
begin
:NEW.TicketPrice := :NEW. TicketPrice + .10 ;
end;/
ix. drop trigger FlightsTrigger;

EXAMPLE3:

Implement the design as specified in the below figure (College):

a) Create a tablespace with a name “CCIS” with extent management local.


b) Create a user named “ahmed” with password “admin”.
c) Create role “controller” and assign the insert, update privileges to
this role.
d) Create three database tables (Teacher – "Teacher_Id",
“Teacher_Name”, “email”), (Courses – “Course_Id”, “Course_Code”,

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

b) CREATE USER "ahmed" PROFILE "DEFAULT" IDENTIFIED BY "admin"


DEFAULT TABLESPACE "CCIS" TEMPORARY TABLESPACE "TEMP"
QUOTA 45MB ON "CCIS " ACCOUNT UNLOCK

c) Create role controller;


Grant insert, update to controller;

d) Create table Teacher (Teacher_Id int(10), Teacher_Name varchar(20),


email varchar(50), constraint teacher PK primary key(Teacher_Id));

Create table Courses (Course_Id int(10), Course_Code varchar(15),


Teacher_Id int(10));

Create table Enrollment (Student_Id int(10), Course_Id int(10), Term


int(2), Year varchar(5);

e) Insert into Teacher values (10001, “Ahsan”, “[email protected]”);


Insert into Courses values (005, “IT 311”, ‘10001’);

4
Insert into Enrollment values (20001, 005, 1, “2017”);

f) Create or replace trigger TeacherTrigger


before insert or update
of email
on Teacher
for each row
begin
:New.email = lower(:New.email);

g) Drop trigger TeacherTrigger;


h) Create index CoursesIndex on Courses (Course_Id, Course_Code);
i) DROP index CourseIndex;

EXAMPLE4:

Implement the design as specified in the below figure (CCIS):

a) Create a tablespace with a name “ccis” with extent management local.


b) Create a user with a name “khalid” and assigned a tablespace and quota
to it.
c) Create role with any name and assign the insert, update privileges to
this role.
d) Assign the above created role (in question 7-c) to the user khalid.
e) Create two database tables (student – "stu_id", “stu_fname”,
“stu_lname”, “stu_email”), (teacher – “tea_fname”, “tea_lname”,
“tea_mobile”, “tea_dept”) with at least one primary and one foreign key
constraints.
f) Create a procedure to insert 500 records in student table.
g) Create a function that will return the total records of student table.
h) Create a trigger before insert or update of student records that
lowercase the stu_email.
i) Drop the trigger created in the above statement.
j) Create an index on the stu_id and stu_email columns of the student
table.
k) Drop the above created index for the student table.
Answer:
a) CREATE TABLESPACE "ccis" DATAFILE
'C:\APP\ADMIN\ORADATA\ORCL\ccis.dbf' SIZE 100M AUTOEXTEND ON

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

You might also like