Oracle_第三弹

来源:互联网 发布:凡科建站 源码 下载 编辑:程序博客网 时间:2024/06/15 02:49

点击下载 1-4 word版本

目 录

 

 

用户的创建    3

第一步:切换到system用户 因为他是dba    3

第二步:创建表空间    3

第三步:创建用户    3

第四步:给用户添加角色 (技能)    3

oracle的基本数据类型    3

1.字符型        3

2.数值        3

3.日期        3

4.大字段        3

约束    3

1.    主键 非空 唯一 检查    3

2.外键约束        4

3.自定义约束        4

表的操作(创建,删除,修改等)    4

创建表    4

删除表    4

复制表(包括数据)    4

表结构操作    4

数据操作    5

事物    5

事物保存点 savepoint    5

事务提交    5

数据库的视图    5

视图简介    5

视图语法    5

视图作用    5

视图的使用    5

数据库的序列    6

创建语法    6

获取值    6

使用    6

数据库的索引    6

简介    6

创建语法    6

单列索引    6

索引效率检验    6

复合索引    7

数据导入导出    7

前提    7

导出全量数据 (3星)    7

导出指定用户数据 (4星)    7

 

 

oracle_day3 CUD操作

用户的创建

  • 第一步:切换到system用户 因为他是dba

  • 第二步:创建表空间

    CREATE tablespace Myspace

    datafile 'c:/Muspace.dbf'

    SIZE 100M

    autoextend ON

    next 10M

  • 第三步:创建用户

    create user me

    identified by liqichen

    default tablespace Myspace

  • 第四步:给用户添加角色 (技能)

    grant dba to me

oracle的基本数据类型

  • 1.    字符型

    char (10)                --定长                例: aaa 长度10 效率高

    VARCHAR (10)        --可变 不推荐

    VARCHAR2 (10)     --可变 推荐 VARCHAR2 bei oracle 优化了 例:aaa 长度3

  • 2.    数值

    number(3)             --例: 999

    number(3,2)            --例: 9.99

  • 3.    日期

date             --日期+ 时间

timestamp     --更加精确的日期格式 秒后9位

  • 4.    大字段

long         --存2g

clob            --存4g

blob            --存4g        存的是2进制

 

约束

  • 1.    主键 非空 唯一 检查

    create table person(

pid number(10) primary key,

pname varchar2(20) not null,

phone varchar2(20) unique,

gender number(1) check (gender in(0,1)) ---0 女 1 男

    )

  • 2.    外键约束

    • 主表

    create table orders(

                 ooid number(10) primary key,

                 ooname varchar2(20)

    )

  • 从表

    create table order_detail(

                    odid number(10) primary key,

                    odname varchar2(20),

                    ooid number(10) ,

                    constraint fk_ooid foreign key(ooid) references orders(ooid)

    )

  • 放弃外键约束

    alter table order_detail drop constraint fk_ooid;

 

  • 3.    自定义约束

    create table person(

pid number(10),

pname varchar2(20) not null,

phone varchar2(20) ,

gender number(1) , ---0 女 1 男

constraint pk_person primary key(pid),    --自定义

constraint uk_phone unique(phone),

constraint ck_gender check (gender in(0,1))

    )

 

表的操作(创建,删除,修改等)

  • 创建表

create table person(

    pid number(10),

    pname VARCHAR2(100)

)

  • 删除表

drop table person;

drop 表在创建表

truncate table person;

  • 复制表(包括数据)

create table u_emp as select * from scott.emp;

 

  • 表结构操作

    • 增加列

    alter table person add sex VARCHAR2(1);

    • 修改列

    alter table person rename COLUMN sex to gender

    • 删除列

    alter table person DROP COLUMN gender

  • 数据操作

    • 插入数据

    --全列

        INSERT INTO person VALUES(1,'啦啦','1')

    --选列

        insert into person(pid,pname) VALUES('2','尺')

  • 更新数据

    update person set pname='库里' where pid=2

  • 删除数据 责任重大 谨慎删除

    delete from person where pid=2

事物

  • 事物保存点 savepoint

update person set pname='啦啦' where pid =1;

savepoint a;

update person set pname='露露' where pid =1;

savepoint b;

update person set pname='陈冠希' where pid=1;

savepoint c;

 

rollback to a;

 

  • 事务提交

commit;

 

数据库的视图

  • 视图简介

    用来查询 view是封装了一段sql语句的对象

  • 视图语法

    create VIEW 视图名称 as sql语句

  • 视图作用

  1. 简化复杂查询 2.隐藏敏感信息
  • 视图的使用

    • 简化复杂查询

        create view t_view as SELECT * from person

        select * from t_view

  • 隐藏敏感信息

        create view t_view2 as select empno,ename,job ,mgr from scott.emp

        SELECT * from t_view2

  • 只读视图设置 with read only

        create view t_view3 as select empno,ename,job,mgr from scott.emp with read only;

        select * from t_view3

        UPDATE t_view3 set job='a' where empno=7369 --权限不足

 

数据库的序列

  • 创建语法

create sequence 序列名称

    例子:

        create sequence seq_person;

  • 获取值

--获取当前值

select seq_person.currval from dual

--获取下一个值

select seq_person.nextval from dual;

  • 使用

insert into person (pid,pname) values(seq_person.nextval,'wang5');

 

 

数据库的索引

  • 简介

相当于目录 大幅度提高查询的效率的 对象

  • 创建语法

    create index 索引名 on 表名 (列)

  • 单列索引

    create index in_person on person(pname);

    select * from person

 

  • 索引效率检验

    • 创建500万数据

    create table stu (

stuid number(10),

stuname varchar2(50)

);

begin

for i in 1..5000000

loop

insert into stu values(i,'agcded'||i);

end loop;

commit;

end;

  • 前查询

select count(*) from stu --1.403

  • 创建索引

create index in_stu on stu (stuid)

  • 后查询

select * from stu where stuid=40000 --0.030

  • 查询时 索引优先

 

  • 复合索引

    • 语法

    create index 索引名 on 表 (列1,列2)

    • 什么情况触发 复合索引 (重点)

    效率高

    select * from biao where 列1=xxx and 列2=xxx;

    效率低 不走索引

    select * from biao where 列2=xxx and 列1=xxx;

 

  • 索引会影响 插入和删除 修改的效率

 

 

数据导入导出

  • 前提

所有 cmd 导入 导出的命令 需要在虚拟机中(oracle安装的机器上) 运行

  • 导出全量数据 (3星)

--exp system/orcl full=y file=c:/xxx.dmp

  • 导出指定用户数据 (4星)

---exp system/orcl owner=scott file=c:/xxx.dmp

 

--导入 imp system/orcl file=c:/xxx.dmp fromuser=scott touser=itcastu

原创粉丝点击