0% found this document useful (0 votes)
2 views31 pages

Dbms

The document contains SQL commands for creating and manipulating student, product, and employee tables, including operations like inserting, updating, and deleting records. It demonstrates the use of constraints such as NOT NULL, UNIQUE, PRIMARY KEY, CHECK, and DEFAULT, as well as aggregate functions and string manipulation functions. The document also includes examples of querying data using various SQL clauses and functions.

Uploaded by

suhel563
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)
2 views31 pages

Dbms

The document contains SQL commands for creating and manipulating student, product, and employee tables, including operations like inserting, updating, and deleting records. It demonstrates the use of constraints such as NOT NULL, UNIQUE, PRIMARY KEY, CHECK, and DEFAULT, as well as aggregate functions and string manipulation functions. The document also includes examples of querying data using various SQL clauses and functions.

Uploaded by

suhel563
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
You are on page 1/ 31

PROGRAM:

SQL> create table student(sid varchar(15),sname varchar(15),age int,branch varchar(5));


Table created.

SQL> alter table student add address varchar(30);


Table altered.

SQL> select * from student;


no rows selected

SQL> insert into student values('&sid','&sname',&age,'&branch','&address');


Enter value for sid: 23f4108
Enter value for sname: shiva
Enter value for age: 18
Enter value for branch: cse
Enter value for address: kuppam
old 1: insert into student values('&sid','&sname',&age,'&branch','&address')
new 1: insert into student values('23f4108','shiva',18,'cse','kuppam')
1 row created.

SQL> /
Enter value for sid: 23f4100
Enter value for sname: dev
Enter value for age: 19
Enter value for branch: cse
Enter value for address: kuppam
old 1: insert into student values('&sid','&sname',&age,'&branch','&address')
new 1: insert into student values('23f4100','dev',19,'cse','kuppam')
1 row created.

SQL> /
Enter value for sid: 23f4109
Enter value for sname: shivansh
Enter value for age: 19
Enter value for branch: cse
Enter value for address: banglore
old 1: insert into student values('&sid','&sname',&age,'&branch','&address')
new 1: insert into student values('23f4109','shivansh',19,'cse','banglore')
1 row created.

SQL> select * from student;


SID SNAME AGE BRANCH ADDRESS
--------------- --------------- ---------- ----- ------------------------------
23f4108 shiva 18 cse kuppam
23f4100 dev 19 cse kuppam
23f4109 shivansh 19 cse banglore

SQL> UPDATE student


2 SET address = 'chennai'
3 WHERE sid = '23f4100';
1 row updated.

SQL> select * from student;


SID SNAME AGE BRANCH ADDRESS
--------------- --------------- ---------- ----- ------------------------------
23f4108 shiva 18 cse kuppam
23f4100 dev 19 cse chennai
23f4109 shivansh 19 cse banglore

SQL> alter table student drop column address;


Table altered.

SQL> select * from student;


SID SNAME AGE BRANCH
--------------- --------------- ---------- -----
23f4108 shiva 18 cse
23f4100 dev 19 cse
23f4109 shivansh 19 cse

SQL> drop table student;


Table dropped.

SQL> desc student;


ERROR:
ORA-04043: object student does not exist

/*
Program executed by K.SREE HARSHA , 23F41A0545.
*/
PROGRAM:

SQL> --Not Null


SQL> create table student(id int not null,name varchar(10),age int);
Table created.

SQL> insert into student values('&id','&name','&age');


Enter value for id: 1
Enter value for name: shiva
Enter value for age: 18
old 1: insert into student values('&id','&name','&age')
new 1: insert into student values('1','shiva','18')
1 row created.

SQL> /
Enter value for id:
Enter value for name: shivansh
Enter value for age: 19
old 1: insert into student values('&id','&name','&age')
new 1: insert into student values('','shivansh','19')
insert into student values('','shivansh','19')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SYSTEM"."STUDENT"."ID")

SQL> drop table student;


Table dropped.
SQL>--Unique
SQL> create table student(id int unique,name varchar(10),age int);
Table created.

SQL> insert into student values(&id,'&name',&age);


Enter value for id: 1
Enter value for name: dev
Enter value for age: 19
old 1: insert into student values(&id,'&name',&age)
new 1: insert into student values(1,'dev',19)
1 row created.

SQL> /
Enter value for id: 1
Enter value for name: shiva
Enter value for age: 18
old 1: insert into student values(&id,'&name',&age)
new 1: insert into student values(1,'shiva',18)
insert into student values(1,'shiva',18)
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.SYS_C005445) violated

SQL> drop table student;


Table dropped.

SQL>--Primary key
SQL> create table student(id int primary key,name varchar(10),age int);
Table created.
SQL> insert into student values('&id','&name','&age');
Enter value for id: 1
Enter value for name: shivansh
Enter value for age: 19
old 1: insert into student values('&id','&name','&age')
new 1: insert into student values('1','shivansh','19')
1 row created.

SQL> /
Enter value for id: 1
Enter value for name: dev
Enter value for age: 19
old 1: insert into student values('&id','&name','&age')
new 1: insert into student values('1','dev','19')
insert into student values('1','dev','19')
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.SYS_C005447) violated

SQL> /
Enter value for id:
Enter value for name: shiva
Enter value for age: 18
old 1: insert into student values('&id','&name','&age')
new 1: insert into student values('','shiva','18')
insert into student values('','shiva','18')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SYSTEM"."STUDENT"."ID")
SQL> drop table student;
Table dropped.

SQL>--check
SQL> create table student(id int primary key,name varchar(10),age int,check(age>18));
Table created.

SQL> insert into student values(&id,'&name',&age);


Enter value for id: 1
Enter value for name: shiva
Enter value for age: 19
old 1: insert into student values(&id,'&name',&age)
new 1: insert into student values(1,'shiva',19)
1 row created.

SQL> /
Enter value for id: 1
Enter value for name: shiva
Enter value for age: 17
old 1: insert into student values(&id,'&name',&age)
new 1: insert into student values(1,'shiva',17)
insert into student values(1,'shiva',17)
*
ERROR at line 1:
ORA-02290: check constraint (SYSTEM.SYS_C005450) violated

SQL> drop table student;


Table dropped.
SQL>--Default
SQL> create table student(id int,name varchar(10) default 'aradhana',age int);
Table created.

SQL> insert into student(id,age) values (101,10);


1 row created.

SQL> select * from student;

ID NAME AGE
---------- ---------- ----------
101 aradhana 10

SQL> drop table student;


Table dropped.

/*
Program executed by K.SREE HARSHA , 23F41A0545.
*/
PROGRAM:

SQL> CREATE TABLE students (


2 roll_number INT PRIMARY KEY,
3 name VARCHAR(100) NOT NULL,
4 marks INT CHECK (marks >= 0),
5 rank INT UNIQUE
6 );
Table created.

SQL> INSERT INTO students (roll_number, name, marks, rank) VALUES (1, 'John Doe',
450, 1);
1 row created.

SQL> INSERT INTO students (roll_number, name, marks, rank) VALUES (2, 'Jane Smith',
440, 2);
1 row created.

SQL> INSERT INTO students (roll_number, name, marks, rank) VALUES (3, 'Alice Brown',
430, 3);
1 row created.

SQL> INSERT INTO students (roll_number, name, marks, rank) VALUES (4, 'Bob
Johnson', 420, 4);
1 row created.

SQL> INSERT INTO students (roll_number, name, marks, rank) VALUES (5, 'Charlie
Davis', 410, 5);
1 row created.

SQL> select * from students;


ROLL_NUMBER
-----------
NAME
--------------------------------------------------------------------------------
MARKS RANK
---------- ----------
1
John Doe
450 1

2
Jane Smith
440 2

ROLL_NUMBER
-----------
NAME
--------------------------------------------------------------------------------
MARKS RANK
---------- ----------

3
Alice Brown
430 3

4
Bob Johnson
ROLL_NUMBER
-----------
NAME
--------------------------------------------------------------------------------
MARKS RANK
---------- ----------
420 4

5
Charlie Davis
410 5

SQL> SELECT roll_number, name


2 FROM students
3 WHERE rank = 4;
ROLL_NUMBER
-----------
NAME
--------------------------------------------------------------------------------
4
Bob Johnson

SQL> -- IN
SQL> SELECT name
2 FROM students
3 WHERE rank IN (1, 2, 3);
NAME
--------------------------------------------------------------------------------
John Doe
Jane Smith
Alice Brown

SQL> --ANY
SQL> SELECT name
2 FROM students
3 WHERE marks > ANY (SELECT marks FROM students WHERE rank > 3);
NAME
--------------------------------------------------------------------------------
John Doe
Jane Smith
Alice Brown
Bob Johnson

SQL> --ALL
SQL> SELECT name
2 FROM students
3 WHERE marks > ALL (SELECT marks FROM students WHERE rank > 3);
NAME
--------------------------------------------------------------------------------
John Doe
Jane Smith
Alice Brown
SQL> --EXISTS
SQL> SELECT 'Fourth rank exists' AS status
2 FROM DUAL
3 WHERE EXISTS (SELECT 1 FROM students WHERE rank = 4);
STATUS
------------------
Fourth rank exists

SQL> --NOT EXISTS


SQL> SELECT name
2 FROM students
3 WHERE NOT EXISTS (
4 SELECT 1
5 FROM students
6 WHERE rank IN (1, 2, 3) AND students.name = name
7 );
no rows selected

SQL> --UNION
SQL> SELECT name
2 FROM students
3 WHERE rank IN (1, 2)
4 UNION
5 SELECT name
6 FROM students
7 WHERE marks > 430;
NAME
--------------------------------------------------------------------------------
Jane Smith
John Doe

SQL> --INTERSECT
SQL> SELECT name
2 FROM students
3 WHERE rank <= 3
4 INTERSECT
5 SELECT name
6 FROM students
7 WHERE marks > 430;
NAME
--------------------------------------------------------------------------------
Jane Smith
John Doe

/*
Program executed by K.SREE HARSHA , 23F41A0545.
*/
PROGRAM:

SQL> create table product(pno int primary key ,pname varchar(30),price float,quantity int);
Table created.

SQL> insert into product values(1,'dairy milk',60,2);


1 row created.

SQL> insert into product values(2,'good day',25,4);


1 row created.

SQL> insert into product values(3,'boost',10,6);


1 row created.

SQL> insert into product values(4,'maggi',5,10);


1 row created.

SQL> insert into product values(5,'book',20,20);


1 row created.

SQL> select * from product;

PNO PNAME PRICE QUANTITY


---------- ------------------------------ ---------- ----------
1 dairy milk 60 2
2 good day 25 4
3 boost 10 6
4 maggi 5 10
5 book 20 20
SQL> select count(price) from product;
COUNT(PRICE)
------------
5

SQL> select count(quantity) from product;


COUNT(QUANTITY)
---------------
5

SQL> select sum(price) from product;


SUM(PRICE)
----------
120

SQL> select sum(quantity) from product;


SUM(QUANTITY)
-------------
42

SQL> select avg(price) from product;


AVG(PRICE)
----------
24

SQL> select avg(quantity) from product;


AVG(QUANTITY)
-------------
8.4

SQL> select max(price) from product;


MAX(PRICE)
----------
60

SQL> select max(quantity) from product;


MAX(QUANTITY)
-------------
20

SQL> select min(price) from product;


MIN(PRICE)
----------
5

SQL> select min(quantity) from product;


MIN(QUANTITY)
-------------
2

SQL> Create table employ(sid int,name varchar(20),dept varchar(10),sal float);


Table created.

SQL> insert into employ values(&sid,'&name','&dept',&sal);


Enter value for sid: 1
Enter value for name: ayisha
Enter value for dept: ece
Enter value for sal: 6000
old 1: insert into employ values(&sid,'&name','&dept',&sal)
new 1: insert into employ values(1,'ayisha ','ece',6000)
1 row created.

SQL> /
Enter value for sid: 2
Enter value for name: sindhu
Enter value for dept: it
Enter value for sal: 50000
old 1: insert into employ values(&sid,'&name','&dept',&sal)
new 1: insert into employ values(2,'sindhu','it',50000)
1 row created.

SQL> /
Enter value for sid: 2
Enter value for name: sai
Enter value for dept: it
Enter value for sal: 80000
old 1: insert into employ values(&sid,'&name','&dept',&sal)
new 1: insert into employ values(2,'sai','it',80000)
1 row created.

SQL> /
Enter value for sid: 4
Enter value for name: lalli
Enter value for dept: ece
Enter value for sal: 8000
old 1: insert into employ values(&sid,'&name','&dept',&sal)
new 1: insert into employ values(4,'lalli','ece',8000)
1 row created.

SQL> select dept,sum(sal) from employ group by dept;


DEPT SUM(SAL)
---------- ----------
it 130000
ece 14000

SQL> select dept,sum(sal) from employ group by dept having sum(sal)>25000;


DEPT SUM(SAL)
---------- ----------
it 130000

/*
Program executed by K.SREE HARSHA , 23F41A0545.
*/
PROGARM:

SQL> --to_char
SQL> CREATE TABLE db (dob DATE);
Table created.

SQL> INSERT INTO db (dob) VALUES (TO_DATE('1996-05-21', 'YYYY-MM-DD'));


1 row created.

SQL> SELECT TO_CHAR(dob, 'Month DD, YYYY') AS formatted_date,TO_CHAR(dob,


'Month DD, YYYYSP')AS formatted_in_words from db;
FORMATTED_DATE FORMATTED_IN_WORDS
------------------ --------------------------------------------------------
May 21, 1996 May 21, ONE THOUSAND NINE HUNDRED NINETY-SIX

SQL> --to_numder
SQL> select to_number('234.87') from dual;
TO_NUMBER('234.87')
-------------------
234.87

SQL> --to_date
SQL> select to_date('jan 21 1998','month dd,yy') from dual;
TO_DATE('
---------
21-JAN-98
/*
Program executed by K.SREE HARSHA , 23F41A0545.
*/
PROGRAM:

SQL> --lower
SQL> select lower('SAI') from dual;
LOW
---
sai

SQL> --upper
SQL> select upper('sai') from dual;
UPP
---
SAI

SQL> --concat
SQL> select concat('hello','sai') from dual;
CONCAT('
--------
hellosai

SQL> --initcap
SQL> select initcap('sai') from dual;
INI
---
Sai

SQL> --length
SQL> select length('sai') from dual;
LENGTH('SAI')
-------------
3

SQL> --instr
SQL> select instr('ruhanika','a') from dual;
INSTR('RUHANIKA','A')
---------------------
4

SQL> --substr
SQL> select substr('ruhanika',5) from dual;
SUBS
----
nika

SQL> --lpad
SQL> select lpad('ruhanika',10,'****') from dual;
LPAD('RUHA
----------
**ruhanika

SQL> --rpad
SQL> select rpad('ruhanika',10,'****') from dual;
RPAD('RUHA
----------
ruhanika**

SQL> --ltrim
SQL> select ltrim(' ruhanika') from dual;

LTRIM('R
--------
ruhanika

SQL> --rtrim
SQL> select rtrim(' ruhanika ') from dual;
RTRIM('RU
---------
Ruhanika

/*
Program executed by K.SREE HARSHA , 23F41A0545.
*/
PROGARM:

SQL> --sysdate
SQL> select sysdate from dual;
SYSDATE
---------
08-JAN-25

SQL> --last_day
SQL> select last_day(sysdate) from dual;
LAST_DAY(
---------
31-JAN-25

SQL> --next_day
SQL> select next_day('20-nov-2015','friday')
NEXT_DAY(
---------
27-NOV-15

SQL> --add months


SQL> select add_months(sysdate,2) from dual;
ADD_MONTH
---------
08-MAR-25

SQL> -- months_between
SQL> select months_between('20-nov-2015','20-j
MONTHS_BETWEEN('20-NOV-2015','20-JAN-2016')
-------------------------------------------
-2

SQL> --least
SQL> select least(10,11,12) from dual;
LEAST(10,11,12)
---------------
10

SQL> select least('s','f','a') from dual;


L
-
a

SQL> -- greatest
SQL> select greatest(10,11,12) from dual;
GREATEST(10,11,12)
------------------
12

SQL> select greatest('s','f','a') from dual;


G
-
s

SQL> --ground
SQL> select round(21.088) from dual;
ROUND(21.088)
-------------
21
SQL> select trim(21.088) from dual;
TRIM(2
------
21.088

/*
Program executed by K.SREE HARSHA , 23F41A0545.
*/
PROGRAM:

SQL> create table student(id int,name varchar(20),class varchar(20));


Table created.

SQL> insert into student values(&id,'&name','&class');


Enter value for id: 1
Enter value for name: shivanya
Enter value for class: first
old 1: insert into student values(&id,'&name','&class')
new 1: insert into student values(1,'shivanya','first')
1 row created.

SQL> /
Enter value for id: 2
Enter value for name: shivika
Enter value for class: second
old 1: insert into student values(&id,'&name','&class')
new 1: insert into student values(2,'shivika','second')
1 row created.

SQL> /
Enter value for id: 3
Enter value for name: avani
Enter value for class: first
old 1: insert into student values(&id,'&name','&class')
new 1: insert into student values(3,'avani','first')
1 row created.
SQL> /
Enter value for id: 4
Enter value for name: akshara
Enter value for class: first
old 1: insert into student values(&id,'&name','&class')
new 1: insert into student values(4,'akshara','first')
1 row created.

SQL>--PL/SQL program
SQL> set serveroutput on;

SQL> declare
2 stu_id number;
3 stu_name varchar(20);
4 cursor stu_cur is
5 select id,name
6 from student
7 where class='first';
8 Begin
9 Open stu_cur;
10 Loop
11 fetch stu_cur into stu_id,stu_name;
12 exit when stu_cur%notfound;
13 dbms_output.put_line('student_id: '|| stu_id || 'student_name:'|| stu_name);
14 end loop;
15 close stu_cur;
16 end;
17 /
Output:
student_id: 1student_name:shivanya
student_id: 3student_name:avani
student_id: 4student_name:akshara

PL/SQL procedure successfully completed.

/*
Program executed by K.SREE HARSHA , 23F41A0545.
*/
PROGRAM:

SQL> Create table stu(name varchar(10),branch varchar(10));


Table created.

SQL> Insert into stu values('sai','it');


1 row created.

SQL> Savepoint h;
Savepoint created.

SQL> set serveroutput on;


SQL> begin
2 savepoint g;
3 insert into stu values('ruhi','cse');
4 exception
5 when dup_val_on_index then
6 rollback to g;
7 commit;
8 end;
9 /
PL/SQL procedure successfully completed.

SQL> Rollback to h;
Rollback complete.

/*
Program executed by K.SREE HARSHA , 23F41A0545.
*/

You might also like