SQL DBA
Table of Contents
- 1 SQL DBA
- 1.1 SQL statement for creating table space
- 1.2 SQL statement for alter table space — ADD DATAFILE
- 1.3 SQL statement for alter table space — RESIZE DATA FILE SIZE
- 1.4 SQL statement for creating user
- 1.5 SQL statement for update / change user password
- 1.6 SQL statement for assign table space quota to user
- 1.7 SQL statement for assign table space unlimited quota to user
- 1.8 SQL statement for Lock the user
- 1.9 SQL statement for Un-Lock the user
- 1.10 SQL statement for to make the user password expire
- 1.11 SQL statement for DBA grants to user
- 1.12 SQL statement for individual objects grants to user
- 1.13 SQL statement for Running Queries
- 2 SQL statement for Objects creation date to check
- 3 Public Database Link
SQL statement for creating table space
Syntax
CREATE TABLESPACE <TablespaceName> DATAFILE '<Datafile Path>' SIZE 100M AUTOEXTEND ON MAXSIZE UNLIMITED ;
Example
CREATE TABLESPACE TUTORIALTBS DATAFILE 'C:oraclexeoradataXETUTORIALTBS.DBF' SIZE 100M AUTOEXTEND ON MAXSIZE UNLIMITED ;
SQL statement for alter table space — ADD DATAFILE
Syntax
ALTER TABLESPACE <TablespaceName> ADD DATAFILE '<Datafile Path>' SIZE 4G AUTOEXTEND ON MAXSIZE UNLIMITED;
Example
ALTER TABLESPACE TUTORIALTBS ADD DATAFILE 'C:oraclexeoradataXETUTORIAL01.DBF' SIZE 4G AUTOEXTEND ON MAXSIZE UNLIMITED;
SQL statement for alter table space — RESIZE DATA FILE SIZE
Syntax
ALTER DATABASE DATAFILE '<Datafile Path>' RESIZE 900M;
Example
ALTER DATABASE DATAFILE 'C:oraclexeoradataXETUTORIALTBS.DBF' RESIZE 900M;
SQL statement for creating user
Syntax
CREATE USER <UserName> IDENTIFIED BY <Password> DEFAULT TABLESPACE <TablespaceName> TEMPORARY TABLESPACE TEMP ACCOUNT UNLOCK;
Example
CREATE USER TESTUSER IDENTIFIED BY TESTPASSWORD DEFAULT TABLESPACE TUTORIALTBS TEMPORARY TABLESPACE TEMP;
SQL statement for update / change user password
Syntax
ALTER USER <UserName> IDENTIFIED BY <Password>;
Example
ALTER USER TESTUSER IDENTIFIED BY TESTNEWPASSWORD;
SQL statement for assign table space quota to user
Syntax
ALTER USER <UserName> QUOTA 100M ON <Tablespace Name>;
Example
ALTER USER TESTUSER QUOTA 100M ON TUTORIALTBS;
SQL statement for assign table space unlimited quota to user
Syntax
ALTER USER <UserName> QUOTA UNLIMITED ON <Tablespace Name>;
Example
ALTER USER TESTUSER QUOTA UNLIMITED ON TUTORIALTBS;
SQL statement for Lock the user
Syntax
ALTER USER <UserName> ACCOUNT LOCK;
Example
ALTER USER TESTUSER ACCOUNT LOCK;
SQL statement for Un-Lock the user
Syntax
ALTER USER <UserName> ACCOUNT UNLOCK;
Example
ALTER USER TESTUSER ACCOUNT UNLOCK;
SQL statement for to make the user password expire
Syntax
ALTER USER <UserName> IDENTIFIED <Password> PASSWORD EXPIRE;
Example
ALTER USER TESTUSER IDENTIFIED <Password> PASSWORD EXPIRE;
SQL statement for DBA grants to user
Syntax
GRANT DBA TO <UserName>;
Example
GRANT DBA TO TESTUSER;
SQL statement for individual objects grants to user
GRANT CREATE ANY TABLE TO TESTUSER; GRANT CREATE ANY VIEW TO TESTUSER; GRANT EXECUTE ON SYS.DBMS_CRYPTO TO TESTUSER; GRANT EXECUTE ON SYS.DBMS_DDL TO TESTUSER; GRANT DROP ANY VIEW TO TESTUSER; GRANT SELECT ANY DICTIONARY TO TESTUSER; GRANT SELECT ANY DICTIONARY TO TESTUSER; GRANT EXECUTE ON SYS.DBMS_CRYPTO TO TESTUSER; GRANT EXECUTE ON SYS.DBMS_DDL TO TESTUSER; GRANT EXECUTE ON SYS.DBMS_STATS TO TESTUSER;
SQL statement for Running Queries
User Running Queries
By using the following statements we can check running queries(active sessions) for particular user.
Syntax
SELECT a.sid, a.username,b.sql_id, b.sql_fulltext FROM V$SESSION a, V$SQL b WHERE a.sql_id = b.sql_id and a.status = 'status' and a.username = '<db-user>';
Example
SELECT a.sid, a.username,b.sql_id, b.sql_fulltext from V$SESSION a, V$SQL b WHERE a.sql_id = b.sql_id and a.status = 'ACTIVE' and a.username = 'TESTUSER';
All Running Queries
Syntax
SELECT a.sid, a.username,b.sql_id, b.sql_fulltext FROM V$SESSION a, V$SQL b WHERE a.sql_id = b.sql_id and a.status = '<status>' ;
Example
SELECT a.sid, a.username,b.sql_id, b.sql_fulltext from V$SESSION a, V$SQL b WHERE a.sql_id = b.sql_id and a.status = 'ACTIVE' ;
Example
SELECT sesion.sid,sesion.serial#, sql_text FROM v$sqltext sqltext, v$session sesion WHERE sesion.sql_hash_value = sqltext.hash_value AND sesion.sql_address = sqltext.address AND sesion.username is not null order by sqltext.piece
SQL statement for Objects creation date to check
By using the following statement we can check objects creation details.
Types of Objects
- CONSUMER GROUP
- INDEX PARTITION
- SEQUENCE
- QUEUE
- SCHEDULE
- TABLE PARTITION
- RULE
- PROCEDURE
- OPERATOR
- LOB PARTITION
- WINDOW
- DATABASE LINK
- LOB
- PACKAGE
- PACKAGE BODY
- LIBRARY
- RULE SET
- PROGRAM
- TYPE BODY
- CONTEXT
- XML SCHEMA
- TRIGGER
- JOB CLASS
- UNDEFINED
- DIRECTORY
- TABLE
- INDEX
- SYNONYM
- VIEW
- FUNCTION
- WINDOW GROUP
- INDEXTYPE
- CLUSTER
- TYPE
- RESOURCE PLAN
- EVALUATION CONTEXT
- JOB
SELECT created,object_name,owner FROM dba_objects WHERE owner = '<db-user>' AND object_type = '<object-name>' AND object_name not like 'BIN%' AND to_date(created,'DD-MM-YY')='<creation-date>'
Example
SELECT created,object_name,owner FROM dba_objects WHERE owner = 'TESTUSER' AND object_type = 'TABLE' AND object_name not like 'BIN%' AND to_date(created,'DD-MM-YY')='11-May-2010'
dba_objects table contains the following metadata.
Query: describe dba_objects; output Name Null Type -------------- ---- ------------- OWNER VARCHAR2(30) OBJECT_NAME VARCHAR2(128) SUBOBJECT_NAME VARCHAR2(30) OBJECT_ID NUMBER DATA_OBJECT_ID NUMBER OBJECT_TYPE VARCHAR2(19) CREATED DATE LAST_DDL_TIME DATE TIMESTAMP VARCHAR2(19) STATUS VARCHAR2(7) TEMPORARY VARCHAR2(1) GENERATED VARCHAR2(1) SECONDARY VARCHAR2(1)
Public Database Link
- Use the CREATE DATABASE LINK statement to create a database link.
- A database link is a schema object in one database that enables you to access objects on another database.
- The other database need not be an Oracle Database system. However, to access non-Oracle systems you must use Oracle Heterogeneous Services.
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.