Common command in SQL: select, inert, update, delete, create, drop
DML(Data Manipulation Language): use to query or modify data
INSERT, UPDATE, DELETE
DDL(Data Definition Language): use to define data structure, such as create/update/delete database object
CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE INDEX, DROP INDEX
DCL(Data Control Language): use to define the privilege of user in database
ALTER PASSWORD, GRANT, REVOKE, CREATE SYNONYM
TCL(Transaction Control Language): use to control transaction
COMMIT, SAVEPOINT, ROLLBACK
DQL(Data Query Language):
SELECT, ORDER BY, GROUP BY
SELECT 'column1'[, 'column2', etc] FROM 'tablename' [WHERE 'condition'];
LIKE 'test%', '%test', 'te%st'
CREATE TABLE 'tablename' (
'column1', 'data_type' [constraint],
'column2', 'data_type [constraint]'
);
CREATE TABLE employee (
First varchar(15),
Last varchar(20)
);
INSERT INTO 'tablename' (1column, 2column,…) VALUES(1value, 2value, …);
DROP TABLE 'employee';
UPDATE 'tablename' SET 'column1' = 'new value' WHERE 'columnname' OPERATOR 'value';
DELETE FROM 'employee' WHERE 'column1' OPERATOR 'value';
SELECT [ALL|DISTINCT] column1[, column2]
FROM table1[, table2]
[WHERE 'conditions']
[GROUP BY 'column-list']
[HAVING 'conditions']
[ORDER BY 'column-list' [ASC|DESC]];
Functions:
MIN, MAX, SUM, AVG, COUNT, COUNT(*)
GROUP BY:
SELECT column1, SUM(column2) FROM 'list-of-tables' GROUP BY 'column-list';
SELECT MAX(sqlary), dept FROM employee GROUP BY dept; //查询出每个部门的平均工资
HAVING:
SELECT column1, SUM(column2) FROM 'list-of-tables' GROUP BY 'column-list' HAVING 'condition';
HAVING子句允许你为每一个组指定条件,换句话说,可以根据你指定的条件来选择行。如果你想使用HAVING子句的话,它应该处于GROUP BY子句之后。
SELECT dept, AVG(salary) FROM employee GROUP BY dept HAVING AVG(salary) > 20000; //查询平均工资大于20000的部门
ORDER BY:
SELECT column1, AVG(column2) FROM 'list-of-tables' ORDER BY 'column-list' [ASC|DESC];
ORDER BY是一个可选的子句,他允许你根据指定要order by的列来意上升或下降的顺序来显示查询的结果
SELECT employee_id, dept, name, age, salary FROM employee_info WHERE dept='sales' ORDER BY salary;
// 从employee_info表中列dept等于sales,并根据他们的salary按升序来列出检索结果;
SELECT employee_id, dept, name, age, salary FROM employee_info WHERE dept='sales' ORDER BY salary, age DESC;
// 从employee_info表中列dept等于sales,并根据他们的salary和age按降序来列出检索结果;
IN, BETWEEN:
SELECT column1, SUM(column2) FROM 'list_of_tables' WHERE column3 IN('Hernandez', 'Jones', 'Roberts', 'Ruiz');
SELECT column1, SUM(column2) FROM 'list_of_tables' WHERE column3 BETWEEN column3 BETWEEN value1 AND value2;
数学运算符:
=, -, *, /, %,
ABS(x)-绝对值,
SIGN(x)-符号判定,
MOD(x, y)-x%y,
FLOOR(x)-返回小于等于x得最大整数,
CEILING(x)-返回大于等于x得最小整数,
POWER(x, y)-返回x的y次方的数值
ROUND(x)-返回最接近于x的整数,四舍五入,
ROUND(x, d)-返回小数点为4的接近于x的数,
SQRT(x)-返回x的平方根;
JOIN:
SELECT c_info.firstname, c_info.lastname, p.item FROM customer_info c_info INNER JOIN purchases p ON c_info.customer_number = p.customer_number;
索引:
CREATE INDEX IOD_IDX ON ANTIQUEOWNERS(OWNERID);
CREATE UNIQUE INDEX name_idx ON owners(ownerlastname, ownerfirstname);
DROP INDEX OID_IDX;
DISTINCT-去除重复的结果:
SELECT DISTINCT sellerid, ownerlastname, ownerfirstname
FROM antiques, antiqueowners
WHERE sellerid = ownerid ORDER BY ownerlastname, ownerfirstname , ownerid;
Alias, in
SELECT own.ownerlastname Last Name, ord.itemdesired Item Ordered
FROM orders ord, antiqueowners own
WHERE ord.ownerId = own.ownerId
AND ord.itemdesired IN(SELECT ITEM FROM QNTIQUES);
EXISTS, ALL
SELECT ownerfirstname, ownerlastname FROM antiqueowners WHERE EXISTS( SELECT * FROM antiques WHERE ITEM = 'Chair'); |
SELECT ownerfirstname, ownerlastname WHERE antiqueowners WHERE price >= ALL( SELECT price FROM antiques); |
UNION和外部链接
SELECT buyerid FROM antiqueowners
UNION
SELECT buyerid FROM orders;
Data Type:
CHAR(length):存储固定长度的字符串,默认长度是1,最长不超过2000字节
VARCHAR2(length):存储可变长度的字符串,默认长度是1,最长不超过4000字节
NUMBER(p, s);
DATE;
TIMESTAMP,
CLOB:存储大的文本,比如存储非结构化的XML文档
BLOB:存储二进制文件,比如图形,视频,声音等
创建表和约束:
创建约束:ALTER TABLE 表名 ADD CONSTRAINT 约束名约束内容
根据结果集创建表:CREATE TABLE 表名 AS SELECT 语句
CREATE TABLE infos1 AS SELECT * FROM infos;
复制表结构:CREATE TABLE infos2 AS SELECT * FROM infos WHERE 1 = 2;
算术运算符:
SELECT ename, sal, (sal*2 + 2000) FROM emp WHERE sal > 2000;
关系运算和逻辑运算: AND, OR, NOT
字符串连接操作符(||):
SELECT (ename || ' is a ' || job) AS "Employee Details"
高级查询:
消除重复行:DISTINCT
NULL操作:如果某条记录中又缺少的数值,就是空值(NULL)。空值不等于0或者空格。
SELECT ename, job, sal, comm FROM emp WHERE emp.comm IS NULL; //非NULL值用NOT IS NULL
LIKE: LIKE '字符串'[ESCAPE '字符'] //
% - 表示零个或者多个任意字符,_ - 代表一个任意字符
集合运算:
INTERSECT(交集),返回两个查询共有的记录
UNION ALL(并集),返回各个查询的所有记录,包括重复记录
UNION(并集),返回各个查询的所有记录,不包括重复记录
MINUS(补集),返回第一个查询检索出的记录减去第二个查询检索出的记录之后剩余的记录。
连接查询:
INNER JOIN, OUTER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN
总结: