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

SQL prac pdf

The document outlines a series of exercises focused on Data Definition Language (DDL) and Data Manipulation Language (DML) operations in SQL. It includes steps for creating tables, altering their structures, inserting records, and demonstrating constraints such as primary keys and foreign keys. The exercises also cover querying data, updating records, and handling exceptions related to unique and not null constraints.

Uploaded by

priyacs104
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL prac pdf

The document outlines a series of exercises focused on Data Definition Language (DDL) and Data Manipulation Language (DML) operations in SQL. It includes steps for creating tables, altering their structures, inserting records, and demonstrating constraints such as primary keys and foreign keys. The exercises also cover querying data, updating records, and handling exceptions related to unique and not null constraints.

Uploaded by

priyacs104
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

EX.

NO: 1
Data Definition of Base Table
DATE: / /23

Aim: To define the Data Definition of base table.


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: Drop the tables using “drop table” command.
Step 7: Stop the process.

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)

SQL> desc marks;


Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
TAMIL NUMBER
ENGLISH NUMBER
MATHS NUMBER
SCIENCE NUMBER
1
SOCIAL NUMBER
Alter table command
SQL> alter table marks add total number;
Table altered.

SQL> describe marks;


Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
TAMIL NUMBER
ENGLISH NUMBER
MATHS NUMBER
SCIENCE NUMBER
SOCIAL NUMBER
TOTAL NUMBER

SQL> alter table student modify id number primary key;


Table altered.

SQL> desc student;


Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER
NAME VARCHAR2(30)
FATHER_NAME VARCHAR2(20)
MOTHER_NAME VARCHAR2(30)
MOBILE_NO NUMBER(15)
EMAIL VARCHAR2(40)

Drop table command


SQL> drop table student;
Table dropped.

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:

Create table command with Primary key constraints


SQL>create table student(id number primary key, name varchar2(30) not null, DOB date);
Table created.

SQL> desc student;


Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER
NAME NOT NULL VARCHAR2(30)
DOB DATE

SQL> alter table student add aadhar number;


Table altered.

SQL> desc student;


Name Null? Type
---------------------------------- -------- ----------------------------
ID NOT NULL NUMBER
NAME NOT NULL VARCHAR2(30)
DOB DATE
AADHAR NUMBER

Drop Primary key constraints


SQL> alter table student drop primary key;
Table altered.

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.

SQL> desc student;


Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER
NAME NOT NULL VARCHAR2(30)
DOB DATE
AADHAR NOT NULL NUMBER

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:

Create table command with Primary key constraints


SQL>create table student(id number primary key, name varchar2(30) not null, DOB date);
Table created.

SQL> desc student;


Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER
NAME NOT NULL VARCHAR2(30)
DOB DATE

SQL>insert into student values(1001,'malar',DATE '2015-12-17',8654321);


1 row created.
SQL>insert into student values (1002,'smith',DATE '1990-12-17',4654321),
1 row created.
SQL>insert into student values (1003,'Roy',DATE '1990-1-1',34654321);
1 row created.
SQL>insert into student values (1004,'Roy',DATE '1990-2-12',67654321);
1 row created.

SQL> select * from student;


ID NAME DOB AADHAR
---------- ------------------------------ --------- ----------
1001 malar 17-DEC-15 8654321
1002 smith 17-DEC-90 4654321

5
1003 Roy 01-JAN-90 34654321
1004 Roy 12-FEB-90 67654321

Violation of Unique Constraints


SQL> insert into student values(1004,'Roy',DATE '1990-2-12',67654321);
SQL>insert into student values(1004,'Roy',DATE '1990-2-12',67654321)
*
ERROR at line 1:
ORA-00001: unique constraint (SYS.PK) violated

Violation of Primary key Constraints


SQL> insert into student(name,dob) values('ankit',DATE '1996-2-6');
insert into student(name,dob) values('ankit',DATE '1996-2-6')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SYS"."STUDENT"."ID")

Violation of Check Constraints


SQL> alter table student add age number check(age>=18 and age<65);
Table altered.

SQL> desc student;


Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER
NAME NOT NULL VARCHAR2(30)
DOB DATE
AADHAR NOT NULL NUMBER
AGE NUMBER

SQL> insert into student values(1005,'jack',DATE '1980-2-1',6543-2189-1234,43);


1 row created.
SQL> select * from student;
ID NAME DOB AADHAR AGE
---------------------------------------- --------- ---------- ----------
1001 malar 17-DEC-15 8654321
1002 smith 17-DEC-90 4654321
1003 Roy 01-JAN-90 34654321
1004 Roy 12-FEB-90 67654321
1005 jack 01-FEB-80 3120 43

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

Violation of Not Null Constraints


SQL> insert into student(dob,age) values(DATE '1945-2-12',101);
insert into student(dob,age) values(DATE '1945-2-12',101)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SYS"."STUDENT"."ID")

SQL> alter table student drop primary key;


Table altered.

SQL> alter table student modify id number primary key;


Table altered.

SQL> desc student;


Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER
NAME NOT NULL VARCHAR2(30)
DOB DATE
AADHAR NUMBER
AGE NUMBER

SQL> create table bmi(id number references student(id), height number,weight number);
Table created.

SQL> insert into student values(1005,123,34);


insert into student values(1005,123,34)
*
ERROR at line 1:
ORA-00947: not enough values

Violation of Foreign key Constraints


SQL> insert into bmi values(1005,123,34);
1 row 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.

insert into employee values('2023ucs102','anitha',10,9087653321,'[email protected]',20500);


1 row created.

insert into employee values('2023ubca101','sai',20,9022254321,'[email protected]',20300);


1 row created.

select * from employee;

ID NAME DEPT_ID MOBILE_NO EMAIL_ID SALARY


---------------------------------------------------------------------------------------------------------------------

2023ucs101 Malar 10 9087654321 [email protected] 20000

2023ucs102 anitha 10 9087653321 [email protected] 20500

2023ubca101 sai 20 9022254321 [email protected] 20300

Update command
update employee set salary=45000 where id='2023ucs101';
1 row updated.

9
select * from employee where id='2023ucs101';

ID NAME DEPT_ID MOBILE_NO EMAIL_ID SALARY


------------------------------------------------------------------------------------------------------------------
2023ucs101 malar 10 9087654321 [email protected] 45000

View creation

create view emp_view as select * from employee where dept_id=10;

View created.

Display view records

select * from emp_view;

ID NAME DEPT_ID MOBILE_NO EMAIL_ID SALARY


---------------------------------------------------------------------------------------------------------------------

2023ucs101 Malar 10 9087654321 [email protected] 20000

2023ucs102 anitha 10 9087653321 [email protected] 20500

Delete command
delete from employee;
3 rows deleted.

select * from employee;


no rows selected

Result:
Thus the above tables are creates and given operations are successfully performed.
10
EX.NO: 5
Demonstration of Query Command
DATE: / /23

Aim: To demonstrate the query command.


Procedure:
Step 1: Start the process.
Step 2: create two table using “Create table” command as given below.
Step 3: Insert the records using “Insert” command.
Step 4: Using select command access the records of the table.
Step 5: Create View using “CREATE VIEW” 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.

create table department(dept_id int PRIMARY KEY,dept_name varchar2(30), dept_location


(varchar2(30));
Table created.

Insert command
insert into employee values('2023ucs101','malar',10,9087654321,'[email protected]',20000);
1 row created.

insert into employee values('2023ucs102','anitha',10,9087653321,'[email protected]',20500);


1 row created.

insert into employee values('2023ubca101','sai',20,9022254321,'[email protected]',20300);


1 row created.

insert into department values(10,'cs','B block');


1 row created.

insert into department values(20,'electrical','D block');


1 row created.

select id,name from employee WHERE name like '_a%';


ID NAME
------------------- -------------------------
2023ucs101 malar

11
2023ubca101 sai

select id, name from employee WHERE salary BETWEEN 15000 and 20000;
ID NAME
------------------------- -------------------------
2023ucs101 malar

select * from department;

DEPT_ID DEPT_NAME DEPT_LOCATION


---------------- --------------------------- ------------------------------
10 cs B block
20 electrical D block

select count(*)from employee;


COUNT(*)
----------
3
select sum(salary) from employee;
SUM(SALARY)
-----------
60800
select max(salary) from employee;
MAX(SALARY)
-----------
20500
select min(salary) from employee;
MIN(SALARY)
-----------
20000
select avg(salary) from employee;
AVG(SALARY)
-----------
20266.6667

Join Query (Inner Join)


SELECT id,name,dept_name from employee e join department d on (e.dept_id=d.dept_id);

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

Aim: To write a PL/SQL code for debit calculation.

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);

insert into acct_master values(2,'brinda',10000);

insert into acct_master values(3,'chandru',110000);

insert into acct_master values(4,'dhivya',7000);

insert into acct_master values(5,'vino',17000);

select * from acct_master;

ACCT_NO ACCT_NAME BALANCE


-------------- ----------------- -----------------
1 annie 1000
2 brinda 10000
3 chandru 110000
4 dhivya 7000
5 vino 17000
set serveroutput on;

declare

xacct_no number(5);

xmin_bal number(5):=500;

13
xbalance number(5);

balances number(5);

begin

xacct_no:=&xacct_no;

select balance into xbalance from acct_master where acct_no=xacct_no;

dbms_output.put_line('The current balance in your account is :'||xbalance);

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

update acct_master set balance=balances where acct_no=xacct_no;

dbms_output.put_line('Rs 2000 is deducted and current balance is'||balances);

end if;

end;

14
Sample output:

Enter value for xacct_no: 2

old 7: xacct_no:=&xacct_no;

new 7: xacct_no:=2;

The current balance in your account is :10000

Rs 2000 is deducted and current balance is8000

PL/SQL procedure successfully completed.

Enter value for xacct_no: 1

old 7: xacct_no:=&xacct_no;

new 7: xacct_no:=1;

The current balance in your account is :1000

The current balance will be less than 500. You are unable to take the amount

PL/SQL procedure successfully completed.

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.

Set serveroutput on;


declare
pi number;
r number;
a number(10,3);
begin
pi:=22/7;
for r in 3..7 loop
a:=pi*r*r;
insert into circle values(r,a);
end loop;

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

Aim: To write a PL/SQL code for reversing a number.

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

PL/SQL procedure successfully completed.

Result:
Thus the above tables are creates and given operations are successfully performed.

19
EX.NO: 9
Audit System
DATE: / /23

Aim: To write a PL/SQL code for debit calculation.

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.

insert into client_master values('ib1001','annie','salem','1000');


1 row created.

insert into client_master values('ib1002','valar','salem','10000');


1 row created.

insert into client_master values('ib1003','victor','bangalore','46000');


1 row created.

insert into client_master values('ib1004','zara','kerala','50000');


1 row created.

insert into client_master values('ib1005','thiru','kerala','54600');


1 row created.
select * from client_master;

CLIENT NAME ADDRESS BAL_DUE


----- ------------------------------------------------------------------------------ ----------
ib1001 annie salem 1000
ib1002 valar salem 10000
ib1003 victor bangalore 46000

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

select * from audit_client;


no rows selected

update client_master set bal_due=2000 where client_no='ib1001';


1 row updated.

select * from client_master where client_no='ib1001';

CLIENT NAME ADDRESS BAL_DUE


------ -------------------------------------------------------------------------------- ----------
ib1001 annie salem 2000

select * from audit_client;


CLIENT NAME ADDRESS BAL_DUE OPER SYS US
------ ----------------------------------------------------------------------------------------------------
ib1001 annie salem 200 Update 11-AUG-23 SYSTEM

Result:
Thus the above tables are creates and given operations are successfully performed.

22

You might also like