重建scott用户
@?/rdbms/admin/utlsampl.sql
@--执行
?--$ORACLE_HOME
字符区分大小写:
SELECT last_name, job_id, department_id
FROM employees
WHERE last_name = 'Whalen' ;
使用字符函数:
SELECT last_name, job_id, department_id
FROM employees
WHERE upper(last_name) = 'WHALEN' ;
SELECT last_name, job_id, department_id
FROM employees
WHERE lower(last_name) = 'whalen';
默认日期格式为"DD-MON-RR"
SELECT last_name
FROM employees
WHERE hire_date = '17-FEB-96' ;
Between...And等价于>= and <=
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500 ;
SELECT last_name, salary
FROM employees
WHERE salary >= 2500 AND salary <= 3500 ;
通配符:
%代表0个或者多个字符
_代表1个字符
create table t(name varchar2(10));
insert into t values('a');
insert into t values('ab');
insert into t values('abc');
insert into t values('abcd');
可以使用ESCAPE标识符来搜索%以及_符号.
insert into t values('ab_c');
insert into t values('ab%cd');
select *
from t
where name like '%\_%' escape '\';
And的优先级大于Or,可以使用小括号来改变优先级:
SELECT last_name, job_id, salary
FROM employees
WHERE job_id = 'SA_REP'
OR job_id = 'AD_PRES'
AND salary > 15000;
SELECT last_name, job_id, salary
FROM employees
WHERE (job_id = 'SA_REP'
OR job_id = 'AD_PRES')
AND salary > 15000;
Order by语句必须在所有子句后边,包括group by,having之后
The ORDER BY clause comes last in the SELECT statement;
按照降序排列:
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date DESC ;
按照别名排序
SELECT employee_id, last_name, salary*12 annsal
FROM employees
ORDER BY annsal ;
也可以按照表达式排序
按照列位置排序:
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY 3;
按照多列排序:
SELECT last_name, department_id, salary
FROM employees
ORDER BY department_id, salary DESC;
隐式排序
SELECT last_name, department_id, salary
FROM employees
ORDER BY first_name;
使用替代变量:
Temporarily store values with single-ampersand (&) and double-ampersand (&&) substitution
select * from emp
where empno=&no
使用DEFINE和UNDEFINE定义和取消变量:
DEFINE employee_num = 200
SELECT employee_id, last_name, salary, department_id
FROM employees
WHERE employee_id = &employee_num ;
UNDEFINE employee_num
SET VERIFY ON
SELECT employee_id, last_name, salary
FROM employees
WHERE employee_id = &employee_num;
SET VERIFY OFF