PL&SQL学习笔记1

2020-03-27 09:00

start d:\\a.sql @ d:\\a.sql

edit [d:\\a.sql]

spool d:\\a.sql 将屏上内容 输出到指定文件 spool off

set linesize 320 set pagesize 100 set autoprint on set serveroutput on variable v1 refcursor

exp imp 备份 恢

只读事务

set transaction read only

用处,设置只读事务后,其他用户提交的事务在这里不可见,用处就是用于统计,但又不想取得统计时发生的事务提交

用户管理 : 登陆:

sqlplus scott/tiger

sqlplus system/root as sysdba

system sys sysdba scott;; sys 超级管理员, 具有角色dba ; system 是系统管理员,角色dbaoper ,比sys 低一级,没有create database 权限

创建用户 create user jixiuf identified by jixiuf_passwd;

create user userName identified by yourPasswrod default tablespace users temporary tablespace temp quota 50M on users quota 400K on temp;

create user userName identified by yourPasswrod default tablespace ts1 temporary tablespace ts2 unlimited on ts1 ;

切换用户 conn system/root; disconnect 显示当前用户 show user

更改密码 password userName

删除用户:drop user jixiuf [cascade] ,如果 jixiuf 用户已经创建过一些表,加cascade 级联删除

权限分系统权限和对象权限,系统权限是用户对数据库的控制权,对象权限是用户对其它用

户所拥有数据对象的操作权限

对象权限 :如select ,update delete ,create index 系统 权限 :如create session 即连接到数据库

grant [系统特权名][角色] to [用户名列表 ][public] [with admin option ]

grant connect to jixiuf ; 角色connect赋予jixiuf 此用户可以连接到数据库 connect,resource,dba 三个重要角色 ,拥有resource 可以在表空间建表, grant resource to jixiuf

授权:

grant select on tableName to jixiuf[ with grant option]; 具有了select * from userName.tableName

grant select,update,delete on userName.tableName to jixiuf ; grant all on userName.table to jixiuf; 增删改查权 如果是对象权限 可以加with grant option , 若是系统权限 则带with admin option 如: grant connnect to jixiuf with admin option; 收回权限 :

revoke select on emp from jixiuf;

如若加了with grant option 则revoke 级联收回其他人的权限 ,with admin option 好像不收回

建立角色:

create role r1 [not identified] ; 常用 create role r2 [identified by password]; 角色授权

系统权限

grant create session to r1 [with admin option];

grant conecton to r1 ;把connect 角色的权限copy 一份给r1;select * from ROLE_ROLE_PRIVS; 对象权限

GRANT SELECT ON SCOTT.EMP TO R1;

数据字典: SELECT * FROM DICT WHERE TABLE_NAME LIKE '%ROLE%';

使用profile 管理用户口令,profile 是口令限制,资源限制的命令集合,当建立数据库时,oracle 会自动建立名称为default 的profile ,当建立用户没有指定profile,则默认用此项分配给用户

(1) 帐户锁定:

指定用户登陆时最多可以输入口令的次数,指定锁定时间,用dba 身份执行此命令

create profile profile_name_lock_user limit failed_login_attempts 3 password_lock_time 2; 最多尝试3次,3次登陆不成功则不能继续登陆,不成功后允许下次登陆时间为2天后 alter user jixiuf profile profile_name_lock_user;

create user jixiuf identified by jixiudf profile profile_name_lock_user; 解锁:alter user jixiuf acoount unlock; 定期修改密码

create profile change_password limit password_life_time 10 password_grace_time 2 一个密码用10天后必须修改,宽限期2天,这两天会提示用户修改密码

口令历史:用户不能使用以前用过的密码

create profile password_history limit password_life_time 10 password_grace_time 2 password_reuse_time 15 ; 15天后可以重用以前的密码 删 除profile

drop profile password_history ;对用户作的限制作废

conn system/root shutdown ;

ORA-01031: insufficient privileges

conn system/root as sysdba 只有作为sysdba 登陆时才有startup shutdown 权限 shutdown ; 关闭数据库 startup; 启动数据库

---------------------------------备份与恢复----------------------------------------------------- 备份与恢复(导入导出)

导出: 三类 ,导出表,导出方案(一个用户对应一个方案),导出数据库 用exp 命令 在导入和导出的时候 要用到C:\\oracle\\product\\10.1.0\\Db_1\\BIN\\exp.exe exp help=y 有帮助提示

(1)导出表( 也可以直接输入 exp 命令,以交互式进行备份) 1导出自已的表: exp userid=scott/tiger@orcl tables=(tableName1,tableName2) file=d:\\tableName.dmp; 2 导出别人的表 exp userid=scott/tiger@orcl tables=(userName.tableName1,userName.tableName2) file=d:\\tableName.dmp; 3 导出表结构,(加一个rows=n ) n means no exp userid=scott/tiger@orcl tables=(userName.tableName1,userName.tableName2) file=d:\\a.dmp rows=n

4直接导出方式 :比常规导出速度快(加一个direct=y ,专门用于导出大表) exp userid=scott/tiger@orcl tables=(userName.tableName1,userName.tableName2) file=d:\\a.dmp direct=y

(2) 导出方案

1导出自己方案

exp userid=scott/tiger@orcl owner=scott file=d:\\scott.dmp 2 导出其他的人方案

exp userid=system/root@orcl owner=(system,scott) file=d:\\scott.dmp

(3) 导出数据库( 须具有dba 权限,或者exp_full_database 权限 full=y inctype=complete 增量备份(第一次complete)

exp userid=system/root@orcl full=y inctype=complete file=d:\\scott.dmp

导入 imp (选项: userid tables fromuser touser file=d:\\a.dmp full=y inctype=complete增量备份 rows=n不导入数据 ignore=y若表存在则只导数据) 1 导入表

(1) 导入自已的表

imp userid=scott/tiger@orcl file=d:\\scott.dmp tables=emp;

imp userid=system/root@orcl file=d:\\d.dmp fromuser=scott touser=jixiuf tables=emp;

把scott.emp 导入到用户jixiuf 名下(前提是emp 没有外键关联到其他表,否则,因为它关联的表并不在jixiuf中,1法实现主外键关联) (2) 只导入表的结构

imp userid=scott/tiger@orcl tables=(emp) file=d:\\scott.dmp rows=n (3) 导入数据:

imp userid=scott/tiger@orcl tables=(emp) file=d:\\scott.dmp ignore=y

2 导入 方案

(1)导入自身方案

imp userid=scott/tiger file=d:\\scott.dmp (2) 导入他人方案

imp userid=system/root fromuser=scott touser=jixiuf file=d:\\scott.dmp (3)导入数据库

imp userid=system/root full=y file=d:\\scott.dmp

注意导入的数据可能会与已有的数据重复(如果原来的数据没丢失,却运行了导入一次命令 则可能数据重复 ,慎!!!)

-------------------------数据字典----------------------------------------------------- user_xxx, all_xxx ,dba_xxx 如user_tables dba_roles

dba_users,dba_sys_privs dba_tab_privs dba_col_privs dba_role_privs select username ,user_id ,password from dba_users; 查用户的信息

select * from dba_role_privs where grantee='JIXIUF'; 查jixiuf所具有的role select * from dba_roles 查oracle 具有的role 查一个角色具有的权限(系统权限,对象权限)

desc dba_sys_privs

select * from dba_sys_privs where grantee='CONNECT'; 或者select * from role_sys_privs where role='CONNECT' 后者以as sysdba 连接,才可以显示全,??? select * from dba_tab_privs where grantee='RESOURCE';

数据字典的数据字典dict

select * from dict where commonts like '%TABLES%'

SELECT * FROM GLOBAL_NAME; 查询当前使用的数据库orcl

----------------表空间----------------------------------- 段 区 块

create tablespace tsName1 datafile 'd:\\a.dbf' size 20m uniform size 128k 大小20M 小 128k

create table t(id int) tablespace tsName1;

select * from all_tables where tablespace_name='TSNAME1';

表空间状态, online offline 联机(可读写),脱机(不可读写,系统维护) 只读表空间 alter tablespace tsName1 offline

alter tablespace tsName1 read only; alter tablespace tsName1 read write; 删除表空间

drop tablespace tsname1 [ including contents [ and datafiles ] ] 扩展表空间 1 增加数据件

alter tablespace tsname1 add datafile 'd:\\b.dbf' size 10M 2 增加datafile的大小

alter tablespace tsname1 'd:\\b.dbf' resize 30M (?????) 3 设置file 自动增长

alter tablespace tsname1 'd:\\a.dbf' autoextend on next 10m maxsize 500m

移动datafile (磁盘损坏,但datafile 区域未坏,可移而用之)

1 select tablespace_name from dba_data_files where file_name='D:\\A.DBF'; tableSpaceName1

2 alter tablespace tableSpaceName1 offline 3 host move d:\\a.dbf c:\\a.dbf

4 alter tablespace tableSpaceName1 rename datafile 'd:\\a.dbf' to 'c:\\a.dbf' t alter tablespace tableSpaceName1 online

相应数据字典:

dba_tablespaces dba_data_files

索引 index ------------------------------------------------------------------------------------

区的大


PL&SQL学习笔记1.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:马原学生用案例材料

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: