Oracle学习笔记(一)
Oracle学习笔记(一)
一.介绍
Oracle数据库: DB / DBMS
数据库的使用者: DBA(Database Administrator) / 开发人员
数据库的种类:
oracle / sybase / db2(IBM)
sql server / mysql /
access
数据表:行和列组成.
行: row / record
列: column / field
SQL分类:
DQL: select
DML: insert / update / delete
DDL: create / drop / alter / truncate
DCL: grant / revoke
TCL: commit / rollback / savepoint
远程登录到Oracle数据库服务器的方式:
C:>telnet IP Add
用户名/密码: username/owd
这组是远程机器的用户名和密码
远程登录后连接数据库的方式:sqlplus
sqlplus是Oracle数据库提供的命令行客户端工具
sunv210% sqlplus openlab/open123
这组是数据库的用户名和密码
了解练习常用到的数据表:
emp: 职员表
dept: 部门表
salgrade: 薪水等级表
desc命令:查看一个表的结构.
desc dept
desc emp
desc salgrade
数据类型:
数字类型:number(p,s)最长p位,小数点后s位
字符类型:char(n) / varchar2(n)
日期:date
number(7,2) 99999.99
number(5) 99999
number 随便
二.SQL
-- *号表示查询全部列
select * from dept;
select * from emp;
select ename, sal, comm from emp;
列别名
列的算术表达式
连接字符串||
处理空值的函数nvl
select ename, deptno, sal from emp
where deptno = 10;
绑定变量&
select ename, deptno, sal from emp
where sal > &salary;
select ename, job from emp
where job = 'MANAGER';
....
sal between low and high;--闭区间
sal >= low and sal <= high
[low, high]
....
where deptno in (10, 20);
....
where ename like '_A%';
like短语易做图殊字符的处理: escape
select ename from emp
where ename like 'A\_%' escape '\';
select ename, comm from emp where comm is not null;
取非
select ename, sal from emp
where deptno not in (10, 20);
where A and B or C;
where A and (B or C);
select ename, sal, deptno
from emp
where sal > 1000
and deptno = 10
or deptno = 20;
select ename, sal, deptno
from emp
where sal > 1000
and (deptno = 10
or deptno = 20);
排序:
select ename, job from emp
order by ename; --正序
select ename, sal from emp
order by sal desc; --倒序
select ename, sal, deptno
from emp
order by deptno, sal desc;
select....
from ...
where ...
order by...
单行函数
虚表dual(Oracle特有的)
select upper('Sql Course') from dual;
Oracle数据库的用户:
sys
system
scott/tiger
openlab/open123
字符函数
upper / lower / initcap / substr / length / lpad / rpad / replace / trim
数字函数
round / trunc / mod
select trunc(45.678, 2) from dual;
select trunc(45.678, 0) from dual;
select trunc(45.678, -1) from dual;
select mod(16,5) from dual;
日期
取系统时间: sysdate(Oracle独有的函数)
select sysdate from dual;
日期 +/- n = n天以后/前
--to_char是转换函数,将日期转换为字符类型
select to_char(sysdate,
'yyyy-mm-dd hh24:mi:ss')
from dual;
select to_char(sysdate,
'year month dd day d hh12 am') from dual;
select to_char(sysdate,
'yyyy"年"mm"月"dd"日"') from dual;
select sysdate from dual;
默认的日期格式是: DD-Mon-RR
假设现在是2011年:
RR YY(直接取当前日期的前两位)
05 2005 2005
99 1999 2099 <---
假设现在是1998年:
RR YY
05 2005 1905 <--
95 1995 1995