Sunteți pe pagina 1din 9

create table SubjectMaster (SubCode Varchar(5) Primary Key, SubName Varchar(10) Unique Not Null, Credits Number(2) Not

Null Check(Credits>0 and Credits<=4)); desc SubjectMaster Create Table Exam(SubCode Varchar(5) references SubjectMaster(SubCode), ExamNo Number(2), ExamDate DATE Unique Not Null, TotalMarks Number(3) Default 100, Constraint PK1 Primary Key(SubCode, ExamNo) ); desc Exam select * from SubjectMaster select * from Exam Insert Into SubjectMaster Values('S1', 'RDBMS', 4) Insert Into SubjectMaster Values('S2', 'BPA', 3) Insert Into Exam Values('S1', '1', '04-13-12', 80) Insert Into Exam Columns(SubCode, ExamNo, ExamDate) Values('S2', '2', '04-15-12') Select SYSDATE from Dual

Alter Table Exam Add (MinPassingMarks Number(2) Check(MinPassingMarks>20 and MinPassingMarks<50)) Alter Table SubjectMaster Add(CollegeName Varchar(10) Check(CollegeName like Upper(CollegeName)), CollegeArea Varchar(10) Check(CollegeArea like InitCap(CollegeArea)), CollegeEmail Varchar(10) Check(CollegeEmail like '___@%')); desc SubjectMaster select * from user_constraints where table_name = 'SUBJECTMASTER' Insert Into SubjectMaster Values('S3', 'SPM', 4, 'SICSR', 'Pune', 'sic@r.com') Alter Table SubjectMaster RENAME COLUMN COLLEGENAME TO COLLNAME Alter Table SubjectMaster MODIFY COLLEGEAREA VARCHAR(20) Alter Table SubjectMaster MODIFY Credits Check(Credits>0 and Credits<=5) Alter Table SubjectMaster DROP COLUMN COLLNAME Alter Table EXAM DISABLE CONSTRAINT PK1

Alter Table EXAM ENABLE CONSTRAINT PK1 Alter Table test1 RENAME TO test3

Select * from exam Update Exam Set TotalMarks = 60 where Subcode = 'S1' Delete from Exam where TotalMarks = 60

create table test1 (PRNno Number(10) Constraint PK11 Primary Key) create table test2 (PRNno1 Number(10) Constraint C1 check(PRNno1>10000))

Create Sequence SeqTest1 Increment By 1 Start with 1 MaxValue 100 MinValue 1 NOCACHE ORDER; CREATE SEQUENCE supplier_seq MINVALUE 1 START WITH 1 INCREMENT BY 1 NOCACHE;

Insert Into SubjectMaster Values(SeqTest1.NextVal, 'JAVA', 2, 'Pune', 'sic@r.com') Insert Into SubjectMaster Values(SeqTest1.NextVal, 'SPM5', 3, 'Pune', 'sic@r.com') select * from SubjectMaster Insert Into test3 Values(SeqTest1.NextVal) Select * from test3 Alter Sequence SeqTest1 MaxValue 50 Increment by 5;

Drop Sequence SeqTest1

Create or replace Procedure UpdateSal(eno Emp.Empno%Type) as Salary Emp.Sal%Type; Nm Emp.Ename%Type; Begin Select Ename, Sal Into Nm, Salary From emp_083 Where empno = eno; dbms_output.put_line(Eno || ' ' || Nm || ' Old Salary = ' || Salary ); Salary := Salary * 1.10; Update Emp_083 Set Sal = Salary Where Empno = eno; dbms_output.put_line(Eno || ' ' || Nm || ' New Salary ' || Salary ); Exception When No_Data_Found then insert into err_tab values(eno, ' Employee Not Found ' );

End;

PROCEDURE Create or replace Procedure UpdateSal2 as Cursor C1 is Select Empno, Job, Sal from emp_083;

eno Emp.Empno%Type; jb Emp.Job%Type; Salary Emp.Sal%Type; Erec Emp%RowType; Begin Open C1; Loop Fetch C1 into eno, jb, Salary; exit when C1%NotFound; if( UPPER(jb) = 'MANAGER') THEN Salary := Salary * 1.11; Update Emp_083 Set Sal = Salary Where Job = 'MANAGER' and Empno = eno; ELSIF( UPPER(jb) = 'CLERK') THEN Salary := Salary * 1.10; Update Emp_083 Set Sal = Salary Where Job = 'CLERK' and Empno = eno; END IF; END LOOP; End;

CREATE OR REPLACE PROCEDURE EVENN (NO NUMBER ) AS I NUMBER(5); BEGIN FOR I IN 1 .. NO LOOP IF (MOD(I,2) = 0) THEN DBMS_OUTPUT.PUT_LINE(I); END IF; END LOOP; END;

First Three Employee that joined select * from (select * from emp order by Hiredate) where rownum <=3

Diffenece of max and min sal Select (Max(Sal) - Min(Sal)) as "Difference" from emp

Employee who are bosses of at least one employee select distinct a.ename from emp a, emp b where a.empno = b.mgr

Truncate year of work to zero decimal places Select Empno, Ename, Hiredate, Trunc((Sysdate-Hiredate)/365,0) as "No of Years Worked" from emp

Supervisor's empno and no of people working under him select a.empno, a.ename, count(b.ename) from emp a, emp b where a.empno = b.mgr group by a.empno, a.ename

min sal under manager and exclude sal less than 1000 select * from (Select a.empno as eno, a.ename as enm, min(b.sal) as minsal from emp a, emp b where a.empno = b.mgr group by a.empno, a.ename) where minsal>1000 order by minsal desc

a table becomes manager of b table

Select * from (Select dept.deptno, dept.dname, dept.loc, count(emp.empno) as cnt from emp right outer join dept on emp.deptno = dept.deptno group by dept.deptno, dept.dname, dept.loc) where cnt > 3 use innerjoin to remove 40

Select * from (Select D, N, L, No from (Select dept.deptno as D, dept.dname as N, dept.loc as L, count(emp.empno) as No from emp right outer join dept on emp.deptno = dept.deptno group by dept.deptno, dept.dname, dept.loc) order by No desc) where rownum = 1

Select * from (Select D, N, L, No from (Select dept.deptno as D, dept.dname as N, dept.loc as L, count(emp.empno) as No from emp right outer join dept on emp.deptno = dept.deptno group by dept.deptno, dept.dname, dept.loc) order by No desc) where rownum = 1

Select * from (Select dept.deptno as D, dept.dname as N, dept.loc as L, count(emp.empno) as No from emp right outer join dept on emp.deptno = dept.deptno group by dept.deptno, dept.dname, dept.loc order by No desc) where rownum = 1

select * from dept

select * from test2 insert into test2 values(5555555) select distinct * from test2

Select * from (Select row_number() over (order by sal) as NO, empno, ename, sal from emp) where NO =2

Select EmpNO, JOB, Case

When JOB= 'PRESIDENT' THEN 'A' When JOB='MANAGER' THEN 'B' When JOB='SALESMAN' THEN 'C' When JOB='CLERK' THEN 'D' When JOB='ANALYST' THEN 'E' ELSE '0' END AS GRADE FROM EMP

Select Empno, deptno from emp where empno not in (Select empno from emp where deptno = 20)

SELECT * FROM EMP

create table emp1 (SubCode Varchar(5), SubName Varchar(10), Credits Number(2) ); INSERT INTO emp1 VALUES('S1', 'BPA', 3) DELETE FROM Emp1 A WHERE a.rowid > ANY ( SELECT B.rowid FROM Emp1 B WHERE

A.Subcode = B.Subcode AND A.Subname = B.Subname AND A.credits = B.Credits );

S-ar putea să vă placă și