0% found this document useful (0 votes)
29 views42 pages

SQL Commands and PL/SQL Examples

The document contains a series of SQL and PL/SQL commands demonstrating various database operations including DDL, DML, TCL commands, and programming examples such as Fibonacci series, factorial calculation, string reversal, and student mark analysis. It showcases the creation, alteration, insertion, updating, and deletion of tables and records, along with the use of cursors and triggers. Additionally, it includes a library management system example using C# for database interaction.

Uploaded by

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

SQL Commands and PL/SQL Examples

The document contains a series of SQL and PL/SQL commands demonstrating various database operations including DDL, DML, TCL commands, and programming examples such as Fibonacci series, factorial calculation, string reversal, and student mark analysis. It showcases the creation, alteration, insertion, updating, and deletion of tables and records, along with the use of cursors and triggers. Additionally, it includes a library management system example using C# for database interaction.

Uploaded by

subhulakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EX.

NO:01
DDL COMMANDS
DATE:

AIM:

ALGORITHM:

1
PROGRAM:
SQL> create table example #create

(
name varchar(10), reg_no
number(5), tamil
number(3), english
number(3), maths
number(3)
);

Table created.

SQL> desc example;


Name Null? Type

NAME VARCHAR2(10)
REG_NO NUMBER(5)
TAMIL NUMBER(3)
ENGLISH NUMBER(3)
MATHS NUMBER(3)

SQL> alter table example ADD dep varchar(6); #Alter

Table altered.

Name Null? Type

NAME VARCHAR2(10)
REG_NO NUMBER(5)
TAMIL NUMBER(3)
ENGLISH NUMBER(3)

2
MATHS NUMBER(3)

SQL> insert into example values('priya',101,78,67,87,'Tamil');


1 row created.
SQL> insert into example values('kalee',102,89,78,65,'che');
1 row created.
SQL> insert into example values('jeevi',103,88,97,67,'cs');
1 row created.

select * from example;

NAME REG_NO TAMIL ENGLISH MATHS DEP


priya 101 78 67 87 Tamil
kalee 102 89 78 65 che
jeevi 103 88 97 67 cs

SQL> update example set dep='eng'where name='priya';


1 row updated.

select * from example;

NAME REG_N TAMIL ENGLISH MATHS DEP


O
priya 101 78 67 87 eng
kalee 102 89 78 65 che
jeevi 103 88 97 67 cs

SQL> truncate table example; #Truncate

Table truncated.
SQL> select * from example;
no rows selected.

SQL> drop table example; #Drop

Table dropped.

3
RESULT:

4
[Link]
DML COMMANDS
DATE:

AIM:

ALGORITHM:

5
PROGRAM:
SQL> create table example #create

(
name varchar(10),
reg_no number(5),
tamil number(3),
english number(3),
maths number(3)
);

Table created.

SQL> desc example;


Name Null? Type

NAME VARCHAR2(10)
REG_NO NUMBER(5)
TAMIL NUMBER(3)
ENGLISH NUMBER(3)
MATHS NUMBER(3)

SQL> insert into example values('priya',101,78,67,87,'Tamil'); #Insert

1 row created.
SQL> insert into example values('kalee',102,89,78,65,'che');
1 row created.
SQL> insert into example values('jeevi',103,88,97,67,'cs');
1 row created.

select * from example;

NAME REG_NO TAMIL ENGLISH MATHS DEP


priya 101 78 67 87 Tamil
kalee 102 89 78 65 che
jeevi 103 88 97 67 cs

6
SQL> update example set dep='eng'where name='priya'; #Update

1 row updated.

select * from example; #Select

NAME REG_N TAMIL ENGLISH MATHS DEP


O
priya 101 78 67 87 eng
kalee 102 89 78 65 che
jeevi 103 88 97 67 cs

RESULT:

7
[Link]
TCL COMMANDS
DATE:

AIM:

ALGORITHM:

8
PROGRAM:
SQL> create table employe
2 (
3 name varchar(10),
4 empid varchar(10),
5 jobrole varchar(15),
6 salary number(20),
7 emp_phno number(10)
8 );
Table created.

SQL> insert into employe values('gobi',101,'manager',70000,3452647586);


1 row created.

SQL> insert into employe values('divi',102,'team leader',60000,6574839223);


1 row created.

SQL> insert into employe values('swathi',103,'ass manager',750000,8675947352);


1 row created.

SQL> savepoint a;
Savepoint created.

SQL> select * from employe;


NAME EMPID JOBROLE SALARY EMP_PHNO

gobi 101 manager 70000 3452647586

divi 102 team leader 60000 6574839223


swathi 103 ass manager 750000 8675947352

SQL> insert into employe values('priya',104,'CEO',80000,7684635281);


1 row created.

9
SQL> savepoint b;
Savepoint created.

SQL> select * from employe;

NAME EMPID JOBROLE SALARY EMP_PHNO

gobi 101 manager 70000 3452647586


divi 102 team leader 60000 6574839223
swathi 103 ass manager 750000 8675947352
priya 104 CEO 150000 7684635281

SQL> insert into employe values('velu',105,'team member',45000,8795745362);


1 row created.

SQL> savepoint c;
Savepoint created.

SQL> select * from employe;

NAME EMPID JOBROLE SALARY EMP_PHNO

gobi 101 manager 70000 3452647586


divi 102 team leader 60000 6574839223
swathi 103 ass manager 750000 8675947352
priya 104 CEO 80000 7684635281
velu 105 team member 45000 8795745362

SQL> rollback to b;
Rollback complete.

SQL> commit;
Commit complete.

10
SQL> select * from employe;

NAME EMPID JOBROLE SALARY EMP_PHNO

gobi 101 manager 70000 3452647586


divi 102 team leader 60000 6574839223
swathi 103 ass manager 750000 8675947352
priya 104 CEO 80000 7684635281

SQL> rollbacke to a;
SP2-0734: unknown command beginning "rollbacke ..." - rest of line ignored.

RESULT:

11
[Link]
FIBONACCI SERIES
DATE:

AIM:

ALGORITHM:

12
PROGRAM:
DECLARE
num_terms NUMBER :=10;
num1 NUMBER :=10;
num2 NUMBER :=1;
num_next NUMBER ;
BEGIN
DBMS_OUTPUT.PUT_LINE(' Fibonacci series ');
DBMS_OUTPUT.PUT_LINE(num1);
DBMS_OUTPUT.PUT_LINE(num2);
FOR i IN 3..num_terms LOOP
num_next :=num1+num2;
DBMS_OUTPUT.PUT_LINE(num_ne
xt); num1 := num2;
num2 :=num_next;
END LOOP;
END;
/

13
OUTPUT:
SQL> set serverout on
SQL> /
Fibonacci series
10
1
11
12
23
35
58
93
151
244

PL/SQL procedure successfully completed.

RESULT:

14
[Link]
FACTORIAL
DATE:

AIM:

ALGORITHM:

15
PROGRAM:
DECLARE
n NUMBER :=&n;
factorial NUMBER :=1;
BEGIN
FOR i IN 1..n LOOP
factorial :=factorial*i;
END LOOP;
DBMS_OUTPUT.PUT_LINE(' Factorial of ' || n || 'is:' || factorial);
END;
/

16
OUTPUT:
Enter value for n: 5
old 2: n NUMBER :=&n;
new 2: n NUMBER :=5;
Factorial of 5is:120

PL/SQL procedure successfully completed.

RESULT:

17
[Link]
STRING REVERSE
DATE:

AIM:

ALGORITHM:

18
PROGRAM:
DECLARE
str VARCHAR(30) :='&n';
len NUMBER;
str1 VARCHAR(30) :=' ';
BEGIN
len :=LENGTH(str);
FOR i IN REVERSE 1..len LOOP
str1 := str1 || SUBSTR (str,i,1);
END LOOP;
DBMS_OUTPUT.PUT_LINE(' Reversed string ' || str1);
END;
/

19
OUTPUT:

Enter value for n: welcome


old 2: str VARCHAR(30) :='&n';
new 2: str
VARCHAR(30) :='welcome'; Reversed
string emoclew

PL/SQL procedure successfully completed.

RESULT:

20
[Link]
SUM OF SERIES
DATE:

AIM:

ALGORITHM:

21
PROGRAM:
DECLARE

n NUMBER :=10;

sum_val
NUMBER;

BEGIN

sum_val :=n*(n+1)/2;

DBMS_OUTPUT.PUT_LINE(' sum of first ' || n || ' natural no is : ' || sum_val);


END;
/

22
OUTPUT:
sum of first 10 natural no is : 55

PL/SQL procedure successfully completed.

RESULT :

23
[Link]
TRIGGER
DATE:

AIM:

ALGORITHM:

24
PROGRAM:
create table
emp_audit( emp_id
NUMBER, created_by
VARCHAR(30),
created_date DATE,
audit_timestamp TIMESTAMP DEFAULT SYSTIMESTAMP
);
CREATE OR REPLACE TRIGGER try_em_before_ins
BEFORE INSERT ON emp
FOR EACH ROW
BEGIN
:NEW.created_by := NVL(:NEW.created_by, USER); -- Replace USER with a variable
:NEW.created_date := NVL(:NEW.created_date, SYSDATE);

INSERT INTO em_audit (emp_id, created_by, created_date)


VALUES (:NEW.em_id, :NEW.created_by, :NEW.created_date);
END;
/
SELECT * FROM em_audit;
INSERT INTO em_audit (emp_id) VALUES (101);
INSERT INTO em_audit(emp_id, created_by) VALUES (102, 'MANAGER');

SQL> create table em_audit(


2 emp_id NUMBER,
3 created_by VARCHAR(30),
4 created_date DATE,
5 audit_timestamp TIMESTAMP DEFAULT SYSTIMESTAMP
6 );

Table created.

25
SQL> CREATE OR REPLACE TRIGGER try_em_before_ins
2 BEFORE INSERT ON em
3 FOR EACH ROW
4 BEGIN
5 :NEW.created_by := NVL(:NEW.created_by, USER); -- Replace USER with a variable if
needed
6 :NEW.created_date := NVL(:NEW.created_date,
SYSDATE); 7
8 INSERT INTO em_audit (emp_id, created_by, created_date)
9 VALUES (:NEW.em_id, :NEW.created_by, :NEW.created_date);
10 END;
11 /
BEFORE INSERT ON em

SQL> SELECT * FROM em_audit;

no rows selected

SQL> INSERT INTO em (em_id) VALUES (101);


INSERT INTO em (em_id) VALUES (101)

SQL> INSERT INTO em (em_id, created_by) VALUES (102, 'MANAGER');


INSERT INTO em (em_id, created_by) VALUES (102, 'MANAGER')

SQL> INSERT INTO em_audit (emp_id) VALUES


(101); 1 row created.
SQL> INSERT INTO em_audit(emp_id, created_by) VALUES (102,
'MANAGER'); 1 row created.

SQL> SELECT * FROM em_audit;

26
OUTPUT:

EMP_ID CREATED_BY CREATED_D

AUDIT_TIMESTAMP

101
04-SEP-25 01.57.35.378000 PM

102 MANAGER
04-SEP-25 01.57.35.393000 PM

RESULT :

27
[Link]
STUDENT MARK ANALYSIS USING CURSOR
DATE:

AIM:

ALGORITHM:

28
PROGARM:
CREATE TABLE Student_Marks
( Roll_No NUMBER PRIMARY
KEY, Name VARCHAR2(50),
Marks NUMBER
);
INSERT INTO Student_Marks VALUES (1, 'John', 85);
INSERT INTO Student_Marks VALUES (2, 'Mary', 55);
INSERT INTO Student_Marks VALUES (3, 'Alex', 90);
INSERT INTO Student_Marks VALUES (4, 'Sara', 35);
COMMIT;

DECLARE
CURSOR student_cursor IS
SELECT * FROM
Student_Marks;
stu_rec
student_cursor%ROWTYPE; result
VARCHAR2(10);
BEGIN
OPEN student_cursor;
LOOP
FETCH student_cursor INTO stu_rec;
EXIT WHEN student_cursor%NOTFOUND;
IF stu_rec.Marks >= 40 THEN
result := 'Pass';
ELSE
result := 'Fail';
END IF;
DBMS_OUTPUT.PUT_LINE('Name: ' || stu_rec.Name || ', Marks: ' || stu_rec.Marks || ', Result: '
|| result);
END LOOP;
CLOSE student_cursor;
END;
29
SQL> CREATE TABLE Student_Marks (
2 Roll_No NUMBER PRIMARY KEY,
3 Name VARCHAR2(50),
4 Marks NUMBER
5 );

Table created.

SQL> INSERT INTO Student_Marks VALUES (1, 'John', 85);

1 row created.

SQL> INSERT INTO Student_Marks VALUES (2, 'Mary', 55);

1 row created.

SQL> INSERT INTO Student_Marks VALUES (3, 'Alex', 90);

1 row created.

SQL> INSERT INTO Student_Marks VALUES (4, 'Sara', 35);

1 row created.

SQL> COMMIT;

Commit complete.

DECLARE
CURSOR student_cursor IS
SELECT * FROM Student_Marks;

30
stu_rec student_cursor%ROWTYPE;
result VARCHAR2(10);
BEGIN
OPEN student_cursor;
LOOP
FETCH student_cursor INTO stu_rec;
EXIT WHEN student_cursor%NOTFOUND;
IF stu_rec.Marks >= 40 THEN
result := 'Pass';
ELSE
result := 'Fail';
END IF;
DBMS_OUTPUT.PUT_LINE('Name: ' || stu_rec.Name || ', Marks: ' || stu_rec.Marks || ', Result:
' || result);
END LOOP;
CLOSE student_cursor;
END;
/

PL/SQL procedure successfully completed.

SQL> set serveroutput on; SQL> /

31
OUTPUT:

Name: John, Marks: 85, Result: Pass Name: Mary,


Marks: 55, Result: Pass Name: Alex, Marks: 90,
Result: Pass Name: Sara, Marks: 35, Result: Fail

PL/SQL procedure successfully completed.

RESULT :

32
[Link]
LIBRARY MANAGEMENT SYSTEM

DATE:

AIM:

ALGORITHM:

33
PROGARM:

using System;
using [Link];using
[Link];
using [Link]; using
[Link];
using [Link];
using [Link];
using [Link];using
[Link];

namespace insert_update_delete
{
public partial class WebForm1 : [Link]
{
SqlConnection con = new SqlConnection(@"Data
Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\system103\document s\visual
studio 2013\Projects\insert update delete\insert update
delete\App_Data\[Link];Integrated Security=True");

protected void Page_Load(object sender, EventArgs e)


{
[Link]();
}

protected void Button1_Click(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand("insert into std values('" +[Link] +
"','" + [Link] + "','" + [Link] + "')", con);
[Link](); [Link]();
[Link]("<script>alert('Data has been Inserted')</script>");
[Link]();
[Link] = "";
[Link] = "";
[Link] = "";
}

protected void Button2_Click(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand("update std set name='" + [Link]
+ "',age='" + [Link] + "' where Id='" + [Link] +"'", con);
[Link](); [Link]();
[Link]("<script>alert('Data has been Updated')</script>");
[Link]();
[Link] = "";
[Link] = "";
[Link] = "";
}
34
protected void Button3_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("delete from std where Id='" +[Link] +
"'", con);
[Link](); [Link]();
[Link]("<script>alert('Data has been Deleted')</script>");[Link]();
[Link] = "";
[Link] = "";
[Link] = "";
}

protected void Button4_Click(object sender, EventArgs e)


{
string find = "select * from std where (Id like '%' +@Id+ '%')";
SqlCommand cmd = new SqlCommand(find, con);
[Link]("Id", [Link]).Value =
[Link];
[Link]();
SqlDataAdapter da = new SqlDataAdapter();
[Link] = cmd;
DataSet ds = new DataSet(); [Link](ds,
"Id"); [Link] = null;
[Link] = ds;
[Link](); [Link]();
[Link] = "data has been selected";

protected void Button5_Click(object sender, EventArgs e)


{
[Link] = "";
[Link] = "";
[Link] = "";
}
}
}

35
OUTPUT:

36
RESULT :

37
[Link]
DATE: STUDENT MARK ANALYSIS

AIM:

ALGORITHM:

38
PROGARM:

using System;
using [Link];using
[Link];
using [Link]; using
[Link];
using [Link];
using [Link];
using [Link];using
[Link];

public partial class _Default : [Link]


{
SqlConnection con = new SqlConnection(@"Data
Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Administrator\Docu
ments\Visual Studio 2013\WebSites\WebSite3\App_Data\[Link];Integrated
Security=True");
protected void Page_Load(object sender, EventArgs e)
{
[Link]();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into aaa values('" +
[Link] + "','" + [Link] + "','" + [Link] + "','" + [Link] +
"','" + [Link] + "')", con);
[Link]();
[Link]();
[Link]="Data has been inserted";
[Link](); [Link]= "";
[Link]= "";
[Link]= "";
[Link] = "";
[Link] = "";
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("update aaa set name='" +
[Link] + "',dept='" + [Link] + "',m1='" + [Link] + "',m2='"
+ [Link] + "' where Id='" + [Link] + "'", con);
[Link]();
[Link]();
[Link] = "Data has been updated";
[Link]();
[Link] = "";
[Link] = "";
[Link] = "";
[Link] = ""; 39
[Link] = "";
}

protected void Button3_Click(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand("delete from aaa where
Id='"+Convert.ToInt32([Link]).ToString()+"'",con);
[Link]();
[Link]();
[Link] = "Data has been deleted";
[Link]();
[Link] = "";
[Link] = "";
[Link] = "";
[Link] = "";
[Link] = "";
}
}

40
OUTPUT:

41
RESULT :

42

You might also like