Join StatementsSelf Join SELECT FROM table alias1, table alias2 WHERE alias1.col1 = alias2.col2 1-17 Bản quyền thuộc COMMIT., JSC... DML Statements• MERGE INTO table1 USING table2 ON co
Trang 1Bản quyền thuộc COMMIT., JSC.
Oracle Database 10g:
SQL Fundamentals – Review
Trang 21-2 Bản quyền thuộc COMMIT., JSC.
SQL Statements
• SELECT
• Data Manipulation Language – DML
• Transaction Control Language – TCL
• Data Definition Language – DDL
• Data Control Language – DCL
Trang 4Single-Row Functions Character Functions
Trang 5Single-Row Functions Number Functions
• ROUND(col|expr, [n])
• TRUNC(col|expr, [n])
• MOD(m, n)
1-5 Bản quyền thuộc COMMIT., JSC.
Trang 7Single-Row Functions Conversion Functions
Trang 8Single-Row Functions General Functions
Trang 9CASE Expression
– CASE expr
– WHEN comparison_expr1 THEN return_expr1
– [WHEN comparison_expr2 THEN return_expr2
–
– WHEN comparison_exprn THEN return_exprn
– ELSE return_expr]
1-9 Bản quyền thuộc COMMIT., JSC.
Trang 13Join Statements Natural Join
Trang 16FROM table1, table2
WHERE table1.col1[(+)] = table2.col2[(+)]
1-16 Bản quyền thuộc COMMIT., JSC.
Trang 17Join Statements
Self Join
SELECT <select_clause>
FROM table alias1, table alias2
WHERE alias1.col1 = alias2.col2
1-17 Bản quyền thuộc COMMIT., JSC.
Trang 18• Classification
– Single-row
– Multiple-row
1-18 Bản quyền thuộc COMMIT., JSC.
Trang 20Multiple-row Subquery
• Return more than one row
• Operator
IN ALL ANY
1-20 Bản quyền thuộc COMMIT., JSC.
Trang 21Top-N Analysis
• Syntax
SELECT ROWNUM, [column_list]
FROM (SELECT [column_list]
FROM tableORDER BY top-N_column ASC|DESC)WHERE ROWNUM <= N;
• Notes:
– ROWNUM: assigns a sequential value starting with 1 to each of the rows returned from the query
– A WHERE clause specifies the n rows to be returned The
comparison operator must be < or <=
1-21 Bản quyền thuộc COMMIT., JSC.
Trang 24DML Statements
• MERGE INTO table1
USING table2 ON (condition)
WHEN MATCHED THEN
UPDATE SET
column = value [, column = value…]
DELETE WHERE conditions WHEN NOT MATCHED THEN
INSERT [(column_list)]
VALUES (value_list)
1-24 Bản quyền thuộc COMMIT., JSC.
Trang 26Creating/Dropping Table
• CREATE TABLE table_name
(column datatype [DEFAULT default_val]
Trang 27Altering Table Add Columns
• ALTER TABLE table ADD
(column datatype [DEFAULT default_val]
[col_lvl_constraints]
[, column datatype [DEFAULT default_val]
[col_lvl_constraints]…])
1-27 Bản quyền thuộc COMMIT., JSC.
Trang 28Altering Table – Modify Column
• ALTER TABLE table MODIFY
(column datatype [DEFAULT default_val]
[col_lvl_constraints]…)
• ALTER TABLE table
RENAME COLUMN old_name TO new_name
• Datatype of any column can be changed if all rows of the column contain nulls
• Size of a character/raw column or the precision of a
numeric column can always be increased
• Size of a datatype of a column can be reduced as long as the change does not require data to be modified
• Datatype of a column can be changed from CHAR/VARCHAR2 to VARCHAR2/CHAR only if all rows of the column contain nulls
or do not change the size
1-28 Bản quyền thuộc COMMIT., JSC.
Trang 29Altering Table – Drop Column
• ALTER TABLE table SET UNUSED COLUMN column
• ALTER TABLE table DROP UNUSED COLUMNS
• ALTER TABLE table DROP COLUMNS CONTINUE
1-29 Bản quyền thuộc COMMIT., JSC.
Trang 30Altering Table – Add/Drop Constraint
• ALTER TABLE table_name ADD
[[CONSTRAINT cons_name] cons_def ]
• ALTER TABLE table_name
DROP CONSTRAINT cons_name [CASCADE]
• ALTER TABLE table
DROP (constraint_list) [CASCADE]
• ALTER TABLE table
DROP PRIMARY KEY [CASCADE]
• ALTER TABLE table
DROP UNIQUE (column_list) [CASCADE]
• When dropping PRIMARY KEY and UNIQUE constraint, the corresponding index will be dropped
1-30 Bản quyền thuộc COMMIT., JSC.
Trang 31Altering Table Enable/Disable Constraint
• ALTER TABLE table
DISABLE CONSTRAINT constraint [CASCADE]
1-31 Bản quyền thuộc COMMIT., JSC.
Trang 32– WITH CHECK OPTION: Ensure INSERTs and UPDATEs
operations performed on through the view cannot create/modify rows that the view cannot select
– WITH READ ONLY: Prevent DML operations occur on views
• Drop view
DROP VIEW view_name
1-32 Bản quyền thuộc COMMIT., JSC.
Trang 33– Unique/Nonunique Index: Specify UNIQUE to indicate that the
value of the column (or columns) upon which the index is based must be unique
– Composite: An index that is created based on multiple columns– Function-based index: An index that is created based on the value
of an expression
1-33 Bản quyền thuộc COMMIT., JSC.
Trang 36Managing Synonym
• Syntax:
– CREATE [PUBLIC] SYNONYM synonym FOR object;
– DROP SYNONYM synonym;
• Notes:
– PUBLIC: create a synonym that is accessible to all users
– Private synonym name must be distinct from all other objects that owned by the same user
– If a public synonym has same name with an object in an user’s schema, his object will be used prior to public synonym
1-36 Bản quyền thuộc COMMIT., JSC.
Trang 37Renaming Objects
• Syntax:
– RENAME old_name TO new_name;
1-37 Bản quyền thuộc COMMIT., JSC.
Trang 38Controlling Access
• Grant roles or system privileges
GRANT role|sys_priv [, role|sys_priv ]
TO user|role [, user|role ] [WITH ADMIN OPTION]
• Grant object privileges
GRANT obj_priv [, obj_priv ]
ON object[(column_list)]
TO user|role [, user|role ] [WITH GRANT OPTION]
• Revoke system privileges
REVOKE role|sys_priv [, role|sys_priv ]
FROM user|role [, user|role ]
• Revoke object privileges
REVOKE {obj_priv [(column_list)]}
ON object
FROM user|role [, user|role ] [CASCADE CONSTRAINT]
1-38 Bản quyền thuộc COMMIT., JSC.
Trang 401-40 Bản quyền thuộc COMMIT., JSC.