01-Oracle学习_引入和DBA基本操作

来源:互联网 发布:聚力体育 for mac 编辑:程序博客网 时间:2024/06/14 07:14

一, 连接到Oracle11g数据库

0, 打开服务

    net start OracleServiceORCL
    net start OracleOraDb11g_home1TNSListener

1, SQLPlus

(1)MS-DOC中敲入
    C:\Windows\System32>set oracle_sid=orcl
    C:\Windows\System32>sqlplus
    
    请输入用户名:  scott
    输入口令:                   
    ERROR:

    ORA-28001: the password has expired


    更改 scott 的口令
    新口令: tiger
    重新键入新口令: tiger
    口令已更改

    连接到:
    Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
(2)注意
    直接使用 SQLPlus时, 可能会出现
        ERROR:
        ORA-12560: TNS: 协议适配器错误
    解决办法
        参考: http://zhidao.baidu.com/question/184654181.html

2, 图形界面客户端

① PL/SQL
② POAD
③ SQLDeveloper


二, DBA操作

MS-DOC命令

1, DBA登陆

    :: 以管理员的身份登陆, 用户:sys.密码:123456
    C:\Windows\System32>sqlplus sys/123456 as sysdba

2, unlock user

    :: 解锁用户scott
    SQL> alter user scott account unlock;

3, 断开连接

    SQL> exit;

4, 更换用户, 并赋予其建表和建视图的权限

    SQL> conn sys/123456 as sysdba;
    已连接。
    SQL> grant create table, create view to scott;
    授权成功。
    SQL> conn scott/tiger;

    已连接。

5, 创建新用户, 并将scott的所有数据复制给新用户. 

步骤: 1, 导出scott数据; 2, 创建新用户并分配权限; 3, 导入;

①, 备份用户 backup scott

   使用 exp 命令
:: 创建目录
E:\desktop\Oracle>md backup_scott
:: 切换进新创建的目录
E:\desktop\Oracle>cd backup_scott
:: 指定使用哪个数据库sid
E:\desktop\Oracle\backup_scott>set oracle_sid=orcl
:: 执行导出命令
E:\desktop\Oracle\backup_scott>exp
:: 输入用户名/密码
用户名: scott/tiger
:: 导出过程中, 使用的缓冲区大小, 回车即可
输入数组提取缓冲区大小: 4096 >
:: 导出的数据, 存放的文件的 文件名
导出文件: EXPDAT.DMP >
:: 选择导出的内容, 默认为"U", 回车即可
(2)U(用户), 或 (3)T(表): (2)U >
:: 然后一直回车

②, 创建新用户 create user

    -- 用户名 zhangsan
    create user zhangsan
    -- 密码 123456
    identified by 123456
    -- 默认的表空间 users
    default tablespace users
    -- 在表空间里为zhangsan分配10M大小的空间
    quota 10M on users

③, 分配权限

    grant create session, create table, create view to zhangsan

④, 导入数据 import the data

   使用 imp 命令
E:\desktop\Oracle\backup_scott>imp
用户名: zhangsan/123456
导入文件: EXPDAT.DMP>
导入整个导出文件 (yes/no): no >
用户名: scott

6,  备份表 

    create table 表名 as select
    新表, 只有原始表数据和结构, 而没有约束等其他信息
SQL> create table emp2 as select * from emp;
表已创建。
SQL> create table dept2 as select * from dept;
表已创建。
SQL> create table salgrade2 as select * from salgrade;
表已创建。