Dbms
Dbms
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.
/*
Program executed by K.SREE HARSHA , 23F41A0545.
*/
PROGRAM:
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> /
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>--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> /
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
ID NAME AGE
---------- ---------- ----------
101 aradhana 10
/*
Program executed by K.SREE HARSHA , 23F41A0545.
*/
PROGRAM:
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.
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> -- 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> --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> /
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.
/*
Program executed by K.SREE HARSHA , 23F41A0545.
*/
PROGARM:
SQL> --to_char
SQL> CREATE TABLE db (dob DATE);
Table created.
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> -- 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> -- greatest
SQL> select greatest(10,11,12) from dual;
GREATEST(10,11,12)
------------------
12
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> /
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
/*
Program executed by K.SREE HARSHA , 23F41A0545.
*/
PROGRAM:
SQL> Savepoint h;
Savepoint created.
SQL> Rollback to h;
Rollback complete.
/*
Program executed by K.SREE HARSHA , 23F41A0545.
*/