1.安装Oracle:
1.1 命名全局数据库
1.2 设置sys,system的密码,安装完成
1.3 启动服务:(1)OracleOraDb10g_home1TNSListener,(2)OracleServiceXXX
2.简单命令:
2.1 conn 用户名/密码[@<connect_identifier>] (SYS should as SYSDBA or SYSOPER)
2.2 create user 用户名 identified by 密码;(DBA 才能创建用户,密码不能用数字开头)
2.3 grant connect to 用户名;
2.4 grant resource to 用户名;
2.5 show user;
2.6 disconnect;
2.7 对口令进行管理:创建一个profile(一个口令规则)。
2.8 alter user scott account unlock;解锁scott
2.9 alter user scott identified by 密码;修改密码
3.表的管理
3.1 表名字母开头
3.2 不能超过30个字符
3.3 不能使用Oracle保留字
3.4 Oracle数据类型
char 定长 最长2000字节 (虽然占用空间,但查询速度很快)
varchar2 变长 最长4000字节 (节省空间,但速度慢)
clob 字符型大对象 最大4G (4000不够用就用clob)
number 可以表示整数,也可以表示小数
number(5,2) 5位有效数字,2为小数
number(5) 5为整数
date 包括年月日时分秒
timestamp 对date的扩展(更精确)
blob 二进制数据,可存图片、声音 最大4G
3.5 建表
create table student(
id number(4),
name varchar2(20),
sex char(2),
birthday date,
sal number(7,2));
4.表的查询
4.1 nvl(col,0)函数的意思:如果col为null,则用0来替代。
4.2 like %:0到多个字符,_:任意单个字符。
4.3 多表查询,查询条件不能少于表的个数减1。
4.4 select worker.ename,boss.ename from emp worker,emp boss where worker.mgr = boss.empno and worker.ename = 'FORD';(自连接,查询FORD的老板姓名)
4.5 多行子查询:in,all,any。
4.6 多列子查询:select * from emp where (deptno,job) = (select deptno,job from emp where ename='SMITH');
4.7 在from中使用子查询时必须给内嵌视图使用别名。(给表取别名别加as)
4.8 分页查询:select * from (select nt.*, rownum rn from (select * from emp) nt where rownum <= 10) where rn >=6;(查询rownum为6-10的数据,rownum是oracle提供的行号字段)
(
说明:
1.查询满足条件的数据:select * from emp
2.查询rownum<=10的记录,将查询结果作为下一次被查询内嵌视图
3.查询内嵌视图nt,查询rownum>=6的记录。
如需查询 指定列,排序,只需修改最底层的查询。
)
4.9 create table tableName (id,username,sal) as select EMPNO,ENAME,SAL from emp;(实用查询创建表)
5.创建新的数据库
5.1 使用工具向导 Database Configuration Assistant
6.表空间管理
6.1 建立表空间:create tablespace sp001 datafile 'd:\sp001.dbf' size 20m uniform size 128k
6.2 使用表空间:create table mt(id number(5),username varchar2(20)) tablespace sp001;
6.3 改变表空间状态:
6.3.1 表空间脱机:alter tablespace 表空间名称 offline;
6.3.2 表空间联机:alter tablespace 表空间名称 online;
6.3.3 表空间只读:alter tablespace 表空间名称 read only;
6.3.4 表空间读写:alter tablespace 表空间名称 read write;
6.4 删除表空间:drop tablespace '表空间名称' including contents and datafiles;
6.5 扩展表空间:
6.5.1 alter tablespace sp001 add datafile 'd:\sp002.dbf' size 20m;
6.5.2 alter database datafile 'd:\sp001.dbf' resize 20m;(大小不要超过500m)
6.5.3 alter database datafile 'd:\sp001.dbf' autoextend on next 10m maxsize 500m;
6.6 移动数据文件:
1.使表空间脱机:alter tablespace sp001 offline;
2.移动文件:host move d:\sp001.dbf c:\sp001.dbf
3.逻辑修改:alter tablespace sp001 rename datafile 'd:\sp001.dbf' to 'c:\sp001.dbf';
4:使表空间联机:alter tablespace sp001 online;
7.约束
例子:
create table goods(
goodsid char(3) primary key,
goodsname varchar2(50),
unitprice number(10,2) check(unitprice > 0),
category varchar2(8),
provider varchar2(50));
create table customer(customerid char(3) primary key,
name varchar2(20) not null,
address varchar2(100),
email varchar2(50) unique,
sex char(2) default '男' check(sex in ('男','女')),
cardid char(18));
create table purchase (customerid char(3) references customer(customerid),
goodsid char(3) references goods(goodsid),
num number(10) check (num between 1 and 50));
概念理解:
1.数据库(一个实例)
2.表空间(数据库的逻辑组成,由一个或多个文件组成。<表空间:段,区,块>)