Common operations of oracle database
The env command is used to display the environment variables already present in the system and to execute instructions in the defined environment.
[dsg@weizhu1 config]$ env |grep ORAORACLE_SID=orcls
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
1. Check the size of the data file
SQL> select sum( bytes)/1024/1024/1024 from dba_data_files;
2. View all tablespaces and tablespace sizes:
select tablespace_name,sum(bytes)/1024/1024 from dba_data_files group by tablespace_name;
TABLESPACE_NAME SUM(BYTES)/1024/1024
-- ---------------------------- --------------------
SYSAUX 600
UNDOTBS1 600
USERS 5
SYSTEM 700
SQL>
3. View the data files corresponding to all tablespaces:
SQL> select tablespace_name,file_name from dba_data_files;
TABLESPACE_NAME
------------------------------
FILE_NAME
--------------------------------------------------------------------------------
SYSTEM
/u01/app/oracle/oradata/orcls/system01.dbf
SYSAUX
/u01/app/oracle/oradata/orcls/sysaux01.dbf
UNDOTBS1
/u01/app/oracle/oradata/orcls/undotbs01.dbf
TABLESPACE_NAME
------------------------------
FILE_NAME
--------------------------------------------------------------------------------
USERS
/u01/app/oracle/oradata/orcls/users01.dbf
SQL>
4、修改数据文件大小:
alter database datafile '/u01/app/oracle/oradata/orcls/users01.dbf' RESIZE 30M;
5. All data files and sizes
SQL> select name, bytes/1024/1024 as M from v$datafile;
NAME
------------------------- -------------------------------------------------- -----
M
----------
/u01/app/oracle/oradata/orcls/system01.dbf
700
/u01/app/oracle/oradata/orcls/sysaux01.dbf
600
/u01/app /oracle/oradata/orcls/undotbs01.dbf
600
NAME
-------------------------------------- ------------------------------------------
M
------- ---
/u01/app/oracle/oradata/orcls/users01.dbf
5
SQL>
6. Add a data file
If you are adding a data file to an existing tablespace, use:
alter tablespace dsgt add datafile '/u01/app/oracle/oradata/orcl/dsgt04.dbf' size 30M;
alter tablespace dsgt add datafile '/u01/app/oracle/oradata/orcls/users04.dbf' size 30M;
if yes Create a new tablespace:
create tablespace dsgt datafile '/u01/app/oracle/oradata/orcls/dsgt01.dbf' size 30M;
create tablespace dsgt datafile '/u01/app/oracle/oradata/orcl/dsgt01.dbf' size 30M;
create tablespace dsgt2 datafile '/u01/app/oracle/oradata/orcls/dsgt02.dbf' size 30M;
7. Find the specific size of the current user table
select segment_name, segment_type, bytes/1024 from user_segments;
view the current user The total size of the table
select sum(bytes)/1024/1024 from user_segments;
8. Query all tables under the current user
select table_name from user_tables;
count the number of the current user's table below
select count(*) from user_tables;
view under dba permissions
select table_name from dba_tables where owner='HR';
select count(*) from all_tables where owner='HR'; #Note that the user name should be larger
select table_name from all_tables where owner='HR';
9. Modify the user default tablespace
alter user hr default tablespace dsgt;
Explanation: The above statement means to re-designate the table space for the hr user as dsgt;
Extension: Specify the table space when creating a user.
create user username identified by userpassword default tablespace userspace;
10. Count the number of rows in the table
select count(*) from table_name;