Les utilisateurs et les autorisations
默认只开启SYS和SYSTEM用户,其他都是锁定的
SQLPLUS / AS SYSDBA
SQLPLUS SCOTT/HI
SQL> start D:\app\sa\product\12.2.0\dbhome_1\rdbms\admin\scott.sql
这个里面有一些SQL语句,帐号:SCOTT 密码:TIGER,建表和插入数据等
D:\app\sa\product\12.2.0\dbhome_1\network\admin
里面有listener.ora,sqlnet.ora,tnsnames.ora
用户与权限:
CREATE ROLE IT;创建IT部门的角色
DROP ROLE IT;删除IT部门的角色
GRANT SELECT ON TEST TO IT;拥有IT角色的所有用户,都有查询TEST表的权限
SELECT USERNAME FROM DBA_USERS;查看所有用户
SELECT USERNAME FROM ALL_USERS;查看你能管理的用户
SELECT USERNAME FROM USER_USERS;SHOW USER;查看当前用户
SELECT * FROM SESSION_PRIVS;当前会话所有拥有的权限
CREATE USER TONY IDENTIFIED BY HI;创建TONY用户,密码为HI
ALTER USER SCOTT IDENTIFIED BY HI ACCOUNT UNLOCK;修改SCOTT用户的密码为HI
GRANT CONNECT,RESOURCE TO TONY;
给TONY授角色权
CONNECT是最基本权利,能够连接到ORACLE数据库中,并对其他用户的表有访问权限时,做SELECT,UPDATE等操作
RESOURCE是授予开发人员的,能在自己的方案中创建表、存储过程、序列、视图等
DBA角色是授予给系统管理员,拥有所有的系统权限
GRANT UNLIMITED TABLESPACE TO TONY;授予TONY有使用表空间的权限,可以在其他表空间随意建表,这个下面操作的前提
GRANT CREATE TABLE,CREATE SESSION,CREATE VIEW TO TONY;授权对表,视图创建权限
GRANT SELECT,INSERT,UPDATE,DELETE,ALL ON A TO TONY;给TONY授予A表的权限
SELECT * FROM SESSION_PRIVS;当前会话所有拥有的权限
ALTER USER TONY ACCOUNT LOCK;锁定用户TONY
ALTER USER TONY ACCOUNT UNLOCK;解锁用户TONY
Tables, vues, champ, index, etc.
SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER='SCOTT';显示这个用户拥有的表
当前会话用户拥有的表
SELECT TABLE_NAME FROM USER_TABLES;
SELECT TABLE_NAME FROM TABS;
DESC DEPT;显示DEPT这张表的数据结构
查看表DEPT,创建的日期
SELECT OBJECT_NAME,CREATED FROM USER_OBJECTS WHERE OBJECT_NAME='DEPT';
查看表DEPT的大小
SELECT SUM(bytes)/(1024*1024) TABLESIZE FROM USER_SEGMENTS WHERE SEGMENT_NAME='DEPT';
查看DEPT表的索引名称,索引类别
SELECT INDEX_NAME,INDEX_TYPE,TABLE_NAME FROM USER_INDEXES WHERE TABLE_NAME='DEPT';
CREATE INDEX IDXTONY ON AA(ID);给表AA的ID字段创建索引IDXTONY
查找索引IDXTONY的大小
SELECT SUM(BYTES)/(1024*1024) AS INDEXSIZE FROM
USER_SEGMENTS WHERE SEGMENT_NAME='IDXTONY';
CREATE TABLE T(COL1 TYPE1 [NOT NULL] [PRIMARY KEY],COL2 TYPE2 [NOT NULL],...);创建表
ALTER TABLE B RENAME TO BB;修改表名
ALTER TABLE BB ADD(V1 INT NOT NULL);增加字段
ALTER TABLE BB MODIFY(V2 VARCHAR(30));修改V2字段的字段类型、默认值、是否为空等
ALTER TABLE BB RENAME COLUMN V1 TO VV;修改V1字段名为V2
ALTER TABLE BB DROP COLUMN V2;
删除字段V2【ORA-12988: 无法删除属于 SYS 的表中的列 ==> 不能删除sys登录后创建的表的表结构】
ALTER TABLE BB ADD CONSTRAINT PK_ID PRIMARY KEY(ID);给ID字段添加主键
ALTER TABLE BB DROP CONSTRAINT PK_ID;删除主键
CREATE [UNIQUE] INDEX IDX_V1 ON BB(V1);创建[唯一]索引
DROP INDEX IDX_V1;删除索引
SELECT * FROM USER_IND_COLUMNS WHERE TABLE_NAME='BB';查看表BB的索引情况
SELECT COUNT(1) FROM USER_OBJECTS WHERE OBJECT_TYPE='VIEW';所有视图
SELECT COUNT(1) FROM ALL_VIEWS;所有视图
SELECT COUNT(1) FROM DBA_VIEWS;当前库所有视图
SELECT * FROM USER_VIEWS;当前用户所有视图
CREATE VIEW TONY_VIEW AS SELECT * FROM TEST;创建视图TONY_VIEW
SELECT * FROM TONY_VIEW;查看视图
DESC TONY_VIEW;查看视图结构
SELECT TEXT FROM USER_VIEWS WHERE VIEW_NAME='TONY_VIEW';查看视图的SQL语句
DROP VIEW TONY_VIEW;删除视图
TRUNCATE TABLE TEST;清空表,保留表结构
DROP TABLE TEST;数据连同表结构一起删除
CREATE TABLE TEST1 AS SELECT * FROM A;复制A表结构和数据,若只想复制表结构,则WHERE 1=2
autres produits divers
show user;
show parameter db_name;
select name from v$database;
sqlplus /@orcl as sysdba;
sqlplus / as sysdba
select table_name from all_tables where owner='scott';
select table_name from user_tables;
select table_name from tabs;
select * from v$version;
select userenv('language') from dual;
ALTER DATABASE CHARACTER SET INTERNAL_USE ZHS16GBK;
show parameter deferred_segment_creation;
alter system set deferred_segment_creation=true|false;
shutdown immediate
C:\Users\Tony>sqlplus /nolog
conn /as sysdba
alter user scott identified by tiger;
alter user scott account unlock;
select * from user_tables where segment_created='NO';
select username,password from dba_users;
"用户名:system 密码:manager
用户名:sys 密码:change_on_install"
shutdown immediate;
startup
SELECT DISTINCT OBJECT_TYPE FROM USER_OBJECTS;
OBJECT_TYPE
SEQUENCE
PROCEDURE
DATABASE LINK
LOB
PACKAGE
PACKAGE BODY
TYPE BODY
TRIGGER
TABLE
INDEX
SYNONYM
VIEW
JAVA CLASS
JAVA SOURCE
FUNCTION
TYPE
查看函数和存储过程:
SELECT OBJECT_NAME,STATUS FROM USER_OBJECTS WHERE OBJECT_TYPE='FUNCTION';
SELECT OBJECT_NAME,STATUS FROM USER_OBJECTS WHERE OBJECT_TYPE='PROCEDURE';
查看函数和存储过程的源码
SELECT TEXT FROM ALL_SOURCE WHERE OWNER=USER AND NAME='名称';
查看C_SUPPLIER这张表,被哪些存储过程使用
SELECT DISTINCT * FROM USER_SOURCE WHERE TYPE = 'PROCEDURE'
AND UPPER(TEXT) LIKE '%C_SUPPLIER%';
table temporaire
create global temporary table tmp_table1(x number) on commit delete rows;
create global temporary table tmp_table1(x number) on commit preserve rows;
insert into tmp_table1 values(1);
insert into tmp_table1 values(2);
insert into tmp_table1 values(3);
insert into tmp_table2 values(1);
insert into tmp_table2 values(2);
insert into tmp_table2 values(3);
commit;
tmp_table1数据清空
tmp_table2数据保留
conn /as sysdba
重连之后两个临时表数据都清空