Anda di halaman 1dari 24

ORACLE

SQL Statements
There are 3 basic categories of SQL Statements:
SQL-Data Statements -- query and modify tables and columns
SELECT Statement -- query tables and views in the database
INSERT Statement -- add rows to tables
UPDATE Statement -- modify columns in table rows
DELETE Statement -- remove rows from tables

SELECT Clause
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
general format:
FROM Clause
The FROM clause always follows the SELECT clause. It lists the tables
accessed by the query.
WHERE Clause

The WHERE clause is optional. When specified,


it always follows the FROM clause. The WHERE clause
filters rows from the FROM clause tables. Omitting the
WHERE clause specifies that all rows are used.
Difference between DDL, DML and DCL
commands

 DDL - Data Definition Language: statements used to define the database


structure or schema. Some examples:

 CREATE - to create objects in the database

 ALTER - alters the structure of the database

 DROP - delete objects from the database

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

 COMMENT - add comments to the data dictionary

 RENAME - rename an object


DML
 DML - Data Manipulation Language: statements used for managing data
within schema objects. Some examples:

 SELECT - retrieve data from the a database

 INSERT - insert data into a table

 UPDATE - updates existing data within a table

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

 MERGE - UPSERT operation (insert or update)

 CALL - call a PL/SQL or Java subprogram

 EXPLAIN PLAN - explain access path to data

 LOCK TABLE - control concurrency


DCL
 DCL - Data Control Language. Some examples:
 GRANT - gives user's access privileges to database
 REVOKE - withdraw access privileges given with the GRANT
command
TCL
 TCL - Transaction Control: statements used to manage the
changes made by DML statements. It allows statements to
be grouped together into logical transactions.
 COMMIT - save work done
 SAVEPOINT - identify a point in a transaction to which you
can later roll back
 ROLLBACK - restore database to original since the last
COMMIT
 SET TRANSACTION - Change transaction options like
isolation level and what rollback segment to use
Difference between TRUNCATE, DELETE and
DROP commands

 The DELETE command is used to remove some or all rows from a table. A
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
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
DELETE triggers on the table to fire.
TRUNCATE
TRUNCATE removes all rows from a table. The operation
cannot be rolled back and no triggers will be fired. As such,
TRUNCATE is faster and doesn't use as much undo space as
a DELETE.
DROP
 The DROP command removes a table from the database.
All the tables' rows, indexes and privileges will also be
removed. No DML triggers will be fired. The operation
cannot be rolled back.
 DROP and TRUNCATE are DDL commands, whereas DELETE
is a DML command. Therefore DELETE operations can be
rolled back (undone), while DROP and TRUNCATE
operations cannot be rolled back.
ORDER BY Clause

 The ORDER BY clause is optional. If used, it must be the


last clause in the SELECT statement. The ORDER BY clause
requests sorting for the results of a query. When the
ORDER BY clause is missing, the result rows from a query
have no defined order (they are unordered).
 The ORDER BY clause defines the ordering of rows based
on columns from the SELECT clause. The ORDER BY clause
has the following general format:
SQL Joins
 The SQL JOIN refers to using the JOIN
keyword in a SQL statement in order to
query data from two tables.
 When you perform a SQL join, you specify
one column from each table to join on.
These two columns contain data that is
shared across both tables.
 You can use multiple joins in the same SQL
statement to query data from as many
tables as you like.
Join Types

 Depending on your requirements, you can do an "inner"


join or an "outer" join. These are different in a subtle way

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

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

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

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

Inner Join:

SELECT * FROM table_name1 INNER


JOIN table_name2 ON
table_name1.column_name =
table_name2.column_name
Left Join:

SELECT * FROM table_name1 LEFT JOIN


table_name2 ON
table_name1.column_name =
table_name2.column_name
Right Join:

SELECT * FROM table_name1 RIGHT JOIN


table_name2 ON table_name1.column_name =
table_name2.column_name
Full Join:
SELECT * FROM table_name1 FULL JOIN
table_name2 ON table_name1.column_name =
table_name2.column_name
Join Examples
 Let's see if we can make it work. Assume we have the
following two tables.
 Table A is on the left, and Table B is on the right. We'll
populate them with four records each.
Table A Table B
id name Id name
1 Pirate 1 Rutabaga
2 Monkey 2 Pirate
3 Ninja 3 Darth Vader
4 Spaghetti 4 Ninja
Inner join produces only the set of records that match in both Table A
and Table B.

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


TableB.name

id name id name
1 Pirate 2 Pirate
3 Ninja 4 Ninja
Full outer join 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
missing side will contain null.

SELECT * FROM TableA FULL OUTER JOIN TableB ON


TableA.name = TableB.name

id name id name

1 Pirate 2 Pirate
2 Monkey null null
3 Ninja 4 Ninja
4 Spaghetti null null
null null 1 Rutabaga
null null 3 Darth Vader
Left outer join produces a complete set of records from Table A, with
the matching records (where available) in Table B. If there is no
match, the right side will contain null.

SELECT * FROM TableA LEFT OUTER JOIN TableB ON


TableA.name = TableB.name

id name id name
1 Pirate 2 Pirate
2 Monkey null null
3 Ninja 4 Ninja
4 Spaghetti null null
To produce the set of records only in Table A, but not in Table B, we
perform the same left outer join, then exclude the records we
don't want from the right side via a where clause.

SELECT * FROM TableA LEFT OUTER JOIN TableB ON


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

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

SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.name =


TableB.name WHERE TableA.id IS null OR TableB.id IS null

id name id name
2 Monkey null null
4 Spaghetti null null
null null 1 Rutabaga
null null 3 Darth Vader

Anda mungkin juga menyukai