
Oracle学习笔记续
一:DDL(data definition language)语句管理表
-- 1.创建空间表
格式:/*
create tablespace 表空间名称
datafile 数据文件路径
size 100m
autoextend on
next 10m;
*/
-- 创建itcast001空间表
create tablespace itcast001
datafile 'e:/itcast001.dbf'
size 100m
autoextend on
next 10m;
-- 3.创建itcastuser用户
create user itcastuser001 identified by itcast001
default tablespace itcastuser001;
-- 4.为itcastuser001用户赋予dba权限
grant dba to itcastuser001;
-- 5.删除表空间并将物理文件一并删除
DROP tablespace heima58 including contents and datafiles;
drop user heima58 cascade
二:Oracle数据类型
数据类型 | 描述 |
---|---|
VARCHAR2(size) | 可变长字符数据 |
CHAR(size) | 定长字符数据 |
NUMBER(p,s) | 可变长数值数据 |
DATE | 日期型数据 |
LONG | 可边长字符数据,最大可达到2G |
CLOB | 字符数据,最大可以达到4G |
RAW ANG LONG RAW | 原始的二进制数据 |
BLOB | 二进制数据,最大可以达到4G |
BFILE | 存储外部文件的二进制数据,最大可以达到4G |
ROEID | 行地址 |
三:Oracle中的增删改,清空(TRUNCATE)
1注意:Oracle中的增删改和Mysql几乎一样,只是在Oracle中修改和删除后要手动commit提交
-- 修改表列的属性
-- 1.给person表增加sex性别列,类型为number(1)
ALTER TABLE person add sex number(1);
-- 2.修改person表列sex类型为char(1)
alter table person modify sex CHAR(1);
-- 3.修改person表的列sex为gender --alter table 表名 rename COLUMN 列名 to 新列名
ALTER table person rename COLUMN sex to gender;
-- 修改表名 alter table 表名 rename to 新表名;
-- 4.删除person表的gender列;
alter TABLE person DROP COLUMN gender;
-- 5.删除person表中的所有数据
DELETE from person WHERE 1=1;
COMMIT;
-- 6.摧毁person表中的所有数据
/*
直接摧毁表结构后重建表,比delete表要快得多,但是没办法按照条件删除数据 TRUNCATE
*/
-- 摧毁重建
TRUNCATE table person;
-- delete语句
delete from 表名称 where 列名称 = 值
四:约束
4.1主键约束
-- 创建person表,pid为主键,pname,gender (主键约束primary key)
--primary key方式
create table person(
pid number primary key,
pname varchar2(50),
gender number
)
-- constraint 主键名 primary key(字段),方式
create table person(
pid number,
pname varchar2(50),
gender number,
constraint PK_PID primary key(pid)
)
4.2非空约束
-- 创建person表,pname非空,gender(非空约束not null)
create table person(
pid number,
pname varchar2(50) not null,
gender number
)
insert into person (pid,gender) values (1,0);
commit;
4.3唯一约束
create table person(
pid number,
pname varchar2(50) unique,
gender number
)
insert into person (pid,pname,gender) values (1,'mwf',0);
commit;
4.4check 约束
-- 给gender添加check约束,只能是1或者0
create table person(
pid number,
pname varchar2(50),
gender number check(gender in(1,0))
)
insert into person (pid,pname,gender) values (2,'mwf',2);
commit;
4.4外键约束
-- 格式
/*
constraint fk_order_orderid foreign key(外键) reference 对应的表(对应的主键)
*/
-- 1.创建orders表,字段为order_id(主键),total_price
create table orders(
order_id number primary key,
total_price number(8,2)
)
-- 2.创建order_detail表,字段为detail_id(主键),order_id(外键),item_name
create table order_detail(
detail_id number primary key,
order_id number,
item_name varchar2(50),
constraint FK_ORDER_ORDER_ID foreign key(order_id) references orders(order_id)
)
-- 注意一下删除和增加的顺序
五:Oracle事物
-- 1.设置savepoint 回滚点,再次修改数据后用rollback to回滚点,回滚数据
select * from person;
update person set pname = 'minwenfei' where pid=1;
savepoint p1;
update person set pname = 'zbz' where pid=1
rollback to p1;
commit;
六:视图,序列,索引
6.1视图
grant dba to scott;
/*
视图就是一张虚拟表,本身不存数据,数据来源于原始表,就可以看成一张表,
创建视图:create [or replace] view 视图名 as sql查询语句;
*/
-- 1.创建视图
create or replace view emp_view as select * from emp;
-- 2.查询视图
select * from emp_view;
-- 3.修改用update删除用drop,跟对原始表的操作一模一样
-- 4.创建只读视图(关键字 with read only)
create or replace view emp_view as select * from emp with read only;
6.2序列
序列:类似于Mysql的自动增长
-- 创建序列
create sequence seq_test start with 5 increment by 2 maxvalue 2
-- 查询序列(currval,nextval)
select emp_sql.currval from dual;
-- 删除序列
drop sequence emp_seq;
6.3索引
索引的目的是提高检索的速度
/*
语法:create index 索引名称 on 表名(列名);
原则:大数据表才创建索引,
索引层数不要超过4层,也就是on后面的表名里面的字段名不要超过4个
*/
-- 创建索引
create index person_index on person(pname);
6.4同义词
/*创建同义词
create public synonym 同义词名 for 目标表名
删除
drop public synonym 同义词名
*/
create create public synonym my_orders for ITcast297.orders;
删除
drop public synonym my_orders;
七:数据库的导入导出
/*
全库导出:exp system/sys full=y file=expdat.dmp
全库导入:imp system/sys full=y file=expdat.dmp
按用户导出:exp scott/sys file=expdat.dmp
按用户导入:imp scott/sys file=expdat.dmp fully=y
*/
暂无评论...