0% found this document useful (0 votes)
8 views10 pages

To Work With Oracle Database 19c On A Linux Virtual Machine

a

Uploaded by

rishij043876
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

To Work With Oracle Database 19c On A Linux Virtual Machine

a

Uploaded by

rishij043876
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

To work with Oracle Database 19c on a Linux virtual machine (like in VirtualBox), you’ll need to

familiarize yourself with the basics of starting, stopping, and managing the database. Here’s a step-
by-step guide with the necessary commands and an overview of key functionalities.

1. Start and Stop Oracle Database 19c

Start the Oracle Database

Command:

sudo su - oracle

sqlplus / as sysdba

STARTUP;

Explanation:

 sudo su - oracle: Switches the user to oracle, which is required for database administration
tasks.

 sqlplus / as sysdba: Connects to SQL*Plus as a System Database Administrator (DBA).

 STARTUP;: Starts the Oracle Database instance.

Result: You will see messages indicating that the database is opening, and it will eventually display
Database opened.

Logic: This command initializes the database instance, mounts the database, and opens it for access.

Stop the Oracle Database

Command:

sudo su - oracle

sqlplus / as sysdba

SHUTDOWN IMMEDIATE;

Explanation:

 SHUTDOWN IMMEDIATE;: Shuts down the database without affecting active sessions
abruptly.

Result: You will see messages indicating that the database is closing, and it will eventually display
Database closed.

Logic: This command allows Oracle to terminate user sessions gracefully and write any pending
changes to disk.

2. Enable and Disable Oracle Listener

Start the Listener


Command:

lsnrctl start

Explanation: Starts the Oracle Listener, which handles incoming client connection requests.

Result: You will see a message stating Listening on: followed by the address and port.

Logic: The listener enables external connections to the database, allowing client applications to
access the database services.

Stop the Listener

Command:

lsnrctl stop

Explanation: Stops the Oracle Listener service.

Result: You will see a message stating LSNRCTL: stopping... followed by Service "..." has 0 instance(s).

Logic: Stopping the listener prevents new client connections until it is restarted.

Check Listener Status

Command:

lsnrctl status

Explanation: Displays the current status of the Oracle Listener.

Result: You will see details about the listener configuration, including services that are registered.

Logic: This command helps ensure the listener is running and that it recognizes the database
services.

3. Connect to the Database

Connect as SYSDBA

Command:

sqlplus / as sysdba

Explanation: Logs you into the database as a SYSDBA user.

Result: You will enter the SQL*Plus prompt, ready to execute SQL commands.

Logic: SYSDBA has full administrative privileges, allowing access to all database objects and
operations.

Connect as a Normal User


Command:

sqlplus username/password@ORCL

Explanation: Connects to the database as a specified user using a service name.

Result: You will enter the SQL*Plus prompt for that user.

Logic: Normal users have restricted access, based on the privileges granted to them.

4. Basic SQL Commands

Show Existing Tables

Command:

SELECT table_name FROM user_tables;

Explanation: Retrieves a list of all tables owned by the connected user.

Result: A list of table names will be displayed.

Logic: This command helps users understand the tables available in their schema.

Create a New Table

Command:

CREATE TABLE employees (

employee_id NUMBER PRIMARY KEY,

first_name VARCHAR2(50),

last_name VARCHAR2(50),

salary NUMBER

);

Explanation: Creates a new table named employees with specified columns.

Result: A message Table created. confirms successful creation.

Logic: This command defines the structure of the table to store employee data.

Insert Data into a Table

Command:

INSERT INTO employees (employee_id, first_name, last_name, salary)

VALUES (1, 'John', 'Doe', 50000);

Explanation: Inserts a new record into the employees table.


Result: A message 1 row inserted. confirms the operation.

Logic: This command adds a new employee record to the database.

View Data from a Table

Command:

SELECT * FROM employees;

Explanation: Retrieves all records from the employees table.

Result: Displays all rows of employee data.

Logic: This command allows users to view the contents of the table.

Update Data

Command:

UPDATE employees SET salary = 55000 WHERE employee_id = 1;

Explanation: Updates the salary of the employee with employee_id 1.

Result: A message 1 row updated. confirms the change.

Logic: This command modifies existing data in the table.

Delete Data

Command:

DELETE FROM employees WHERE employee_id = 1;

Explanation: Deletes the record of the employee with employee_id 1.

Result: A message 1 row deleted. confirms the operation.

Logic: This command removes specific records from the table.

5. Managing Tablespaces

Check Current Tablespaces

Command:

SELECT tablespace_name FROM dba_tablespaces;

Explanation: Retrieves a list of all tablespaces in the database.

Result: Displays the names of all existing tablespaces.

Logic: This command helps understand the logical storage structure of the database.
Create a New Tablespace

Command:

CREATE TABLESPACE my_tablespace DATAFILE '/u01/app/oracle/oradata/orcl/mytablespace01.dbf'


SIZE 50M;

Explanation: Creates a new tablespace named my_tablespace with a specified data file and size.

Result: A message Tablespace created. confirms successful creation.

Logic: This command defines a new logical storage unit for data.

Add Data Files to a Tablespace

Command:
ALTER TABLESPACE my_tablespace ADD DATAFILE
'/u01/app/oracle/oradata/orcl/mytablespace02.dbf' SIZE 100M;

Explanation: Adds another data file to the my_tablespace.

Result: A message Tablespace altered. confirms the operation.

Logic: This command allows expanding storage capacity for the tablespace.

6. Database Backup and Recovery

Start RMAN

Command:

rman target /

Explanation: Starts Oracle Recovery Manager (RMAN) to manage backups.

Result: You will enter the RMAN prompt.

Logic: RMAN provides powerful tools for backup and recovery.

Backup the Database

Command:

BACKUP DATABASE;

Explanation: Creates a backup of the entire database.

Result: RMAN will display progress messages as it backs up the database.

Logic: This command is essential for data protection and recovery strategies.
Restore the Database

Command:

RESTORE DATABASE;

Explanation: Restores the database from backup.

Result: RMAN will display progress messages indicating the restoration process.

Logic: This command is critical for recovering from failures or data loss.

7. Monitor the Database

View System Processes

Command:

SELECT username, sid, serial#, status FROM v$session WHERE username IS NOT NULL;

Explanation: Retrieves information about currently active user sessions.

Result: Displays a list of sessions with details such as username and status.

Logic: This command helps monitor user activity and troubleshoot performance issues.

View Running SQL Queries

Command:

SELECT sql_text FROM v$sql WHERE parsing_user_id = (SELECT user_id FROM dba_users WHERE
username = 'SCOTT');

Explanation: Retrieves SQL queries executed by a specific user.

Result: Displays the SQL text for queries run by the user SCOTT.

Logic: This command helps analyze which queries are running and their performance impact.

8. Check Oracle Database Status

Check Database Status

Command:

sqlplus / as sysdba

SELECT status FROM v$instance;

Explanation: Retrieves the current status of the database instance.

Result: Displays the instance status (e.g., OPEN, MOUNTED).

Logic: This command provides quick insight into whether the database is running.
9. Automatic Database Startup/Shutdown on System Boot

Configure Automatic Startup

Command:

sudo vi /etc/oratab

Change:

ORCL:/u01/app/oracle/product/19.0.0/dbhome_1:N

To:

ORCL:/u01/app/oracle/product/19.0.0/dbhome_1:Y

Explanation: Edit the oratab file to allow Oracle to start automatically on system boot.

Result: The database will start automatically during system boot.

Logic: This configuration is essential for high availability.

10. Security Management

Create a New User

Command:

CREATE USER new_user IDENTIFIED BY password;

Explanation: Creates a new database user with a specified password.

Result: A message User created. confirms successful creation.

Logic: This command establishes a new user account in the database.

Grant Privileges to a User

Command:

GRANT CONNECT, RESOURCE TO new_user;

Explanation: Grants specific privileges to the new user.

Result: A message Grant succeeded. confirms the operation.

Logic: This command provides necessary access rights to the user.

Conclusion

This guide provides a detailed overview of essential Oracle database commands, their explanations,
results, and underlying logic. You can use this as a quick reference for managing an Oracle Database
19c environment. If you have any further queries or need assistance with specific commands, feel
free to ask!

Oracle Database 19c Cheat Sheet


1. Start and Stop Oracle Database

Command Description Result

sudo su - oracle Switch to oracle user -

sqlplus / as sysdba Connect as SYSDBA user SQL*Plus prompt

STARTUP; Start the Oracle Database Database opened

SHUTDOWN IMMEDIATE; Stop the Oracle Database Database closed

2. Enable and Disable Oracle Listener

Command Description Result

lsnrctl start Start the Oracle Listener Listening on: address

lsnrctl stop Stop the Oracle Listener LSNRCTL: stopping...

lsnrctl status Check Listener Status Displays services registered

3. Connect to the Database

Command Description Result

sqlplus / as sysdba Connect as SYSDBA SQL*Plus prompt

sqlplus username/password@ORCL Connect as a specific user SQL*Plus prompt

4. Basic SQL Commands


Command Description Result

SELECT table_name FROM user_tables; Show existing tables List of table names

CREATE TABLE employees (...) Create a new table Table created

INSERT INTO employees (...) VALUES (...); Insert data into a table 1 row inserted

SELECT * FROM employees; View data from a table Displays all rows

UPDATE employees SET salary = ...; Update data in a table 1 row updated

DELETE FROM employees WHERE ...; Delete data from a table 1 row deleted

5. Managing Tablespaces

Command Description Result

SELECT tablespace_name FROM dba_tablespaces; Check current tablespaces List of tablespaces

Tablespace
CREATE TABLESPACE my_tablespace ...; Create a new tablespace
created

ALTER TABLESPACE my_tablespace ADD DATAFILE Add data files to a


Tablespace altered
...; tablespace

6. Database Backup and Recovery

Command Description Result

rman target / Start RMAN for backup management RMAN prompt

BACKUP DATABASE; Backup the entire database Progress messages displayed

RESTORE DATABASE; Restore the database from backup Progress messages displayed

7. Monitor the Database

Command Description Result

SELECT username, sid, serial#, status FROM v$session WHERE View active user List of active
username IS NOT NULL; sessions sessions

SELECT sql_text FROM v$sql WHERE parsing_user_id = (SELECT View running SQL Displays SQL
user_id FROM dba_users WHERE username = 'SCOTT'); queries text

8. Check Oracle Database Status


Command Description Result

sqlplus / as sysdba Connect as SYSDBA SQL*Plus prompt

SELECT status FROM v$instance; Check database instance status Displays instance status

9. Automatic Database Startup/Shutdown on System Boot

Command Description Result

sudo vi /etc/oratab Edit oratab file to enable automatic startup -

Change N to Y for the database entry Configure for automatic startup -

10. Security Management

Command Description Result

CREATE USER new_user IDENTIFIED BY password; Create a new database user User created

GRANT CONNECT, RESOURCE TO new_user; Grant privileges to the new user Grant succeeded

Note:

 Always ensure you have the necessary privileges to execute these commands.

 This cheat sheet is a quick reference guide; for more in-depth information, consult Oracle
documentation or your DBA.

You might also like