oracle training in kochi | trivandrum |thrissur

65
ISO 9001 : 2008 Certified Company THRISSUR KOCHI TRIVANDRUM www.facebook.com/indiaoption

Upload: indiaoptions-softwares

Post on 11-Jan-2017

154 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Oracle Training in Kochi | Trivandrum |Thrissur

ISO 9001 : 2008 Certified Company

THRISSUR KOCHI TRIVANDRUM

www.facebook.com/indiaoption

Page 2: Oracle Training in Kochi | Trivandrum |Thrissur

Only Authorised Oracle Training Partner in Kerala

Page 3: Oracle Training in Kochi | Trivandrum |Thrissur

SQL *Plus

This is a tool of Oracle which supports SQL ( A language developed by IBM) 100%.

Page 4: Oracle Training in Kochi | Trivandrum |Thrissur

SQL Commands

DDL (DATA DEFINITION LANGUAGE)

DML (DATA MANIPULATON LANGUAGE)

TCL (TRANSACTION CONTROL LANGUAGE)

DCL (DATA CONTROL LAGUAGE)

CREATE INSERT COMMIT GRANT

ALTER SELECT ROLLBACK REVOKE

TRUNCATE UPDATE SAVEPOINT

DROP DELETE

Page 5: Oracle Training in Kochi | Trivandrum |Thrissur

Basic Data Types

NUMBER Numeric values with or with out decimal points.Eg. SALARY NUMBER(6); COMMISSION NUMBER(7,2);

CHAR Accepts alphanumeric type of data. Its size is predefined

and doesn’t vary according to input.Eg ENAME CHAR(20);

Page 6: Oracle Training in Kochi | Trivandrum |Thrissur

Data Types (contd)VARCHAR

Accepts alphanumeric type of data. Its size is predefined and varies according to input.

Eg ENAME VARCHAR(20);

DATE Date type of data. Date format should be DD-MON-YYYY.Eg. DOJ DATE;

Page 7: Oracle Training in Kochi | Trivandrum |Thrissur

CREATE

This is used to create a table.

Syntax:CREATE TABLE <TABLE NAME> (COL1 DATA TYPE, COL2 DATA TYPE, …..)

Eg. CREATE TABLE EMP (ENO NUMBER(4), ENAME VARCHAR(20), DGN CHAR(18), DOJ DATE, SAL NUMBER(6));

Page 8: Oracle Training in Kochi | Trivandrum |Thrissur

DESCTo display the structure of the table.

Syntax: DESC <TABLE NAME>Eg: DESC EMP

Page 9: Oracle Training in Kochi | Trivandrum |Thrissur

ALTER

To change the structure of the table.

Eg to increase the size of a column. ALTER TABLE EMP MODIFY ENAME VARCHAR (25);

Note: Do not reduce the size of the data type. Eg: For adding a new column

ALTER TABLE EMP ADD COMMISSION NUMBER (7,2);Eg to remove an existing column

ALTER TABLE EMP DROP COLUMN COMMISSION;

Page 10: Oracle Training in Kochi | Trivandrum |Thrissur

INSERTTo insert records (rows) into a table.Syntax: INSERT INTO <TABLE NAME> VALUES (COL1 VALUE,COL2 VALUE, …..)Eg: INSERT INTO EMP VALUES(1,’ANU’,’MANAGER’,’02-APR-2000’,80000);

Note: Ensure that non numeric data are enclosed in single quotes. Eg for inserting multiple valuesINSERT INTO EMP VALUES(&ENO,’&ENAME’,’&DGN’,’&DOJ’,&SAL);Note. You will be asked to enter records one by one. After entering all the records you may give a / and press Enter key so that you can keep on inserting records.

INSERTING NULL VALUEEg to insert null value to the column SALINSERT INTO EMP (ENO,ENAME,DGN,DOJ) VALUES (14,’JOY’,’CLERK’,’01-MAR-2013’);

Page 11: Oracle Training in Kochi | Trivandrum |Thrissur

SELECTTo retrieve records from a table.Syntax: SELECT COL1,COL2,… FROM <TABLE NAME> [WHERE CLAUSE]

Eg:SELECT ENAME, DGN, SAL FROM EMP;SELECT * FROM EMP;SELECT * FROM EMP WHERE ENAME = ‘ANU’;SELECT * FROM EMP WHERE SAL > 20000;SELECT * FROM EMP WHERE SAL > 20000 AND DGN = ‘MANAGER’;SELECT * FROM EMP WHERE SAL > 20000 OR DGN = ‘MANAGER’;

Page 12: Oracle Training in Kochi | Trivandrum |Thrissur

SELECT ( contd)SELECT * FROM EMP WHERE SAL BETWEEN 10000 AND 20000;

SELECT * FROM EMP WHERE SAL NOT BETWEEN 10000 AND 20000;

SELECT * FROM EMP WHERE DGN IN (‘MANAGER’,’CLERK’);

SELECT * FROM EMP WHERE DGN NOT IN (‘MANAGER’,’CLERK’);

SELECT * FROM EMP WHERE ENAME LIKE (‘B%’);

SELECT ENO, ENAME, SAL*12 AS “ ANNUAL SALARY” FROM EMP;

SELECT SYSDATE FROM DUAL ; (DUAL is a system table)

SELECT * FROM TAB ; (To display all tables, views, synonyms created by the current user)

Page 13: Oracle Training in Kochi | Trivandrum |Thrissur

UPDATETo change the records of a table.Syntax:

UPDATE <TABLE NAME> SET COLUMN NAME=NEW VALUE [WHERE CLAUSE]

Eg: UPDATE EMP SET ENAME = ‘ARUN KUMAR’ WHERE ENAME = ‘ARUN’;

UPDATE EMP SET SAL = SAL + 1000;

Page 14: Oracle Training in Kochi | Trivandrum |Thrissur

DELETE

To remove records from table.

Syntax:

DELETE FROM <TABLE NAME> [WHERE CLAUSE]

Eg: DELETE FROM EMP WHERE ENO = 12;

Note: If you do not give any condition, all the records will get deleted.

Page 15: Oracle Training in Kochi | Trivandrum |Thrissur

TRUNCATE

This will delete all the records from table.

Syntax: TRUNCATE TABLE <TABLE NAME>

Eg:

TRUNCATE TABLE EMP;

Page 16: Oracle Training in Kochi | Trivandrum |Thrissur

DROPThis will remove the table itself.

Syntax: DROP TABLE <TABLE NAME>

Eg:

DROP TABLE EMP;

Page 17: Oracle Training in Kochi | Trivandrum |Thrissur

COMMITThis will save changes(INSERT, UPDATE, DELETE) permanently to the database server. When you make an exit from an oracle session, all the changes are automatically commited. DDL commands are automatically commited.

Syntax: COMMIT;

ROLLBACKThis will cancel(undo) changes up to the previous commit or savepoint.Syntax: ROLLBACK;

SAVEPOINTThis will set a mark in between transactions Eg SAVEPOINT S; (S is the savepoint name)

Page 18: Oracle Training in Kochi | Trivandrum |Thrissur

GRANT

To grant privileges (INSERT,SELECT, UPDATE,DELETE) to other users.Syntax: GRANT <PRIVILEGE LIST> ON <TABLE NAME> TO <USER NAME>Eg. GRANT INSERT, SELECT ON EMP TO SCOTT; GRANT ALL ON EMP TO SCOTT; (ALL means DML commands)

GRANTING PRIVILEGES WITH GRANT OPTIONHere the other user can grant privileges to some other users. Eg. GRANT ALL ON EMP TO SCOTT WITH GRANT OPTION;

REVOKEThis will take back the privileges from the other user;

Eg. REVOKE UPDATE, DELETE ON EMP FROM SCOTT;

Page 19: Oracle Training in Kochi | Trivandrum |Thrissur

ORDER BYTo sort the records in ascending or descending order.

Eg: SELECT * FROM EMP ORDER BY ENAME;

SELECT * FROM EMP ORDER BY ENAME DESC; (For displaying in descending order)

Page 20: Oracle Training in Kochi | Trivandrum |Thrissur

SQL * Plus Functions

1. Date functions2. Character functions3. Numeric functions4. Conversion functions5. Miscellaneous functions6. Group functions

Page 21: Oracle Training in Kochi | Trivandrum |Thrissur

Date functionsSELECT ADD_MONTHS (SYSDATE, 4) FROM DUAL;

SELECT MONTHS_BETWEEN (‘11-SEP-2011’, ’11-FEB-2011’) FROM DUAL;

SELECT MONTHS_BETWEEN (SYSDATE, DOJ)/12 FROM EMPLOYEE;

SELECT ROUND (SYSDATE, ’YEAR’) FROM DUAL;

SELECT ROUND(SYSDATE, ’MONTH’) FROM DUAL;

SELECT ROUND (SYSDATE, ’DAY’) FROM DUAL; (Rounds the date to the nearest Sunday).

Page 22: Oracle Training in Kochi | Trivandrum |Thrissur

Character functionsSELECT UPPER (ENAME) FROM EMPLOYEE;

SELECT LOWER (ENAME) FROM EMPLOYEE;

SELECT INITCAP (ENAME) FROM EMPLOYEE;

SELECT REPLACE (‘GOOD MORNING’, ’GOOD’, ’BAD’) FROM DUAL;

SELECT SUBSTR (‘GOOD MORNING’,1,4) FROM DUAL;

SELECT CONCAT (‘HELLO’,’ WORLD’) FROM DUAL;

SELECT LENGTH (‘HAI’) FROM DUAL;

Page 23: Oracle Training in Kochi | Trivandrum |Thrissur

Numeric functionsSELECT ABS (-100) FROM DUAL;

SELECT ROUND (16.6) FROM DUAL;

SELECT ROUND (16.237,2) FROM DUAL;

SELECT CEIL (4.6) FROM DUAL;

SELECT FLOOR (12.6) FROM DUAL;

SELECT SQRT (16) FROM DUAL;

SELECT POWER (2,3) FROM DUAL;

SELECT MOD (11,2) FROM DUAL;

Page 24: Oracle Training in Kochi | Trivandrum |Thrissur

Conversion functionsTO_CHAR( )

Converts date type data to character type data.Eg.SELECT TO_CHAR (SYSDATE, ’MM-DD-YYYY’ FROM DUAL;SELECT TO_CHAR (SYSDATE, ’DD-MM-YYYY’ FROM DUAL;SELECT TO_CHAR (SYSDATE, ’DAY’ FROM DUAL;

TO_DATE( )Converts character type data to Oracle’s date format.Eg.SELECT TO_DATE (’06-03-2012’, ’MM-DD-YYYY’) FROM DUAL;

TO_NUMBER( )Converts character type data to number.Eg.SELECT TO_NUMBER(‘100’) FROM DUAL;

Page 25: Oracle Training in Kochi | Trivandrum |Thrissur

Miscellaneous functions

SELECT USER FROM DUAL;

SELECT UID FROM DUAL;

Page 26: Oracle Training in Kochi | Trivandrum |Thrissur

GROUP FUNCTIONS

• SELECT MAX (SAL) FROM EMP;• SELECT MIN (SAL) FROM EMP;• SELECT SUM (SAL) FROM EMP;• SELECT AVG (SAL) FROM EMP;• SELECT COUNT(ENAME) FROM EMP;• SELECT COUNT (*) FROM EMP;

Page 27: Oracle Training in Kochi | Trivandrum |Thrissur

GROUP BY

To group the records of a table.Eg: for counting no of employees in each department (column name is deptno)

Eg: SELECT DEPTNO, COUNT( *) FROM EMP GROUP BY DEPTNO;

SELECT DEPTNO, AVG(SAL) FROM EMP GROUP BY DEPTNO;

Page 28: Oracle Training in Kochi | Trivandrum |Thrissur

HAVING

To give a condition for a GROUP BY statement.

Eg: For displaying only those deptno with more than 5 people.

SELECT DEPTNO, COUNT (*) FROM EMP GROUP BY DEPTNO HAVING COUNT (*) > 5;

Page 29: Oracle Training in Kochi | Trivandrum |Thrissur

JOIN

Displaying records from more than one table. There should be a common column with the same data type and size.

TYPES OF JOIN1. INNER JOIN2. OUTER JOIN3. SELF JOIN

Page 30: Oracle Training in Kochi | Trivandrum |Thrissur

OUTER JOIN can be classified into 3 types.

• LEFT OUTER JOIN• RIGHT OUTER JOIN• FULL OUTER JOIN

Page 31: Oracle Training in Kochi | Trivandrum |Thrissur

INNER JOINThis will display only matching (common) records from two tables.

Eg:SELECT DEPT.DEPTNO, DNAME, ENAME, SAL, EMP.DEPTNO FROM DEPT INNER JOIN EMP ON DEPT.DEPTNO = EMP.DEPTNO;

Page 32: Oracle Training in Kochi | Trivandrum |Thrissur

LEFT OUTER JOIN

This will display all the records from the left table only matching records from the right table.

Eg: SELECT DEPT.DEPTNO, DNAME, ENAME, SAL, EMP.DEPTNO FROM DEPT LEFT OUTER JOIN EMP ON DEPT.DEPTNO = EMP.DEPTNO;

Page 33: Oracle Training in Kochi | Trivandrum |Thrissur

RIGHT OUTER JOINOpposite to LEFT OUTER JOIN

Eg: SELECT DEPT.DEPTNO, DNAME, ENAME, SAL, EMP.DEPTNO FROM DEPT RIGHT OUTER JOIN EMP ON DEPT.DEPTNO = EMP.DEPTNO;

Page 34: Oracle Training in Kochi | Trivandrum |Thrissur

FULL OUTER JOIN

This will display all the records from both the tables

Eg: SELECT DEPT.DEPTNO, DNAME, ENAME, SAL, EMP.DEPTNO FROM DEPT FULL OUTER JOIN EMP ON DEPT.DEPTNO = EMP.DEPTNO;

Page 35: Oracle Training in Kochi | Trivandrum |Thrissur

SELF JOIN

Joining a table to itself is called SELF JOIN.

Page 36: Oracle Training in Kochi | Trivandrum |Thrissur

Equi Join

Similar to Inner join. Joining two tables using equal to operator is called equi join.

Eg.SELECT DEPT.DEPTNO, DNAME, ENAME, SAL, EMP.DEPTNO FROM DEPT, EMP WHERE DEPT.DEPTNO = EMP.DEPTNO;

Page 37: Oracle Training in Kochi | Trivandrum |Thrissur

Non Equi join

Joining two tables using an operator other than equal to operator is called non equi join.

Page 38: Oracle Training in Kochi | Trivandrum |Thrissur

SET OPERATORS

This will combine the results of two queries. Both queries should have the same structure. There 4 types of Set operators.

1. UNION2. UNION ALL3. INTERSECT4. MINUS

Page 39: Oracle Training in Kochi | Trivandrum |Thrissur

UNION

This will display records from both the queries excluding duplicate records.

Eg: SELECT * FROM STUD1 UNION SELECT * FROM STUD2;

Page 40: Oracle Training in Kochi | Trivandrum |Thrissur

UNION ALL This will display all the records from both the queries including duplicate records.

Eg: SELECT * FROM STUD1 UNION ALL SELECT * FROM STUD2;

Page 41: Oracle Training in Kochi | Trivandrum |Thrissur

INTERSECT

This will display only common records from both the queries.Eg. SELECT * FROM STUD1 INTERSECT SELECT * FROM STUD2;

Page 42: Oracle Training in Kochi | Trivandrum |Thrissur

MINUSThis will display distinctive records from the first query only.

Eg. SELECT * FROM STUD1 MINUS SELECT * FROM STUD2;

Page 43: Oracle Training in Kochi | Trivandrum |Thrissur

CONSTRAINTS

Specifies some restrictions on the table. Ensures data integrity.Constraints are classified into 3 types.1. Domain constraints2. Entity constraints3. Referential integrity constraints

Page 44: Oracle Training in Kochi | Trivandrum |Thrissur

DOMAIN CONSTRAINTS

• Not null constraint• Check constraint

Page 45: Oracle Training in Kochi | Trivandrum |Thrissur

NOT NULL CONSTRAINT

Does not accept null value.Eg: CREATE TABLE EMP (ENO NUMBER(4) CONSTRAINT EMP_ENO_NN NOT NULL, ENAME VARCHAR (20), SAL NUMBER (6));

You may give the following command to check weather NOT NULL constraint is working.

INSERT INTO EMP (ENAME, SAL) VALUES (‘JOSE’,20000);

Page 46: Oracle Training in Kochi | Trivandrum |Thrissur

CHECK CONSTRAINTSpecifies a condition.

Eg: CREATE TABLE EMP (ENO NUMBER(4), ENAME VARCHAR (20), SAL NUMBER (6) CONSTRAINT EMP_SAL_CK CHECK (SAL BETWEEN 4000 AND 80000));

Page 47: Oracle Training in Kochi | Trivandrum |Thrissur

ENTITY CONSTRAINTS

• UNIQUE CONSTRAINT• PRIMARY KEY CONSTRAINT

Page 48: Oracle Training in Kochi | Trivandrum |Thrissur

UNIQUE CONSTRAINT

Does not accept duplicate values, but will accept null value.

Eg: CREATE TABLE EMP (ENO NUMBER(4) CONSTRAINT EMP_UK UNIQUE, ENAME VARCHAR (20), SAL NUMBER (6));

Page 49: Oracle Training in Kochi | Trivandrum |Thrissur

PRIMARY KEY CONSTRAINT

This constraint will not accept duplicate or null values. Table created with this constraint is called parent table or master table which can be referenced by other tables. There can be only one primary key constraint for a table.

Eg: CREATE TABLE DEPT (DEPTNO NUMBER(2) CONSTRAINT DEPT_PK PRIMARY KEY, DNAME VARCHAR (20));

Page 50: Oracle Training in Kochi | Trivandrum |Thrissur

REFERENTIAL INTEGRITY CONSTRAINTS

• FOREIGN KEY CONSTRAINT

Page 51: Oracle Training in Kochi | Trivandrum |Thrissur

FOREIGN KEY CONSTRAINTTable created with this constraint can be called child table.Eg: CREATE TABLE EMP (ENO NUMBER(4), ENAME VARCHAR (30), SAL NUMBER (6), DEPTNO NUMBER(2) REFERENCES DEPT (DEPTNO));

Note: Ensure that the primary key column of the parent table and foreign key column of the child table have the same data type and size. While inserting records into the child table’s foreign key column, ensure that the record exist in the parent table’s primary key column. Normally it is not possible to delete or update a parent table record if it has corresponding records in the child table.

Page 52: Oracle Training in Kochi | Trivandrum |Thrissur

ON DELETE CASCADE

If you give this command along with a foreign key declaration, it becomes possible to delete a parent table record even if it has corresponding records in the child table. What happens is that along with the parent table record, corresponding records from the child table also get deleted.

Eg: CREATE TABLE EMP (ENO NUMBER(4), ENAME VARCHAR (20), SAL NUMBER (6), DEPTNO NUMBER(2) REFERENCES DEPT (DEPTNO) ON DELETE CASCADE);

Page 53: Oracle Training in Kochi | Trivandrum |Thrissur

SUB QUERY Query inside a query (select statement) is called Sub query. This will improve performance because it reduces the network traffic.Eg to find employee details whose designation is same as that of HARI. SELECT * FROM EMPLOYEE WHERE DGN = (SELECT DGN FROM EMPLOYEE WHERE ENAME = ‘HARI’);

SELECT * FROM EMPLOYEE WHERE SAL > (SELECT AVG (SAL) FROM EMPLOYEE); Eg to find out second highest salary. SELECT MAX (SAL) FROM EMPLOYEE WHERE SAL NOT IN (SELECT MAX (SAL) FROM EMPLOYEE);

Page 54: Oracle Training in Kochi | Trivandrum |Thrissur

MULTIPLE SUB QUERY

Eg: SELECT * FROM EMPLOYEE WHERE DGN IN (SELECT DGN FROM EMPLOYEE WHERE ENAME = ‘HARI’) AND SAL = (SELECT SAL FROM EMPLOYEE WHERE ENAME = ‘SEEMA’);

Note You can club any no of queries like this using ‘AND’ operator or ‘OR’ operator.

Page 55: Oracle Training in Kochi | Trivandrum |Thrissur

MULTILEVEL SUBQUERY

Sub query inside a sub query is called Multilevel sub query.

Eg to display details of the employee who gets the second highest salary. SELECT ENO, ENAME, SAL FROM EMPLOYEE WHERE SAL IN (SELECT MAX (SAL) FROM EMPLOYEE WHERE SAL NOT IN (SELECT MAX (SAL) FROM EMPLOYEE));

Page 56: Oracle Training in Kochi | Trivandrum |Thrissur

CORRELATED SUBQUERY

It is a type of sub query which is executed once for each row processed by the main query. The execution of the sub query is co-related to the candidate row of the main query.Eg: SELECT * FROM EMPLOYEE E WHERE E.SAL > (SELECT AVG (SAL) FROM EMPLOYEE WHERE E.DEPTNO = DEPTNO);

Page 57: Oracle Training in Kochi | Trivandrum |Thrissur

OTHER DATABASE OBJECTS

1. SYNONYM2. VIEW3. SEQUENCE4. INDEX

Page 58: Oracle Training in Kochi | Trivandrum |Thrissur

SYNONYM Synonym is like a duplicate table which is created on a table. Whatever changes (DML commands) you make in a synonym will be reflected in the associated table also.

Syntax.CREATE SYNONYM <SYNONYM NAME> FOR <TABLE NAME>

Eg. Assume the table name is employeeCREATE SYNONYM EMPS FOR EMPLOYEE;Public synonym is a type of synonym which can be created only by the DBA.

DROPPING A SYNONYMSyntax.DROP SYNONYM <SYNONYM NAME> Eg. DROP SYNONYM EMPS ;

Page 59: Oracle Training in Kochi | Trivandrum |Thrissur

VIEW

View can be defined as a virtual table which is created by using a query. Whatever changes you make in a view will be reflected on the associated table also. Syntax: CREATE VIEW <VIEW NAME> AS QUERYEg: CREATE VIEW EMPV AS SELECT * FROM EMPLOYEE;

Note: Here empv is the view name and employee is the table name

CREATE VIEW EMPV AS SELECT ENO, DGN, SAL FROM EMPLOYEE WHERE DEPTNO = 20;

Page 60: Oracle Training in Kochi | Trivandrum |Thrissur

VIEW (contd)

Changing column names in a viewEg: CREATE VIEW EMPV (EMPNO, DESIGNATION, SALARY) AS SELECT ENO, DGN, SAL FROM EMPLOYEE WHERE DEPTNO = 20;

DROPPING A VIEWEg DROP VIEW EMPV;

Page 61: Oracle Training in Kochi | Trivandrum |Thrissur

SEQUENCE

Sequence is used to generate unique integer values.

Eg.CREATE SEQUENCE SS INCREMENT BY 1 MAXVALUE 10 MINVALUE 1;Note.

Assume that you have a table called stud which has a single column rollno (data type number(3)). Now we may insert records into this table using the sequence which we have created.

Page 62: Oracle Training in Kochi | Trivandrum |Thrissur

SEQUENCE (Contd)

Eg.INSERT INTO STUD VALUES (SS.NEXTVAL);Then type / to repeat the process.INSERT INTO STUD VALUES (SS.CURRVAL);

Eg.CREATE SEQUENCE SS START WITH 6 INCREMENT BY 1 MAXVALUE 10 MINVALUE 1 CYCLE CACHE 7;

DROPPING A SEQUENCEEg DROP SEQUENCE SS;

Page 63: Oracle Training in Kochi | Trivandrum |Thrissur

INDEXIndex is a database object which is created on a table or view. Records are stored in a sorted order. It is not possible to open an Index. Creating an Index for a table will improve performance because of greater accessing speed of records. Syntax: CREATE INDEX <INDEX NAME> ON <TABLE NAME> (COLUMN NAME)EG: CREATE INDEX EMPX ON EMPL (ENO);

CREATE INDEX EMPX ON EMPL (ENO) REVERSE;

COMPOSITE INDEX Specifying for than one column in an index is called COMPOSITE INDEX. Eg:

CREATE INDEX EMPX ON EMPL( ENO,ENAME);

Page 64: Oracle Training in Kochi | Trivandrum |Thrissur

INDEX (contd)

UNIQUE INDEX This type of Index can’t be created on a table which has duplicate records in the key column (column to be indexed).Eg: CREATE UNIQUE INDEX EMPX ON EMPL (ENO);

Note: When you create a table with Unique or Primary Key constraint, a Unique Index is automatically created for that table.

DROPPING AN INDEXDROP INDEX EMPX;

Page 65: Oracle Training in Kochi | Trivandrum |Thrissur

Thank You