oracle training in kochi | trivandrum |thrissur
TRANSCRIPT
ISO 9001 : 2008 Certified Company
THRISSUR KOCHI TRIVANDRUM
www.facebook.com/indiaoption
Only Authorised Oracle Training Partner in Kerala
SQL *Plus
This is a tool of Oracle which supports SQL ( A language developed by IBM) 100%.
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
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);
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;
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));
DESCTo display the structure of the table.
Syntax: DESC <TABLE NAME>Eg: DESC EMP
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;
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’);
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’;
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)
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;
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.
TRUNCATE
This will delete all the records from table.
Syntax: TRUNCATE TABLE <TABLE NAME>
Eg:
TRUNCATE TABLE EMP;
DROPThis will remove the table itself.
Syntax: DROP TABLE <TABLE NAME>
Eg:
DROP TABLE EMP;
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)
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;
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)
SQL * Plus Functions
1. Date functions2. Character functions3. Numeric functions4. Conversion functions5. Miscellaneous functions6. Group functions
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).
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;
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;
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;
Miscellaneous functions
SELECT USER FROM DUAL;
SELECT UID FROM DUAL;
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;
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;
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;
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
OUTER JOIN can be classified into 3 types.
• LEFT OUTER JOIN• RIGHT OUTER JOIN• FULL OUTER JOIN
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;
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;
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;
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;
SELF JOIN
Joining a table to itself is called SELF JOIN.
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;
Non Equi join
Joining two tables using an operator other than equal to operator is called non equi join.
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
UNION
This will display records from both the queries excluding duplicate records.
Eg: SELECT * FROM STUD1 UNION SELECT * FROM STUD2;
UNION ALL This will display all the records from both the queries including duplicate records.
Eg: SELECT * FROM STUD1 UNION ALL SELECT * FROM STUD2;
INTERSECT
This will display only common records from both the queries.Eg. SELECT * FROM STUD1 INTERSECT SELECT * FROM STUD2;
MINUSThis will display distinctive records from the first query only.
Eg. SELECT * FROM STUD1 MINUS SELECT * FROM STUD2;
CONSTRAINTS
Specifies some restrictions on the table. Ensures data integrity.Constraints are classified into 3 types.1. Domain constraints2. Entity constraints3. Referential integrity constraints
DOMAIN CONSTRAINTS
• Not null constraint• Check constraint
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);
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));
ENTITY CONSTRAINTS
• UNIQUE CONSTRAINT• PRIMARY KEY CONSTRAINT
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));
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));
REFERENTIAL INTEGRITY CONSTRAINTS
• FOREIGN KEY CONSTRAINT
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.
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);
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);
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.
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));
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);
OTHER DATABASE OBJECTS
1. SYNONYM2. VIEW3. SEQUENCE4. INDEX
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 ;
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;
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;
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.
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;
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);
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;
Thank You