This document provides an overview of SQL commands and concepts in Oracle. It discusses topics such as data types, DDL commands like CREATE and ALTER, DML commands like INSERT and UPDATE, functions, joins, constraints, subqueries and other database objects like views and sequences. The document is a training material for SQL and PL/SQL concepts.
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;
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;
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
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));
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);
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;