关系型数据库管理系统RDBMS
Relational Database Management Systems
面向对象的关系型数据库管理系统ORDBMS
Object Relational Database Management Systems
数据定义语言DDL Data Definition Language
数据操纵语言DML Data Manipulation Language
数据控制语言DCL Data Control Language
主键(Primary Key)约束
外键(Foreign Key)约束
属性值上的约束(Null、Check、Create Domain)
全局约束(Create Assertions)
权限、授权(Grant)、销权(Revoke)
创建触发器(Create Trigger)
封装 Encapsulation 多态性Polymorphism
select */column_name(列名) from tablename(表名);--检索表
select column_name column_heading(列标题)tablename(表名);--显示友好列
select DISTNICT column_name from tablename;--显示非重复行即检索表列中唯一的值
并置运算符||
for instance: select "Antony"||"Martin" from dual;
the result: AntonyMartin
select select_list(选择列) from tablename where condition(条件);--显示特定条件的列
通配符Wild Card Character --%
定义标记符Position Marker Character --_
for example:
select column_name from tablename where column_name like'%a%a%'
the result:显示包含两个a字母的列
select column_name from tablename where column_name like'_s%'
the result:显示第二个字母为s列
select select_list from tablename where column_name IS NULL;--检索空值的列
select select_list from tablename where column_name ORDER BY column_name ASC(升序)/DESC(降序);
select function_name(函数名) (column_name/'character_expression')from tablename where condition;
Character函数
UPPER -- 以大写方式显示
LOWER--以小写方式显示
INITCAP--以大写方式显示所指字的首字母(只能用于'character_expression'返回一个值)
LTRIM--自左向右地删除输入串的字符(两个参数'character_expression')
RTRIM--自右向左地删除输入串的字符(两个参数'character_expression')
CONCAT('character_expression1','character_expression2')--将两个字符串并置
SUBSTR('character_expression',number1,number2)--从该函数的number1开始到number2抽取字符
INSTR('character_expression',single_character[单个字符])--返回指定字符在字符串中的位置值
LPAD('number_expression',number,'str')--将'str'左补差
RPAD('numer_expression',number,'str')--将'str'右补差
LENGTH('character_expression')--返回指定字符串的长度
Number函数
ABS-- 返回整形的正值
CEIL--返回大于参数的最小整数
FLOOR--返回数值的整数部分
POWER(num1,num2)--计算num1的num2的乘次幂
MOD(num1,num2)--返回num1除以num2的余数
ROUND(num1,num2)--num1四舍五入后保留num2位小数
TRUNC(num1,num2)--num1截断到num2位小数
for instance:
trunc(129.4576,2)
the result:129.45
trunc(129.4567,2)
the result:100
SQRT(number)--返回number的平方根
Date Functions
ADD_MONTHS('date',number)--显示date加上number后的结果,date中月份不能写成数字
GREATEST('date1','date2')--返回两个日期中较大的一个.
LEST('date1','date2')--返回两个日期中较小的一个.
LAST_DAY('date')--显示本月的最后一天日期
MONTHS_BETWEEN('date1','date2')--返回两个日期之间相隔的月份数('date1'-'date2'得到的值)
NEXT_DAY('date','month')--显示'date'的头一个'month'的日子
数据类型转换(ORACLE3.27)
隐式转换
显式转换
日期格式化函数
TO_CHAR(sysdate,'DD-MONTH-YYYY')--按'DD-MONTH-YYYY'显示系统日期
TO_DATE('date','DD-MONTH-YYYY')--以'DD-MONTH-YYYY'显示函数说明的日期(注意:此处与TO_CHAR的区别,TO_DATE年份显示两位数,而TO_CHAR显示四位数,TO_DATE中'DD-MON-YY'指的是10XX年,'DD-MON-RR'指的是20XX年)
ROUND('date','MONTH'/'YEAR')--指日期舍入到月/年的第一天
TRUNC('date','MONTH'/'YEAR')--指日期截断到月/年的第一天 ,到六就算过半
TO_NUMBER--将CHAR或VARCHAR数据类型转换成NUMBER数据类型
NVL(value,substitute)--列中所有空值由值substitute替代
COALESCE(exp_name1,exp_name2...exp_n)--返回前一个.
条件子句
IF boolean_expression/sql_statement/statement_block
ELSE boolean_expression/sql_statement/statement_block
CASE column_name
WHEN condition
THEN statement1
ELSE statement2
END
查询多表中数据
SELECT table1.column_name,table2.column_name FROM table1,table2 WHERE table1.column1 JOIN_OPERATOR table2.column2
SELECT table_name.column_name
FROM table_name
CROSS JOIN table_name
/NATURAL JOIN table_name
JOIN table_name USING column_name
/JOIN table_name ON(table_name.column_name JOIN_OPERATOR table_name.column_name)
/LEFT JOIN /RIGHT JOIN/OUTER JOIN table_name
ON (table_name.column_name JOIN_OPERATOR table_name.column_name)
等值联接 EQUIJOIN
非等值联接 NONEQUIJOIN
自然联接NATURAL JOIN
交叉联接CROSS JOIN
自联接SELFF JOIN
EQUIJOIN
SELECT column_name FROM table1 JOIN table2
ON table1.ref_column_name = table2.ref_column_name;
SELECT table1.column_name,table2.column_name
FROM table1,table2
WHERE table1.column1 = table2.column2;--显式实现
NONEQUIJOIN 联接其于多个表时,不使用等号.
NATURALJOIN 基于两个表的一个或多个同名列的操作.
如果两个表有同名列,就用USING子句
for instance:
select cEmployeecode,vFirstName,nAnnualSalary,nYear From Employee JOIN AnnualSalary USING(cEmployeecode);
外部联接
SELECT column_name,column_name... FROM table_name LEFT/RIGHT/FULL OUTER JOIN table_name
ON table_name.ref_column_name join_operator table_name.ref_column.name
LEFT OUTER JOIN --返回第一个表所有行和第二个表的匹配行.
RIGHT OUTER JOIN --返回第二个表所有行和第一个表的匹配行.
FULL OUTER JOIN --返回二个表所有行
自联接用于联接在同一个表内的行.表的某一行与同一个表中的其他行相关联.
多行函数/组合函数
AVG --返回N个值的平均值 SELECT AVG(column_name)FROM table_name WHERE condition;
SUM--返回N个值的和 SELECT SUM(column_name)FROM table_name WHERE condition;
MAX--返回一个列的最大值 SELECT MAX(column_name)FROM table_name;
MIN--返回一个列的最小值 SELECT MIN(column_name)FROM table_name;
COUNT--返回行数 SSELECT COUNT(column_name)FROM table_name WHERE condition;
STDDEV--返回一组值的标准偏差 SELECT STDDEV(column_name)FROM table_name;
VARIANCE--返回一组值的方差 SELECT VARIANCE(column_name)FROM table_name;
GROUP BY子句
SELECT column_name1,column_name2
FROM table_name
WHERE search_condition
GROUP BY group_expression(分组列名)
HAVING search_condition
子查询
SELCET outer_select_list
FROM outer_table_name
WHERE expression(SELECT inner_select_list FROM inner_table_name);
WITH 子句
for example:
WITH
EMP_SAL AS(SELECT cEmployeecode,SUM(nMonthlysalary)AS EMP_TOTAL FROM Monthlysalary GROUP BY cEmployeecode)
SELECT*FROM EMP_SAL WHERE EMP_TOTAL>(SELECT AVG(EMP_TOTAL)
FROM EMP_SAL);
相关子查询
SELECT outer_select_list FROM outer_tablename alias
WHERE expression(SELECT inner_select_list FROM inner_tablename
WHERE column_name=alias.column_name);
相关DELETE
DELETE FROM table1 alias1 WHERE expression
(SELECT column FROM table2 alias2
WHERE alias1.column=alias2.column);
创建表和SQL一样
更改表
ALTER TABLE tablename ADD
(columnname datatype [DEFAULT expression]);
修改列
ALTER TABLE tablename MODIFY
(columnname datatype [DEFAULT expression]);
撤消列
ALTER TABLE tablename DROP COLUMN columnname;-- 一次只能撤消一列,必须保证表至少还有一列.
重命名表
RENAME old_table_name TO new_table_name;
撤消表
DROP TABLE table_name;
更新表
UPDATE tablename
SET columnname=value
WHERE condition;
删除表中行
DELETE table_name WHERE condition;--用此方法能恢复.
TRUNCATE TABLE table_name;--用此方法无法恢复.
合并行--MERGE
MERGE INTO table_name table_alias
USING (table/sub_query/view)
ON(condition_join_exp)
WHEN MATCHED THEN
UPDATE SET
col_name1=Col_Name1,
col_name2=Col_Name2
WHEN NOT MATCHED THEN
INSERT (col_list)
VALUES(col_values);
约束
CREATE TABLE table_name(col1 data_type(width),col2 data_type(width), CONSTRAINT constraint_name
PRIMARY KEY/UNIQUE/CHECK/NOT NULL(col1_name)
[(FOREIGN KEY(col1_name)REFERNCES table_name1(Col_name1));]
添加约束
for instance:
ALTER TABLE candidate ADD CONSTRAINT fke_001 FOREIGN KEY(cEmployeecode) REFERENCES Employee (cEmployeecode);
ALTER TABLE candidate MODIFY (cEmployeeCode CONSTRAINT nke_001 NOT NULL);
撤消约束
ALTER TABLE table_name DROP CONSTRSINT constraint_name;
禁用和启用约束
ALTER TABLE table_name DISABLE/ENABLE CONSTRSINT constraint_name;
级联约束
ALTER TABLE table_name DROP(col)CASCADE CONSTRAINTS;
显式事务
SQL statement1;
SQL statement2;
COMMIT;--保证statement1,statement2所做的修改持久有效.
隐式事务
SHOW AUTOCOMMIT
SET AUTOCOMMIT ON--打开
SET AUTOCOMMIT value--设定对多少个语句有效.
回滚事务(9.13)
SQL statement1;
SQL statement2;
ROLLBACK;--丢弃自上一个COMMIT语句执行以来所做的数据修改.
保存点
SQL statement1;
SAVEPOINT savepoint_name;
SQL statement2;
ROLLBAKC TO savepoint_name;
创建视图
CREATE [FORCE|NOFORCE]VIEW view_name[alias]
AS SQLQuery
[WITH CHECK OPTION [CONSTRAINT constraint_name]]
[WITH READ ONLY [CONSTRAINT constraint_name]];
更改视图
CREATE OR REPLACE VIEW view_name
AS subquery;
重命名视图
RENAME old_view_name TO new_view_name;
撤消视图
DROP VIEW view_name;
Top-n分析法
SELECT ROWNUM,columnlist
FROM(SELECT columnlist FROM tablename ORDER BY Top-n_columnname)
WHERE ROWNUM<=N;
创建序列
CREATE SEQUENCE sequence_name
INCREMENT BY value
START WITH value
[MAXVALUE value|NOMAXVALUE]
[MINVALUE value|NOMINVALUE]
[CYCLE|NOCYCLE]
[CACHE|NOCACHE];
更改序列
ALTER SEQUENCE sequence_name
INCREMENT BY value
START WITH value
[MAXVALUE value|NOMAXVALUE]
[MINVALUE value|NOMINVALUE]
[CYCLE|NOCYCLE]
[CACHE|NOCACHE];
NEXTVAL和CURRVAL
for instance:
insert into college (cCollegecode,cCollegeName)values(EmpID_seq.nextval,'Scott Christian College');
select EmpId_seq.currval from dual;
撤消序列
DROP SEQUENCE sequence_name;
创建索引
CREATE[UNIQUE]INDEX index_name
ON table_name(column1,column2);
确认索引
SELECT index_name FROM USER_INDEXS WHERE table_name='EMPLOYEE';
撤消索引
DROP INDEX index_name;
创建公共同义词
CREATE PUBLIC SYNONYM synonym_name
FOR object_name;
创建私有同义词
CREATE SYNONYM synonym_name
FOR object_name;
撤消同义词
DROP SYNONYM synonym_name;
显示同义属性
DESC synonym_name;
创建用户
CREATE USER user
IDENTIFIED BY password;
授予用户访问权(12.5)
GRANT privilege[CREATE SESSION /TABLE /VIEW /SEQUENCE]
TO user;
GRANT object_priv(columns)
ON object
TO user/PUBLIC--所有用户
WITH GRANT OPTION;
修改用户密码
ALTER USER user
IDENTIFIED BY password;
PASSWORD;
OLD PASSWORD:password;
NEW PASSWORD:password1;
RETYPE NEW PASSWORD:password1;
撤消用户
DROP USER user;
废除用户访问
REVOKE privilege
FROM user/PUBLIC;
REVOKE privilege
FROM user/PUBLIC
CASCADE CONSTRAINTS;--除去与指定对象有关联的完整性约束
创建角色
CREATE ROLE role;
CTEATE ROLE role
IDENTIFIED BY password;
角色分配密码
ALTER ROLE role
IDENTIFIED BY password;
角色授予权限
GRANT privilege
TO role;
角色授权用户
GRANT role
TO user;
SET ROLE NONE;--冻结一个用户
撤消角色
DROP ROLE role;
替代变量--&
DEFINE/UNDEFINE
VERIFY
SET VERIFY ON
select vFirst_name from employee where cemployeecoede=&emp_cod;
COLUMN(14.10)--格式化
COL[UMN] column_name[option]
BREAK--生成报表,防止重复值
BREAK ON column_name;
TTITLE/BTITLE--生成页眉和页脚
TTITLE[text|OFF|ON]
BTITLE[text|OFF|ON]
DESCRIBE
[schema.]--对象模式
object
[@connect_identifier]--数据库连接名
COMPUTE--计算和打印概要结果
[function[LABEL]text]
OF{expr/column/alias}
ON{expr/column/alias|REPORT|ROW}