sql file

66

Transcript of sql file

Page 1: sql file
Page 2: sql file

S. NO. CONTENTS PG. NO.

SIGNATURE

1 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

2 ASSIGNMENT – 2 Rename Command Distinct Command Where Clause Order By Clause

3 ASSIGNMENT – 3 Between Operator Like Operator Delete Command Truncate Command Drop Command

4 ASSIGNMENT – 4 Alter Command

Adding a Column Modifying a Column Dropping a Column

Update Command Operators

Arithmetic Relational Logical

5 ASSIGNMENT – 5 IN & NOT IN Operator Use of Dual & Sysdate Group/Aggregate Functions

Sum, Avg, Count, Max, Min

6 ASSIGNMENT – 6

Page 3: sql file

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

7 ASSIGNMENT – 7 Conversion Fun

To_Number To_Char To_Date

Date Fun Add_Months,

Months_Between Last_Day, Next_Day

8 ASSIGNMENT – 8 DCL Commands

Commit Rollback Savepoint

9 ASSIGNMENT – 9 Data Constraints

Primary Key Foreign Key Unique Key

Business Constraints Check Default Not Null

Page 4: sql file

ASSIGNMENT - 1

Use of Create Command

Creating a table employee1 having fields name as name,fathername,address,phone number,salary,date of birth,job.

SQL> create table employee1(name varchar2(15),fathername varchar2(15),address varchar2(30),sal number(10),phoneno number(10),d_o_b date,job varchar2(15) 2 );

Table created.

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)

Page 5: sql file

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','&fathername','&address',&sal,&phoneno,'&d_o_b','&job');Enter value for name: ashishEnter value for fathername: vinitEnter value for address: nitEnter value for sal: 10000Enter value for phoneno: 9711111111Enter value for d_o_b: 30-jun-89Enter value for job: clerkold 1: insert into employee1(name,fathername,address,sal,phoneno,d_o_b,job) values('&name','&fathenew 1: insert into employee1(name,fathername,address,sal,phoneno,d_o_b,job) values('ashish','vinit

Page 6: sql file

1 row created.

SQL> /Enter value for name: monuEnter value for fathername: vijayEnter value for address: delhiEnter value for sal: 20000Enter value for phoneno: 9210045823Enter value for d_o_b: 17-jul-90Enter value for job: managerold 1: insert into employee1(name,fathername,address,sal,phoneno,d_o_b,job) values('&name','&fathenew 1: insert into employee1(name,fathername,address,sal,phoneno,d_o_b,job) values('monu','vijay',

1 row created.

SQL> /Enter value for name: vinitEnter value for fathername: shyamlalEnter value for address: surajkundEnter value for sal: 30000Enter value for phoneno: 9786997865Enter value for d_o_b: 30-jun-90Enter value for job: managerold 1: insert into employee1(name,fathername,address,sal,phoneno,d_o_b,job) values('&name','&fathenew 1: insert into employee1(name,fathername,address,sal,phoneno,d_o_b,job) values('vinit','shyaml

1 row created.

Page 7: sql file

SQL> select * from employee1;

NAME FATHERNAME ADDRESS SAL--------------- --------------- ------------------------------ ---------- PHONENO D_O_B JOB---------- --------- ---------------ashish vinit nit 100009711111111 30-JUN-89 clerk

monu vijay delhi 200009210045823 17-JUL-90 manager

vinit shyamlal surajkund 300009786997865 30-JUN-90 manager

Use of tab

To show all the existing tables in the sql :-Output:-SQL> select * from tab;TNAME TABTYPE CLUSTERID------------------------------ ------- ----------STUDENT1 TABLESTUDENT2 TABLEEMPLOYEE2 TABLESTUDENT5 TABLECUSTOMER4 TABLEEMPLOYEE TABLESTUDENT6 TABLEBIN TABLEDEPT TABLEEMP TABLEBONUS TABLE

TNAME TABTYPE CLUSTERID------------------------------ ------- ----------

Page 8: sql file

SALGRADE TABLENV TABLEBIN$swhHVDtZRD+H5AwM2Q2nuA==$0 TABLESTUDENT TABLE

Use of Set line size command

To set the line size of the table :-

Output;-

SQL> set linesize 999;SQL> select * from employee1;

NAME FATHERNAME ADDRESS SAL PHONENO D_O_B JOB--------------- --------------- ------------------------------ ---------- ---------- --------- -----ashish vinit nit 10000 9711111111 30-JUN-89 clerkmonu vijay delhi 20000 9210045823 17-JUL-90 managervinit shyamlal surajkund 30000 9786997865 30-JUN-90 managervikas rajbir surajkund 15000 9711811651 27-DEC-89 peon

Page 9: sql file

ASSIGNMENT - 2

Use of where clause

To show the records of the employee whose name starts with “v”:-Output;-SQL> select * from employee1 where name like 'v%';

NAME FATHERNAME ADDRESS SAL PHONENO D_O_B JOB--------------- --------------- ------------------------------ ---------- ---------- --------- -----vinit shyamlal surajkund 30000 9786997865 30-JUN-90 managervikas rajbir surajkund 15000 9711811651 27-DEC-89 peon

Use of distinct command

To remove the duplicate in the table:-

SQL> select distinct * from employee1;NAME FATHERNAME ADDRESS SAL PHONENO D_O_B JOB--------------- --------------- ------------------------------ ---------- ---------- --------vinit shyamlal surajkund 30000 9786997865 30-JUN-90 managervikas rajbir surajkund 15000 9711811651 27-DEC-89 peonashish vinit nit 10000 9711111111 30-JUN-89 clerkmonu vijay delhi 20000 9210045823 17-JUL-90 manager

Page 10: sql file

SQL> select distinct address from employee1;ADDRESS------------------------------surajkundnit delhi

Use of rename command

To show the renaming of table name from employee1 to employee2 :-Output:-

SQL> rename employee1 to employee2;

Table renamed.

NAME FATHERNAME ADDRESS SAL PHONENO D_O_B JOB--------------- --------------- ------------------------------ ---------- ---------- --------- -----ashish vinit nit 10000 9711111111 30-JUN-89 clerkmonu vijay delhi 20000 9210045823 17-JUL-90managervinit shyamlal surajkund 30000 9786997865 30-JUN-90 managervikas rajbir surajkund 15000 9711811651 27-DEC-89 peon

Page 11: sql file

Use of order by clause

To show the table order by name and sal :-Output:- SQL> select * from employee2 order by name;

NAME FATHERNAME ADDRESS SAL PHONENO D_O_B JOB--------------- --------------- ------------------------------ ---------- ---------- --------- -----ashish vinit nit 10000 9711111111 30-JUN-89 clerkmonu vijay delhi 20000 9210045823 17-JUL-90 managervikas rajbir surajkund 15000 9711811651 27-DEC-89 peonvinit shyamlal surajkund 30000 9786997865 30-JUN-90 manager

SQL> select * from employee2 order by sal;

NAME FATHERNAME ADDRESS SAL PHONENO D_O_B JOB--------------- --------------- ------------------------------ ---------- ---------- --------- -----ashish vinit nit 10000 9711111111 30-JUN-89 clerkvikas rajbir surajkund 15000 9711811651 27-DEC-89 peonmonu vijay delhi 20000 9210045823 17-JUL-90 managervinit shyamlal surajkund 30000 9786997865 30-JUN-90 manager

Page 12: sql file

ASSIGNMENT - 3

Use of between operator

To show the sal between 10000 to 20000 :-Output:

SQL> select * from employee2 where sal between 10000 and 20000;

NAME FATHERNAME ADDRESS SAL PHONENO D_O_B JOB--------------- --------------- ------------------------------ ---------- ---------- --------- -----ashish vinit nit 10000 9711111111 30-JUN-89 clerkmonu vijay delhi 20000 9210045823 17-JUL-90 managervikas rajbir surajkund 15000 9711811651 27-DEC-89 peon

Use of like operator

To show the records of job starts with “m” :-

Output:SQL> select * from employee2 where job like 'm%';

NAME FATHERNAME ADDRESS SAL PHONENO D_O_B JOB--------------- --------------- ------------------------------ ---------- ---------- --------- -----monu vijay delhi 20000 9210045823 17-JUL-90 managervinit shyamlal surajkund 30000 9786997865 30-JUN-90 manager

Page 13: sql file

Use of Delete command

To delete the contents of a table.

SQL> Delete from employee1;

10 rows deleted.

SQL> Delete from employee1 where job = ‘Manager’;

5 rows deleted.

Use of truncate command

To remove the data type from the table :-Output :-

SQL> truncate table employee2;

Table truncated.

Use of drop command

To delete the whole table from sql :-

SQL> drop table employee2 2 ;

Table dropped.SQL> desc employee2;ERROR:ORA-04043: object employee2 does not exist

Page 14: sql file

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

Page 15: sql file

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 managerOutput: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

Page 16: sql file

7654 MARTIN SALESMAN 7698 28-SEP-81 1875 1400 30 7788 SCOTT ANALYST 7566 19-APR-87 4500 20 7839 KING PRESIDENT 17-NOV-81 7500 10 7844 TURNER SALESMAN 7698 08-SEP-81 2250 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1650 20 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

Use of arithmetic operator :-

Incrementing the employee salary by 50%.Output:

SQL> update emp set sal =sal +((sal*50)/100);14 rows updated.

SQL> select * from emp;

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 7566 JONES MANAGER 7839 02-APR-81 4462.5 20

Page 17: sql file

7654 MARTIN SALESMAN 7698 28-SEP-81 1875 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 4275 30 7782 CLARK MANAGER 7839 09-JUN-81 3675 10 7788 SCOTT ANALYST 7566 19-APR-87 4500 20 7839 KING PRESIDENT 17-NOV-81 7500 10 7844 TURNER SALESMAN 7698 08-SEP-81 2250 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1650 20

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

Page 18: sql file

7566 JONES MANAGER 7839 02-APR-81 4462.5 20 7698 BLAKE MANAGER 7839 01-MAY-81 4275 30 7782 CLARK MANAGER 7839 09-JUN-81 3675 10 7788 SCOTT ANALYST 7566 19-APR-87 4500 20 7839 KING PRESIDENT 17-NOV-81 7500 10 7844 TURNER SALESMAN 7698 08-SEP-81 2250 0 30 7902 FORD ANALYST 7566 03-DEC-81 4500 20

8 rows selected.

Select the employee whose salary is less than 1500.Output:

SQL> select * from emp where sal <1500;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7369 SMITH CLERK 7902 17-DEC-80 1200 20 7900 JAMES CLERK 7698 03-DEC-81 1425 30

Select the employee where saqlary is not equal to 5000Output:

SQL> select * from emp where sal<>5000;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO---------- ---------- --------- ---------- --------- ---------- ---------- ----------

Page 19: sql file

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 7566 JONES MANAGER 7839 02-APR-81 4462.5 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1875 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 4275 30 7782 CLARK MANAGER 7839 09-JUN-81 3675 10 7788 SCOTT ANALYST 7566 19-APR-87 4500 20 7839 KING PRESIDENT 17-NOV-81 7500 10 7844 TURNER SALESMAN 7698 08-SEP-81 2250 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1650 20

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.

Page 20: sql file

ASSIGNMENT – 4

Use of alter command

To add a new column into the table.

Adding a column location into the table:-SQL> alter table student2 2 add(location varchar2(10));

Table altered.

Output RNO NAME ADDRESS DOB AGE LOCATION---------- ---------- ---------- --------- ---------- ---------- 2 ajay 3-h-124 09-JUN-85 20

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

Page 21: sql file

To modify the data type or size in a table.

Modify the column of name:-

SQL> desc student2;

Name Null? Type

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

RNO NUMBER(4)

NAME VARCHAR2(10)

ADDRESS VARCHAR2(10)

DOB DATE

LOCATION VARCHAR2(10)

SQL> alter table student2

2 modify(name varchar(22));

Table altered.

SQL> desc student2;

Output

Name Null? Type

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

RNO NUMBER(4)

NAME VARCHAR2(22)

ADDRESS VARCHAR2(10)

DOB DATE

LOCATION VARCHAR2(10)

Page 22: sql file

ASSIGNMENT – 5

Use of in and not in operator

Showing the ename from table emp using in and not in operatorOutput:In operator:

SQL> select * from emp where ename in('SMITH','JONES','SCOTT','MARTIN');

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO AGE---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------- 7369 SMITH CLERK 7902 17-DEC-80 800 20 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7788 SCOTT ANALYST 7566 19-APR-87 3000 20

Not in operator:

Page 23: sql file

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

Use of dual table

Page 24: sql file

Showing the arithmetic operations using dual tableOutput:

SQL> desc dual; Name --------------------------------------------------------------------------------------------------- DUMMY

SQL> select 2*2 "MULTIPLY" from dual;

MULTIPLY---------- 4

SQL> select 500/25 "DIVIDE" FROM DUAL;

DIVIDE---------- 20

SQL> Select 300-257 "SUBSTRACTION" from dual;

SUBSTRACTION------------ 43

Use of Sysdate Column

SQL> select sysdate from dual;

SYSDATE ----------------- 24-OCT-09

Page 25: sql file

Use of group functions

Showing the average function:

SQL> select avg(sal) from emp;Output:

AVG(SAL)----------2073.21429

Showing the sum function

SQL> select sum(sal) from emp;Output:

SUM(SAL)---------- 29025

Showing the minimum function

SQL> select min(sal) from emp;

Output:

MIN(SAL)---------- 800

Page 26: sql file

Showing the count function

SQL> select count(sal) from emp;

Output:

COUNT(SAL)---------- 14

Page 27: sql file

ASSIGNMENT – 6

Numeric Functions

Showing the various numeric functions:

*Absolute function:

SQL> select abs (-7) from dual;Output:

ABS(-7)---------- 7

*Power function:

SQL> select power (4,8) from dual;Output:

POWER(4,8)---------- 65536

*Mode function:

SQL> select mod (3,5) from dual;Output:

MOD(3,5)----------

Page 28: sql file

3

*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

Page 29: sql file

*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:

Page 30: sql file

SQL> select extract(month from date '2009-07-06') "month" from dual;Output:

month---------- 7

*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

Page 31: sql file

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

Page 32: sql file

*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

Page 33: sql file

*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

Page 34: sql file

*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

Page 35: sql file

*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

Page 36: sql file

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;

Page 37: sql file

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

Page 38: sql file

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

*Next _day function:

SQL> select next_day('26-oct-2009','monday') "date" from dual;

Page 39: sql file

Output:

date---------02-NOV-09

*Months_between function:

SQL> select months_between('27-oct-2009','27-aug-2009') "months" from dual;

Output:

months---------- 2

Page 40: sql file

ASSIGNMENT - 8

Use of commit command

To commit the data into the table :-Output:-

SQL> commit;

Commit complete.

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 102 rows selected.

SQL> rollback;Rollback complete.

Page 41: sql file

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 102 rows selected.

Rollback with savepoint

Savepoint marks and saves the current point in the processing of a transaction. when it is used with a rollback statement, parts of a transaction can be undone.

SQL> update emp set ename='hello' where empno=7902;1 row updated.SQL> update emp set ename='hello' where empno=7934;1 row updated.SQL> savepoint s1;Savepoint created.SQL> update emp set ename='hello' where empno=7900;1 row updated.SQL> rollback to savepoint s1;Rollback complete.SQL> select * from emp;

Output:

EMPNO ENAME JOB MGR HIREDATE SAL COMM---------- ---------- --------- ---------- --------- ---------- ----------

Page 42: sql file

DEPTNO AGE---------- ---------- 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 hello ANALYST 7566 03-DEC-81 3000 20 7934 hello CLERK 7782 23-JAN-82 1300 10

4 rows selected.

Page 43: sql file

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

PRIMARY KEY CONSTRAINT

FEATURES OF PRIMARY KEY:

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));

Page 44: sql file

Table created.

To insert the values

SQL> insert into teacher2(tno, tname, t_add) values(1001, 'mohit', 358/7) ;

I row created.

SQL> insert into teacher2(tno, tname, t_add) values(1001, 'sonia', 567/7);

insert into teacher2(tno, tname, t_add) values(1001, 'sonia', 567/7)*ERROR at line 1:ORA-00001: unique constraint (SCOTT.SYS_C005469) violated

SQL> insert into teacher2(tname, t_add) values('ajay', 667/4);

insert into teacher2(tname, t_add) values('ajay', 667/4)*ERROR at line 1:ORA-01400: cannot insert NULL into ("SCOTT"."TEACHER2"."TNO")

SQL> select * from teacher2;

TNO TNAME T_ADD---------- ---------- ---------- 1001 mohit 51

SQL> insert into teacher2(tno, tname, t_add) values(1006, 'rohit', 345);

1 row created.

Page 45: sql file

FOREIGN KEY / REFERENCE KEY:

It represents relationship between tables. It is a column or a group of column whose value are derive from primary key of some other table.

The table in which foreign key is defined is called DETALED TABLE, FOREIGN TABLE, CHILD TABLE OR JUNCTION TABLE.

.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: 10001Enter value for name: rahulold 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 number(10));

Table created.

SQL> insert into stud_personal8 values(&id,&c_no);Enter value for id: 10001Enter value for c_no: 987654321old 1: insert into stud_personal8 values(&id,&c_no)new 1: insert into stud_personal8 values(10001,987654321)

1 row created.

Page 46: sql file

SQL> select * from stud_personal8;

ID C_NO---------- ---------- 10001 987654321

SQL> select * from stud_acadmic5;

ID NAME---------- ---------- 10001 rahul

SQL> delete from stud_acadmic5 where id=10001;

1 row deleted.

SQL> select * from stud_personal8;

no rows selected

UNIQUE CONSTRAINT

FEATURE OF UNIQUE CONSTRAINT:

1. It will not allow duplicate values.

2. Index is created automatically.

3. It supports not null.

SQL> create table student1(name varchar(11), age number(2), address varchar(4)unique);

Table created.

Page 47: sql file

SQL> /Enter value for name: mohitEnter value for age: 20Enter value for address: 56/7old 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: rahulEnter value for age: 22Enter value for address: 56/7old 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

SQL> insert into student1(name, age) values('harsh', 22);

1 row created.

SQL> select * from student1;

mohit 20 56/7

rahul 23

harsh 22

Page 48: sql file

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. Repetition can be done.

2. 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: 1001Enter value for name: rajEnter value for address: 543/9old 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: 1002Enter value for name: mohitEnter value for address: 432/8old 1: insert into stud values(&rno, '&name', '&address')new 1: insert into stud values(1002, 'mohit', '432/8')

1 row created.

Page 49: sql file

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.

Page 50: sql file

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: 1001Enter value for name: ankkurEnter value for fees: 5000old 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