Академический Документы
Профессиональный Документы
Культура Документы
Create the various tables in SQL using Data Definition Language statements
and apply the given constraints on them.
1. STUDENT
Street VARCHAR2(25),
City VARCHAR2(15),
Zip VARCHAR(5),
Birthday DATE,
FacultyID NUMBER(3),
MajorID NUMBER(3),
Phone CHAR(10)
);
2. FACULTY
3. CRSSECTION
4. COURSE
PreReq VARCHAR2(6)
);
StudentID CHAR(5),
CSID NUMBER(4),
PRIMARY KEY(StudentID,CSID)
);
6. ROOM
Column Name Data Type Constraints
Room Type CHAR Primary Key
Room Desc VARCHAR2(9)
RoomDesc VARCHAR2(9)
);
7. LOCATION
8. TERM
Column Name Data Type Constraints
TermID CHAR(4) Primary Key
TermDesc VARCHAR2(11)
StartDate Date
EndDAte Date
10. MAJOR
MajorDesc VARCHAR2(25)
);
Lab Record 2
Display all student names and birth dates with format '20
October 1970'.
SELECT
Avg(COALESCE(Commission,0)),AVG(NVL(COALESCE(Commission,0))) FROM
Employee
8. Find out the names and number of years worked along with
their department names in descending order by number of
years worked.
SELECT e.fname||' '|| e.lname as name, extract( year from
sysdate )- extract(year from e.hiredate) as years-worked,
d.deptname from employee e, dept d where e.deptid=d.deptid
order by years-worked desc;
DECLARE
a number(5)
b number(5)
temp number(6)
BEGIN
a:=&a;
b:=&b;
DBMS-OUTPUT.PUT_LINE(a);
DBMS-OUTPUT.PUT_LINE(b);
DBMS-OUTPUT.PUT_LINE(a);
DBMS-OUTPUT.PUT_LINE(b);
END;
a varchar(20);
b varchar(20);
BEGIN
a:='&a';
b:='&b';
DBMS-OUTPUT.PUT_LINE(b||' , '||a);
END;
DECLARE
a number(4);
BEGIN
a:='&a';
END IF;
END;
4. Using for loop, print the values 10 to 1 in reverse order.
BEGIN
loop
DBMS-OUTPUT.PUT_LINE(i);
END LOOP;
END;
DECLARE
input integer(10):=&input;
temp1 integer(10);
temp2 integer(10);
BEGIN
temp2:=0;
while input>0
loop
temp2:=temp1+temp2;
input:=floor(input/10);
end loop;
END;
6. Write a PL/SQL block to check whether a number n is prime
number or not.
DECLARE
n number;
i number;
prime number;
BEGIN
i=2;
prime:=1;
n:=&n;
for i in 2..n/2
loop
if mod(n,i)=0 then
prime:=0
end if;
end loop;
if flag=1 then
DBMS-OUTPUT.PUT_LINE('Prime');
else
DBMS-OUTPUT.PUT_LINE('Not Prime');
end if;
END;
7. Write a PL/SQL block to check whether an input integer is
perfect number or not.
DECLARE
n number;
i number;
perfect number;
BEGIN
n=&n;
perfect:=0;
for i in 1..n/2
loop
if mod(n,i)=0 then
perfect:=perfect+1;
end if;
end loop;
if n=perfect then
DBMS-OUTPUT.PUT_LINE('Perfect');
else
DBMS-OUTPUT.PUT_LINE('Not Perfect');
end if;
END;
8. Write a PL/SQL block to print the first n numbers of a
Fibonacci sequence.
DECLARE
first number:=0;
second number:=1;
third number;
n number:=&n;
i number;
BEGIN
DBMS-OUTPUT.PUT_LINE(first);
DBMS-OUTPUT.PUT_LINE(second);
for i in 2..n
loop
third=first+second;
DBMS-OUTPUT.PUT_LINE(third);
first:=second;
second:=third;
end loop;
END;
9. Write a PL/SQL block to check the given String is Palindrome
or not.
DECLARE
len number;
str Varchar(20):='&input_string';
chkstr VARCHAR2(20);
BEGIN
len:=length(str);
loop
chkstr:=chkstr||substr(str,i,1);
end loop;
DBMS-OUTPUT.PUT_LINE(str||" is a Plaindrome");
else
End IF;
END;
Lab Record 7
DECLARE
cursor csr is select lname,fname, salary, hiredate, from employee;
BEGIN
for i in csr then
loop
end if;
end loop;
END;
DECLARE
CURSOR CSR is select(name, fname, salary, deptname,location,e.deptid
from employee e, dept d where e.deptid=d.deptid);
BEGIN
for i in CSR
loop
if (i.deptid=20) then
DBMS-OUTPUT.PUT_LINE(i.lname||' '||i.fname||' '||i.salary||' '||i.deptname||'
'||i.location);
end if;
end loop;
END;
3. CREATE a PL/SQL block that declares a cursor to pass a
parameter to the cursor data of the type that is same as the
salary column in employee retrieve information into the
cursor for a salary higher than parameter value. Use a loop
to print each employee information for the cursor.
DECLARE
EmpRow employee%rowtype;
salary employee.salary%type;
BEGIN
salary:=salary;
OPEN CSR(salary);
loop
DBMS-OUTPUT.PUT_LINE(employee.lname||' '||emprow.fname||'
'||emprow.positionid||' '||emprow.suprevisorid||'
'||emprow.hiredate||' '||emprow.salary||'
'||emprow.commisiion||' '||emprow.deptid);
end loop;
CLOSE CSR;
END;
Lab Record 8
BEGIN
raise_application_error(-20000,"Can't perform DML on Monday");
End;
DECLARE
UName VARCHAR2(30);
NewDate DATE;
BEGIN
END;
BEGIN
END;
DECLARE
StudentID NUMBER;
LName STUDENT.LAST%TYPE;
FName STUDENT.FIRST%TYPE;
Phone STUDENT.PHONE%TYPE;
BEGIN
StudentID:= & StudentID;
PROC_STUD(StudentID, LName, FName, Phone);
DBMS_OUTPUT.NEW_LINE;
DBMS_OUTPUT.PUT_LINE(FName||' '||LName||' '||Phone);
END;
2.Write a function and pass a department number to it. if the
table does not contain that department number, return a false
value, otherwise return true. print the appropriate message in
the calling program based on the result.
DECLARE
VDEPT DEPT.DEPTID%TYPE;
Counter NUMBER(2);
flag VARCHAR2(5);
BEGIN
dept_id :=&dept_id;
SELECT count(*) INTO Counter FROM DEPT WHERE DeptID = dept_id;
flag:= func_dept (Counter);
IF(flag ='TRUE') THEN
DBMS_OUTPUT.PUT_LINE(flag ||' '|| dept_id ||'Exists');
ELSE
DBMS_OUTPUT.PUT_LINE(flag ||' '|| dept_id ||Not Exists');
END IF;
END;
3. write a package that contains a procedure and a function.
the procedure is passed a room number. if the room number exists,
the procedure gets the capacity of the room and the building name
from the location table. if the room number does not exist, the
procedure performs the appropriate exception-handling routine.
the function is passed a csid and returns maximum number of seats
available in the course section.
DECLARE
maxseat number;
csid number;
room_no number;
begin
csid := &csid;
maxseat := pack.fseat(csid);
dbms_output.put_line('for course '|| csid||' max seats are
'||maxseat);
room_no :=&room_no;
pack.room_proc(room_no);
END;
Lab Record 9
(Exceptions)