+ All Categories

ORACLE

Date post: 12-Nov-2014
Category:
Upload: venkat-rambabu
View: 509 times
Download: 0 times
Share this document with a friend
Popular Tags:
24
ORACLE ORACLE SQL Statements SQL Statements There are 3 basic categories of SQL Statements: There are 3 basic categories of SQL Statements: SQL-Data Statements SQL-Data Statements -- query and modify tables and columns -- query and modify tables and columns SELECT Statement SELECT Statement -- query tables and views in the database -- query tables and views in the database INSERT Statement INSERT Statement -- add rows to tables -- add rows to tables UPDATE Statement UPDATE Statement -- modify columns in table rows -- modify columns in table rows DELETE Statement -- remove rows from tables DELETE Statement -- remove rows from tables SELECT Clause SELECT Clause The SELECT clause is mandatory. It specifies a list of The SELECT clause is mandatory. It specifies a list of columns to be retrieved from the tables in the FROM columns to be retrieved from the tables in the FROM clause. It has the following general format: clause. It has the following general format: FROM Clause FROM Clause The FROM clause always follows the SELECT clause. It lists The FROM clause always follows the SELECT clause. It lists the tables accessed by the query. the tables accessed by the query.
Transcript
Page 1: ORACLE

ORACLEORACLE

SQL StatementsSQL StatementsThere are 3 basic categories of SQL Statements: There are 3 basic categories of SQL Statements: SQL-Data StatementsSQL-Data Statements -- query and modify tables and columns -- query and modify tables and columnsSELECT StatementSELECT Statement -- query tables and views in the database -- query tables and views in the database INSERT StatementINSERT Statement -- add rows to tables -- add rows to tables UPDATE StatementUPDATE Statement -- modify columns in table rows -- modify columns in table rows DELETE Statement -- remove rows from tablesDELETE Statement -- remove rows from tables

SELECT ClauseSELECT ClauseThe SELECT clause is mandatory. It specifies a list of columns to be The SELECT clause is mandatory. It specifies a list of columns to be retrieved from the tables in the FROM clause. It has the following retrieved from the tables in the FROM clause. It has the following general format: general format: FROM ClauseFROM ClauseThe FROM clause always follows the SELECT clause. It lists the tables The FROM clause always follows the SELECT clause. It lists the tables accessed by the query. accessed by the query.

Page 2: ORACLE

WHERE ClauseWHERE Clause

The WHERE clause is optional. When specified, it The WHERE clause is optional. When specified, it always follows the FROM clause. The WHERE clause filters always follows the FROM clause. The WHERE clause filters rows from the FROM clause tables. Omitting the WHERE rows from the FROM clause tables. Omitting the WHERE clause specifies that all rows are used. clause specifies that all rows are used.

Page 3: ORACLE

Difference between DDL, DML and DCL Difference between DDL, DML and DCL commandscommands

DDLDDL - Data Definition Language: statements used to define the database - Data Definition Language: statements used to define the database structure or schema. Some examples: structure or schema. Some examples:

CREATE - to create objects in the database CREATE - to create objects in the database

ALTER - alters the structure of the database ALTER - alters the structure of the database

DROP - delete objects from the database DROP - delete objects from the database

TRUNCATE - remove all records from a table, including all spaces allocated TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed for the records are removed

COMMENT - add comments to the data dictionary COMMENT - add comments to the data dictionary

RENAME - rename an object RENAME - rename an object

Page 4: ORACLE

DMLDML DMLDML - Data Manipulation Language: statements used for managing data - Data Manipulation Language: statements used for managing data

within schema objects. Some examples: within schema objects. Some examples:

SELECT - retrieve data from the a database SELECT - retrieve data from the a database

INSERT - insert data into a table INSERT - insert data into a table

UPDATE - updates existing data within a table UPDATE - updates existing data within a table

DELETE - deletes all records from a table, the space for the records remain DELETE - deletes all records from a table, the space for the records remain

MERGE - UPSERT operation (insert or update) MERGE - UPSERT operation (insert or update)

CALL - call a PL/SQL or Java subprogram CALL - call a PL/SQL or Java subprogram

EXPLAIN PLAN - explain access path to data EXPLAIN PLAN - explain access path to data

LOCK TABLE - control concurrency LOCK TABLE - control concurrency

Page 5: ORACLE

DCLDCL

DCLDCL - Data Control Language. Some examples: - Data Control Language. Some examples: GRANT - gives user's access privileges to database GRANT - gives user's access privileges to database REVOKE - withdraw access privileges given with the GRANT REVOKE - withdraw access privileges given with the GRANT

command command

Page 6: ORACLE

TCLTCL TCLTCL - Transaction Control: statements used to manage the - Transaction Control: statements used to manage the

changes made by DML statements. It allows statements to changes made by DML statements. It allows statements to be grouped together into logical transactions. be grouped together into logical transactions.

COMMIT - save work done COMMIT - save work done SAVEPOINT - identify a point in a transaction to which you SAVEPOINT - identify a point in a transaction to which you

can later roll back can later roll back ROLLBACK - restore database to original since the last ROLLBACK - restore database to original since the last

COMMIT COMMIT SET TRANSACTION - Change transaction options like SET TRANSACTION - Change transaction options like

isolation level and what rollback segment to use isolation level and what rollback segment to use

Page 7: ORACLE

Difference between TRUNCATE, DELETE and Difference between TRUNCATE, DELETE and DROP commandsDROP commands

The DELETE command is used to remove The DELETE command is used to remove some or allsome or all rows from a table. A rows from a table. A WHERE clause can be used to only remove some rows. If no WHERE WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. After performing a DELETE condition is specified, all rows will be removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. Note that this operation will cause all change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to fire. DELETE triggers on the table to fire.

Page 8: ORACLE

TRUNCATETRUNCATE

TRUNCATE removes TRUNCATE removes all rowsall rows from a table. The operation from a table. The operation cannot be rolled back and no triggers will be fired. As such, cannot be rolled back and no triggers will be fired. As such, TRUNCATE is faster and doesn't use as much undo space as TRUNCATE is faster and doesn't use as much undo space as a DELETE. a DELETE.

Page 9: ORACLE

DROPDROP

The DROP command removes a table from the database. All The DROP command removes a table from the database. All the tables' rows, indexes and privileges will also be the tables' rows, indexes and privileges will also be removed. No DML triggers will be fired. The operation removed. No DML triggers will be fired. The operation cannot be rolled back. cannot be rolled back.

Page 10: ORACLE

DROP and TRUNCATE are DDL commands, whereas DELETE DROP and TRUNCATE are DDL commands, whereas DELETE is a DML command. Therefore DELETE operations can be is a DML command. Therefore DELETE operations can be rolled back (undone), while DROP and TRUNCATE rolled back (undone), while DROP and TRUNCATE operations cannot be rolled back. operations cannot be rolled back.

Page 11: ORACLE

ORDER BY ClauseORDER BY Clause

The ORDER BY clause is optional. If used, it must be the last The ORDER BY clause is optional. If used, it must be the last clause in the SELECT statement. The ORDER BY clause clause in the SELECT statement. The ORDER BY clause requests sorting for the results of a query. When the ORDER requests sorting for the results of a query. When the ORDER BY clause is missing, the result rows from a query have no BY clause is missing, the result rows from a query have no defined order (they are defined order (they are unorderedunordered). ).

The ORDER BY clause defines the ordering of rows based on The ORDER BY clause defines the ordering of rows based on columns from the SELECT clause. The ORDER BY clause has columns from the SELECT clause. The ORDER BY clause has the following general format: the following general format:

Page 12: ORACLE

SQL JoinsSQL Joins

The SQL JOIN refers to using the JOIN The SQL JOIN refers to using the JOIN keyword in a SQL statement in order to keyword in a SQL statement in order to query data from two tables.query data from two tables.

When you perform a SQL join, you specify When you perform a SQL join, you specify one column from each table to join on. one column from each table to join on. These two columns contain data that is These two columns contain data that is shared across both tables.shared across both tables.

You can use multiple joins in the same SQL You can use multiple joins in the same SQL statement to query data from as many statement to query data from as many tables as you like.tables as you like.

Page 13: ORACLE

Join TypesJoin Types

Depending on your requirements, you can do an "inner" Depending on your requirements, you can do an "inner" join or an "outer" join. These are different in a subtle wayjoin or an "outer" join. These are different in a subtle way

INNER JOIN: This will only return rows when there is at least INNER JOIN: This will only return rows when there is at least one row in both tables that match the join condition. one row in both tables that match the join condition.

LEFT OUTER JOIN (or LEFT JOIN): This will return rows that LEFT OUTER JOIN (or LEFT JOIN): This will return rows that have data in the left table (left of the JOIN keyword), even if have data in the left table (left of the JOIN keyword), even if there's no matching rows in the right table. there's no matching rows in the right table.

RIGHT OUTER JOIN (or RIGHT JOIN): This will return rows RIGHT OUTER JOIN (or RIGHT JOIN): This will return rows that have data in the right table (right of the JOIN keyword), that have data in the right table (right of the JOIN keyword), even if there's no matching rows in the left table. even if there's no matching rows in the left table.

FULL OUTER JOIN (or FULL JOIN): This will return all rows, as FULL OUTER JOIN (or FULL JOIN): This will return all rows, as long as there's matching data in one of the tables. long as there's matching data in one of the tables.

Page 14: ORACLE

Join SyntaxJoin Syntax

Inner Join:Inner Join:

SELECT * FROM table_name1 SELECT * FROM table_name1 INNERINNER JOIN table_name2 ON JOIN table_name2 ON table_name1.column_name = table_name1.column_name = table_name2.column_name table_name2.column_name

Page 15: ORACLE

Left Join:Left Join:

SELECT * FROM table_name1 SELECT * FROM table_name1 LEFTLEFT JOIN JOIN table_name2 ON table_name2 ON table_name1.column_name = table_name1.column_name = table_name2.column_nametable_name2.column_name

Page 16: ORACLE

Right Join:Right Join:

SELECT * FROM table_name1 SELECT * FROM table_name1 RIGHTRIGHT JOIN JOIN table_name2 ON table_name1.column_name = table_name2 ON table_name1.column_name = table_name2.column_name table_name2.column_name

Page 17: ORACLE

Full Join:Full Join:

SELECT * FROM table_name1 SELECT * FROM table_name1 FULLFULL JOIN JOIN table_name2 ON table_name1.column_name = table_name2 ON table_name1.column_name = table_name2.column_name table_name2.column_name

Page 18: ORACLE

Join ExamplesJoin Examples Let's see if we can make it work. Assume we have the Let's see if we can make it work. Assume we have the

following two tables. following two tables. Table ATable A is on the left, and is on the left, and Table BTable B is on the right. We'll is on the right. We'll

populate them with four records each. populate them with four records each.

Page 19: ORACLE

Table ATable A Table BTable B

idid namename Id Id namename

1 Pirate 1 Rutabaga 1 Pirate 1 Rutabaga

2 Monkey 2 Pirate 2 Monkey 2 Pirate

3 Ninja 3 Darth Vader 3 Ninja 3 Darth Vader

4 Spaghetti 4 Ninja 4 Spaghetti 4 Ninja

Page 20: ORACLE

Inner joinInner join produces only the set of records that match in both Table A produces only the set of records that match in both Table A and Table B. and Table B.

SELECT * FROM TableA SELECT * FROM TableA INNER JOININNER JOIN TableB ON TableA.name = TableB ON TableA.name = TableB.nameTableB.name

id id namename id id name name

1 Pirate 2 Pirate1 Pirate 2 Pirate

3 Ninja 4 Ninja 3 Ninja 4 Ninja

Page 21: ORACLE

Full outer joinFull outer join produces the set of all records in Table A and Table B, with produces the set of all records in Table A and Table B, with matching records from both sides where available. If there is no match, the matching records from both sides where available. If there is no match, the missing side will contain null. missing side will contain null.

SELECT * FROM TableA SELECT * FROM TableA FULL OUTER JOINFULL OUTER JOIN TableB ON TableB ON

TableA.name = TableB.nameTableA.name = TableB.name

id id name name id id name name

1 Pirate 2 Pirate 1 Pirate 2 Pirate

2 Monkey null null2 Monkey null null

3 Ninja 4 Ninja3 Ninja 4 Ninja

4 Spaghetti null null 4 Spaghetti null null

null null 1 Rutabaga null null 1 Rutabaga

null null 3 Darth Vader null null 3 Darth Vader

Page 22: ORACLE

Left outer joinLeft outer join produces a complete set of records from Table A, with produces a complete set of records from Table A, with the matching records (where available) in Table B. If there is no the matching records (where available) in Table B. If there is no match, the right side will contain null.match, the right side will contain null.

SELECT * FROM TableA SELECT * FROM TableA LEFT OUTER JOINLEFT OUTER JOIN TableB ON TableB ON

TableA.name = TableB.name TableA.name = TableB.name

id id name name id id namename

1 Pirate 2 Pirate1 Pirate 2 Pirate

2 Monkey null null2 Monkey null null

3 Ninja 4 Ninja 3 Ninja 4 Ninja

4 Spaghetti null null 4 Spaghetti null null

Page 23: ORACLE

To produce the set of records only in Table A, but not in Table B, we To produce the set of records only in Table A, but not in Table B, we perform the same left outer join, then perform the same left outer join, then exclude the records we exclude the records we don't want from the right side via a where clausedon't want from the right side via a where clause. .

SELECT * FROM TableA LEFT OUTER JOIN TableB ONSELECT * FROM TableA LEFT OUTER JOIN TableB ON

TableA.name = TableB.name TableA.name = TableB.name WHERE TableB.id IS nullWHERE TableB.id IS null

idid name name idid namename

2 Monkey null null2 Monkey null null

4 Spaghetti null null 4 Spaghetti null null

Page 24: ORACLE

To produce the set of records unique to Table A and Table B, we To produce the set of records unique to Table A and Table B, we perform the same full outer join, then perform the same full outer join, then exclude the records we exclude the records we don't want from both sides via a where clausedon't want from both sides via a where clause. .

SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.name = SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.name = TableB.name TableB.name WHERE TableA.id IS null OR TableB.id IS nullWHERE TableA.id IS null OR TableB.id IS null

id name id nameid name id name

2 Monkey null null2 Monkey null null

4 Spaghetti null null4 Spaghetti null null

null null 1 Rutabaga null null 1 Rutabaga

null null 3 Darth Vader null null 3 Darth Vader


Recommended