sysdba 可以管理 普通dba,及普通用户
普通dba可以管理普通用户
-------查看数据库用户
--select * from all_users;
---create user bitc identified by bitc;
----显示当前用户
SQL> show user;
USER is "SYS"
-------切换用户
SQL> conn bitc/bitc ERROR: ORA-01045: user BITC lacks CREATE SESSION privilege; logon denied
--------bitc用户缺乏连接session 权限--
SQL> conn / as SYSDBA
Connected. SQL> grant connect to bitc 2 ;
Grant succeeded.
SQL> conn bitc/bitc
Connected. SQL> show user USER is "BITC" SQL>
SQL> create table t1(id int,name varchar(3));
create table t1(id int,name varchar(3)) * ERROR at line 1: ORA-01031: insufficient privileges
SQL> conn / as SYSDBA
Connected. SQL> grant resource to bitc;
Grant succeeded.
SQL> conn bitc/bitc;
Connected. SQL> create table t1(id int,name varchar(3));
Table created.
SQL> insert all
2 into t1 values(2,'2') 3 into t1 values(3,'3') 4 select * from dual; 2 rows inserted SQL>
SQL> insert all
2 into t1 values(4,'4') 3 into t2 values(1,'1') 4 select * from dual;
2 rows inserted
SQL> insert all into t1 values(6,'6') into t2 values(2,'2') select * from dual;
2 rows inserted |
(a)
系统权限:是指执行特定类型sql命令的权利。他用于控制用户可以执行的一个或是一组
数据库操作。在oracle中大概有100多权限 常用的有: create session 连接数据库 create view 建视图 create procedure 建过程、函数、包 create cluster 建族 create table create public synonym 建同义词 create trigger 建立触发器
---------------------------------------------------------------
显示系统权限
查询数据字典视图 system_privilege_map select * from system_privilege_map order by name;
SQL> desc system_privilege_map;
Name Type Nullable Default Comments --------- ------------ -------- ------- -------------------------------------------------------------- PRIVILEGE NUMBER Numeric privilege type code NAME VARCHAR2(40) Name of the type of privilege PROPERTY NUMBER Property flag of privilege like not export this privilege, etc
授予系统权限
dba身份授予,如果带有 with admin option选项,则被授予权限的用户还可以给其他用户授予权限
(b)
对象权限:指访问其它方案对象的权限。比如zqh1用户要访问scott.emp表,必须在emp表上
具有对象权限 常用的有: alter delete select insert update index索引 reference 引用 execute执行
显示对象权限
可以显示用户或是角色所具有的对象权限,视图 dba_tab_privs select distinct privilege from dba_tab_privs; select grantor ,owner,table_name,privilege from dba_tab_privs where grantee='BLAKE'
授予对象权限
对象权限可以授予用户、角色和public。授予用户的时候可以用 with grant option 但是授予角色的时候不可以用。
(c)
角色:相关权限命令的集合,使用角色就是为了简化管理。
分类:预定义和自定义角色两类
预定义角色是oracle提供的角色,每种角色
都用于执行一些特定的管理任务。connect/resource/dba connect角色包括的权限:create session、create table、create view、create synonym、create sequence、create database link、create cluster、alter session。 resource角色包括的权限:create table、create procedure、create sequence、create trigger、create type、create cluster、create indextype、create opeator。
显示角色信息:
(1)显示所有的角色 select * from dba_roles;---------25个角色 (2)显示角色具有的系统权限 select privilege,admin_option from roles_sys_privs where role='角色名'; (3)显示角色具有的对象权限 查询dba_tab_privs (4)显示用户具有的角色,及默认角色 查询dba_role_privs select grant_role,default_role from dba_role_privs where grantee='用户名';
----查看用户角色
----select * from DBA_ROLE_PRIVS;
SQL> conn /as SYSDBA;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 Connected as SYS@ORCL AS SYSDBA
SQL> create user bitcdba identified by bitcdba;
User created SQL> conn bitcdba/bitcdba SQL> show user User is "SYS" SQL> grant dba to bitcdba; Grant succeeded SQL> conn bitcdba/bitcdba Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 Connected as bitcdba SQL> select * from bitc.t1; ID NAME --------------------------------------- ---- 2 2 3 3 4 4 5 5 6 6
-----------------------------------------------
SQL> create or replace procedure hello is
2 begin 3 DBMS_OUTPUT.PUT_LINE('HELLO'); 4 end hello; 5 /
Procedure created.
SQL> exec hello
PL/SQL procedure successfully completed.
SQL> set serveroutput on;
SQL> exec hello HELLO
PL/SQL procedure successfully completed.
SQL> conn / as SYSDBA; Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 Connected as SYS@ORCL AS SYSDBA SQL> revoke dba from bitcdba; Revoke succeeded |
SQL> conn /as SYSDBA;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 Connected as SYS@ORCL AS SYSDBA
SQL> create profile test1 limit
2 FAILED_LOGIN_ATTEMPTS 3 3 PASSWORD_LOCK_TIME 5 4 PASSWORD_LIFE_TIME 30 5 ; Profile created ------=======create profile 语法-------------------
------ http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_6010.htm
/*
密码寿命
PASSWORD_LIFE_TIME Specify the number of days the same password can be used for authentication. If you also set a value for
PASSWORD_GRACE_TIME , the password expires if it is not changed within the grace period, and further connections are rejected. If you do not set a value for
PASSWORD_GRACE_TIME , its default of
UNLIMITED will cause the database to issue a warning but let the user continue to connect indefinitely.
*/
SQL> CREATE USER test2 identified by abc profile test1; User created
-------------------------------------
SQL> conn test2/111
ERROR: ORA-01017: invalid username/password; logon denied SQL> conn test2/222 ERROR: ORA-01017: invalid username/password; logon denied SQL> conn test2/333 ERROR: ORA-28000: the account is locked
-----------------------------------------------
select username,sid,serial#,machine from v$session;
================或者修改用户profile===========================
-----alter user test7 profile test1;
|
SQL> conn /as SYSDBA;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 Connected as SYS@ORCL AS SYSDBA
SQL> create role role1;
Role created SQL> grant create any table ,create procedure to role1; Grant succeeded SQL> create user test3 identified by test3; User created
SQL> conn test3/test3
ERROR: ORA-01045: user TEST3 lacks CREATE SESSION privilege; logon denied
SQL> grant connect to role1;
Grant succeeded
SQL> drop role role1;
Role dropped |
SQL> conn /as SYSDBA
Connected. SQL> create user test4 identified by test4;
User created.
SQL> conn /as SYSDBA
Connected. SQL> create user test4 identified by test4;
User created.
SQL> alter user test4 account lock;
User altered.
SQL> conn test4/test4;
ERROR: ORA-28000: the account is locked
SQL> alter user test4 account lock;
User altered.
SQL> conn test4/test4;
ERROR: ORA-28000: the account is locked
-------------------------------------------------
SQL> alter user test4 account unlock;
User altered.
SQL> conn test4/test4
Connected. |
SQL> conn test4/test4
Connected. SQL> conn /as SYSDBA Connected. SQL> drop user test4 2 ;
User dropped.
----------------------级联删除用户------------------------
SQL> create user test5 identified by abc;
User created.
SQL> grant dba to test5;
Grant succeeded.
SQL> conn test5/abc;
Connected. SQL> create table t2(id int);
Table created.
SQL> conn /as SYSDBA
Connected. SQL> drop user test5; drop user test5 * ERROR at line 1: ORA-01922: CASCADE must be specified to drop 'TEST5' SQL> drop user test5 CASCADE;
User dropped.
SQL>
------------------------不同用户资源权限---------------------
SQL> create user test5 identified by abc;
User created.
SQL> grant dba to test5;
Grant succeeded.
SQL> conn test5/abc;
Connected. SQL> create table t2(id int);
Table created.
SQL> conn /as SYSDBA
Connected. SQL> drop user test5; drop user test5 * ERROR at line 1: ORA-01922: CASCADE must be specified to drop 'TEST5' SQL> drop user test5 CASCADE;
User dropped.
SQL> grant connect,resource to test5;
Grant succeeded.
SQL> create user test6 identified by abc;
User created.
SQL> grant connect,resource to test6;
Grant succeeded.
SQL> conn test5/abc
Connected. SQL> create table t3(id int);
Table created.
SQL> insert into t3 values(3);
1 row created.
SQL> commit;
Commit complete.
SQL> conn test6/abc
Connected. SQL> select * from test5.t3; select * from test5.t3 * ERROR at line 1: ORA-00942: table or view does not exist SQL> conn /as SYSDBA Connected. SQL> grant select,update,delete on test5.t3 to test6;
Grant succeeded.
SQL> conn test6/abc
Connected. SQL> select * from test5.t3;
ID
---------- 3
SQL>
-------------------普通dba之间删除----------------------
SQL> conn /as SYSDBA
Connected. SQL> create user rr100 identified by abc;
User created.
SQL> create user rr200 identified by abc;
User created.
SQL> grant dba to rr100,rr200;
Grant succeeded.
SQL> conn rr100/abc
Connected. SQL> drop user rr200;
User dropped.
SQL>
-------------------------copy table---------------
SQL> create table t1 as select * from test5.t3;
Table created.
--------删除profile,用户不受profile约束-----------------
SQL> alter user test5 profile test1;
User altered.
SQL> drop profile test1 cascade;
Profile dropped.
SQL> conn test5/abc
Connected. SQL> select * from t3;
ID
---------- 3 |
vi 1.ctl
load data
infile 'G:\Oracle\lst\1.txt' replace into table stu fields terminated by "," optionally enclosed by '"' trailing nullcols ( id, name )
-------------------------=======================
-----optionally enclosed by '"'是两个单引号中一双引号
-----字符串按双引号封闭
----- terminated by ","
-----逗号分隔列
-----参数详见 http://docs.oracle.com/cd/E11882_01/server.112/e22490/ldr_modes.htm#SUTIL1349
-------------------------=======================
vi 1.txt
1,"zhangsan",11
2,"xisi",12 3,"wangwu",13 4,"chensi",14
-----------------------------------------------------
C:\Users\陈>sqlplus system/oracle
SQL*Plus: Release 11.2.0.1.0 Production on Sat Oct 27 13:26:32 2012
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> create table stu(id int,name varchar(30));
Table created.
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Pr oduction With the Partitioning, OLAP, Data Mining and Real Application Testing options
--------------------------------------------------------------------------------
C:\Users\陈>sqlldr system/oracle control=G:\Oracle\lst\1.ctl log=G:\Oracle\lst\1
.log
SQL*Loader: Release 11.2.0.1.0 - Production on Sat Oct 27 13:28:05 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 3
Commit point reached - logical record count 4
---------------------=====================================
/*----------sqlldr 导入者是见表用户
------------如果建表者是SYS,则
sqlldr system/oracle control=G:\Oracle\lst\1.ctl log=G:\Oracle\lst\1
.log
-----------------*/
---------------------=====================================
C:\Users\陈>sqlplus system/oracle
SQL*Plus: Release 11.2.0.1.0 Production on Sat Oct 27 13:28:30 2012
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select * from stu;
ID NAME
---------- ------------------------------ 1 zhangsan 2 xisi 3 wangwu 4 chensi
SQL>
|
SQL> create table emp90 as select * from scott.emp where 1=0;
Table created -----------copy建表不插入数据
SQL> create table emp90 as select * from scott.emp ;
Table created ------copy表结构及数据 SQL>
-----------------------------------------------------------
C:\Users\陈>sqlplus "/as SYSDBA"
SQL*Plus: Release 11.2.0.1.0 Production on Sat Oct 27 14:00:43 2012
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> create user cc1 identified by abc;
User created.
SQL> grant dba to cc1;
Grant succeeded.
SQL> conn cc1/abc
Connected. SQL> create table dept 2 ( 3 deptno number(3) primary key, 4 dname varchar2(14), 5 loc varchar2(13) 6 ) 7 ;
Table created.
SQL> create table emp
2 ( 3 empno number(4) primary key, 4 ename varchar2(10), 5 job varchar2(9), 6 mgr number(4), 7 hiredate date, 8 sal number(7,2), 9 comm number(7,2), 10 deptno number(3) references dept 11 ) 12 /
Table created.
SQL> insert into dept select * from scott.dept;
4 rows created.
SQL> insert into emp select * from scott.emp;
14 rows created.
SQL> commit;
Commit complete.
SQL>
---------------------------------------------------------------
SQL> create or replace type emp_type as object
2 ( 3 empno number(4), 4 ename varchar(10), 5 job varchar2() 6 /
Warning: Type created with compilation errors.
SQL> commit;
Commit complete.
SQL> create or replace type emp_type as object
2 ( 3 empno number(4), 4 ename varchar2(10), 5 job varchar2(9), 6 mgr number(4), 7 hiredate date, 8 sal number(7,2), 9 comm number(7,2)); 10 /
Type created.
SQL> create or replace type emp_tab_type as table of emp_type;
2 /
Type created.
SQL> create table dept_and_emp
2 ( 3 deptno number(2) primary key, 4 dname varchar2(14), 5 loc varchar2(13), 6 emps emp_tab_type 7 ) nested table emps store as emps_nest;
Table created.
SQL> insert into
2 dept_and_emp 3 select dept.*, 4 cast( 5 multiset(select empno,ename,job,mgr,hiredate,sal, 6 comm from emp where emp.deptno=dept.deptno) 7 AS emp_tab_type) from dept 8 /
4 rows created.
SQL>
/*
http://docs.oracle.com/cd/E11882_01/server.112/e26088/operators006.htm#SQLRF0032
*/
|
C:\Users\陈>sqlplus system/oracle
SQL*Plus: Release 11.2.0.1.0 Production on Sat Oct 27 16:12:33 2012
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> create tablespace tbs1 datafile 'G:\Oracle\lst\tbs\tbs1.dbf' size 100M;
Tablespace created.
SQL> create user user2 identified by abc;
User created.
SQL> grant connect,resource to user2;
Grant succeeded.
SQL> conn user2/abc;
Connected. SQL> create table t1 (id int);
Table created.
SQL> insert into t1 values(1);
1 row created.
SQL> conn system/oracle
Connected.
SQL> alter user user2 default tablespace tbs1 ;
User altered.
SQL> create table t2 (id int);
Table created.
SQL> insert into t2 values(1);
1 row created.
SQL> commit;
Commit complete.
SQL>
----------------
select * from all_tables where OWNER='USER2';
-------------------------------------------------------
SQL> conn system/oracle
Connected. SQL> create tablespace test_data 2 datafile 'G:\Oracle\lst\tbs\user_data.dbf' 3 size 50M 4 autoextend on 5 next 1000m maxsize 2048M 6 extent management local;
Tablespace created.
---------------------------------
autoextend on/off 自动扩展
SQL>
----------------------------------------------------------------------------------------------------------------------- SQL> conn cswggod/198543 Connected. SQL> create table test00 (id int) tablespace tbs1;
Table created.
----------------------------------------------------------
select * from all_tables where OWNER='CSWGGOD';
----------------------------------------------------------------------------------------------------------
SQL> drop tablespace tbs1 including contents;
Tablespace dropped.
----------------------但物理文件仍存在,强制online无效
---------------------------------------------------------------------------------------------------------------
SQL> show user
USER is "CSWGGOD" SQL> create tablespace ts2 2 datafile 'G:\Oracle\lst\tbs\ts2.dbf' 3 size 50M 4 autoextend on 5 next 1000m maxsize 2048M 6 extent management local;
Tablespace created.
SQL> create table uu0(id int) tablespace ts2;
Table created.
SQL> insert into uu0 values(1);
1 row created.
SQL> commit;
Commit complete.
SQL> conn system/oracle
Connected. SQL> drop tablespace ts2 including contents and datafiles;
Tablespace dropped.
SQL> conn cswggod/198543
Connected. SQL> select * from uu0; select * from uu0 * ERROR at line 1: ORA-00942: table or view does not exist
-------------================表已删除==================
----------------------建临时表空间-----------------------------------
SQL> conn system/oracle
Connected. SQL> create temporary tablespace temp1 2 tempfile 'G:\Oracle\lst\tbs\temp1.dbf' 3 size 50M 4 autoextend on 5 next 50M maxsize 1024M 6 extent management local;
Tablespace created.
SQL> alter user USER2 temporary tablespace temp1;
User altered.
-----------------------------删除临时表空间---------------------------
SQL> drop tablespace temp1 including contents and datafiles;
Tablespace dropped.
------------------------------tablespace 及datafile 视图
select * from v$datafile;
select * from v$tablespace;
连接字段 TS#
-------------------------------------
SQL> desc v$tablespace;
Name Type Nullable Default Comments --------------------------- ------------ -------- ------- -------- TS# NUMBER Y NAME VARCHAR2(30) Y INCLUDED_IN_DATABASE_BACKUP VARCHAR2(3) Y BIGFILE VARCHAR2(3) Y FLASHBACK_ON VARCHAR2(3) Y ENCRYPT_IN_BACKUP VARCHAR2(3) Y --------------------------------------------------------------------------------------------------------
SQL> desc v$datafile;
-------------------------------------------------------------------------------- Name Type Nullable Default Comments ------------------------ ------------- -------- ------- -------- FILE# NUMBER Y CREATION_CHANGE# NUMBER Y CREATION_TIME DATE Y TS# NUMBER Y RFILE# NUMBER Y STATUS VARCHAR2(7) Y ENABLED VARCHAR2(10) Y CHECKPOINT_CHANGE# NUMBER Y CHECKPOINT_TIME DATE Y UNRECOVERABLE_CHANGE# NUMBER Y UNRECOVERABLE_TIME DATE Y LAST_CHANGE# NUMBER Y LAST_TIME DATE Y OFFLINE_CHANGE# NUMBER Y ONLINE_CHANGE# NUMBER Y ONLINE_TIME DATE Y BYTES NUMBER Y BLOCKS NUMBER Y CREATE_BYTES NUMBER Y BLOCK_SIZE NUMBER Y NAME VARCHAR2(513) Y PLUGGED_IN NUMBER Y BLOCK1_OFFSET NUMBER Y AUX_NAME VARCHAR2(513) Y FIRST_NONLOGGED_SCN NUMBER Y FIRST_NONLOGGED_TIME DATE Y FOREIGN_DBID NUMBER Y FOREIGN_CREATION_CHANGE# NUMBER Y FOREIGN_CREATION_TIME DATE Y PLUGGED_READONLY VARCHAR2(3) Y PLUGIN_CHANGE# NUMBER Y PLUGIN_RESETLOGS_CHANGE# NUMBER Y PLUGIN_RESETLOGS_TIME DATE Y |