Académique Documents
Professionnel Documents
Culture Documents
What is database?
A database can be defined as a collection of coherent meaningful data.
Features of SQL
1. SQL can be used by a range of users, including those with little or no
programming experience 2. It is a non procedural language 3. It reduces the amount of time required for creating and maintaining system 4. It is English like language Rules for SQL 1. SQL starts with a verb eg: select statement. This verb may have additional adjectives eg: from 2. Each verb is followed by number of clauses Eg: from, where, having 3. A space separate clauses. Eg: drop table emp 4. A : is used to end SQL statement 5. A identifier can contain up to 30 characters and must start with an alphabetic characters 6. Characters and date literals must be enclosed with in single quotes 7. Comments may be enclosed between /* and */ symbols and may be multilane. Single line comments may be prefixed with a _ symbol Component of SQL
1. DDL(Data Defination Language)
It is a set SQL commands used to create, modify and delete database structure but not data. These commands are normally not used by a general user, who should be accessing the database via an application. They are normally used by the DBA to a limited extent, a database designer or application developer 2. DML(Data Manipulation Language) It is the area of SQL that allows changing data with in the database
3. DCL(Data Control Language) It is the component of SQL statement that controls access to data and to that database. DCL statement are grouped with DML statements 4. DQL(Data Query Language) It is the component of SQL statement that allows getting data from the database and imposing ordering upon it. It includes the select statement. This command is the heart of SQL. It allows getting the data out of the database perform operation with it. When a select is fired against a table or tables the result is compiled into a further temporary table which is displayed or perhaps received by the program i.e. front end
Update- updates existing data with in a table Delete- deletes all records from a table Call- call a PL/SQL or java program Lock- table control concurrency
Save point- identity a point in a transaction to which you can later rollback Rollback- restore database to original since the last commit DQL: Data Query Language
select- retrieve data from the data base
Basic Data Types numeric(p,s): The maximum precision is 38 digits. Where p is precision, s is sale number(p,s) for example number(7,2) is a number that has 5 digits before the decimal and 2 digit after the decimal char(size): Upto 3276 bytes in PL/SQL. Upto 2000 bytes where size is the number of characters to store. Fixed length strings space padded varchar2(size): Upto 4000 bytes, where size is the number of characters to store variable length strings date: A date between Jan 1,4712BC and Dec 31,9999AD The create table Command: The create table command defines each column of the table uniquely. Each column has a minimum of three attributes. Each table column definition is a separated from the other by a comma. Finally, the SQL statement is terminated with a semicolon.
1. A name can have max upto 30 characters 2. Alphabet from A-Z, a-z and number from 0-9 allows 3. A name should begin with an alphabet 4. The use of the special character like _ is allowed and also recommended
5. SQL reserved words not allowed For eg: create, select and so on Syntax: create table<table name> (<column name1><data type>(<size>),<column name2><data type>(<size>));
Delete operations
Delete command deletes rows from the table that satisfies the condition provided by its where clause, and returns the number of records deleted. Delete in SQL is used to remove either All the rows from a table or A set of rows from a table
Renaming tables
Oracle allows renaming of tables. The rename operation is done automatically, which means that no other thread can access any of the tables while the rename process is running. Syntax: rename <tablename> to <newtablename>;
Truncating tables
Truncating table empties a table completely logically, this is equivalent to a delete statement that deletes all rows, but there are practical differences are under some circumstances. Truncate table differ from delete in the following ways Truncate operations drop and recreate the table, which is much faster than deleting rows one by one Truncate operations are not transaction safe The number of deleted rows is not returned. Syntax: truncate table<tablename>;
Destroying tables
If a table is dropped all records held within it are lost and cannot be recovered. Syntax: drop table<tablename>;
Data Constraints
To understand the concept of data constraints, several tables will be created and different types of constraints will be applied to table columns or the table itself.
10
Check constraints
Variation can be applied to a table column by using check constraints. Check constraints defined at column level Syntax: <columnname><datatype>(<size>) check (<logical expression>) At the table level Syntax: check(<logical expression>)..
12
13
DEPTNO DNAME LOC ---------- -------------- ------------10 ACCOUNTING DELHI 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON
D. Add a column called telephone of data type number & size=10 to the employee table.
SQL> alter table emp add telno number(10); Table altered. SQL> desc emp; Name Null? Type ----------------------------------------- -------- --------------EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(7,2) COMM NUMBER(7,2) DEPTNO NUMBER(2) TELNO NUMBER(10)
14
15
SQL> create table studentdetails(rollno number(10),name char(25),address varchar2(50),date_of_joining date); Table created. SQL> insert into studentdetails values(&rollno,'&name','&address','&date_of_joining'); Enter value for rollno: 0309801 Enter value for name: abc Enter value for address: h.no:87287,colony,hyd Enter value for date_of_joining: 10-jul-08 old 1: insert into studentdetails values(&rollno,'&name','&address','&date_of_joining') new 1: insert into studentdetails values(0309801,'abc','h.no:87287,colony,hyd','10-jul-08') 1 row created. SQL> ed Wrote file afiedt.buf 1* insert into studentdetails values(&rollno,'&name','&address','&date_of_joining') 2 / Enter value for rollno: 0309823 Enter value for name: xyz Enter value for address: h.no:7832,nagar,hyd Enter value for date_of_joining: 02-sep-08 old 1: insert into studentdetails values(&rollno,'&name','&address','&date_of_joining') new 1: insert into studentdetails values(0309823,'xyz','h.no:7832,nagar,hyd','02-sep-08') 1 row created. SQL> select * from studentdetails; ROLLNO NAME ADDRESS DATE_OF_J ---------- ------------------------- ------------------------- --------309801 abc h.no:87287,colony,hyd 10-JUL-08 309823 xyz h.no:7832,nagar,hyd 02-SEP-08
16
QUERIES
Q1. Describe the details of employee table.
SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7839 KING PRESIDENT 17-NOV-81 5000 10 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7900 JAMES CLERK 7698 03-DEC-81 950 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7369 SMITH CLERK 7902 17-DEC-80 800 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7788 SCOTT ANALYST 7566 09-DEC-82 3000 20 7876 ADAMS CLERK 7788 12-JAN-83 1100 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10
SQL> select * from dept; DEPTNO DNAME LOC ---------- -------------- ------------10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON
18
19
Q9. Display the name of all employees who are working under deptno 10.
SQL> select ename from emp where deptno=10; ENAME ---------KING CLARK MILLER
20
TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------EMPLOYE007 TABLE ENROLL TABLE ENROLL8033 TABLE ENROLL8080 TABLE EMPLOYEE007 TABLE ENROLL8012 TABLE DEPART8082 TABLE DEPT8033 TABLE EMP28 TABLE EMP8027 TABLE BIN$7lmfGzcKQgGyLNZo/xTsWg==$0 TABLE
Q13. Display employee numbers, ename, hire date, salary such that employee number appears first.
SQL> select empno,ename,hiredate,sal from emp; EMPNO ENAME HIREDATE ---------- ---------- --------- ---------7839 KING 17-NOV-81 7698 BLAKE 01-MAY-81 7782 CLARK 09-JUN-81 7566 JONES 02-APR-81 7654 MARTIN 28-SEP-81 7499 ALLEN 20-FEB-81 7844 TURNER 08-SEP-81 7900 JAMES 03-DEC-81 7521 WARD 22-FEB-81 7902 FORD 03-DEC-81 7369 SMITH 17-DEC-80 EMPNO ENAME HIREDATE ---------- ---------- --------- ---------SAL 5000 2850 2450 2975 1250 1600 1500 950 1250 3000 800 SAL
21
---------- ---------- ---------KING 5000 5400 BLAKE 2850 3250 CLARK 2450 2850 JONES 2975 3375 MARTIN 1250 1650 ALLEN 1600 2000 TURNER 1500 1900 JAMES 950 1350 WARD 1250 1650 FORD 3000 3400 SMITH 800 1200 ENAME SAL SAL+400 ---------- ---------- ---------SCOTT 3000 3400 ADAMS 1100 1500 MILLER 1300 1700
22
Q16. Display the name as last name, multiply the salary by 12 and display annual salary from emp table.
SQL> select ename as LASTNAME,sal,sal*12 as annualsalary from emp; LASTNAME SAL annualsalary ---------- ---------- ---------KING 5000 60000 BLAKE 2850 34200 CLARK 2450 29400 JONES 2975 35700 MARTIN 1250 15000 ALLEN 1600 19200 TURNER 1500 18000 JAMES 950 11400 WARD 1250 15000 FORD 3000 36000 SMITH 800 9600 LASTNAME SAL annualsalary ---------- ---------- ---------SCOTT 3000 36000 ADAMS 1100 13200 MILLER 1300 15600
23
Q18. Combine the lastname with job as employee details from emp table.
SQL>select ename||job as employeedetails from emp EMPLOYEEDETAILS ------------------KINGPRESIDENT BLAKEMANAGER CLARKMANAGER JONESMANAGER MARTINSALESMAN ALLENSALESMAN TURNERSALESMAN JAMESCLERK WARDSALESMAN FORDANALYST SMITHCLERK EMPLOYEEDETAILS ------------------SCOTTANALYST ADAMSCLERK MILLERCLERK
Q19. Display the lastname with job reported by 'is a' and name as employee details.
SQL>select ename||' is a '||job as employeedetails from emp;
EMPLOYEEDETAILS ------------------------KING is a PRESIDENT BLAKE is a MANAGER CLARK is a MANAGER JONES is a MANAGER MARTIN is a SALESMAN ALLEN is a SALESMAN TURNER is a SALESMAN JAMES is a CLERK WARD is a SALESMAN FORD is a ANALYST SMITH is a CLERK EMPLOYEEDETAILS ------------------------SCOTT is a ANALYST
24
Q21. Display ename from emp table whose department number is 30.
SQL> select ename,deptno from emp where deptno='30'; ename DEPTNO ---------- ---------BLAKE 30 MARTIN 30 ALLEN 30 TURNER 30 JAMES 30 WARD 30
25
Q24. Display LASTNAME and salary of employee earning more than 1000.
SQL> select ename as LASTNAME,sal from emp where sal>1000; LASTNAME SAL ---------- -------------------- ---------KING 5000 BLAKE 2850 CLARK 2450 JONES 2975 MARTIN 1250 ALLEN 1600 TURNER 1500 WARD 1250 FORD 3000 SCOTT 3000 ADAMS 1100 LASTNAME SAL ---------- ---------MILLER 1300
Q25. Display ename as lastname and deptno from emp where empno=7934.
SQL> select ename as LASTNAME,deptno from emp where empno=7934; LASTNAME DEPTNO ---------- ---------MILLER 10
Q26. Display LASTNAME,sal from emp whose sal is in between 2500 and 3500.
SQL> select LASTNAME,sal from emp where sal between 2500 and 3500; LASTNAME SAL ---------- ---------BLAKE 2850 JONES 2975 FORD 3000 SCOTT 3000
26
SQL> select empno,ename as lastname,job from emp where empno=any(7839,7698); EMPNO LASTNAME JOB ---------- ---------- --------7698 BLAKE MANAGER 7839 KING PRESIDENT
Q29. List the name and job of employee who does not report to manager.
SQL> select ename,job from emp where job! =any('CLERK','ANALYST','MANAGER','SALESMAN'); ename JOB ---------- --------KING PRESIDENT
27
Q32. Display the names of employee working in deptno 30 or 40 or 50 & working as a clerk.
SQL>select ename from emp where deptno=any(30,40,50) and job='CLERK'; ENAME ---------JAMES
Q33. Display the names of employee working in deptno 30 or 40 or50 0r working as a clerk.
SQL> select ename from emp where deptno=all(30,40,50) or job='CLERK'; ENAME ---------JAMES SMITH ADAMS MILLER
28
30 30 30 30 30
Q37. Display the employee no,employee name and salary in ascending order of salary.
SQL> select ename,empno,sal from emp order by sal; ENAME EMPNO SAL ---------- ---------- ---------SMITH 7369 800 JAMES 7900 950 ADAMS 7876 1100 WARD 7521 1250 MARTIN 7654 1250 MILLER 7934 1300 TURNER 7844 1500 ALLEN 7499 1600 CLARK 7782 2450 BLAKE 7698 2850 JONES 7566 2975 ENAME EMPNO SAL ---------- ---------- ---------FORD 7902 3000 SCOTT 7788 3000
29
KING
7839
5000
Q38. Display the name and hire date in ascending order of hire data.
SQL> select ename,hiredate from emp order by hiredate; ENAME HIREDATE ---------- --------SMITH 17-DEC-80 ALLEN 20-FEB-81 WARD 22-FEB-81 JONES 02-APR-81 BLAKE 01-MAY-81 CLARK 09-JUN-81 TURNER 08-SEP-81 MARTIN 28-SEP-81 KING 17-NOV-81 JAMES 03-DEC-81 FORD 03-DEC-81 ENAME HIREDATE ---------- --------MILLER 23-JAN-82 SCOTT 09-DEC-82 ADAMS 12-JAN-83
Q39. Display ename ,job ,startdate of employee who's hiredate in range of 20-feb and 1-mar-81.
SQL> select ename,sal,job,hiredate as startdate from emp where hiredate between '20-FEB-81' and '1-MAR-81'; ENAME SAL JOB startdate ---------- ---------- --------- --------ALLEN 1600 SALESMAN 20-FEB-81 WARD 1250 SALESMAN 22-FEB-81
Q40. Display ename & hiredate of every employee who was hired in 1982.
SQL> select ename,hiredate from emp where hiredate between '01-jan-82' and '31-dec-82'; ENAME HIREDATE ---------- --------SCOTT 09-DEC-82 MILLER 23-JAN-82
30
Q43. Display ename,deptno for all employee who have 'A' in their name.
SQL> select ename,deptno from emp where ename like '%%A%%'; ENAME DEPTNO ---------- ---------BLAKE 30 CLARK 10 MARTIN 30 ALLEN 30 JAMES 30 WARD 30 ADAMS 20
31
EMPNO ENAME MGR EMPNO ENAME ---------- ---------- ---------- ---------- ---------7876 ADAMS 7788 7788 SCOTT 7934 MILLER 7782 7782 CLARK
Q45. Display the name of employee who are working as salesman clerk & analyst.
SQL> select ename from emp where job in ('ANALYST','SALESMAN','CLERK'); ENAME ---------MARTIN ALLEN TURNER JAMES WARD FORD SMITH SCOTT ADAMS MILLER
32
14
Q47. Display total salary paid to all employees working in the company.
SQL> select sum(sal) from emp; SUM(SAL) ---------29025
33
34
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7839 KING PRESIDENT 17-NOV-81 5000 10 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10
Q55. Display empno ename depno salary sorted their output in order of name deptno salary.
SQL> select ename,empno,deptno from emp order by ename,deptno,sal;
ENAME EMPNO DEPTNO ---------- ---------- ---------ADAMS 7876 20 ALLEN 7499 30 BLAKE 7698 30 CLARK 7782 10 FORD 7902 20 JAMES 7900 30 JONES 7566 20 KING 7839 10 MARTIN 7654 30 MILLER 7934 10 SCOTT 7788 20 ENAME EMPNO DEPTNO ---------- ---------- ---------SMITH 7369 20 TURNER 7844 30 WARD 7521 30
35
Q56. Display ename along with their annual salary, highest salary should come first.
SQL> select ename,sal*12 as annualsalary from emp order by sal desc; ENAME ANNUALSALARY ---------- -----------KING 60000 SCOTT 36000 FORD 36000 JONES 35700 BLAKE 34200 CLARK 29400 ALLEN 19200 TURNER 18000 MILLER 15600 WARD 15000 MARTIN 15000 ENAME ANNUALSALARY ---------- -----------ADAMS 13200 JAMES 11400 SMITH 9600
Q58. Display the various jobs and total no of employees in each job.
SQL> select job,count(job) from emp group by job; JOB COUNT(JOB) --------- ---------SALESMAN 4 CLERK 4 PRESIDENT 1 MANAGER 3 ANALYST 2
36
Q60. Display each job along with min salary, paid in each job.
SQL> select job,min(sal) as minimumsalary from emp group by job; JOB MINIMUMSALARY --------- ------------SALESMAN 1250 CLERK 800 PRESIDENT 5000 MANAGER 2450 ANALYST 3000
Q61. Display the deptno,if morethan a 3 employees are working in each department.
SQL> select deptno from emp having count(deptno)>3 group by deptno; DEPTNO ---------30 20
Q62. Display the various jobs along with total salary for each job and group by total salary more than 4000.
SQL> select job,sum(sal) as totalsalary from emp having sum(sal)>4000 group by job; JOB TOTALSALARY --------- ----------SALESMAN 5600 CLERK 4150 PRESIDENT 5000
37
MANAGER ANALYST
8275 6000
38
39
Miller
LENGTH('IKRAM') ---------------5
Q70. Using appropriate function and extract the second character from string DCET.
SQL> select substr('DCET',2,2) from dual; SU -CE
40
TRANSL -----BLLENS
Q72. Display information from emp table whenever job as a manager is called it should be display as boss.
SQL> select replace(job,'MANAGER','BOSS') from emp; REPLACE(JOB,'MANAGER','BOSS') -----------------------------------PRESIDENT BOSS BOSS BOSS SALESMAN SALESMAN SALESMAN CLERK SALESMAN ANALYST CLERK REPLACE(JOB,'MANAGER','BOSS') -----------------------------------ANALYST CLERK CLERK
41
Q75. Find the day of the nearest Saturday form the current day.
SQL> select next_day(sysdate,'SATURDAY') from dual; NEXT_DAY( --------02-OCT-10
Q76. Display the date three months before the current day.
SQL> select add_months(sysdate,-3)from dual; ADD_MONTH --------21-JUN-10
Q77. Display those employ whose name contain not less than the four character.
SQL> select ename from emp where length(ename )>4; ENAME ---------BLAKE CLARK JONES MARTIN ALLEN TURNER JAMES SMITH SCOTT ADAMS MILLER
Q78. Display the department name where atleast two employees are working.
SQL> select ename,dept.deptno,dname from emp,dept where emp.deptno=dept.deptno and emp.deptno in(select deptno from emp group by deptno having count(deptno)>2); ENAME DEPTNO DNAME ---------- ---------- -------------WARD 30 SALES JAMES 30 SALES TURNER 30 SALES ALLEN 30 SALES MARTIN 30 SALES
42
ENAME DEPTNO DNAME ---------- ---------- -------------MILLER 10 ACCOUNTING CLARK 10 ACCOUNTING KING 10 ACCOUNTING
Q79. Display the names of those manager whose salary should be greater than the average salary of the company.
SQL> select ename,sal from emp where job='MANAGER' and sal>(select avg(sal) from emp); ENAME SAL ---------- ---------BLAKE 2850 CLARK 2450 JONES 2975
43
SQL> select ename from emp where sal=(select max(sal) from emp); ENAME ---------KING
Q82. Display the ename ,employee number ,job of all employees who is working as a clerk and earning highest salary among clerk.
SQL> select ename,empno,job from emp where job='CLERK' and sal=(select max(sal) from emp group by job having job='CLERK'); ENAME EMPNO JOB ---------- ---------- --------MILLER 7934 CLERK
Q83. Display the names of employee a who earns the salary more than the john and Scott.
SQL> select ename,sal from emp where sal>any(select sal from emp where ename='SCOTT' or ename='JONES'); ENAME SAL ---------- ---------KING 5000 SCOTT 3000 FORD 3000
Q84. Display the name who earn the highest salary in there respective dept.
SQL> select ename,sal,deptno from emp where sal in(select max(sal) from emp group by deptno) ENAME SAL DEPTNO ---------- ---------- ---------BLAKE 2850 30 SCOTT 3000 20 FORD 3000 20 KING 5000 10
Q85. Display the names of employees who earns highest salary in there respective jobs.
44
SQL> select ename,sal,deptno from emp where sal in(select max(sal) from emp group by job) ENAME SAL DEPTNO ---------- ---------- ---------ALLEN 1600 30 MILLER 1300 10 KING 5000 10 JONES 2975 20 SCOTT 3000 20 FORD 3000 20
Q89. list all the enames ,job and salary, dname in company except the clerk.
SQL> select emp.ename,emp.job,emp.sal,dept.dname from emp,dept where dept.deptno=emp.deptno and job! ='CLERK';
45
ENAME JOB SAL DNAME ---------- --------- ---------- -------------CLARK MANAGER 2450 ACCOUNTING KING PRESIDENT 5000 ACCOUNTING JONES MANAGER 2975 RESEARCH FORD ANALYST 3000 RESEARCH SCOTT ANALYST 3000 RESEARCH ALLEN SALESMAN 1600 SALES TURNER SALESMAN 1500 SALES MARTIN SALESMAN 1250 SALES BLAKE MANAGER 2850 SALES WARD SALESMAN 1250 SALES
Q93. Display those employees whose salary is more than 3000 after giving 20% rise.
SQL> select ename,sal+sal*0.2 from emp where sal+sal*0.2>3000;
46
ENAME SAL+SAL*0.2 ---------- ----------KING 6000 BLAKE 3420 JONES 3570 FORD 3600 SCOTT 3600
A) SQL> select to_char(to_date('jan 31 90','mon dd yy')) from dual; TO_CHAR(T --------31-JAN-90 B) SQL> select to_char(to_date('01-mar-90'),'dd/mm/yy') from dual; TO_CHAR( -------01/03/90 c) SQL> select to_char(to_date('30-october-1990'),'ddth monthyyyy') from dual; TO_CHAR(TO_DATE(' ----------------30th october 1990
Q96.Display all rows from emp table, that should wait after every screen full of information.
SQL> set pause on SQL> select * from emp;
47
EMPNO ENAME JOB HIREDATE SAL COMM DEPTNO ---------- ---------- --------- --------- ---------- ---------- ---------7839 KING PRESIDENT 17-NOV-81 5000 10 7698 BLAKE MANAGER 01-MAY-81 2850 30 7782 CLARK MANAGER 09-JUN-81 2450 10 7566 JONES MANAGER 02-APR-81 2975 20 7654 MARTIN SALESMAN 28-SEP-81 1250 1400 30 7499 ALLEN SALESMAN 20-FEB-81 1600 300 30 7844 TURNER SALESMAN 08-SEP-81 1500 0 30 7900 JAMES CLERK 03-DEC-81 950 30 7521 WARD SALESMAN 22-FEB-81 1250 500 30 7902 FORD ANALYST 03-DEC-81 3000 20 7369 SMITH CLERK 17-DEC-80 800 20 EMPNO ENAME JOB HIREDATE SAL COMM DEPTNO ---------- ---------- --------- --------- ---------- ---------- ---------7788 SCOTT ANALYST 09-DEC-82 3000 20 7876 ADAMS CLERK 12-JAN-83 1100 20 7934 MILLER CLERK 23-JAN-82 1300 10
Q98. Display ename,sal,hra,pf,da and total sal for each employee. the output should be in the order of total salary (hra=15% of sal,da=10% of sal,pf=5% of sal, total=sal+hra+da-pf.
SQL> select ename,sal,sal*0.15 as HRA,sal*0.05 as PF,sal*0.1 as DA,sal+ (sal*0.15)+(sal*0.05)+(sal*0. 1) as TOTAL from emp; ENAME SAL HRA PF ---------- ---------- ---------- ---------- ---------KING 5000 750 250 BLAKE 2850 427.5 142.5 CLARK 2450 367.5 122.5 JONES 2975 446.25 148.75 MARTIN 1250 187.5 62.5 ALLEN 1600 240 80 TURNER 1500 225 75 JAMES 950 142.5 47.5 WARD 1250 187.5 62.5 FORD 3000 450 150 SMITH 800 120 40 DA TOTAL ---------500 6500 285 3705 245 3185 297.5 3867.5 125 1625 160 2080 150 1950 95 1235 125 1625 300 3900 80 1040
48
ENAME SAL HRA PF DA TOTAL ---------- ---------- ---------- ---------- ---------- ---------SCOTT 3000 450 150 300 3900 ADAMS 1100 165 55 110 1430 MILLER 1300 195 65 130 1690
*Create the table as client master with fields Columnname datatype size default attribute Clientno varchar2 6 primary key/ First letter must start with c not
49
SQL> create table client_mast89(clientno varchar2(6) primary key check(clientno like c%), 2 name varchar2(20) NOT NULL, 3 address varchar2(30), 4 city varchar2(15)); Table created.
*Create the table with product master Column datatype size default Productno varchar2 6 description profit_percent sellprice costprice varchar2 number number number 15 4,2 8,2 8,2
attribute primary key/ first letter must start with p NOTNULL NOTNULL NOTNULL NOTNULL
SQL> create table product_mast89(productno varchar2(6) primary key check(productno like p%), 2 description varchar2(15) NOT NULL, 3 profit_percent number(4,2) NOT NULL, 4 sellprice number(8,2) NOT NULL check(sellprice<>0), 5 costprice number(8,2) NOT NULL check(costprice<>0)); Table created.
attribute
Orderno varchar2 Clientno varchar2 Orderdate date Delyaddr varchar2 Delydate date Order status varchar2
6 6 6 6 6 10
primary key/first letter start with 0 foreign key client no. of client _mast NOT NULL values(in proces ,fullfilled,back Order,cancel)
SQL> create table sales_order34(orderno varchar2(6) primary key check(orderno like o%), clientno varchar2(6), orderdate date NOT NULL, delydate date, delyaddr varchar2(20), orderstatus varchar2(10) check(orderstatus in (inprocess,fullfilled,backorder,cancelled)),foreign key(clientno) references client_mast89(clientno));
PL/SQL PROGRAMS
Program to find even or odd
SQL> set serveroutput on 1 declare 2 num number; 3 begin 4 num:=# 5 if(mod(num,2)=0) then 6 dbms_output.put_line('given num is even no'); 7 else 8 dbms_output.put_line('given num is odd no'); 9 end if; 10* end;
51
11 / Enter value for num: 8 old 4: num:=# new 4: num:=8; given num is even no PL/SQL procedure successfully completed.
53
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
i number(2); begin n:=&n; i:=n/2; loop if(mod(n,i)=0) then dbms_output.put_line('given num is not prime'); exit; else dbms_output.put_line('given num is prime'); exit; end if; i:=i-1; exit when i=2; end loop; end; /
Enter value for n: 5 old 5: n:=&n; new 5: n:=5; PL/SQL procedure successfully completed.
54
old 6: n:=&n; new 6: n:=5; 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 5*10=50 PL/SQL procedure successfully completed.
55
FUNCTIONS
Program to convert given currency into dollar
SQL> create or replace function doll(d number) return number is 2 begin 3 return(d*39.91); 4 end; 5 / Function created. SQL> select doll(12) from dual; DOLL(12) ---------478.92
56
1 create or replace function pri(n number) return varchar2 is 2 b varchar2(20):='prime'; 3 begin 4 for i in 2..n-1 loop 5 if(mod(n,i)=0) then 6 b:='not prime'; 7 end if; 8 end loop; 9 return(b); 10* end; 11 / Function created. SQL> select pri(5) from dual; PRI(5) -------------------------------------------------------------------------------prime
57
PROCEDURES
Program to display salary of an employee
SQL> set serveroutput on SQL> create or replace procedure dispasal(name emp.ename %type)is vsal emp.sal%type; 2 begin 3 select sal into vsal from emp where ename=name; 4 dbms_output.put_line('the sal of emp is:'||vsal); 5 end; 6 / Procedure created. SQL> execute dispasal('KING'); the sal of emp is:5000 PL/SQL procedure successfully completed.
58
sal of emp updated is :10000 PL/SQL procedure successfully completed. SQL> select ename,sal from emp; ENAME SAL ---------- ---------SMITH 800 ALLEN 1600 WARD 1250 JONES 2975 MARTIN 1250 BLAKE 2850 CLARK 2450 SCOTT 3000 KING 10000 TURNER 1500 ADAMS 1100 ENAME SAL ---------- ---------JAMES 950 FORD 3000 MILLER 1300
59
TRIGGER PROGRAMS
Create a trigger to implement the condition that the number of records should not cross 5
SQL> create table std0308(sid numeric(20),sname char(20),branch char(20)); Table created. SQL> create or replace trigger cse 2 after insert 3 on std0308 4 declare 5 c integer; 6 begin 7 select count(*) into c 8 from std0308; 9 if(c>5) 10 then raise_application_error(-20501,'cant cross 5 records'); 11 end if; 12 end; 13 / Trigger created.
ERROR at line 1: ORA-20501:cant cross 5 records ORA-06512:at SCOTT.CSE,line 7 ORA-04088: error during execution of trigger SCOTT.CSE
Create a trigger to implement the condition that only three students can be admitted in cse branch
SQL> create or replace trigger cse 2 after insert 3 on std0308 4 declare 5 c integer; 6 begin 7 select count(*) into c 8 from std0308; 9 if(c>3) 10 then raise_application_error(-20501,'cant cross more records'); 11 end if; 12 end; 13 / Trigger created.
SQL> insert into std0308 2 values(9,cxf,cse); Insert into std0308 * ERROR at line 1: ORA-20501:cant cross more records ORA-06512:at SCOTT.CSE,line 7 ORA-04088: error during execution of trigger SCOTT.CSE
61
Create a trigger to implement the condition that total salary of all employee should not cross 80000
SQL> create or replace trigger r11 2 after insert 3 on emp5030 4 declare 5 c integer; 6 begin 7 select sum(sal) into c 8 from emp5030; 9 if(c>80000) 10 then raise_application_error(-20501,'cant cross 80000'); 11 end if; 12 end; 13 / Trigger created.
SQL> insert into emp5030 2 values(50000); Insert into emp5030 * ERROR at line 1: ORA-20506:cant cross 80000 ORA-06512: at SCOTT.R11,line 7 ORA-04088: error during execution of trigger SCOTT.R11
FORMS
To Log on to Oracle 10g Forms/Reports, Go to Start Programs Oracle 10g Developer Suite Forms Developer, Click Start OC4J Instance(Very Important you must keep OC4J running while using
62
Oracle 10g Forms/Reports!), After OC4J Initialized, Then Click Forms Builder.
Go to tools and Click on the Data Block Wizard, Now You will see the Welcome to Data Block Wizard Window.
63
Click Next to Proceed, You will see the window for the Data Block Wizard. Select table or View option and Click next. Now you will see a window that prompts you to select table or view. Click on Browse button, The Connect Window will appear. Type Password and Database to Connect to the Database.
Now You will see list of tables Created on Database, Select a table, select all the columns, by clicking on button >> .Then Click Next.
64
Now a window appears that prompts you to Enter a name for your Data Block. Type table name selected Previously if the block is empty and Click Next.
You Will now see the Congratulation window, make sure that Create the Data Block, then call the Layout Wizard is selected and Click Next.
65
Now you will see welcome to layout wizard window, Click Next.
66
Now you will see a window as shown below, make sure the Data Block Contains the Name of selected table, Then click the double right arrow, all the column will be selected. Then click next.
Now you will see a window that prompt you to set height and width. Click next to accept the default values.
67
Now you will see a Layout Wizard that prompts you to select a title for the form that you are creating. Click next after typing as student form.
Congratulation! You have now successfully created your form. Click Finish to View the form.
68
Now you will see the layout of form. Then go to programs and Click run form.
Student Form
69
Library Form
70
REPORTS
71
To Log on to Oracle 10g Forms/Reports, Go to Start Programs Oracle 10g Developer Suite Forms Developer, Click Start OC4J Instance(Very Important you must keep OC4J running while using Oracle 10g Forms/Reports!), After OC4J Initialized, Then Click Report Builder. Now Welcome to Report Builder Wizard will appear Click Ok.
72
Now a window appears as below, select Create both Web and paper Layout. Then Click Next.
Now a wizard appears that prompts to select the type of view. Select Tabular and Click next.
73
74
Now Type the Query and Click connect and Provide login, password. Then Click Query Builder.
75
Now select all the column of the table by clicking on the double right arrow. Then Click next.
Again Click next. Now a wizard appears that prompts to select width. Click next to select the default values.
76
Now you will see a wizard that prompts to select the colour of the layout. Select a colour and Click next.
77
Now a wizard appears as shown below. To view complete layouts go to programs, and click run web layout
78
79
Student Report
Library Report
80
81