SQL prac pdf
SQL prac pdf
NO: 1
Data Definition of Base Table
DATE: / /23
SOURCE CODE:
Create table command
SQL> create table student(id number, name varchar2(30), father_name varchar2(20),
mother_name varchar2(30), mobile_no number(15), email varchar2(40));
Table created.
SQL>create table marks(id number, tamil number, english number, maths number, science
number, social number);
Table created.
Describe command
SQL> desc student;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
NAME VARCHAR2(30)
FATHER_NAME VARCHAR2(20)
MOTHER_NAME VARCHAR2(30)
MOBILE_NO NUMBER(15)
EMAIL VARCHAR2(40)
Result:
Thus the tables are created and given operations were successfully performed.
2
EX.NO: 2
DDL with Primary Key Constraints
DATE: / /23
Aim: To define the Data Definition Language with Primary key constraints.
Procedure:
Step 1: Start the process.
Step 2: create the table using “Create table” command as given below.
Step 3: Display the structure of the tables using “desc / describe” command.
Step 4: Using “Alter table” command add or modify the structure of the table.
Step 5: Display the structure of the tables after alteration.
Step 6: Stop the process.
SOURCE CODE:
3
SQL> desc student;
Name Null? Type
---------------------------- -------- ----------------------------
ID NUMBER
NAME NOT NULL VARCHAR2(30)
DOB DATE
AADHAR NUMBER
Alter table to add Primary key constraint that include multiple column
SQL> alter table student add constraint pk primary key(id, aadhar);
Table altered.
Result:
Thus the above tables are creates and given operations are successfully performed.
4
EX.NO: 3 DDL with Constraints & verification by Insert
DATE: / /23 command
Aim: To define the Data Definition Language with Primary key constraints.
Procedure:
Step 1: Start the process.
Step 2: create the table using “Create table” command as given below.
Step 3: Display the structure of the tables using “desc / describe” command.
Step 4: Using Insert command insert some records in the table.
Step 5: Using “Alter table” add or modify the key constraints for the fields.
Step 6: Stop the process.
SOURCE CODE:
5
1003 Roy 01-JAN-90 34654321
1004 Roy 12-FEB-90 67654321
6
SQL> insert into student values(1006,'hari Roy',DATE '1945-2-12',645674321,101);
insert into student values(1006,'hari Roy',DATE '1945-2-12',645674321,101)
*
ERROR at line 1:
ORA-02290: check constraint (SYS.SYS_C007099) violated
SQL> create table bmi(id number references student(id), height number,weight number);
Table created.
7
SQL> insert into bmi values(1007,153,60);
insert into bmi values(1007,153,60)
*
ERROR at line 1:
ORA-02291: integrity constraint (SYS.SYS_C007101) violated - parent key not
Found
Result:
Thus the above tables are creates and given operations are successfully performed.
8
EX.NO: 4
Data Manipulation of Base tables &Views
DATE: / /23
Aim: To define the Data Definition Language with Primary key constraints.
Procedure:
Step 1: Start the process.
Step 2: create the table using “Create table” command as given below.
Step 3: Insert the records using “Insert” command.
Step 4: Using “Alter table” command add or modify the structure of the table.
Step 5: Update the table using “Update command.
Step 6: Stop the process.
SOURCE CODE:
Create table
CREATE TABLE employee(id varchar2(25) PRIMARY KEY,name varchar2(25),dept_id
number,mobile_no number,email_id varchar2(30),salary number);
Table created.
Insert command
insert into employee values('2023ucs101','malar',10,9087654321,'[email protected]',20000);
1 row created.
Update command
update employee set salary=45000 where id='2023ucs101';
1 row updated.
9
select * from employee where id='2023ucs101';
View creation
View created.
Delete command
delete from employee;
3 rows deleted.
Result:
Thus the above tables are creates and given operations are successfully performed.
10
EX.NO: 5
Demonstration of Query Command
DATE: / /23
SOURCE CODE:
Create table
CREATE TABLE employee(id varchar2(25) PRIMARY KEY,name varchar2(25),dept_id
number,mobile_no number,email_id varchar2(30),salary number);
Table created.
Insert command
insert into employee values('2023ucs101','malar',10,9087654321,'[email protected]',20000);
1 row created.
11
2023ubca101 sai
select id, name from employee WHERE salary BETWEEN 15000 and 20000;
ID NAME
------------------------- -------------------------
2023ucs101 malar
ID NAME DEPT_NAME
------------------ ------------------- ------------------------------
2023ucs101 Malar cs
2023ucs102 anitha cs
2023ubca101 sai electrical
Result:
Thus the above tables are creates and given operations are successfully performed.
12
EX.NO: 6
Debit calculation
DATE: / /23
Procedure:
Step 1: Start the process.
Step 2: Create the acct_master table using “Create table” command.
Step 3: Insert the records.
Step 4: Get the account number and check if the balance is less than 500.
Step 5: If the balance is greater than 500, then debit 2000 from the account.
Step 6: Stop the process.
SOURCE CODE
create table acct_master(acct_no number(5) primary key, acct_name varchar2(10), balance
number(10));
insert into acct_master values(1,'annie',1000);
declare
xacct_no number(5);
xmin_bal number(5):=500;
13
xbalance number(5);
balances number(5);
begin
xacct_no:=&xacct_no;
balances:=xbalance-2000;
if(balances<500) then
dbms_output.put_line('The current balance will be less than 500. You are unable to take the
amount');
else
end if;
end;
14
Sample output:
old 7: xacct_no:=&xacct_no;
new 7: xacct_no:=2;
old 7: xacct_no:=&xacct_no;
new 7: xacct_no:=1;
The current balance will be less than 500. You are unable to take the amount
Result:
Thus the above tables are creates and given operations are successfully performed.
15
EX.NO: 7
Area of a Circle
DATE: / /23
Aim: To write a PL/SQL code to find the area of circle for given radius.
Procedure:
Step 1: Start the process.
Step 2: create the table by the name of circle.
Step 3: include two fields (radius, area).
Step 4: calculate the area of the circle.
Step 5: Display the circle table.
Step 6: Stop the process.
SOURCE CODE:
create table circle(r number, a number);
Table created.
end;
/
PL/SQL procedure successfully completed.
16
SQL> select * from circle;
R A
---------- ----------
3 28.286
4 50.286
5 78.571
6 113.143
7 154
Result:
Thus the above tables are creates and given operations are successfully performed.
17
EX.NO: 8
Reversing a Number
DATE: / /23
Procedure:
Step 1: Start the process.
Step 2: Enter a number.
Step 3: Reverse the number.
Step 4: Display the reversed number.
Step 5: Stop the process.
SOURCE CODE:
Set serveroutput on;
declare
n number;
rev number:=0;
rem number;
begin
n:=&n;
while n>0
loop
rem:=mod(n,10);
rev:=(rev*10)+rem;
n:=trunc(n/10);
end loop;
dbms_output.put_line('the reverse of '||n||' is: '||rev);
end;
/
18
Sample output
Enter value for n: 7654
old 7: n:=&n;
new 7: n:=7654;
the reverse of 0 is: 4567
Result:
Thus the above tables are creates and given operations are successfully performed.
19
EX.NO: 9
Audit System
DATE: / /23
Procedure:
Step 1: Start the process.
Step 2: Create two tables client master and audit client.
Step 3: Insert the records in client master table.
Step 4: Record the deletion or updation of client master table in audit client.
Step 5: Stop the process.
SOURCE CODE
create table client_master(client_no varchar2(6),name varchar2(30), address
varchar2(50),bal_due number(7,2));
Table created.
20
ib1004 zara kerala 50000
ib1005 thiru kerala 54600
Trigger code
create trigger audit_trail after update or delete on client_master for each row
declare
oper varchar2(8);
sys date;
us varchar2(10);
client_no varchar2(6));
client_name varchar2(20);
bal_due number(10,2);
begin
if updating then
oper:='Update';
end if;
if deleting then
oper:='Delete';
end if;
client_no:=:OLD.client_no;
client_name:=:OLD.name;
bal_due:=OLD.bal_due;
select current_date into sys from dual;
select user into us from dual;
insert into audit_client values(client_no,client_name,bal_due,oper,sys,us);
end;
/
21
Sample output
Result:
Thus the above tables are creates and given operations are successfully performed.
22