oracle常用命令

目录

一、基础增删改查

1.解锁用户

2.修改用户密码

3.查看所有用户的缺省表空间

4.查看表空间路径,名称,大小

5.查看数据库大小

6.查询数据库连接数-总数

7.查询数据库连接数-详细连接数

8.查询字符集

9.数据库启停

10.修改密码过期时间

11.通过表名-->找用户

12.性能排查sql   a.查看耗时SQL;b.查看CPU耗时最多的SQL语句;c.查看消耗磁盘读取最多的SQL

13.查询表空间-数据文件可扩展

14.数据库备份


一、基础增删改查

--条件查询
select * from Kess a where a.UUID = 'b487-757';

nner join(等值连接) 只返回两个表中联结字段相等的行
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
INNER JOIN 语法:
INNER JOIN 连接两个数据表的用法:
select * FROM 表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号

--去重
select distinct(a.cust_code) from otc_match_app a where a.app_date=20181108 and to_char(a.app_timestamp,'mmddhhMM')<'11080901';

--查数据库中所有的表的话
select count(*) FROM dba_tables;

--模糊查询
select * from Persons where City like 'N%';

--以逆字母顺序显示公司名称,并以数字顺序显示顺序号:asc升序-默认、desc降序
select Company, Number FROM kess order by Company DESC, Number ASC;

--资金总和,分组
select Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer



--插入语句
INSERT INTO 表名称 VALUES (值1, 值2,....);

--也可以指定所要插入数据的列:
INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....);

--修改语句
UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值;

UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing' where  LastName = 'Wilson'


--删除数据:删除满足条件的记录
DELETE from KESS where UUID = 'b487-7';

--删除所有数据,不会影响表结构,不会记录日志,数据不能恢复--》删除很快
truncate table KESS;


--删除所有数据,包括表结构一并删除,不会记录日志,数据不能恢复--》删除很快
drop table KESS;



1.解锁用户

alter user test account unlock;

2.修改用户密码

alter user sys  identified by pswd12asd1a;

3.查看所有用户的缺省表空间

SELECT username, default_tablespace FROM dba_users;

4.查看表空间路径,名称,大小

select file_name,tablespace_name,bytes from dba_data_files;

5.查看数据库大小

select round(sum(bytes)/1024/1024/1024,2) as size_GB from dba_segments;

6.查询数据库连接数-总数

select inst_id, resource_name, current_utilization, max_utilization, initial_allocation, limit_value
from gv$resource_limit
where resource_name in('processes','sessions')
order by inst_id, resource_name;

7.查询数据库连接数-详细连接数

select inst_id, username, machine, module, program, service_name, count(*)
from gv$session
where type<>'BACKGROUND'
and username not in ('SYS','PUBLIC','SYSRAC','DBSNMP')
and status<>'KILLED'
group by service_name, username, inst_id, machine, module, program
order by service_name, username, inst_id, machine, module, program;


8.查询字符集

SELECT
    SYS_CONTEXT('USERENV', 'LANG')                           AS LANG,
    SYS_CONTEXT('USERENV', 'LANGUAGE')                       AS LANGUAGE
FROM
    dual;

9.数据库启停

1. 使用root用户通过SSH登录BIEE资料库服务器shell命令控制台
2. 切换到oracle用户下:
#su - oracle
3. 使用数据库管理员连接oracle数据库:
$sqlplus / as sysdba
4. 启动数据库:
>startup
5. 退出数据库连接 :
>exit



注:关闭数据库:
关闭监听服务:
$lsnrctl stop
使用数据库管理员连接oracle数据库:
$sqlplus / as sysdba
关闭数据库:
>shutdown immediate
退出数据库连接 :
>exit
 
 

6. 启动数据库监听:
$lsnrctl    start
7. 查看服务启动状态,看到以下回显信息,服务启动成功:
$lsnrctl    status

10.修改密码过期时间


sqlplus / as sysdba

SELECT username, PROFILE FROM dba_users;


查看密码周期
SELECT * FROM dba_profiles s WHERE s.profile='DEFAULT' AND resource_name = 'PASSWORD_LIFE_TIME';



修改为密码周期为不限制;

alter profile default limit password_life_time unlimited;



6、如果已经提示:java.sql.SQLException: ORA-28001: 口令已经失效这样的错误,还需要修改一下密码,改为本密码就可以。

alter user 用户名 identified by 旧密码;



commit;

11.通过表名-->找用户

select owner,table_name from dba_tables where table_name='RENWEI';

12.性能排查sql   a.查看耗时SQL;b.查看CPU耗时最多的SQL语句;c.查看消耗磁盘读取最多的SQL

定位问题


1、通过PID与v$process、v$session两张视图快速定位出语句的sql_id,如下:


SQL> select sql_id from v$session where paddr= (select addr from v$process  where spid ='29889');
SQL_ID
-------------
59vtwwcggzcwh


2、通过sql_id查看具体sql


SQL> select sql_text from v$sql where sql_id='59vtwwcggzcwh';
SQL_TEXT
--------------------------------------------------------------------------------
select * from scott.t3 where name=dbms_random.string('u', 10)



--1、查看耗时SQL

select *

from (select v.sql_id,

v.child_number,

v.sql_text,

v.elapsed_time,

v.cpu_time,

v.disk_reads,

rank() over(order by v.elapsed_time desc) elapsed_rank

from v$sql v) a

where elapsed_rank <= 10;

-- 2.查看CPU消耗时间最多的前10条SQL语句

select *

from (select v.sql_id,

v.child_number,

v.sql_text,

v.elapsed_time,

v.cpu_time,

v.disk_reads,

rank() over(order by v.cpu_time desc) elapsed_rank

from v$sql v) a

where elapsed_rank <= 10;

--3.查看消耗磁盘读取最多的前10条SQL语句

select *

from (select v.sql_id,

v.child_number,

v.sql_text,

v.elapsed_time,

v.cpu_time,

v.disk_reads,

rank() over(order by v.disk_reads desc) elapsed_rank

from v$sql v) a

where elapsed_rank <= 10;

13.查询表空间-数据文件可扩展

SELECT
    S.TABLESPACE_NAME,
    S.CURSIZE   AS "CURRENT_TBS_SIZE(MB)",
    TRUNC(NVL2(F.FREE, S.CURSIZE - F.FREE, S.CURSIZE)) AS "USED_SIZE(MB)",
    NVL(F.FREE, 0) AS "CURRENT_FREE_SIZE(MB)",
    CASE
        WHEN S.MAXSIZE - S.CURSIZE > 0 THEN
            S.MAXSIZE - S.CURSIZE
        ELSE
            0
    END AS "EXTENSIBLE_FREE_SIZE(MB)",
    S.MAXSIZE   AS "MAX_SIZE(MB)",
    LPAD(TO_CHAR(TRUNC(NVL2(F.FREE, S.CURSIZE - F.FREE, S.CURSIZE) * 100 / S.MAXSIZE)), 3, ' ')
    || '%' AS "PCT_USED"
FROM
    (
        SELECT
            TABLESPACE_NAME,
            TRUNC(SUM(BYTES) / 1024 / 1024) AS CURSIZE,
            TRUNC(SUM(
                CASE
                    WHEN(MAXBYTES < BYTES
                         OR MAXBYTES IS NULL
                         OR AUTOEXTENSIBLE = 'NO') THEN
                        BYTES
                    ELSE
                        MAXBYTES
                END
            ) / 1024 / 1024) AS MAXSIZE
        FROM
            DBA_DATA_FILES
        GROUP BY
            TABLESPACE_NAME
        UNION ALL
        SELECT
            TABLESPACE_NAME,
            TRUNC(SUM(BYTES) / 1024 / 1024) AS CURSIZE,
            TRUNC(SUM(
                CASE
                    WHEN(MAXBYTES < BYTES
                         OR MAXBYTES IS NULL
                         OR AUTOEXTENSIBLE = 'NO') THEN
                        BYTES
                    ELSE
                        MAXBYTES
                END
            ) / 1024 / 1024) AS MAXSIZE
        FROM
            DBA_TEMP_FILES
        GROUP BY
            TABLESPACE_NAME
    ) S,
    (
        SELECT
            TABLESPACE_NAME,
            TRUNC(SUM(BYTES) / 1024 / 1024) AS FREE
        FROM
            DBA_FREE_SPACE
        GROUP BY
            TABLESPACE_NAME
    ) F
WHERE
    F.TABLESPACE_NAME (+) = S.TABLESPACE_NAME
ORDER BY
    7 DESC;

14.数据库备份

1.查看管理理员目录

select * from dba_directories;


2.赋于要导出数据表的所属用户权限

sql>grant read,write on directory dpdata1 to RENWEI;

3.备份 :RENWEI

expdp system/passwd123@orcl directory=PUMP_DIR dumpfile=RENWEI20221008.dmp logfile=RENWEI20221008.log schemas=RENWEI

你可能感兴趣的:(数据库,oracle,数据库)