Beruflich Dokumente
Kultur Dokumente
desc department;
salary number(10));
alter table employee1 add (loc varchar2(10));//To enter a new column in the
existing table
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>>>>>
WHOLE PROGRAM>>>>>>>
Ans->
create table student_personal(
sname varchar2(10),
age number(10),
dept varchar2(10) ,
year number(4) ,
regno number(10) ;
Ans->
alter table student_personal add primary key(regno);
Comparison Operator---------->
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
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'
8.)Change value of location to USA where branch is CSE and regno less than 1700.
Ans--->
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;
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------------------>
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;
----------------------------
-------------
Joints------------->A concept or command to retrieve the data from more than one
table on the basis of certain conditions
LEFT JOIN--------->First it will give the common values of both the tables along
with the remaining values of first table(customer table here)
RIGHT JOIN------->First it will give the common values of both the tables along
with the remaining values of second table(order1 table here)
FULL JOIN--------->First it will give the common values of both the tables along
with the remaining values of both tables
OUTER JOIN------>
-----------------------------------
create table student4(
regno number(10),
courseid number(10),
year number(10));
Ans---->
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
Having --->
Ans--->
update v1 set salary=25000 where empid=23;
SET OPERATIONS----->
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;
-----------------------------------------------------------------------------------
----------------------------------
-----------------------------------------------------------------------------------
---------------------------
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;
-----------------------------------------------------------------------------------
-----------------------------------------------
-----------------------------------------------------------------------------------
-------------------------------------------------------------
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;
-----------------------------------------------------------------------------------
---------------------------------------------------------------
DECLARE
BEGIN
insert into employee(empid,empname,salary)values(v_empno,v_ename,v_salary);
-----------------------------------------------------------------------------------
--------------------------------------------------------------
//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;
-----------------------------------------------------------------------------------
--------------------------------------------------------------
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
--USING SQL%FOUND------>
DECLARE A NUMBER:=:ENTERNUMBER;
BEGIN
IF(SQL%FOUND)
THEN DBMS_OUTPUT.PUT_LINE('DELETE SUCCESSFUL');
ELSE
DBMS_OUTPUT.PUT_LINE('DELETE NOT SUCCESSFUL AS NO DATA');
END IF;
END;
-----------------------------------------------------------------------------------
------------------------------------------
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;
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
-----------------------------------------------------------------------------------
----------------------------------------
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);
END LOOP;
CLOSE C2;
END;
-----------------------------------------------------------------------------------
---------------------------------------
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;
-----------------------------------------------------------------------------------
-------------------------------------
------------
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
END;