Oracle数据库实验

  1. 启动SQL*Plus工具,用system用户连接到orcl数据库。

    sqlplus system/12345678@orcl
    
  2. 进行用户切换,以SYSDBA身份连接数据库orcl。

    conn system/12345678@orcl as sysdba
    
  3. 首先使用记事本编写一个脚本文件(内容如下),然后分别在SQL*Plus中显示、执行该文件。
    以下为文件内容:

    Prompt 显示工资高于XX的员工信息
    Prompt 按<Enter键>继续
    Pause
    Accept value number prompt '请输入工资界限:'
    select empno,ename,sal from scott.emp where sal>&value
    
    start d:\1.sql
    
  4. 查询scott.emp表中的员工号(empno)与员工工资(sal),要求在员工工资数值前显示本地货币符号(RMB或¥)。

    column sal format L99999.99
    select empno,sal from scott.emp;
    
  5. 查询scott.emp表中员工信息,要求为查询页生成标题与标注。标题名称为“员工信息:”,显示居中;注脚为制作人:你的姓名。

    ttitle center '员工信息'
    btitle right '张三'
    select * from scott.emp;
    
  6. 显示环境变量autocommit的默认值,然后设置其值为on。

    show autocommit
    set autocommit on
    
  7. 为表空间USERS添加一个数据文件,文件名为userdata03.dbf,大小为50MB。

    alter tablespace USERS add datafile 'd:\userdata03.dbf' size 50M;
    
  8. 修改表空间USERS中的userdata03.dbf为自动扩展方式,每次扩展5MB,最大为100MB。

    alter database datafile 'd:\userdata03.dbf' autoextend on next 5M maxsize 100M;
    
  9. 修改表空间USERS中的userdata03.dbf更名为userdata04.dbf。

    alter database rename datafile 'd:\userdata03.dbf' to 'd:\userdata04.dbf';
    
  10. 将数据库的控制文件以二进制文件的形式备份。

    alter database backup controlfile to 'd:\orcl_ctlfile.bak';
    
  11. 为数据库添加一个重做日志文件组,组内包含两个成员文件,分别为redo4a.log、redo4b.log,大小均为5MB。

    alter database add logfile group 4('d:\redo4a.log','d:\redo4b.log') size 5M;
    
  12. 为新建的重做日志文件组添加一个成员文件,名称为redo4c.log。

    alter database add logfile member 'd:\redo4c.log' to group 4;
    
  13. 创建一个本地管理方式下的自动分区管理的表空间USERTBS1,其包含的数据文件20MB。

    create tablespace USERTBS1 datafile 'd:\usertbs1_1.dbf' size 20M extent management local autoallocate segment space management auto;
    
  14. 将表空间USERTBS1脱机然后进行联机操作。

    alter tablespace USERTBS1 offline;
    alter tablespace USERTBS1 online;
    
  15. 查询USERTBS1表空间及其包含的数据文件信息。

    select tablespace_name,datafile_name from dba_data_files where tablespace_name='USERTBS1';
    
  16. 按下列表结构利用SQL语句创建表student。
    Table name: student

    列名 数据类型 约束
    SNO NUMBER(4) 主键
    SNAME VARCHAR(10) 唯一
    SAGE NUMBER
    SEX CHAR(2)
    CNO NUMBER(2)
    create table student
    (SNO NUMBER(4) PRIMARY KEY,
     SNAME VARCHAR(10) UNIQUE,
     SAGE NUMBER,
     SEX CHAR(2),
     CNO NUMBER(2)
    );
    
  17. 为student表的SAGE列添加一个检查约束,保证该列取值在0~100之间。

    alter table student add constraint ck_sage check(sage between 0 and 100);
    
  18. 利用子查询分别创建一个事务级别的临时表,其结构与student表的结构相同。

    create global temporary table student_temp 
    on commit delete rows 
    as 
    select * from student;
    
  19. 创建一个student_range表(列、类型与student表的列、类型相同),按学生年龄分为三个区,低于20岁的学生信息放入part1区,存储在EXAMPLE表空间;20岁到30岁的学生信息放在part2区;存放在ORCLTBS1表空间中;其他数据放在part3区,存放在ORCLTBS2中。

    create tablespace ORCLTBS1 datafile 'd:\orcltbs1_1.dbf';
    create tablespace ORCLTBS2 datafile 'd:\orcltbs2_1.dbf';
    create table student_range 
    partition by range(sage)
    (partition part1 values less than(20) tablespace EXAMPLE,
     partition part2 values less than(30) tablespace ORCLTBS1,
     partition part3 values less than(maxvalue) tablespace ORCLTBS2
    )
    as 
    select * from student;
    
  20. 创建一个起始值为10000的序列,步长为2,最大值为100000,不可循环。

    create sequence example_seq increment by 2 start with 10000 maxvalue 100000 nocycle;
    
  21. 为SCOTT模式下的emp表创建一个公共同义词,名称为employee。

    create public synonym employee for SCOTT.emp;
    
  22. 创建一个口令认证的数据库用户usera,口令为usera,默认表空间为USERS,配额为10MB。

    create user usera identified by usera default tablespace USERS quota 10M on USERS;
    
  23. 为usera用户授予Create session、scott.dept的select权限和update权限,同时允许该用户将获得的权限授予其他用户。

    grant create session to usera with admin option;
    grant select,update on scott.dept to usera with grant option;
    
  24. 禁止用户usera 将获得的scott.dept的select权限和update权限再授予其他用户。

    revoke select,update on scott.dept from usera;
    grant select,update on scott.dept to usera;
    
  25. 创建角色rolea和roleb,将create table、scott.dept的insert和delete权限授予rolea;将connect角色授予roleb,并将rolea、roleb授予用户usera。

    create role rolea;
    create role roleb;
    grant create table to rolea;
    grant insert,delete on scott.dept to rolea;
    grant connect to userb;
    grant rolea,roleb to usera;
    
  26. 回收用户usera的rolea角色,并验证有效性。

    revoke rolea from usera;
    
  27. 为用户usera创建一个概要文件,限定该用户的最长会话时间为30分钟,如果连续10分钟空闲,则结束会话。同时,限定其口令有效期为20天,连续登陆4次失败后将锁定账户,10天后自动解锁。

    create profile example_profile limit connect_time 30 idle_time 10 password_life_time 20 failed_login_attempts 4 password_lock_time 10;
    alter user usera profile example_profile;
    

猜你喜欢

转载自blog.csdn.net/qq_44491553/article/details/111828785