Sie sind auf Seite 1von 18

RELATIONAL MODEL:

create table department(


deptname varchar2(10),
deptid number(10),
location varchar2(10));

desc department;

insert into department values('cse',12,'ldh');


insert into department values('mec',22,'jal');
insert into department values('cse',32,'ldh');
insert into department values('mec',25,'ldh');

select deptid,deptname from department


select * from department

select * from department where deptname='CSE';

PRIMARY KEY CONSTRAINTS:

create table employee(


fname varchar2(10),
lname varchar2(10),
empid number(10) CONSTRAINT PK1 PRIMARY KEY,
loc varchar2(10),
salary number(10));

insert into employee values('RAM','SINHA',1,'ldh',3209);


insert into employee values('RAM','SINHA',1,'ldh',3209);

drop table employee; //means employee table deleted

create table employee(


fname varchar2(10),
lname varchar2(10),
empid number(10) CONSTRAINT PK1 PRIMARY KEY,
loc varchar2(10) CONSTRAINT uq1 UNIQUE,

salary number(10));

describe employee========desc employee;

create table department(


dname varchar2(10),
empid number(10),
constraint fk1 FOREIGN KEY(empid)references employee(empid));

insert into department values('cse',3);//error(because empid has no value like 3 in


it in employee table)
insert into department values('cse',1);//run(because empid is 1 in employee table
as well);

alter table employee1 add (loc varchar2(10));//To enter a new column in the
existing table

modify modifies the datatype of the column(like from varchar2 to number)


alter table employee1 modify(loc number(20));

alter table employee1 drop(loc);//used to delete a column from the table

insert into employee1(empname,empid) values('Ram',454)//will enter the values in


the specific part of the table

delete from employee2;//to delete entire rows


delete from employee2 where empid=25;//to delete specific row

alter table employee1 rename column empid to employeeid;//to change the name of the
column

alter table employee1 rename to employee123;//to change the name of the table

Like if statement>>>>>

create table emp(


empname varchar2(10),
salary number(10)constraint s1 check(salary>10));

create table student_more(


regno number(10),
interests varchar2(10));

WHOLE PROGRAM>>>>>>>

create table student_personal(


sname varchar2(10),
age number(10) not null,
dept varchar2(10) CONSTRAINT uq2 UNIQUE,
year number(4) constraint s2 check(year>2017),
regno number(10) CONSTRAINT PK2 PRIMARY KEY);
1.)Create table student_personal with column names as sname,age,dept,year,regno

Ans->
create table student_personal(
sname varchar2(10),
age number(10),
dept varchar2(10) ,
year number(4) ,
regno number(10) ;

2.)Make regno as primary key,age as not null,dept as unique,year should be greater


than 2017

Ans->
alter table student_personal add primary key(regno);

alter table student_personal modify(age number(10) constraint not null);

alter table student_personal modify(dept varchar2(10) constraint unique);

alter table student_personal add check(year>2017);

3.) Create another table student_more with columns as regno,interests


Ans->
create table student_more(regno number(10),
interests varchar2(10));

4.)Regno in student_more table should be foreign key referring to regno of


student_personal table
Ans->
alter table student_more add foreign key(regno) references student_personal(regno);

Comparison Operator---------->

create table employee2(


ename varchar2(10),
empid number(10),
salary number(10),
age number(10));

Comparison:----->
= -->Equal to
> --->Greater than
< --->Less than
>= ---->Greater than equal to
<= ---->Less than equal to
<> ----->Not equal to
Logical Operators:---->
BETWEEN ---->like (select empid from employee where salary between 10000 and
20000)
or(select empid from employee where salary >10000 and <20000)
OR ---->select ename,empid from employee where empid=123 or empid=146
select ename,empid from employee where ename like 'S%' OR
salary<20000
IN ---->select * from employee where empid IN(123,1234,1640)
NOT IN ---->select * from employee where salary NOT IN(15000)
ORDER --->select empid from employee ORDER BY salary DESC;
select empid from employee ORDER BY salary;//for ascending order

select AVG(salary)from employee2;


select SUM(salary)from employee2;
select MAX(salary)from employee2;
select MIN(salary)from employee2;

1.)Construct a table student having constraint primary key to regno.


Ans--->

create table student(


fname varchar2(10),
lname varchar2(10),
regno number(10) constraint a primary key,
year number(10),
branch varchar2(10),
loc varchar2(10));

insert into student values('Ram','Singh',1464,2016,'CSE','Jal')

insert into student values('Sita','Panth',1667,2017,'CSE','Lud')

insert into student values('Rahul','Oberoe',1686,2017,'Civil','Delhi')

insert into student values('Rohit','Sam',1727,2019,'MEC','Delhi')

insert into student values('Ishwar','Singh',1800,2014,'MEC','Lucknow')

2.)Add a column year with not null constraint to table.


Ans-->

alter table student modify(year number(10) constraint b not null)

3.)Delete from table where the lname of student ends with h.


Ans--->

delete from student where lname like '%h';

4.)Get fname and regno of student where year is greater than equal to 2017 and
location is delhi.
Ans--->
select fname,regno from student where year>=2017 and loc ='Delhi'

5.)Get fname,lname and branch of student where loc is either jal,lud,delhi,lucknow.


Ans--->

select fname,lname,branch from student where loc IN('Jal','lud','Delhi','Lucknow');

6.)Display data of table according to descending order of regno.


Ans-->

select * from student ORDER BY regno DESC;

7.)Delete all rows of table with structure remaining as it is.


Ans--->

delete * from student;

8.)Change value of location to USA where branch is CSE and regno less than 1700.
Ans--->

update student set loc='USA' where branch='CSE' and regno<1700;

9.)Change the location of the student to Agra where year is between 2014 to 2017.
Ans--->

update student set loc='Agra' where year between 2014 and 2017;

10.)Create a table student_personal


regno as foreign key referring to primary key of student table.

Ans--->create table student_personal(


name varchar2(10),
age number(2),
year number(4),
marks number(10),
cgpa number(4),
constraint fk FOREIGN KEY(regno)references student(regno));

11.)Inserting 4 rows in student_personal table.


Ans--->

insert into student_personal values('Ram',23,2017,98,9.8,231);

insert into student_personal values('Rima',19,2016,80,8,232);

insert into student_personal values('Shyam',22,2018,70,7,233);

insert into student_personal values('Sita',18,2019,76,7.6,234);

12.)Get regno of student with maximum CGPA.


Ans--->

select MAX(cgpa) from student_personal;

13.)Get the avg marks of student.


Ans--->

select AVG(cgpa)from student_personal;

14.)Get the details of student with semester marks less than 40 and cgpa less than
5.

Ans--->
select * from student where marks<40 and cgpa<5;

GROUPBY------------------>

create table employee5(ename varchar2(10),


empid number(10),
deptid number(10),
salary number(10));

insert into employee5 values('Avi',12,34,23456);


insert into employee5 values('Ram',34,67,90888);
insert into employee5 values('Sham',37,67,5000);

GROUP BY command------->Like here ,I need avg salary using deptid,so, the people
whose deptid is same,the avg of their salaries will be displayed
select deptid,avg(salary) from employee5 group by deptid;

Using group by along with the conditions and order :-------->


select deptid,empid,sum(salary) from employee5 where deptid>40 group
by deptid,empid order by deptid;

Use having not where after group id----------------->


select deptid, max(salary) from employee5 group by deptid having
max(salary)>10000;

dbms korth ppt


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

create table customer(


id number(10),
name varchar2(10),
age number(10),
address varchar2(10),
salary number(10));

insert into customer values(1,'Ramesh',32,'Ahmedabad',2000);

insert into customer values(2,'Khilan',25,'Delhi',2500);

insert into customer values(3,'Suraj',56,'Chandigarh',6000);

insert into customer values(4,'Nish',45,'Ludhiana',3000);

insert into customer values(5,'Anu',48,'Mumbai',12000);

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

create table order1(


oid number(10),
odate date,
customer_id number(10),
amount number(10));

insert into order1 values(105,'28-oct-2014',1,1500);

insert into order1 values(104,'26-mar-2015',3,11500);

insert into order1 values(108,'28-oct-2016',4,15500);

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

join using select statement----------->

select id,name,age,amount from customer,order1 where


customer.id=order1.customer_id; //selecting data from two table using id

Joints------------->A concept or command to retrieve the data from more than one
table on the basis of certain conditions

INNER JOIN----------->Gives common values between both the tables

select id,name,amount,odate from customer inner join order1 on


customer.id=order1.customer_id;

LEFT JOIN--------->First it will give the common values of both the tables along
with the remaining values of first table(customer table here)

select id,name,amount,odate from customer left join order1 on


customer.id=order1.customer_id;

RIGHT JOIN------->First it will give the common values of both the tables along
with the remaining values of second table(order1 table here)

select id,name,amount,odate from customer right join order1 on


customer.id=order1.customer_id;

FULL JOIN--------->First it will give the common values of both the tables along
with the remaining values of both tables

select id,name,amount,odate from customer full join order1 on


customer.id=order1.customer_id;

CROSS JOIN------->every column is multiplied by the every column of other table

select id,name,amount,odate from customer,order1;

OUTER JOIN------>

select id,name,amount,odate from customer outer join order1 on


customer.id=order1.customer_id;

-----------------------------------
create table student4(
regno number(10),
courseid number(10),
year number(10));

insert into student4 values(1,23,2017);

insert into student4 values(2,24,2016);

insert into student4 values(1,22,2018);

giving a temporary name to the table itself is known as alas name.

SELF JOIN------->when comparison is done using one table only(cross product+


condition)
select a.id,b.name,a.salary from customer a,customer b where
a.salary<b.salary;

Subquery---->Qery in other query(do the same fxn. as that of join)

create table employee6


(
fname varchar2(10),
lname varchar2(10),
salary number(10),
empid number(10),
jobid number(10),
deptname varchar2(10),
location varchar2(10));

insert into employee6 values('Ram','Sharma',23000,23,56,'cse','pnb');

insert into employee6 values('SRam','arma',26000,25,56,'cse','Delhi');

insert into employee6 values('Sham','rma',15000,28,58,'civil','Haryana');

insert into employee6 values('Naitik','lang',45000,29,59,'mec','Jalndhar');

Que--->Display salary of employee6 where salary greater than salary of Ram


Ans---->

select fname,lname,salary from employee6 where salary>(select salary from


employee6 where fname='SRam');
//2 queries in this statement.First the inner query which is in
brackets will be executed, then the outer one.

//it can also work for different table name as well

Que--->Display details of employee whose jobid is same as empid 25

Ans---->

select fname,lname,empid from employee6 where jobid=(select jobid from


employee where empid=25)

Limitations of subquery:----->It cannot return more than one rows and this
limitation is overcome by join
select fname,lname,salary from employee6 where salary=(select min(salary) from
employee6);//Group fxn. application on subquery

Que--->Find min salary corresponding to each jobid in employee6 table

Having --->

select jobid,min(salary) from employee6 group by jobid having


min(salary)>(select min(salary) from employee6 where jobid=56);

VIEW---->To view only some columns of the table

create or replace view v1 as select fname,salary,empid from employee6 where


jobid=56;//view will be created here

select * from v1;//can view the data

To update the value in view------>

Ans--->
update v1 set salary=25000 where empid=23;

select *from v1;


select * from employee6;//it will also get updated if we update the view

SET OPERATIONS----->

create table employee7(


empid number(10),
jobid number(10));

insert into employee7 values(22,56);

insert into employee7 values(48,58);

insert into employee7 values(25,46);

UNION----->
select empid from employee6 union select empid from employee7;//it will
not display common values twice

Union ALL-------->it will display common values as many times as it come in the
program

select empid from employee6 union all select empid from employee7;

INTERSECT------->select empid from employee6 intersect select empid from


employee7;//display only common values between the tables
MINUS------>(A-B)

select empid from employee6 minus select empid from employee7;

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

create table employee(empid number(10),


empname varchar2(10),
salary number(10),
dept varchar2(10));

insert into employee values(12,'Sam',12000,'CSE');

insert into employee values(14,'Ram',15000,'ECE');

insert into employee values(16,'Sham',1000,'CIV');

insert into employee values(15,'Lak',13000,'MECH');

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

DECLARE
v_empno employee.empid%type:=12;
//defining the type of the v_empno same as that of the type of empid in the table
employee
v_fname employee.empname%type;

BEGIN
select empname into v_fname from employee where empid=v_empno; //giving
a specific value to v-fname from table employee
dbms_output.put_line('The name is'||v_fname); ||
(concatenation) //to print

END;

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

%type----> related to only one column

%rowtype-----> related to all the columns of the table

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

DECLARE
v_empno employee.empid%type:= :employeeid; //Colon(:)
to get input from the user(in the box)
empRec employee%ROWTYPE;

BEGIN
select empid,empname,salary,dept INTO empRec;
from employee where empid=v_empno;
dbms_output.put_line(empRec.empid);
dbms_output.put_line(empRec.empname);
dbms_output.put_line(empRec.salary);
dbms_output.put_line(empRec.dept);

END;

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

// Working with insert statement

DECLARE

v_empno employee.empid%type:= :empid;


v_ename employee.empname%type:= :name;
v_salary employee.salary%type:= :salary;

BEGIN
insert into employee(empid,empname,salary)values(v_empno,v_ename,v_salary);

COMMIT; //save the values entered by the user in table


END;

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

//Update

DECLARE
v_empno employee.empid%type;
v_salary employee.salary%type:= :sal;

BEGIN
update employee set salary=v_salary where empid=12; //for updating value
in the table

COMMIT;
END;

-----------------------------------------------------------------------------------
---------------------------------------------------------------
//working with DELETE statement

DECLARE
v_empno employee.empid%type:= :empno;
BEGIN
DELETE employee where empid=v_empno;
COMMIT;
END;

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

//if-else statement

DECLARE
A number(10):=20;
B number(10):=30;
BEGIN
IF A>B
THEN DBMS_OUTPUT.PUT_LINE('NUMBER A GREATER THAN B');
ELSE
DBMS_OUTPUT.PUT_LINE('B GREATER);
END IF;
END;

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

//using IF statement in employee table

DECLARE
v_ename employee.empname%type;
v_empid employee.empid%type:= :empid;
v_sal employee.salary%type;
BEGIN
select empname,salary into v_ename,v_sal from employee where empid=v_empid;
IF v_sal>2500
THEN
dbms_output.put_line(v_ename || 'earns salary greater than 2500');
ELSE
dbms.output_put_line(v_ename || 'earns salary less than 2500');
END IF;
END;

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

CASE------->

DECLARE
GRADE CHAR(1):='A';
BEGIN
CASE GRADE
WHEN 'A' THEN DBMS_OUTPUT.PUT_LINE('EXCELLENT');
WHEN 'B' THEN DBMS_OUTPUT.PUT_LINE('VERY GOOD');
WHEN 'C' THEN DBMS_OUTPUT.PUT_LINE('WELL DONE');
WHEN 'D' THEN DBMS_OUTPUT.PUT_LINE('YOU PASSED');
WHEN 'F' THEN DBMS_OUTPUT.PUT_LINE('BETTER TRY AGAIN');
ELSE DBMS_OUTPUT.PUT_LINE('NO SUCH GRADE');
END CASE;
END;

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

CREATE A TABLE

CURSORS--------->

CURSORS
--USING SQL%ROWCOUNT-------->

DECLARE

A NUMBER(10);

BEGIN

DELETE FROM EMPLOYEE WHERE EMPID=23;


A:=SQL%ROWCOUNT;
DBMS_OUTPUT.PUT_LINE('TOTAL RECORDS DELETED' || A);
END;

--USING SQL%FOUND------>

--(COMMENTS IN DBMS-----------------------------> REPRESENTED BY(--)

DECLARE A NUMBER:=:ENTERNUMBER;
BEGIN

DELETE FROM EMPLOYEE WHERE EMPID='A';

IF(SQL%FOUND)
THEN DBMS_OUTPUT.PUT_LINE('DELETE SUCCESSFUL');
ELSE
DBMS_OUTPUT.PUT_LINE('DELETE NOT SUCCESSFUL AS NO DATA');
END IF;
END;

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

EXPLICIT CURSOR(Declare,open,fetch,close)-------> CURSOR cursor_name IS


select_statement
FETCHING FROM A CURSOR------> FETCH cursor_name INTO list_of_variables
or
FETCH cursor_name INTO record_type_variables

cursor_name%found and cursor_name%notfound

cursor_name%rowcount and cursor_name%columncount


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

DECLARE
CUSTOMER EMPCUR IS SELECT EMPID,EMPNAME FROM EMPLOYEE;
V_EMPID EMPLOYEE.EMPID%TYPE;
V_ENAME EMPLOYEE.EMPNAME%TYPE;
BEGIN
OPEN EMPCUR;

LOOP
FETCH EMPCUR INTO V_EMPID,V_ENAME;
EXIT WHEN EMPCUR%NOTFOUND;

DBMS_OUTPUT.PUT_LINE('EMPLOYEEID'|| V_EMPID || 'NAME IS' || V_ENAME);


END LOOP;

CLOSE EMPCUR;
END;

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

DECLARE
TYPE T_EMPREG IS RECORD
(
EMPNAME EMPLOYEE.ENAME%TYPE,
SALARY EMPLOYEE.SALARY%TYPE
);

R_EMPREC T_EMPREC;
CURSOR C1 IS SELECT EMPNAME,SALARY FROM EMPLOYEE;

BEGIN
OPEN C1;

LOOP

FETCH C1 INTO R_EMPREC;


EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(R_EMPREC.EMPNAME || R_EMPREC.SALARY);
END LOOP;
CLOSE C1;
END;

-----------------------------------------------------------------------------------
----------------------------------------
DECLARE
CURSOR C2 IS SELECT EMPID,EMPNAME FROM EMPLOYEE;
V_EMPID EMPLOYEE.EMPID%TYPE;
V_ENAME EMPLOYEE.EMPNAME%TYPE;

BEGIN
OPEN C2;
FETCH C2 INTO V_EMPID,V_ENAME;
WHILE C2%FOUND AND C2%ROWCOUNT <=5

LOOP
DBMS_OUTPUT.PUT_LINE('EMPLOYEE NAME IS'||V_ENAME||'EMPLOYEE ID IS'||V_EMPID);

FETCH C2 INTO V_EMPID,V_ENAME;

END LOOP;
CLOSE C2;
END;

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

CREATE TABLE DEPARTMENT(DEPTID NUMBER(10),


LOCATIONID NUMBER(10),
DEPTNAME VARCHAR2(10));

INSERT INTO DEPARTMENT VALUES(23,12,'DDJE')


INSERT INTO DEPARTMENT VALUES(32,13,'DDWE')

--CURSOR FOR LOOP

DECALRE
CURSOR C3 IS SELECT EMPID,EMPNAME,SALARY,DEPARTMENT.DEPTID FROM EMPLOYEE,DEPARTMENT
WHERE EMPLOYEE.EMPID=DEPARTMENT.DEPTID;

BEGIN

FOR V_REC IN C3

LOOP
DBMS_OUTPUT.PUT_LINE('NAME:' || V_REC.EMPNAME || 'EMPLOYEEID:' || V_REC.EMPID ||
'DEPARTMENTID:' || V_REC.DEPTID);
END LOOP;
END;

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

--EXCEPTION HANDLING---------------->

DECLARE
MYNAME VARCHAR2(10);
BEGIN
SELECT EMPNAME INTO MYNAME FROM EMPLOYEE WHERE EMPID=:EMPID;
DBMS_OUTPUT.PUT_LINE('NAME IS' || MYNAME);

EXCEPTION
WHEN NO_DATA_FOUND
THEN DBMS_OUTPUT.PUT_LINE('WRONG EMPLOYEE NUMBER');

WHEN VALUE_ERROR
THEN DBMS_OUTPUT.PUT_LINE('EMPLOYEE ID LARGER THAN LENGTH');
END;

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

DECLARE
V_LOCATION EMPLOYEE.LOCATION%TYPE;
EMP_REC EMPLOYEE%ROWTYPE;
BEGIN
V_LOCATION:= ':LOCATION';
SELECT * INTO EMP_REC FROM EMPLOYEE WHERE UPPER(LOCATION)=UPPER(V_LOCATION);
DBMS_OUTPUT.PUT_LINE(EMP_REC.EMPNAME || 'HAS SALARY' || EMP_REC.SALARY);
EXCEPTION
WHEN NO_DATA_FOUND
THEN DBMS_OUTPUT.PUT_LINE('NO LOCATION EXIST');
WHEN TOO_MANY_ROWS
THEN DBMS_OUTPUT.PUT_LINE('MORE THAN ONE EMPLOYEE');
WHEN OTHERS
THEN DBMS_OUTPUT.PUT_LINE('OTHER ERROR');
END;

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

CREATE TABLE CUSTOMERS(NAME VARCHAR2(10),


ID NUMBER(10),
ADDRESS VARCHAR2(20));

INSERT INTO CUSTOMERS VALUES('AVI',12,'LDH');


INSERT INTO CUSTOMERS VALUES('RAM',45,'DEL');

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

DECLARE
C_ID CUSTOMERS.ID%TYPE :=&CC_ID;
C_NAME CUSTOMERS.NAME%TYPE;
C_ADDR CUSTOMERS.ADDRESS%TYPE;
--USER DEFINED EXCEPTION
EX_INVALID_ID EXCEPTION;
BEGIN
IF C_ID<=0 THEN
RAISE EX_INVALID_ID;
ELSE
SELECT NAME,ADDRESS INTO C_NAME,C_ADDR FROM CUSTOMERS WHERE ID=C_ID;
DBMS_OUTPUT.PUT_LINE('NAME: '|| C_NAME);
DBMS_OUTPUT.PUT_LINE('ADDRESS: '|| C_ADDR);
END IF;

EXCEPTION

WHEN EX_INVALID_ID THEN


DBMS_OUTPUT.PUT_LINE('ID MUST BE GREATER THAN ZERO!');
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('NO SUCH CUSTOMER!');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERROR!');

END;

Das könnte Ihnen auch gefallen