Sie sind auf Seite 1von 4

Oracle SQL Expert Notes (IZ0 - 047) Group Functions DISTINCT and ALL can be used with aggregate

functions A is less then Z and Z is less than a select rank(300) within group (order by sq_ft) from ship_cabins - provides the rank if a row with sq_ft 300 is introduced in the table select max(sq_ft) keep (dense_rank {first last} order by guests) from ship_cabin s Aggregate functions can be nested up to two levels and a select list should hav expressions at the same level of aggregation GROUP BY and HAVING must follow WHERE (if included) and should precede ORDER BY SELECT a,b,c FROM ships a {inner} join ports ON x=y ORDER BY a; A table prefix is not allowed on a join column in a natural join. In a natural join, there is no ON clause USING clause can be used by both inner and outer joins (..FROM ....USING (A+)) CROSS JOIN SYNTAX - select * from A,B OR select * from A CROSS JOIN b Scalar subqueries are forbidden in GROUP BY and HAVING clauses, in CHECK constra ints and in function-based indexes TIMESTAMP WITH TIME ZONE, BLOB and CLOB columns can never have UNIQUE, PRIMARY K EY or FOREIGN KEY constraints against them Whenever you use the keyword CONSTRAINT, you have to name the constraint Tables, Views, Sequences, Private Synonyms form a part of the the same namespace while constraints, indexes are part of their own namespace. A NOT NULL constraint can never be defined out of line FROM keyword is optional in the DELETE statement ALTER, CREATE, DROP, GRANT, REVOKE all perform implicit commits (DDL s) SAVEPOINT XYZ;ROLLBACK {WORK} TO XYZ; A column alias can only be used in a ORDER BY clause (not in HAVING, GROUP BY or WHERE) NULL values sort higher than other values undo_retention - time in seconds as to how long data is retained (select name,va lue from v$system_parameter where name= undo_retention ) FQ - select * from A as of SCN or TIMESTAMP (scn_expression timestamp expressi on) FVQ - select * from A versions between {TIMESTAMP SCN} A and B FVQ - other columns of use - minvalue, maxvalue (of timestamp), versions_startsc n, versions_endscn,versions_operation,versions_xid versions_endtime is not inclusive Flashback Transaction Query (FTQ) - select undo_sql from flashback_transaction_ query where xid={$version_xid - from the FVQ}

INSERT all WHEN mod(i,2)=0 then INTO even values(i) WHEN mod(i,2) ^= 0 then INTO odd values(i) SELECT t1 as i from t; Table aliases used in sub query cannot be referenced in the when or into clause ..use column aliases instead. Can have an optional ELSE clause at the end - for a default

MERGE into table USING table subquery ON condition WHEN matched then {update set col=? {where expression} WHEN not matched then insert() values(?,?) where expr where condition

delete}

SELECT EMPLOYEE_ID,LEVEL{,SYS_CONNECT_BY_PATH(TITLE, / )}{,CONNECT BY ROOT COL AS X} FROM EMPLOYEE START WITH EMPLOYEE_NAME= CEO CONNECT BY PRIOR EMPLOYEE_ID=MANAGER_ID {AND X CONDITION Y - to EXCLUDE branches } {ORDER SIBLINGS BY COL} (here MANAGER_ID refers to the child column..i.e the child rows manager_id colum n has the same value as the parent (i.e current rows) employee_id column) FLASHBACK TABLE HOUDINI TO {BEFORE DROP SCN scn_exp TIMESTAMP tstamp_exp RES TORE_POINT rstr_pnt_exp} {RENAME TO new_name} SELECT * FROM RECYCLEBIN (USER_RECYCLEBIN) REGEX_INSTR => X, pattern, start, occurence, return_option (0 start of pattern in X, 1= first index after pattern in X) REGEX_REPLACE => X, pattern,replace_string, start, occurence REGEX_SUBSTR => X, pattern, start, occurence REGEX_LIKE => X, pattern REGEX_COUNT => X, pattern case <expression> when <condition1> then expr-1 when <condition2> then expr-2 else expr-3 end GROUPING(COL) returns 1 for superaggregates and 0 for others GRANT CREATE {SEQUENCE SYNONYM SESSION TABLE ANY TABLE ER VIEW ROLE} TO {user role} GRANT DROP {TABLE ANY TABLE USER} TO {user role} REVOKE CREATE {SEQUENCE SYNONYM SER VIEW} FROM {user role} SESSION TABLE ANY TABLE PROCEDURE US

PROCEDURE

system privileges once granted by A to B, followed by a revoke on A with still r emain with B GRANT EXECUTE ANY PROCEDURE { WITH ADMIN OPTION} GRANT {SELECT UPDATE{COL1, COL2..} INSERT DELETE} ON table TO [user] {with G RANT OPTION}; REVOKE {SELECT UPDATE{COL1, COL2..} INSERT DELETE} ON table FROM [user]; CREATE {PUBLIC} SYNONYM synonym_name FOR object_name; unlike system_privileges, object privileges are cascading Tables =============== USER_SYS_PRIVS USER_TAB_PRIVS USER_COL_PRIVS USER_ROLE_PRIVS

ROLE_SYS_PRIVS ROLE_TAB_PRIVS USER_CONSTRAINTS USER_TAB_COMMENTS USER_COL_COMMENTS USER_INDEXES =============== COMMENT ON TABLE table_name IS 'comment here' COMMENT ON COLUMN table_name.column_name IS 'comment here' CREATE INDEX index_name on table_name CREATE INDEX index_name on table_name(expr) - function based index CREATE VIEW view_name AS subquery {WITH CHECK OPTION CONSTRAINT CONSTRAINT cons_name} WITH READ ONLY

A subquery may not contain a order by clause. All ordering should be done in a o uter query. You can nest subqueries to a depth of 255 INSERT INTO tablename(col1,col2...) SELECT A,B..FROM table WHERE condition When a table is retrieved from recycle bin, all indexes, constraints (with the e xception of referential constraints) are also retrieved CREATE RESTORE POINT balance_acct_01; (can be queried from v$restore_point) FLASHBACK TABLE HOUDINI TO RESTORE POINT balance_acct_01; DROP RESTORE POINT balance_acct_01; External tables cannot have LOB data type columns, cannot have constraints, cann ot have columns set as UNUSED (column will be immediately dropped) CREATE {OR REPLACE} DIRECTORY BANK_FILES AS 'F:\bnk_files\trnsfr'; GRANT READ ON DIRECTORY directory_name TO username; 01 CREATE TABLE INVOICES_EXTERNAL 02 ( INVOICE_ID CHAR(3), 03 INVOICE_DATE CHAR(9), 04 ACCOUNT_NUMBER CHAR(13) 05 ) 06 ORGANIZATION EXTERNAL 07 (TYPE ORACLE_LOADER 08 DEFAULT DIRECTORY INVOICE_FILES 09 ACCESS PARAMETERS 10 (RECORDS DELIMITED BY NEWLINE 11 SKIP 2 12 FIELDS (INVOICE_ID CHAR(3), 13 INVOICE_DATE CHAR(9), 14 ACCOUNT_NUMBER CHAR(13)) 15 ) 16 LOCATION ('INVOICE_DATA.TXT') 17 ); alter table table_name set unused column (column_name1,column2..);alter table ta ble_name drop unused columns alter table drop primary key {cascade keep index drop index}

alter table alter table cannot drop alter table ) on delete

drop unique(col1,col2,col3...) {cascade keep index drop index} drop constraint cons_name not null constraint, instead use alter table modify .. add constraint cons_name foreign key(..) references table_name(..,.. {cascade set null}

CTAS - Only explicity create not null constraints are picked up Multitable Insert - Table aliases from subquery wont work outside it - use colum n aliases instead Order by cannot be used by any but the last select statement while using SET ope rations In set operations you can use order by using position or reference SELECT CURRENT_TIMESTAMP(6) from DUAL; - returns session time (TIMESTAMP WITH TI ME ZONE) SELECT LOCALTIMESTAMP(6) from DUAL; - reurns session time (TIMESTAMP) SELECT SYSTIMESTAMP FROM DUAL; - DBtime (TIMESTAMP WITH TIME ZONE) CURRENT_TIMESTAMP(6) -------------------06-SEP-10 05.41.44.548618000 PM ASIA/CALCUTTA 1 rows selected LOCALTIMESTAMP(6) ------------------------06-SEP-10 05.41.45.401851000 PM 1 rows selected SYSTIMESTAMP ------------06-SEP-10 12.11.46.211798000 PM +00:00 1 rows selected FROM_TZ - Takes a TIMESTAMP and converts to TIMESTAMP WITH TIME ZONE TO_TIMESTAMP_TZ - from String representation to TIMESTAMP WITH TIME ZONE CAST(X AS Y) AT TIME ZONE - GROUP BY is processed before select list and hence cannot have column aliases

Das könnte Ihnen auch gefallen