ORACLE12C Database Management Common statements

Source: Internet
Author: User
Tags dba rollback sqlplus

ORACLE12C database Management Common statements:
Select name from V$database; #查看数据库名:
Desc v$database;
Select instance_name from V$instance; #数据库实例名
The above three SQL statements can only be queried to the current connected instance and to the database under that instance.

--dba/all/user/v$/GV$/session/index Most of the openings are views
-Dba_tables means all the relational tables that the DBA has or can access.
--All_tables All relational tables that a user has or can access.
-User_tables means all the relational tables owned by a user.
Dba_tables and All_tables are equivalent when a user itself is a DBA for the database.
--Dba_tables >= all_tables >= user_tables
Note that the case is sensitive in the Oracle database, and the data in the three tables is in uppercase by default, so it is possible that the data cannot be traced when the query is made with the lowercase data.

SELECT from dba_views WHERE view_name like ' dba% ';
SELECT
from Dba_views WHERE view_name like ' all% ';
SELECT from dba_views WHERE view_name like ' user% ';
SELECT
from dba_views WHERE viewname is like ' V$% '; --View for an instance
SELECT from dba_views WHERE viewname like ' GV$% ';--global view for multiple instance environments
SELECT
from Dba_views WHERE view_name like ' session% ';
SELECT * from Dba_views WHERE view_name like ' index% ';

SELECT count (1) from Dba_tables;
SELECT count (1) from All_tables;
SELECT count (1) from User_tables;

--Most of the v$/gv$ begins with the alias of the V$/GV$ table
SELECT from dba_synonyms WHERE synonym_name like ' v$% ';
SELECT
from dba_synonyms WHERE synonym_name like ' gv$% ';

--x$ no corresponding x_$
SELECT * from dba_synonyms WHERE synonym_name like ' x$% ';

--The more common view of DBA openings is
SelectFrom Dba_users; --Database user information
Select
From Dba_roles; --Role information
SelectFrom dba_segments;--Table segment information
Select
From Dba_extents; --Data area information
SelectFrom dba_objects;--Database object information
Select
From Dba_lobs; --lob Data Information
SelectFrom dba_tablespaces;--Database tablespace information
Select
From Dba_data_files; --Data file setup Information
SelectFrom dba_temp_files;--Temporary data file information
Select
From Dba_rollback_segs; --Rollback segment information
SelectFrom Dba_ts_quotas;--User table space quota information
Select
From Dba_free_space; --Database free space information
SelectFrom dba_profiles;--Database User resource restriction information
Select
From Dba_sys_privs; --User's system permission information
Selectfrom Dba_tab_privs;--the object permission information that the user has
Select
From Dba_col_privs; --User has permission information for Column objects
Selectfrom Dba_role_privs;--Role information that the user has
Select
From Dba_audit_trail; --Audit trail record information
SelectFrom dba_stmt_audit_opts;--Audit setup Information
Select
From Dba_audit_object; --Object Audit result information
SelectFrom Dba_audit_session;--session Audit results information
Select
From Dba_indexes; --index information for user mode

--The view with the more common start of all has
Select from All_users;--Information for all users of the database
Select
from all_objects;--Information on all objects in the database
Select from All_def_audit_opts;--All default audit setup information
Select
from All_tables;--All Table object information
Select from all_in dexes; --Information for all database object indexes
Select
from all_tab_comments; --Query All users ' tables, views, etc.
Select from all_col_comments;--Query the column names and comments for all users ' tables.
Select
from All_tab_columns; --Query the column names of all users ' tables (verbose but no notes)

--The more common view at the beginning of all is
Select from user_objects;--User object information
Select
from User_source; --All resource object information for the database user
Select from User_segments;--User's Table segment information
Select
from User_tables; --User's Table object information
Select from User_tab_columns;--User's table column information
Select
from User_constraints; --User's object constraint information
Select from User_sys_privs;--system permissions information for the current user
Select
from User_tab_privs; --Object permission information for the current user
Select from User_col_privs;--table column permission information for the current user
Select
from User_col_comments; --Query the column names and comments for this user's table
Select from User_role_privs;--Role permission information for the current user
Select
from User_indexes; --User's index information
Select from User_ind_columns;--The table column information corresponding to the user's index
Select
from User_cons_columns; --The table column information for the user's constraints
Select from User_clusters;--all cluster information of the user
Select
from User_clu_columns; --The content information contained in the user's cluster
SELECT * from User_cluster_hash_expressions; --Information of the hash cluster

--the more commonly used v$ begins with aliases that have
SelectFrom V$database; --Database information
Select
From V$datafile; --Data file information
SelectFrom v$controlfile;--Control file information
Select
From V$logfile; --Redo Log information
SelectFrom v$instance;--DB instance Information
Select
From V$log; --Log Group information
SelectFrom v$loghist;--Log history information
Select
From V$SGA; --Database SGA Information
SelectFrom V$parameter;--Initialization of parameter information
Select
From V$process; --Database server process information
SelectFrom v$bgprocess;--Database background process information
Select
From V$controlfile_record_section; --control of the information in each part of the document
SelectFrom v$thread;--Thread information
Select
From V$datafile_header; --Information recorded in the header of the data file
SelectFrom V$archived_log; --Archive log information
Select
From V$archive_dest; --Setup information for archived logs
Selectfrom V$logmnr_contents;--DML DDL result information for archived log analysis
Select
From V$logmnr_dictionary; --Dictionary file information for log analysis
Selectfrom V$logmnr_logs;--log list information for log analysis
Select
From V$tablespace; --Table space information
SelectFrom v$tempfile;--Temporary file information
Select
From V$filestat; --I/O statistics for data files
SelectFrom v$undostat;--undo Data information
Select
From V$rollname; --Online rollback segment Information
SelectFrom V$session;--session information
Select
From V$transaction; --Transaction information
SelectFrom V$rollstat;--Rollback segment statistics
Select
From V$pwfile_users; --Privileged User information
SelectFrom V$sqlarea;--the resources and related information accessed by the currently queried SQL statement
Select
From V$sql; --basic information about the same as V$sqlarea
SELECT * from V$sysstat; --Database System state information

--the more commonly used view at the beginning of a session is
Select from Session_roles;--session role information
Select
from Session_privs; --Permission information for the session

--the more commonly used views at the beginning of the index are
SELECT * from Index_stats; --Indexing settings and storage information

--pseudo-table, refer to Oracle Dual detailed: http://blog.csdn.net/ozhouhui/article/details/7935196
SELECT * from dual; --System pseudo-list information
Select Sysdate from dual; --sysdate can be treated as a function whose result is the current date and time, and sysdate can be used wherever an Oracle function can be used. It can also be thought of as a hidden column or pseudo-column for each table.
Select current_date from dual; --The system date in the time zone of the report session. Note: You can set your own time zone to distinguish it from the time zone of the database.
Select Systimestamp from dual; --Report the system date in timestamp data type format.

--System permissions
--GRANTEE user name to accept this permission
-Owner of Owner Object
--Grantor the user who gives permission
SELECT from dba_sys_privs WHERE grantee = ' sys ';
SELECT
from Dba_sys_privs WHERE grantee = ' CONNECT ';
SELECT * FROM Dba_sys_privs WHERE grantee = ' RESOURCE ';

--Role permissions
--See what roles a user has
Select from dba_role_privs where grantee= ' SYS ';
--See which users a role has been given
SELECT
from dba_role_privs WHERE granted_role = ' DBA ';

--Object permissions
SELECT * from Dba_tab_privs;

--Grant certain roles to a user
GRANT connect,resource to ' USER ';
GRANT dba to ' USER '; --When you grant a DBA role to a normal user, you need to reconnect to take effect
REVOKE dba to ' USER ';
--Grant certain permissions directly to a user
GRANT CREATE VIEW to ' USER ';

--See if a system user has SYSDBA or sysoper permissions
--the difference between the three oracle:dba,sysdba,sysoper:
SELECT * from V$pwfile_users;

--Lock, unlock users
SELECT * from dba_users WHERE username = ' SCOTT ';
ALTER USER SCOTT account LOCK; --Lock user
ALTER USER SCOTT account UNLOCK; --Unlock User
COMMIT;

SELECT password from dba_users WHERE username = ' SCOTT ';
Alter user SCOTT identified by New_password; --Modify User password

--Service_names:
SELECT from global_name;--View Oracle's global database name
SELECT
from V$database; --View database name show parameter db_name;

--The database instance name corresponds to Sid
--SID:
--In the case of configuring Oracle environment variables under Linux, you can use the Echo $ORACLE _sid, and if you can not query using PS-EF |grep Oracle, the result of XXXX is the corresponding SID.
--Oracle 2548 1 0 Aug17? 00:00:00 ora_pmon_xxxx
--In the Windows environment, Oracle is managed as a backend service, so look at the name: "ORACLESERVICEORCL" in the service, management tools, Control Panel, then ORCL is SID;
SELECT * from V$instance; --View the database instance name show parameter instance_name;
Select instance from V$thread;

--Show parameter is an Oracle command, not a standard SQL statement
--can be executed in sqlplus or PL/SQL Dev's command window
-show parameter aaaa; equivalent to select from v$parameter WHERE name like '%aaaa% ';
SELECT
from V$parameter WHERE name is like '%name% '; --equivalent to show parameter name;
SELECT * from V$parameter where name is like '%db_domain% '; --Querying the database domain name

Select username from all_users where username like '%scott% ';
Drop user SCOTT cascade;
Commit

--ERROR at line 1:
--Ora-01940:cannot drop a user that is currently connected

Select ' ALTER SYSTEM KILL SESSION ' | | sid| | ', ' | | serial#| | "' | | '; ' as KILLER from v$session where username= ' SCOTT ';
--KILLER
--ALTER SYSTEM KILL SESSION ' 363,35 ';
--ALTER SYSTEM KILL SESSION ' 364,51 ';
Commit

SELECT * from dba_roles where role like '%connect% ';
Drop role CONNECT;
Commit

SELECT * from dba_tablespaces where tablespace_name like ' EXAMPLE ';
Drop tablespace EXAMPLE including contents and datafiles cascade constraints;
---including contents delete the contents of the table space, if the table space before the delete table space has content, without this parameter, the table space is not deleted, so the habitual add this parameter.
--including datafiles delete the data files in the tablespace.
--cascade constraints also deletes foreign key references for tables in tablespace.

--If you need to create a global DBLink, you need to first determine the user has permission to create DBLink:
SELECT * from User_sys_privs where privilege like Upper ('%database link% ');

--If not, you need to use the SYSDBA role to empower the user:
Grant create public database link to dbusername;

--If you create a global dblink, you must use the SYSTM or SYS user to add public before database.
Create/ public/Database link DBLINK1
Connect to Dbusername identified by Dbpassword
Using ' (DESCRIPTION = (Address_list = (ADDRESS = (PROTOCOL = TCP) (HOST = 192.168.0.1) (PORT = 1521))) (Connect_data = (service_ NAME = ORCL)) ';

--After creating the Dblink, you can create the view directly on the Dblink
Create or Replace View CPTP as (select SJDH from [email protected]); Drop View CPTP;

--Lock Table query sql
SELECT object_name, Machine, S.sid, s.serial#
From Gv$locked_object L, dba_objects O, gv$session s
WHERE l.object_id = o.object_id
and l.session_id = S.sid;

--Unlock Table
Alter system kill session ' Sid, Serial# ';

--Back up a table
CREATE TABLE new_table as SELECT * from Old_table;

--See if the database is in a cluster in the RAC environment
Show parameter cluster_database;
SELECT * from v$parameter where name = ' Cluster_database ';

--Column operations
--Adding and modifying columns does not need to add a keyword column
--To delete a single column, be sure to add column, delete multiple columns when the column keyword cannot be added

--Add a column
ALTER TABLE Emp4 Add test varchar2 (10);
--Modify a column
ALTER TABLE Emp4 Modify Test varchar2 (20);
--Delete a column
ALTER TABLE emp4 drop column test;
--Add multiple columns
ALTER TABLE Emp4 Add (Test varchar2 (Ten), test2 number);
--Modify multiple columns
ALTER TABLE Emp4 Modify (test varchar2, Test2 varchar2 (20));
--Delete multiple columns
ALTER TABLE Emp4 drop (TEST,TEST2);

--Start the database as an administrator under Windows
net start ORACLESERVICEORCL--ORCL is the name of the DB instance you installed later
net start Oracleoradb11g_home1tnslistener--Non-essential

--Linux under SYSDBA User login, and then start the database
Sqlplus/as SYSDBA
Startup

--Sqlplus Landing mode
Sqlplus/as SYSDBA--Oracle SYS administrator logged on with operating system Authority authentication

Sqlplus/nolog
Conn/as SYSDBA--Oracle SYS administrator logged on with operating system Authority authentication

Sqlplus Sys/[email protected] as SYSDBA--Login with SYS user must use as SYSDBA

Sqlplus/nolog--does not expose password login mode in cmd or teminal
Conn Sys/password as Sysdba

Sqlplus--Login without revealing the password
Enter User-name:sys
Enter Password:password as Sysdba--the AS SYSDBA clause must be added to the login with the SYS user

Sqlplus Scott/[email Protected]--Non-admin user Login

Desc v$database; --Querying the table structure of the V$database database

--Execute SQL script in Sqlplus, the following two ways can be
START file_name
@file_name

--Determine if the table exists and delete if it exists
Declare
Num number;
Begin
Select COUNT (1) into NUM from all_tables where table_name = ' EMP ' and owner= ' SCOTT ';
If Num=1 Then
Execute immediate ' drop table EMP ';
End If;
End
/
--Create a table
CREATE TABLE EMP
(EMPNO number (4) Not NULL,
Ename VARCHAR2 (10),
JOB VARCHAR2 (9),
MGR Number (4),
HireDate DATE,
SAL Number (7, 2),
COMM Number (7, 2),
DEPTNO number (2));
You can load the above stored procedures in front of each create table.

--oracle to determine if a sequence exists, delete it if it exists

Declare
V_num number;

BEGIN
V_num is set to 0 each time----multiple deletions
V_num: = 0;
----Determine if sequence seq_name_1 exist (case-sensitive)
Select COUNT (0) into V_num from user_sequences where sequence_name = ' seq_business_process_index_id ';
----Delete immediately if present
If V_num > 0 Then
Execute immediate ' DROP SEQUENCE seq_business_process_index_id ';
End If;
END;

--set Sqlplus mode to show total rows
Show pagesize; --View the current pagesize
Set pagesize 300;

--Set Sqlplus mode to display line width
Show Linesize; --View the current linesize
Set Linesize 300;

--Modify the installation directory Glogin.sql file to ensure that the previous settings are permanently active
Set pagesize 300;
Set Linesize 300;

--Delete Table object
Select ' Drop table ' | | Segment_name from dba_segments where owner= ' Vpmuser ' and segment_type= ' TABLE ';
--Create Table Object
Select
' CREATE TABLE ' | | Segment_name | | ' AS SELECT * from ' | | segment_name | | ' @DBLINK '
From dba_segments where owner= ' Vpmuser ' and segment_type= ' TABLE ';

--Check whether the table is fully imported
Select segment_name from [email protected] where owner= ' vpmuser ' and segment_type= ' TABLE '
and (segment_name not like ' bin$% '
and segment_name not like '%201% ')
Minus
Select Segment_name from dba_segments where owner= ' Vpmuser ' and segment_type= ' TABLE ' and segment_name ' don't like ' bin$% '

--Query the statement of all tables of the user 1
Select t.table_name,t.comments from User_tab_comments t

--Query Statement 2 for all tables of the user:
Select R1, R2, R3, R5
From (select A.table_name R1, A.column_name R2, a.comments R3
From User_col_comments a),
(select T.table_name R4, t.comments R5 from User_tab_comments t)
where R4 = R1

--Find all indexes of the table (including index name, type, constituent column):
Select T., I.index_type from User_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = I.T. Able_name and t.table_name = table to query
--lookup table's primary key (including name, constituent column):
Select Cu.
From User_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and Au.constraint_type = ' P ' and au.table_name = table to query

--Lookup Table uniqueness constraints (including name, constituent columns):
Select column_name from User_cons_columns cu, user_constraints au where cu.constraint_name = Au.constraint_name and Au.con Straint_type = ' U ' and au.table_name = table to query

--Find the foreign key of the table (including the name, the table name of the reference table and the corresponding key name, which is divided into multi-step query):
SELECT * from user_constraints c where c.constraint_type = ' R ' and c.table_name = table to query

--Query the column name of the FOREIGN KEY constraint:
SELECT * from User_cons_columns cl where cl.constraint_name = FOREIGN key Name

--Query the column name of the key referencing the table:
SELECT * from User_cons_columns cl where cl.constraint_name = foreign key reference table key name

--Querying all columns of a table and their properties
Select t.*,c.comments from User_tab_columns t,user_col_comments c where t.table_name = C.table_name and T.column_name = c. column_name and t.table_name = table to query

--Backup table data
CREATE TABLE EMP as SELECT * FROM Scott.emp

--Restore Table data
INSERT INTO EMP select * FROM Scott.emp

--View the SQL that has been executed these are present in the shared pool, the user name needs to be capitalized and must have DBA authority
SELECT * from V$sqlarea t where T.parsing_schema_name in (' User name ') Order BY t.last_active_time Desc

--oracle11g Character Set changes (change here to Al32utf8)
Sqlplus SYS as SYSDBA

--Execute the following command, it is possible to cause data chaos in the database, so before the operation, to make the database backup operation
Shutdown immediate;
STARTUP MOUNT;
ALTER SESSION SET sql_trace=true;
ALTER SYSTEM ENABLE RESTRICTED SESSION;
ALTER SYSTEM SET job_queue_processes=0;
ALTER SYSTEM SET aq_tm_processes=0;
ALTER DATABASE OPEN;
ALTER DATABASE Character Set Internal_use Al32utf8;
ALTER SESSION SET Sql_trace=false;
Shutdown immediate;
Startup

--View Nls_lang information:
SELECT parameter, value from v$nls_parameters WHERE parameter like '%characterset ';

UPDATE Staff
SET modify_time = to_date (' 2016/04/22 00:01:00 ', ' yyyy/mm/dd hh24:mi:ss ')
WHERE Modify_time < To_date (' 2016/04/22 00:01:00 ', ' yyyy/mm/dd hh24:mi:ss ');

UPDATE Staff
SET modify_time = To_timestamp (' 19-03-2008 02:36:00.360000 ', ' dd-mm-yyyy hh24:mi:ss.ff ')
WHERE staff_id = ' 01 ';

To search for junk information:
SELECT T.object_name,t.type, t.original_name from User_recyclebin t;

Clear junk information: Purge table origenal_tablename;
Purge index origenal_indexname;

To delete all junk information from the Recycle Bin:
PURGE RecycleBin;
To remove a table that does not enter recycle:
drop table TableName Purge;

查看密码策略:

SELECT * from dba_profiles where profile= ' default ';

ORACLE12C Database Management Common statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.