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

SQL File-1

SQL is a standard language used to store, manipulate, and retrieve data in relational database management systems. It has four main types of commands: data definition language for creating/modifying/deleting objects; data manipulation language for inserting/selecting/updating/deleting data; data control language for granting/revoking privileges; and transaction control language for committing/rolling back transactions. SQL is widely used because it allows users to access, define, embed, and manage data across many relational database systems.

Uploaded by

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

SQL File-1

SQL is a standard language used to store, manipulate, and retrieve data in relational database management systems. It has four main types of commands: data definition language for creating/modifying/deleting objects; data manipulation language for inserting/selecting/updating/deleting data; data control language for granting/revoking privileges; and transaction control language for committing/rolling back transactions. SQL is widely used because it allows users to access, define, embed, and manage data across many relational database systems.

Uploaded by

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

STRUCTURED QUERY LANGUAGE

SQL is Structured Query Language, which is a computer language for storing,


manipulating and retrieving data stored in a relational database.

SQL is the standard language for Relational Database System. All the
Relational Database Management Systems (RDMS) like MySQL, MS Access,
Oracle, Sybase, Informix, Postgres and SQL Server use SQL as their standard
database language.

Also, they are using different dialects, such as:


 MS SQL Server using T-SQL,
 Oracle using PL/SQL,
 MS Access version of SQL is called JET SQL (native format) etc.

SQL is widely popular because it offers the following advantages:

 Allows users to access data in the relational database management


systems.
 Allows users to describe the data.
 Allows users to define the data in a database and manipulate that
data.
 Allows to embed within other languages using SQL modules, libraries
&
 pre-compilers.
 Allows users to create and drop databases and tables.
 Allows users to create view, stored procedure, functions in a database.
 Allows users to set permissions on tables, procedures and views.
SQL has four basic types of queries:

1. Data Definition Language (DDL)

Command Description
CREATE Creates a new table, a view of a table, or other object in the
database.

ALTER Modifies an existing database object, such as a table.

DROP Deletes an entire table, a view of a table or other objects in the


database.

2. Data Manipulation Language (DML)

Command Description
SELECT Retrieves certain records from one or more tables.

INSERT Creates a record.

UPDATE Modifies records.


DELETE Deletes records.

3. Data Control Language (DCL)

Command Description
GRANT Gives a privilege to user.

REVOKE Revokes a privilege to user.

4. Transaction Control Language (TCL)

Command Description
COMMIT To commit any transaction (updates, insertions etc)

ROLLBACK To rollback the changes.


1. Program to show first normal form with basic command in SQL
command-line interface.

CREATE Statement

SQL> create table employee (

empnumber number(5) primary key,

empname varchar2(30),

salary number(9,2),

emails varchar2(100)

);

Output

Table created.

INSERT Statement

SQL> insert into employee (empnumber, empname, salary, emails)

Values (1001, ‘Jatin’, 40000, ‘[email protected]’);

Output

1 row created.
SELECT Statement

SQL> select * from employee;

Output

Empnumber empname salary emails

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

1001 Jatin 40000 [email protected]

DROP Statement

SQL> drop table employee;

Output

Table dropped.

SQL> create table employee

( empnumber number(4) primary key,

empfirstname varchar2(16),

emplastname varchar2(16),

salary number(9,2),

email1 varchar2(50),

email2 varchar2(50)

);
Output

Table created.

SQL> insert into employee (empnumber, empfirstname, emplastname,


salary, email1, email2)

Values (1001, ‘Jatin’, ‘Arora’, 32000, ‘[email protected]’,


[email protected]’);

Output

1 row created.

In the above table, columns email1 and email2 are repeating groups.
We can use a separate table to store email addresses and map them
with our employee table using foreign key reference as below.

SQL> create table employee

(empnumber number(4) primary key,

empfirstname varchar2(16),

emplastname varchar2(16),

salary number(9,2)

);

Output

Table created.
SQL> create table email_address

(id number(5) primary key,

email varchar2(50),

empnumber number(5),

foreign key (empnumber) references employee(empnumber) on


delete cascade

);

Output

Table created.

SQL> insert into employee (empnumber, empfirstname, emplastname,


salary)

Values(1001, ‘Jatin’, ‘Arora’, 40000);

Output

1 row created.

SQL> insert into email_address (id, email, empnumber)

Values(1, ‘[email protected]’, 1001);

Output

1 row created.
SQL> insert into email_address (id, email, empnumber)

Values(2, ‘[email protected]’, 1001);

Output

1 row created.

SQL> select * from employee;

Output

empnumber empfirstname emplastname salary

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

1001 Jatin Arora 40000

SQL> select * from email_address;

Output

id email empnumber

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

1 [email protected] 1001

2 [email protected] 1001
2. Program to show DDL (create, alter, drop) command.

SQL> create table student

(rno number(5), sname varchar(15), m1 number(3), m2 number(3) );

Output

Table created.

SQL> desc student;

Output

Name Null? Type

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

Rno number(5)

Sname varchar2(15)

M1 number(3)

M2 number (3)

SQL> alter table student add (m3 number(3), total number (3), result
char(4));

Output

Table altered.
SQL> desc student;

Output

Name Null? Type

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

Rno number(5)

Sname varchar2(15)

M1 number(3)

M2 number (3)

Total number(3)

Result char(4)

SQL> alter table student modify (result varchar2(4));

Output

Table altered.

SQL> desc student;


Output

Name Null? Type

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

Rno number(5)

Sname varchar2(15)

M1 number(3)

M2 number (3)

Total number(3)

Result varchar2(4)

SQL> drop table student;

Output

Table dropped.

SQL> desc student;

Output

Object does not exist.


3. Program to show DML (select, insert, delete, update) command.

INSERT Statement

SQL> insert into student (rno, sname, m1, m2, m3)

Values(1001, ‘Jatin’, 60,70,80);

Output

1 row created.

SQL> insert into student (rno, sname, m1, m2, m3)

Values(1002, ‘Arora’, 65,86,92);

Output

1 row created.

SQL> insert into student (rno, sname, m1, m2, m3)

Values(1003, ‘Neha’, 49,50,55);

Output

1 row created.
SELECT Statement

SQL> select * from student;

Output

Rno sname m1 m2 m3 total result

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

1001 Arora 60 70 80 210 pass

1002 Jatin 65 86 92 243 pass

1003 Neha 49 50 55 154 Fail

SQL> select rno, result from student;

Output

Rno Result

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

1001 pass

1002 pass

1003 fail
SQL> select * from student;

Output

Rno sname m1 m2 m3 total result

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

1001 Arora 60 70 80

1002 Jatin 65 86 92

1003 Neha 49 50 55

UPDATE Statement

SQL> update student set total=m1+m2+m3;

Output

3 rows updated.

SQL> update student set result=’pass’ where m1>=50 and m2>=50 and
m3>=50;

Output

2 rows updated.
SQL> update student set result=’fail’ where m1<50 or m2<50 or m3>50;

Output

1 row updated.

SQL> select * from student;

Output

Rno sname m1 m2 m3 total result

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

1001 Arora 60 70 80 210 pass

1002 Jatin 65 86 92 243 pass

1003 Neha 49 50 55 154 Fail

DELETE Statement

SQL> delete from student where rno=1002;

Output

1 row deleted.
SQL> select * from student;

Output

Rno sname m1 m2 m3 total result

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

1001 Arora 60 70 80 210 pass

1003 Neha 49 50 55 154 Fail

4. Program to show TCL (commit, savepoint, rollback) command.

Commit : This command ends a transaction. The changes (made by


transactions) are made permanent to the database.

Example

SQL> create table stud (rno number(5), result varchar2(6));

Output

Table created.

SQL> insert into stud values (1001,’pass’);


Output

1 row created.

SQL> insert into stud values (1002,’fail’);

Output

1 row created.

SQL> insert into stud values (1003,’pass’);

Output

1 row created.

SQL> select * from stud;

Output

Rno result

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

1001 pass

1002 fail

1003 pass
SQL> commit;

Output

Commit complete.

Savepoint : A savepoint is a marker which represents a position in a


transaction.

Example

SQL> select * from stud;

Output

Rno result

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

1001 pass

1002 fail

1003 pass

SQL> update stud set result=’pass’ where rno=1002;

Output

1 row updated.
SQL> savepoint sp;

Output

Savepoint created.

Rollback : This command is used to rollback (undo) the changes done


in a table.

Example

SQL> update stud set result=’fail’ where rno=1001;

Output

1 row updated.

SQL> update stud set result=’fail’ where rno=1003;

Output

1 row updated.

SQL> select * from stud;


Output

Rno result

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

1001 fail

1002 pass

1003 fail

SQL> rollback to savepoint sp;

Output

Rollback complete.

SQL> select * from stud;

Output

Rno result

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

1001 pass

1002 pass

1003 pass
5. Program to show union command.

SQL> select * from stud1;

Output

Rno result

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

1001 pass

1002 fail

1003 pass

SQL> select * from stud2;

Output

Rno result

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

1001 pass

1004 fail
SQL> select * from stud1 union select * from stud2;

Output

Rno result

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

1001 pass

1002 fail

1003 pass

1004 fail

6. Program to show intersect command.

SQL> select * from stud1 intersect select * from stud2;

Output

Rno result

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

1001 pass
7. Write a PL/SQL code block to find sum and average of three
numbers.

declare
a number:=&a;
b number:=&b;
c number:=&c;
sm number;
av number;
begin
sm:=a+b+c;
av:=sm/3;
dbms_output.put_line('Sum = '||sm);
dbms_output.put_line('Average = '||av);
end;

Output
Enter value for a: 12
old 2: a number:=&a;
new 2: a number:=12;
Enter value for b: 4
old 3: b number:=&b;
new 3: b number:=4;
Enter value for c: 21
old 4: c number:=&c;
new 4: c number:=21;
Sum = 37
Average = 12.33333333333333333333333333333333333333
PL/SQL procedure successfully completed.

8. Write a PL/SQL code block to find Simple Interest.

declare
p number(9,2);
r number(9,2);
t number(9,2);
si number(9,2);
begin
p:=&p;
r:=&r;
t:=&t;
si:=(p*r*t)/100;
dbms_output.put_line('Simple Interest = '||si);
end;

Output
Enter value for p: 150
old 7: p:=&p;
new 7: p:=150;
Enter value for r: 4
old 8: r:=&r;
new 8: r:=4;
Enter value for t: 3
old 9: t:=&t;
new 9: t:=3;
Simple Interest = 18
PL/SQL procedure successfully completed.

9. Write a PL/SQL code block to find area of circles with radius


greater than 3 and less than equal to 7 and store the result in a
table with attributes radius and area.

declare
area number(5,2);
radius number(1):=3;
pi constant number(3,2):=3.14;
begin
while radius<=7
loop
area:=pi*radius*radius;
insert into areas values (radius,area);
radius:=radius+1;
end loop;
end;

Output
PL/SQL procedure successfully completed.
SQL> select * from areas;
RADIUS AREA
---------- ----------
3 28.26
4 50.24
5 78.5
6 113.04
7 153.86

10. Write a PL/SQL code block to find factorial of a number.

declare
n number;
i number;
f number:=1;
begin
n:=&n;
for i in 1..n
loop
f:=f*i;
end loop;
dbms_output.put_line(n||'! = '||f);
end;

Output

Enter value for n: 5

old 6: n:=&n;
new 6: n:=5;
5! = 120
PL/SQL procedure successfully completed.
11. Write a PL/SQL code block to find reverse of a number.
declare
N number;
S NUMBER := 0;
R NUMBER;
K number;
begin
N := &N;
K := N;
loop
exit WHEN N = 0;
S := S * 10;
R := MOD(N,10);
S := S + R;
N := TRUNC(N/10);
end loop;
dbms_output.put_line('THE REVERSED DIGITS OF '||K||' = '||S);
end;

Output

Enter value for n: 4567


old 7: N := &N;
new 7: N := 4567;
THE REVERSED DIGITS OF 4567 = 7654
PL/SQL procedure successfully completed.
12. Write a PL/SQL code block to find greatest of three
numbers.
declare
a number := &a;
b number := &b;
c number := &c;
begin
if a>b and a>c then
dbms_output.put_line(a||' is greatest.');
elsif b>a and b>c then
dbms_output.put_line(b||' is greatest.');
else
dbms_output.put_line(c||' is greatest.');
end if;
end;

Output

Enter value for a: 6

old 2: a number := &a;


new 2: a number := 6;
Enter value for b: 3
old 3: b number := &b;
new 3: b number := 3;
Enter value for c: 4
old 4: c number := &c;
new 4: c number := 4;
6 is greatest.
PL/SQL procedure successfully completed.

13. Write a PL/SQL code block to generate Fibonacci series.

declare
a number:= 0 ;
b number:= 1;
c number;
begin
dbms_output.put(a||' '||b||' ');
for i in 3..10 loop
c := a + b;
dbms_output.put(c||' ');
a := b;
b := c;
end loop;
dbms_output.put_line(' ');
end;

Output

0 1 1 2 3 5 8 13 21 34
PL/SQL procedure successfully completed.
14. Write a PL/SQL code block to find sum of digits of a
number.

declare
N number ;
S number:=0;
R number;
begin
N:=&N;
WHILE N<>0 LOOP
R := MOD(N,10);
S := S + R;
N := TRUNC(N/10);
end loop;
dbms_output.put_line('THE SUM OF THE DIGITS = '||S);
end;

Output

Enter value for n: 535

old 6: N:=&N;
new 6: N:=535;
THE SUM OF THE DIGITS = 13
PL/SQL procedure successfully completed.

You might also like