SlideShare ist ein Scribd-Unternehmen logo
1 von 65
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));
DESC
To 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;
INSERT
To 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 values
INSERT 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 VALUE
Eg to insert null value to the column SAL
INSERT INTO EMP (ENO,ENAME,DGN,DOJ) VALUES (14,’JOY’,’CLERK’,’01-
MAR-2013’);
SELECT
To 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)
UPDATE
To 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;
DROP
This will remove the table itself.
Syntax:
DROP TABLE <TABLE NAME>
Eg:
DROP TABLE EMP;
COMMIT
This 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;
ROLLBACK
This will cancel(undo) changes up to the previous commit or savepoint.
Syntax:
ROLLBACK;
SAVEPOINT
This 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 OPTION
Here the other user can grant privileges to some other users.
Eg.
GRANT ALL ON EMP TO SCOTT WITH GRANT OPTION;
REVOKE
This will take back the privileges from the other user;
Eg.
REVOKE UPDATE, DELETE ON EMP FROM SCOTT;
ORDER BY
To 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 functions
2. Character functions
3. Numeric functions
4. Conversion functions
5. Miscellaneous functions
6. Group functions
Date functions
SELECT 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 functions
SELECT 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 functions
SELECT 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 functions
TO_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 JOIN
1. INNER JOIN
2. OUTER JOIN
3. SELF JOIN
OUTER JOIN can be classified into 3
types.
• LEFT OUTER JOIN
• RIGHT OUTER JOIN
• FULL OUTER JOIN
INNER JOIN
This 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 JOIN
Opposite 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. UNION
2. UNION ALL
3. INTERSECT
4. 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;
MINUS
This 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 constraints
2. Entity constraints
3. 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 CONSTRAINT
Specifies 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 CONSTRAINT
Table 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. SYNONYM
2. VIEW
3. SEQUENCE
4. 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 employee
CREATE SYNONYM EMPS FOR EMPLOYEE;
Public synonym is a type of synonym which can be created only by the DBA.
DROPPING A SYNONYM
Syntax.
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 QUERY
Eg:
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 view
Eg:
CREATE VIEW EMPV (EMPNO, DESIGNATION, SALARY) AS SELECT ENO,
DGN, SAL FROM EMPLOYEE WHERE DEPTNO = 20;
DROPPING A VIEW
Eg
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 SEQUENCE
Eg
DROP SEQUENCE SS;
INDEX
Index 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 INDEX
DROP INDEX EMPX;
ISO 9001 Certified Company in Kerala

Weitere ähnliche Inhalte

Was ist angesagt?

Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Vidyasagar Mundroy
 
Si0302 20140320131934
Si0302 20140320131934Si0302 20140320131934
Si0302 20140320131934Dex Winadha
 
SQL Quick Reference Card
SQL Quick Reference CardSQL Quick Reference Card
SQL Quick Reference CardTechcanvass
 
Lecture 3 sql {basics ddl commands}
Lecture 3 sql {basics  ddl commands}Lecture 3 sql {basics  ddl commands}
Lecture 3 sql {basics ddl commands}Shubham Shukla
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteAl-Mamun Sarkar
 
Lecture 4 sql {basics keys and constraints}
Lecture 4 sql {basics  keys and constraints}Lecture 4 sql {basics  keys and constraints}
Lecture 4 sql {basics keys and constraints}Shubham Shukla
 
Oracle naveen Sql
Oracle naveen   SqlOracle naveen   Sql
Oracle naveen Sqlnaveen
 
Null values, insert, delete and update in database
Null values, insert, delete and update in databaseNull values, insert, delete and update in database
Null values, insert, delete and update in databaseHemant Suthar
 
MySQL partitions tutorial
MySQL partitions tutorialMySQL partitions tutorial
MySQL partitions tutorialGiuseppe Maxia
 

Was ist angesagt? (16)

Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
Oracle: DDL
Oracle: DDLOracle: DDL
Oracle: DDL
 
Si0302 20140320131934
Si0302 20140320131934Si0302 20140320131934
Si0302 20140320131934
 
Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
 
Babitha2.mysql
Babitha2.mysqlBabitha2.mysql
Babitha2.mysql
 
SQL Quick Reference Card
SQL Quick Reference CardSQL Quick Reference Card
SQL Quick Reference Card
 
Lecture 3 sql {basics ddl commands}
Lecture 3 sql {basics  ddl commands}Lecture 3 sql {basics  ddl commands}
Lecture 3 sql {basics ddl commands}
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
Lecture 4 sql {basics keys and constraints}
Lecture 4 sql {basics  keys and constraints}Lecture 4 sql {basics  keys and constraints}
Lecture 4 sql {basics keys and constraints}
 
Oracle: DML
Oracle: DMLOracle: DML
Oracle: DML
 
Oracle naveen Sql
Oracle naveen   SqlOracle naveen   Sql
Oracle naveen Sql
 
Les02
Les02Les02
Les02
 
Null values, insert, delete and update in database
Null values, insert, delete and update in databaseNull values, insert, delete and update in database
Null values, insert, delete and update in database
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
MySQL partitions tutorial
MySQL partitions tutorialMySQL partitions tutorial
MySQL partitions tutorial
 
Commands
CommandsCommands
Commands
 

Ähnlich wie ISO 9001 Certified Company in Kerala

Ähnlich wie ISO 9001 Certified Company in Kerala (20)

My SQL.pptx
My SQL.pptxMy SQL.pptx
My SQL.pptx
 
Sql 2006
Sql 2006Sql 2006
Sql 2006
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
 
Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
Select To Order By
Select  To  Order BySelect  To  Order By
Select To Order By
 
012. SQL.pdf
012. SQL.pdf012. SQL.pdf
012. SQL.pdf
 
8. sql
8. sql8. sql
8. sql
 
Oracle select statment
Oracle select statmentOracle select statment
Oracle select statment
 
Oracle selectstatmentbasics
Oracle selectstatmentbasicsOracle selectstatmentbasics
Oracle selectstatmentbasics
 
Sql
SqlSql
Sql
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
Sql
SqlSql
Sql
 
Database Query Using SQL_ip.docx
Database Query Using SQL_ip.docxDatabase Query Using SQL_ip.docx
Database Query Using SQL_ip.docx
 
Oracle 9i notes(kamal.love@gmail.com)
Oracle 9i  notes(kamal.love@gmail.com)Oracle 9i  notes(kamal.love@gmail.com)
Oracle 9i notes(kamal.love@gmail.com)
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
 
Sql
SqlSql
Sql
 

Kürzlich hochgeladen

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 

Kürzlich hochgeladen (20)

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 

ISO 9001 Certified Company in Kerala

  • 1. ISO 9001 : 2008 Certified Company THRISSUR KOCHI TRIVANDRUM www.facebook.com/indiaoption
  • 2. Only Authorised Oracle Training Partner in Kerala
  • 3. SQL *Plus This is a tool of Oracle which supports SQL ( A language developed by IBM) 100%.
  • 4. 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
  • 5. 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);
  • 6. 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;
  • 7. 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));
  • 8. DESC To display the structure of the table. Syntax: DESC <TABLE NAME> Eg: DESC EMP
  • 9. 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;
  • 10. INSERT To 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 values INSERT 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 VALUE Eg to insert null value to the column SAL INSERT INTO EMP (ENO,ENAME,DGN,DOJ) VALUES (14,’JOY’,’CLERK’,’01- MAR-2013’);
  • 11. SELECT To 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’;
  • 12. 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)
  • 13. UPDATE To 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;
  • 14. 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.
  • 15. TRUNCATE This will delete all the records from table. Syntax: TRUNCATE TABLE <TABLE NAME> Eg: TRUNCATE TABLE EMP;
  • 16. DROP This will remove the table itself. Syntax: DROP TABLE <TABLE NAME> Eg: DROP TABLE EMP;
  • 17. COMMIT This 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; ROLLBACK This will cancel(undo) changes up to the previous commit or savepoint. Syntax: ROLLBACK; SAVEPOINT This will set a mark in between transactions Eg SAVEPOINT S; (S is the savepoint name)
  • 18. 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 OPTION Here the other user can grant privileges to some other users. Eg. GRANT ALL ON EMP TO SCOTT WITH GRANT OPTION; REVOKE This will take back the privileges from the other user; Eg. REVOKE UPDATE, DELETE ON EMP FROM SCOTT;
  • 19. ORDER BY To 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)
  • 20. SQL * Plus Functions 1. Date functions 2. Character functions 3. Numeric functions 4. Conversion functions 5. Miscellaneous functions 6. Group functions
  • 21. Date functions SELECT 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).
  • 22. Character functions SELECT 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;
  • 23. Numeric functions SELECT 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;
  • 24. Conversion functions TO_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;
  • 25. Miscellaneous functions SELECT USER FROM DUAL; SELECT UID FROM DUAL;
  • 26. 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;
  • 27. 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;
  • 28. 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;
  • 29. JOIN Displaying records from more than one table. There should be a common column with the same data type and size. TYPES OF JOIN 1. INNER JOIN 2. OUTER JOIN 3. SELF JOIN
  • 30. OUTER JOIN can be classified into 3 types. • LEFT OUTER JOIN • RIGHT OUTER JOIN • FULL OUTER JOIN
  • 31. INNER JOIN This 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;
  • 32. 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;
  • 33. RIGHT OUTER JOIN Opposite to LEFT OUTER JOIN Eg: SELECT DEPT.DEPTNO, DNAME, ENAME, SAL, EMP.DEPTNO FROM DEPT RIGHT OUTER JOIN EMP ON DEPT.DEPTNO = EMP.DEPTNO;
  • 34. 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;
  • 35. SELF JOIN Joining a table to itself is called SELF JOIN.
  • 36. 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;
  • 37. Non Equi join Joining two tables using an operator other than equal to operator is called non equi join.
  • 38. SET OPERATORS This will combine the results of two queries. Both queries should have the same structure. There 4 types of Set operators. 1. UNION 2. UNION ALL 3. INTERSECT 4. MINUS
  • 39. UNION This will display records from both the queries excluding duplicate records. Eg: SELECT * FROM STUD1 UNION SELECT * FROM STUD2;
  • 40. UNION ALL This will display all the records from both the queries including duplicate records. Eg: SELECT * FROM STUD1 UNION ALL SELECT * FROM STUD2;
  • 41. INTERSECT This will display only common records from both the queries. Eg. SELECT * FROM STUD1 INTERSECT SELECT * FROM STUD2;
  • 42. MINUS This will display distinctive records from the first query only. Eg. SELECT * FROM STUD1 MINUS SELECT * FROM STUD2;
  • 43. CONSTRAINTS Specifies some restrictions on the table. Ensures data integrity. Constraints are classified into 3 types. 1. Domain constraints 2. Entity constraints 3. Referential integrity constraints
  • 44. DOMAIN CONSTRAINTS • Not null constraint • Check constraint
  • 45. 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);
  • 46. CHECK CONSTRAINT Specifies 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));
  • 47. ENTITY CONSTRAINTS • UNIQUE CONSTRAINT • PRIMARY KEY CONSTRAINT
  • 48. 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));
  • 49. 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));
  • 51. FOREIGN KEY CONSTRAINT Table 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.
  • 52. 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);
  • 53. 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);
  • 54. 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.
  • 55. 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));
  • 56. 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);
  • 57. OTHER DATABASE OBJECTS 1. SYNONYM 2. VIEW 3. SEQUENCE 4. INDEX
  • 58. 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 employee CREATE SYNONYM EMPS FOR EMPLOYEE; Public synonym is a type of synonym which can be created only by the DBA. DROPPING A SYNONYM Syntax. DROP SYNONYM <SYNONYM NAME> Eg. DROP SYNONYM EMPS ;
  • 59. 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 QUERY Eg: 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;
  • 60. VIEW (contd) Changing column names in a view Eg: CREATE VIEW EMPV (EMPNO, DESIGNATION, SALARY) AS SELECT ENO, DGN, SAL FROM EMPLOYEE WHERE DEPTNO = 20; DROPPING A VIEW Eg DROP VIEW EMPV;
  • 61. 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.
  • 62. 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 SEQUENCE Eg DROP SEQUENCE SS;
  • 63. INDEX Index 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);
  • 64. 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 INDEX DROP INDEX EMPX;