Professional Documents
Culture Documents
Alter
Create
Comment
Oracle 10g (SQL)
Truncat
Rename
Insert
Delete
Revoke
Grant
Commit
Rollback
Page 1
Save point
Oracle 10g (SQL)
Note: With the DATEand TIMESTAMPdata types, you can use the addition and subtraction
operators only
Comparison Conditions:
Logical Conditions:
Single row function ( Return one result per row) Multi row function( Return one result per set of rows)
rows)rows)row)
Number Functions
Note:SYSDATEis a SQL function that returns the current date and time. Your results may
differ from the example.
Note:CHARto NUMBERconversions succeed only if the character string represents a valid number.
These are some of the format elements that you can use with the TO_CHARfunction to display a
number value as a character:
Using the TO_CHARFunction with Numbers
These functions have an fxmodifier. This modifier specifies the exact matching for the
character argument and date format model of a TO_DATEfunction
RRDate Format
RRDate Format: Exampl: To find employees hired before 1990, use the RRdate format,
which produces the same results whether the command is run in 1999 or now:
Nesting Functions
Single-row functions can be nested to any level.
Nested functions are evaluated from the deepest level to the least deep level.
NVL Function
Converts a null value to an actual value:
• Data types that can be used are date, character, and number.
• Data types must match:
– NVL(commission_pct,0)
– NVL(hire_date,'01-JAN-97')
– NVL(job_id,'No Job Yet')
The COALESCE function returns the first non-null expression in the list
All columns in the SELECT list that are not in group functions must be in the GROUP BY
clause.
ON Clause:
Use the ON clause to specify a join condition. This lets you specify join
conditions separate from any search or filter conditions in theWHEREclause.
Non equijoins:
A non equijoin is a join condition containing something other than an equality operator.
Cartesian Products:
A Cartesian product is formed when A join condition is omitted, invalid and
All rows in the first table are joined to all rows in the second table.
To avoid a Cartesian product, always include a valid join condition.
The HAVINGClause with Subqueries: You can use subqueries not only in the
WHERE clause but also in the HAVING clause.
The number of columns and the data types of the columns being selected must be
identical in all the SELECT statements used in the query. The names of the columns need
not be identical.
UNION operates over all of the columns being selected.
NULL values are not ignored during duplicate checking.
The IN operator has a higher precedence than the UNION operator.
By default, the output is sorted in ascending order ofthe first column of theSELECTclause
UNION ALLOperator
The UNION ALL operator returns results from both queries, including all duplications.
The guidelines for UNION and UNION AL Lare the same, with the following two
exceptions that pertain to UNION ALL:
Unlike UNION, duplicate rows are not eliminated and the output is not sorted by
default.
The DISTINCT keyword cannot be used.
INTERSECT Operator
The INTERSECT operator returns rows that are common to both queries.
The number of columns and the data types of the columns being selected by the
SELECT statements in the queries must be identical in all the SELECT statements used
in the query. The names of the columns need not be identical.
Reversing the order of the intersected tables does not alter the result.
INTERSECT does not ignore NULL values
MINUSOperator:
The MINUS operator returns rows in the first query that are not present in the second
query.
INSERTStatement Syntax:
You can add new rows to a table by issuing the INSERT statement.
Note:This statement with the VALUESclause adds only one row at a time to a table.
UPDATEStatement Syntax
Modify existing rows with the UPDATEstatement:
DELETE Statement
You can remove existing rows by using the DELETE statement.
TRUNCATE Statement
Removes all rows from a table, leaving the table empty and the table structure intact.
Controlling Transactions
Data Types
Constraints:
Including Constraints:
Constraints enforce rules at the table level.
• Constraints prevent the deletion of a table if there are dependencies.
• The following constraint types are valid:
– NOT NULL – UNIQUE – PRIMARY KEY – FOREIGN KEY – CHECK
Defining Constraints:
NOT NULLConstraint:
The NOT NULL constraint ensures that the column contains no null values. Columns without
the NOT NULL constraint can contain null values by default. NOT NULL constraints must be
defined at the column level.
UNIQUEConstraint:
Defined at either the table level or the column level:
CHECK Constraint
Violating Constraints:
For example, if you attempt to update a record with a value that is tied to an integrity
constraint, an error is returned. In the example in the slide, department 55 does not exist in
the parent table, DEPARTMENTS, and so you receive the parent key violation ORA-02291.
View: A view is a logical table based on a table or another view. A view contains no
data of its own but is like a window through which data from tables can be viewed or
changed.
Advantages of Views
View Syntax:
Creating a View
Sequences:
Can automatically generate unique numbers Is a sharable object
Can be used to create a primary key valueReplaces application code
Speeds up the efficiency of accessing sequence values when cached in memory
Using a Sequence:
Insert a new department named “Support” in location ID 2500:
Modifying a Sequence:
Change the increment value, maximum value, minimum value,
cycle option, or cache option:
Indexes:An Oracle server index is a schema object that can speed up the retrieval of
rows by using a pointer.
Create an index on one or more columns:
Improve the speed of query access to the LAST_NAME column in the EMPLOYEES table:
Removing an Index
Synonyms
Create an easier reference to a table that is owned by another user:
Drop a synonym:
Table Information:
Column Information:
Constraint Information:
You can find out the names of your constraints, the type of constraint, the table
name to which the constraint applies, the condition for check constraints, foreign
key constraint information, deletion rule for foreign key constraints, the status, and
many other types of information about your constraints.
View Information:
Sequence Information:
Synonym Information:
Chapter 12
Controlling User Access:
Privileges:Privileges are the right to execute particular SQL statements. The
database administrator (DBA) is a high-level user with the ability to create users
and grant users access to the database and its objects.
System privileges:
– Creating new users Create session
– Removing users – Create table
– Removing tables – create sequence
– Backing up tables – create view
Object privileges: – create procedure
An object privilegeis a privilege or right to perform a particular action on a specific
table, view, sequence, or procedure. Each object has a particular set of grantable
privileges.
ALTER
DELETE
EXECUTE
INDEX
INSERT
REFERENCES
SELECT
UPDATE
Creating Users:
The DBA creates users with the CREATE USERstatement
A role is a named group of related privileges that can be granted to the user. This
method makes it easier to revoke and maintain privileges.
Object Privileges:
Note: Current system privileges can be found in the SESSION_PRIVS dictionary view.
Example:
alter table anik
Rename to sahria;
You use the ADD clause to add columns:
You can change a column’s data type, size, and default value.
Select *
From user_unused_col_tabs:
ON DELETE CASCADE:
Delete child rows when a parent key is deleted:
Deferring Constraints:
DEFERR ABLE or NOT DEFERRABLE INITIALLY DEFERRED or INITIALLY IMMEDIATE
Dropping a Constraint:
Remove the manager constraint from the EMP2table:
Remove the PRIMARY KEY constraint on the DEPT2table and drop the
associated FOREIGN KEY constraint on the EMP2.DEPARTMENT_IDcolumn:
Disabling Constraints:
Execute the DISABLE clause of the ALTER TABLE statement to deactivate integrity
constraint.
Apply the CASCADE option to disable dependent integrity constraints.
Enabling Constraints:
Activate an integrity constraint currently disabled in the table definition by using
the ENABLE clause.
A UNIQUE index is automatically created if you enable a UNIQUE key or a PRIMARY
KEY constraint
Cascading Constraints:
The CASCADE CONSTRAINTS clause is used along with the DROP COLUMN clause.
The CASCADE CONSTRAINTS clause drops all referential integrity constraints that
refer to the primary and unique keys defined on the dropped columns.
The CASCADE CONSTRAINTS clause also drops all multicolumn constraints defined
on the dropped columns.
Overview of Indexes:
Indexes are created:
• Automatically
– PRIMARY KEY creation – UNIQUE KEY creation
• Manually– CREATE INDEX statement – CREATE TABLE statement.
Removing an Index:
Remove an index from the data dictionary by using the DROP INDEX command: