Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CONTENTS
ASSIGNMENT 1 Create Command Describe Command Insert Command One Record at a time Multiple Records at a time Use of Tab Use of Set Linesize ASSIGNMENT 2 Rename Command Distinct Command Where Clause Order By Clause ASSIGNMENT 3 Between Operator Like Operator Delete Command Truncate Command Drop Command ASSIGNMENT 4 Alter Command Adding a Column Modifying a Column Dropping a Column Update Command Operators Arithmetic Relational Logical ASSIGNMENT 5 IN & NOT IN Operator Use of Dual & Sysdate Group/Aggregate Functions Sum, Avg, Count, Max, Min
PG. NO.
SIGNATURE
ASSIGNMENT 6 Scalar/Single Row Functions Numeric Fun Abs, Power, Mod Greatest, Least Floor, Ceil Sqrt, Round, Truncate Extract, Exponent String Fun Lower, Upper, Initcap Length, Lapd, Rpad Trim, Ltrim, Rtrim Substr, Instr, Ascii Truncate
ASSIGNMENT 7 Conversion Fun To_Number To_Char To_Date Date Fun Add_Months, Months_Between Last_Day, Next_Day ASSIGNMENT 8 DCL Commands Commit Rollback Savepoint
ASSIGNMENT 9 Data Constraints Primary Key Foreign Key Unique Key Business Constraints Check Default Not Null
Describe Command
SQL> desc employee1 Name Null? Type ----------------------------------------- -------- ---------------------------NAME VARCHAR2(15) FATHERNAME VARCHAR2(15) ADDRESS VARCHAR2(30) SAL NUMBER(10) PHONENO NUMBER(10) D_O_B DATE JOB VARCHAR2(15)
Use of insert command Inserting one record at a time:SQL> desc student1; Name Null? Type ----------------------------------------- -------- --------------------ST_ID NUMBER(4) ST_NAME VARCHAR2(10) ADDRESS VARCHAR2(10) CONTACT_NO NUMBER(10) SQL> insert into student1 values(12,'vinit','surajkund',999999999); 1 row created.
Inserting multiple values at a time:Output:SQL> insert into employee1(name,fathername,address,sal,phoneno,d_o_b,job) values('&name','&fathernam e','&address',&sal,&phoneno,'&d_o_b','&job'); Enter value for name: ashish Enter value for fathername: vinit Enter value for address: nit Enter value for sal: 10000 Enter value for phoneno: 9711111111 Enter value for d_o_b: 30-jun-89 Enter value for job: clerk old 1: insert into employee1(name,fathername,address,sal,phoneno,d_o_b,job) values('&name','&fathe
new 1: insert into employee1(name,fathername,address,sal,phoneno,d_o_b,job) values('ashish','vinit 1 row created. SQL> / Enter value for name: monu Enter value for fathername: vijay Enter value for address: delhi Enter value for sal: 20000 Enter value for phoneno: 9210045823 Enter value for d_o_b: 17-jul-90 Enter value for job: manager old 1: insert into employee1(name,fathername,address,sal,phoneno,d_o_b,job) values('&name','&fathe new 1: insert into employee1(name,fathername,address,sal,phoneno,d_o_b,job) values('monu','vijay', 1 row created. SQL> / Enter value for name: vinit Enter value for fathername: shyamlal Enter value for address: surajkund Enter value for sal: 30000 Enter value for phoneno: 9786997865 Enter value for d_o_b: 30-jun-90 Enter value for job: manager old 1: insert into employee1(name,fathername,address,sal,phoneno,d_o_b,job) values('&name','&fathe new 1: insert into employee1(name,fathername,address,sal,phoneno,d_o_b,job) values('vinit','shyaml 1 row created.
SQL> select * from employee1; NAME FATHERNAME ADDRESS --------------- --------------- ------------------------------ ---------PHONENO D_O_B JOB ---------- --------- --------------ashish vinit nit 10000 9711111111 30-JUN-89 clerk monu vijay delhi 9210045823 17-JUL-90 manager vinit shyamlal surajkund 9786997865 30-JUN-90 manager 20000 30000 SAL
Use of tab
To show all the existing tables in the sql :Output:SQL> select * from tab; TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------STUDENT1 TABLE STUDENT2 TABLE EMPLOYEE2 TABLE STUDENT5 TABLE CUSTOMER4 TABLE EMPLOYEE TABLE STUDENT6 TABLE BIN TABLE DEPT TABLE EMP TABLE
BONUS
TABLE
TNAME TABTYPE CLUSTERID ------------------------------ ------- ---------SALGRADE TABLE NV TABLE BIN$swhHVDtZRD+H5AwM2Q2nuA==$0 TABLE STUDENT TABLE
ASSIGNMENT - 2
nit delhi
SQL> select distinct address from employee1; ADDRESS -----------------------------surajkund nit delhi
delhi surajkund
ASSIGNMENT 4 Use of update command:Updating the job and hiredate of employee where empno =7499 . Output : SQL> update emp set job ='clerk' , hiredate ='20-mar-81' where empno =7499; 1 row updated. SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN clerk 7698 20-MAR-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 14 rows selected.
Use of logical operator:Select the employee where job =clerk and hiredate =20-mar-81. Output: SQL> select * from emp where job ='clerk' and hiredate ='20-mar-81'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7499 ALLEN clerk 7698 20-MAR-81 1600 300 30 Select the employee where job is not manager Output: SQL> select * from emp where not job='MANAGER'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7369 SMITH CLERK 7902 17-DEC-80 1200 20 7499 ALLEN clerk 7698 20-MAR-81 2400 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1875 500 30
7654 MARTIN 30 7788 SCOTT 20 7839 KING 10 7844 TURNER 30 7876 ADAMS 20 7900 JAMES 30 7902 FORD 20 7934 MILLER 10
1875 4500
1400
7698 08-SEP-81 7788 23-MAY-87 7698 03-DEC-81 7566 03-DEC-81 7782 23-JAN-82
7654 MARTIN 30 7698 BLAKE 30 7782 CLARK 10 7788 SCOTT 20 7839 KING 10 7844 TURNER 30 7876 ADAMS 20
1400
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7900 JAMES CLERK 7698 03-DEC-81 1425 30 7902 FORD ANALYST 7566 03-DEC-81 4500 20 7934 MILLER CLERK 7782 23-JAN-82 1950 10 14 rows selected..8
Use of relational operator:Select the employee whose salary is greater than 2000. Output :
SQL> select * from emp where sal>2000; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7499 ALLEN clerk 7698 20-MAR-81 2400 300 30
7566 JONES 20 7698 BLAKE 30 7782 CLARK 10 7788 SCOTT 20 7839 KING 10 7844 TURNER 30 7902 FORD 20 8 rows selected.
7369 SMITH 20 7499 ALLEN 30 7521 WARD 30 7566 JONES 20 7654 MARTIN 30 7698 BLAKE 30 7782 CLARK 10 7788 SCOTT 20 7839 KING 10 7844 TURNER 30 7876 ADAMS 20
CLERK clerk
1200 2400 300 1875 4462.5 1875 4275 3675 4500 7500 2250 1650 0 1400 500
7698 22-FEB-81 7839 02-APR-81 7698 28-SEP-81 7839 01-MAY-81 7839 09-JUN-81 7566 19-APR-87 17-NOV-81
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------7900 JAMES CLERK 7698 03-DEC-81 1425 30 7902 FORD ANALYST 7566 03-DEC-81 4500 20 7934 MILLER CLERK 7782 23-JAN-82 1950 10 14 rows selected.
ASSIGNMENT 4
To delete an existing column Deleting a column age from the table:SQL> alter table student2 2 drop column age; Table altered. SQL> select * from student2; Output RNO NAME ADDRESS DOB LOCATION ---------- ---------- ---------- --------- ---------2 ajay 3-h-124 09-JUN-85
----------------------------------------- -------- ---------------------------RNO NAME ADDRESS DOB LOCATION SQL> alter table student2 2 modify(name varchar(22)); Table altered. SQL> desc student2; Output Name Null? Type NUMBER(4) VARCHAR2(10) VARCHAR2(10) DATE VARCHAR2(10)
----------------------------------------- -------- ---------------------------RNO NAME ADDRESS DOB LOCATION NUMBER(4) VARCHAR2(22) VARCHAR2(10) DATE VARCHAR2(10)
ASSIGNMENT 5
Not in operator:
Output:
SQL> select * from emp where ename not in('ALLEN','WARD','JONES','KING'); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO AGE ---------- ---------- --------- ---------- --------- ---------- ---------- ------------------7369 SMITH CLERK 7902 17-DEC-80 800 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 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20
*Greatest function: SQL> select greatest (3,7,5) from dual; Output: GREATEST(3,7,5) --------------7
*Least function: SQL> select least (5,1,9) from dual; Output: LEAST(5,1,9) -----------1 *Floor function: SQL> select floor(32.6) from dual; Output: FLOOR(32.6) ----------32
*Ceil function: SQL> select ceil(34.6) from dual; Output: CEIL(34.6) ---------35 *Round function: SQL> select round(25.567) from dual; Output: ROUND(25.567) ------------26
*Squareroot function: SQL> select sqrt(25) "sqare" from dual; Output: sqare ---------5 *Extract function: SQL> select extract(month from date '2009-07-06') "month" from dual;
*Truncate function: SQL> select trunc(24.67) "truncate" from dual; Output: truncate ---------24 *Exponent function: SQL> select exp(5) "exponent" from dual; Output: exponent ---------148.413159
String functions
Showing the various string functions which are as follows:
*Lower function:
SQL> select lower('COMPUTER') "COMPUTER" FROM DUAL; Output: COMPUTER -------Computer
*Upper function:
SQL> select upper('computer') "computer" from dual; Output: computer -------COMPUTER
*Inticap function:
SQL> select initcap ('vinit chauhan') "vinit chauhan" from dual; Output: vinit chauhan ------------Vinit Chauhan
*Length function:
SQL> select length ('character') from dual; Output: LENGTH('CHARACTER') ------------------9
*Ltrim function:
SQL> select ltrim('xcomputer','x') "xcomputer" from dual; Output: xcompute -------Computer
*Rtrim function:
SQL> select rtrim('characterx','x') "characterx" from dual; Output: character --------Character
*Trim function:
In trim function the trim is done through three different methods like leading,trailing &both: Trim leading:
SQL> select trim(leading'x' from 'xcomputer') "xcomputer" from dual; Output: xcompute -------Computer
Trim trailing:
SQL> select trim(trailing'x' from 'computerx') "computerx" from dual; Output: computer -------Computer
Trim both:
SQL> select trim(both'x' from 'xcomputerx') "xcomputerx" from dual; Output: xcompute -------Computer
*Substring function:
SQL> select substr('vinit',1,(3)) "vinit" from dual; Output: vin --Vin
*Instring function:
SQL> select instr('vinit','n',(2),(1)) "vinit" from dual; Output: vinit ---------3
*Lpad function:
SQL> select lpad('computer',10,('x')) "computer" from dual; Output: computer ---------Xxcomputer
*Rpad function:
SQL> select rpad('computer',10,('x')) "computer" from dual; Output: computer ---------Computerxx
*Translate function:
SQL> select translate('rihit','ri','vi') "rihit" from dual; Output: rihit ----Vihit
*Ascii function:
SQL> select ascii('a') from dual; Output: ASCII('A') ---------97
ASSIGNMENT 7
Conversion functions
Showing the various conversion functions:
*To_number function
Output: SQL> select to_number('25') "number" from dual; number ---------25
*To_char function
For convert data type
SQL> select to_char(245600,('$099,999')) "datatype" from dual; Output: datatype --------$245,600
*To_char function:
For sysdate
SQL> select to_char(sysdate,('month-dd-yyyy')) "sysdate" from dual;
Output:
sysdate ----------------october -27-2009
*To_date function:
For date format
SQL> select to_date('27-10-2009','dd-mm-yyyy') "date" from dual; Output: date --------27-OCT-09
Date functions
Showing the various date functions: *Add_month function:
SQL> select add_months('26-oct-2009',2) "date" from dual;
Output:
date --------26-DEC-09
*Last_day function:
SQL> select last_day('26-oct-2009') "date" from dual;
Output:
date --------31-OCT-09
Output:
date --------02-NOV-09
*Months_between function:
SQL> select months_between('27-oct-2009','27-aug-2009') "months" from dual;
Output:
months ---------2
Rollback command:
Rollback command undoes any changes made during the transaction. SQL> update emp set ename ='ashish' where empno=7902; 1 row updated. SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------DEPTNO AGE ---------- ---------7902 ashish ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 2 rows selected. SQL> rollback; Rollback complete.
Output:
SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO AGE ---------- ---------- --------- ---------- --------- ---------- ---------- ------------------7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 2 rows selected.
Output:
EMPNO ENAME JOB MGR HIREDATE SAL COMM ---------- ---------- --------- ---------- --------- ---------- ---------DEPTNO AGE ---------- ----------
7876 ADAMS CLERK 20 7900 JAMES CLERK 30 7902 hello ANALYST 20 7934 hello CLERK 10 4 rows selected.
1300
ASSIGNMENT 8
CONSTRAINTS
CONSTRAINTS are the rules which are enforced on data being stored in a table. TYPES OF CONSTRAINTS:
Data Constraints
PRIMARY KEY NOT NULL UNIQUE
Business Constraints
CHECK REFERENCES DEFAULT
1. Record should be unique. 2. It will not allow duplicate values, 3. It does not support null values.
To create teacher table SQL> create table teacher2(tno number(5) primary key, tname varchar2(10), t_add number(5));
Table created.
SQL> insert into teacher2(tno, tname, t_add) values(1006, 'rohit', 345); 1 row created.
SQL> create table stud_acadmic5(id number(5) primary key, name varchar2(10)); Table created. SQL> insert into stud_acadmic5 values(&id, '&name'); Enter value for id: 10001 Enter value for name: rahul old 1: insert into stud_acadmic5 values(&id, '&name') new 1: insert into stud_acadmic5 values(10001, 'rahul') 1 row created.
SQL> create table stud_personal8(id number(5) references stud_acadmic5(id)on delete cascade,c_no nu mber(10)); Table created.
SQL> insert into stud_personal8 values(&id,&c_no); Enter value for id: 10001 Enter value for c_no: 987654321 old 1: insert into stud_personal8 values(&id,&c_no) new 1: insert into stud_personal8 values(10001,987654321) 1 row created.
SQL> create table student1(name varchar(11), age number(2), address varchar(4)unique); Table created.
SQL> / Enter value for name: mohit Enter value for age: 20 Enter value for address: 56/7 old 1: insert into student1 values('&name', &age, '&address') new 1: insert into student1 values('mohit', 20, '56/7') 1 row created. SQL> / Enter value for name: rahul Enter value for age: 22 Enter value for address: 56/7 old 1: insert into student1 values('&name', &age, '&address') new 1: insert into student1 values('rahul', 22, '56/7') insert into student1 values('rahul', 22, '56/7') *ERROR at line 1: ORA-00001: unique constraint (SCOTT.SYS_C005470) violated
BUSINESS CONSTRAINTS
NOT NULL CONSTRAINT IT ensures that table column cannot be left empty. IT means that column becomes a mandatory column. FEATURES OF NOT NULL CONSTRAINTS: 1. 2. Repetition can be done. Column cannot be kept empty.
SQL> create table stud(rno number(5) not null, name varchar2(10), address varchar2(10)); Table created. SQL> insert into stud values(&rno, '&name', '&address'); Enter value for rno: 1001 Enter value for name: raj Enter value for address: 543/9 old 1: insert into stud values(&rno, '&name', '&address') new 1: insert into stud values(1001, 'raj', '543/9') 1 row created. SQL> / Enter value for rno: 1002 Enter value for name: mohit Enter value for address: 432/8 old 1: insert into stud values(&rno, '&name', '&address') new 1: insert into stud values(1002, 'mohit', '432/8') 1 row created.
SQL> insert into stud(name, address) values('sakshi', '675/8'); insert into stud(name, address) values('sakshi', '675/8') * ERROR at line 1: ORA-01400: cannot insert NULL into ("SCOTT"."STUD"."RNO") SQL> select * from stud; RNO NAME ADDRESS ---------- ---------- ---------1001 raj 543/9 1002 mohit 432/8
CHECK CONSTRAINT
It checks the logical expression, i.e. true or false. SQL> create table stud2 (id number(5), name varchar2(10) check(name = upper(name)) , fees number (7,2), gender char(1) check(gender in (M,F) ); Table created.
DEFAULT CONSTRAINT
At the time of table creation default value can be assign to a column when a record is loaded into a table & column is left empty, the oracle engine will default value specified. Data type of default value should match data type of column. SQL> create table stud2 (id number(5), name varchar2(10) , fees number (7,2) default 2000); Table created. SQL> insert into stud2 values(&rno,'&name', &fees); Enter value for rno: 1001 Enter value for name: ankkur Enter value for fees: 5000 old 1: insert into stud2 values(&rno,'&name', &fees) new 1: insert into stud2 values(1001,'ankkur', 5000) 1 row created. SQL> insert into stud2(rno, name) values(1002, 'naveen'); 1 row created.
SQL> select * from stud2; RNO NAME FEES ---------- -------- ---------1001 ankkur 5000 1002 naveen 1000