Está en la página 1de 2

sql_cheatsheet.html http://www.yagc.ndo.co.uk/cheatsheets/sql_cheatsheet.

html

Oracle Relational SQL Cheatsheet.

Types Creating and Deleting Tables


CHAR(n) CREATE TABLE
Fixed length string of character n.
CHARACTER(n) <table name> CREATE TABLE part
(<column definition (part_number CHAR(4), part_name
Character string of maximum length n, but
VARCHAR2(n) list>, VARCHAR(25),
of varying size.
(<column name>)); PRIMARY_KEY(part_number));
NUMBER Integers.
Numbers of precision p, with s digits after
NUMBER(p,s) CREATE TABLE CREATE TABLE department
the decimal point.
<table name> (department_number CHAR(4)
DATE Date information. ( <colum_definition CONSTRAINT prim_dept
TIME Time information. [ <constraint > ], .. PRIMARY_KEY,
BLOB Binary Large Object. ); department_name VARCHAR2(10))
CLOB Character binary large object. DROP TABLE
Delete table from database.
<table_name>;
NCLOB National character sets.
BFILE Read only external file. Changing Tables.
RAW/LONG RAW Binary data, used for import and export. ALTER TABLE<table_name> Creates a primary
ADD(CONSTRAINT <constriant_name> key constraint for
Conversions PRIMARY_KEY (<column_names>)); column.
to_char(x) ALTER TABLE
Converts it's argument to the appropiate
to_number(x) employee
type. ALTER TABLE <table_name>
to_date(x) ADD
ADD(<colmn_definition>);
to_multi_byte() Converts between single & multi byte (department_num
to_single_byte() international strings. VARCHAR(12));
chartorowid(x) Converts character strings to ROWID's & Creates a foreign k
rowidtochar(x) back. constraint for a
hextoraw(x) Converts between hex and RAW binary column, mapping t
ALTER TABLE <table_name>
rawtohex(x) format (see types). a column on anoth
ADD (CONSTRAINT <constraint_name>
table. Optional
Operators FOREIGN_KEY(<colum_name>)
DELETE CASCAD
=,>,<,>=, Usual comparisons. != & <> & ^= are all REFERENECES
maintains referent
<=,!=,<> negative equality tests. foriegn_table_name(<foreign_column_name>)
integrity by deletin
[ON DELETE CASCADE]);
AND OR NOT Boolean operations. rows in table when
SELECT emp_id, name, dept_no FROM row in foreign tabl
BETWEEN epmployee WHEREemp_id BEWTEEN1 AND is deleted.
4; ALTER TABLE
Relax constraint.
SELECT emp_id, name, dept_no FROM DISABLE CONSTRAINT name;
IN
epmployee WHERE emp_id IN (1,2,3,4); ALTER_TABLE Delete constraint
Regexp match. % = n characters, _ = 1 DROP CONSTRAINT<constraint_name>; forever.
LIKE
character, \ escapes. Modifying and deleting rows
Constraints INSERT
INSERT INTO employee
Allow/don't allow missing INTO<table_name>
NULL/NOT NULL (employee_number, employee_name,
values. (<colum_name, ..>)
salary)
[CONSTRAINT <constraint VALUES (<value,
VALUES ('7092', 'FORD', 175,66);
For candidate keys - ..>);
name>
alternatives to primary key. UPDATE
UNIQUE (<column_name>,..)]
<table_name> UPDATE wine_list
PRIMARY KEY This is the key field for lookup. SET note = ''Ideal as an aperitif'
SET <column> =
<value>, .. WHERE wine_name = 'Ch.Haut-Rian';

1 of 2 12/11/12 10:49 AM
sql_cheatsheet.html http://www.yagc.ndo.co.uk/cheatsheets/sql_cheatsheet.html

[CONSTRAINT WHERE
<constraint_name> Verification/validation. <condition>;
CHECK (condition)]; DELETE FROM
DELETE FROM members
This is an index to another <table_name>
FOREIGN KEY WHERE name
table. [WHERE
LIKE 'Sharon%';
Single Valued Functions <conditon>]
lpad(<string>,<width>, Pad a string to the right or left Querying with Select.
[<char>]); rpad(<string>, with the given width with the SELECT emp_table.emp_id,
<width>,[<char>]); given char. Project and Join. emp_table.dept_no, dept_table.description
lower(<string>); SELECT <columns> FROM employee_db.emp_table,
Uppercase, lowercase, or inital FROM <table> employee_db.dept_table
uppper(<string>);
upcase the string. WHERE <criterion>; WHERE emp_table.dept_no =
initcap(<string>);
dept_table.dept_no;
Returns length, in chars of the
length(<string>); SELECT *
string.
Sorting. FROM emp
substr(<string>,<start>, Returns a substring from start
SELECT .<clauses> ORDER BY empid DESC; -or- SELECT
<end>); index, to end index.
ORDER BY <column empid, lastname
abs(<number>)
Absolute value and sign number. [DESC|ASC],..>; FROM emp
sign(<number>) ORDER BY 2;
Ceiling and floor: Highest and Grouping.
ceil(<number>)
lowest integer with smallest SELECT SELECT dept, AVG(salary)
floor(<number>)
difference from float. <select_clauses> FROM emp
mod(<number0>, GROUP BY <column GROUP BY dept
<number1>) [DESC|ASC]..> HAVING avg(salary)>80000
Remainder of x / y; Round x to y
round(<number0>, HAVING ORDER BY avg(salary) DESC;
decimal places. Truncate x to y
<number1>) <criterion>;
decimal places
trunc(<number0>, Column
<<number1>) concatenation -
sqrt(<number>) Square root. formatting. SELECT firstname||','||lastname
greatest(<experession>,..) Largest and smallest from a listSELECT
of <column>|| full_name
least(<expression>,...) dates, numbers or strings. <string>||<column> FROM team;
<column_alias>...
vsize(<expression>) The storage size in bytes for x.
...;
sysdate() Current system date
On the fly
add_months(<date>, Add given number of month to calculations.
<integer>) dates; SELECT 7 * 9
SELECT
FROM DUAL;
last_day(<date>) Return the last day of the month.<expression> FROM
months_between(<date0>, Return the number of months DUAL;
<date1>) betwwen two dates/ Column aliasing.
SELECT name, NVL(spouse,'unmarried')
new_time(<date>, SELECT <column>
Convert date from one timezone AS spouse
<current_timezone>, AS
to another. FROM emp_db,emp_table;
<other_timezone>) <alias_column>..;
Substitute <value> for NULL in SELECT empid, dept, salary
nvl(<column>,<value>) Subqueries. FROM emp
the column.
SELECT ... WHERE dept = (
Return soundex string for fuzzy
soundex(x) WHERE column = SELECT dept
matching.
(<subquery>);, FROM emp
For every instance of <value> in WHERE empid = 78483);
decode(<column>,<value>,
column return the matching
<return>,<value>, Group functions.
<return> value. A bit like a
<return>...) Average of all numbers in column.
case/switch. avg()
Standard deviation f all numbers in
stddev()
column.
variance()
Variance of all numbers in column.
sum(x) Sum total of all numbers in the column.
count(x) Toal number of items in a culumn.
max(x) Maximum value found in a column.
min(x) Minimum value found in column.

2 of 2 12/11/12 10:49 AM

También podría gustarte