Skip to main content

Welcome to DBA Master – Database Tips, Tricks, and Tutorials

Welcome to DBA Master ! This blog is dedicated to all things related to database administration , SQL optimization , and performance tuning . Whether you're a beginner or a seasoned DBA, you'll find practical guides, troubleshooting tips, and real-world tutorials to help you work smarter with data. What to Expect: SQL performance tuning tips Indexing strategies Backup and recovery best practices High availability and replication techniques Database creation, configuration, and setup Monitoring queries and scripts for proactive performance management Migration guides across different database platforms Security essentials and best practices Recommended tools for DBAs Real-world error fixes and how to solve them Stay tuned — exciting content is coming soon. Feel free to bookmark and share: www.dbamaster.com ! Thanks for visiting!

Operators, Condition statements, CASE and looping structures

Operators:

Operator Type

Operator

Description

Example

Arithmetic

+

Addition

result := 10 + 5;


-

Subtraction

result := 10 - 5;


*

Multiplication

result := 10 * 5;


/

Division

result := 10 / 5;


MOD

Modulus (remainder)

result := 10 MOD 3;

Relational

=

Equal to

result := (a = b);


!=/<>

Not equal to

result := (a != b);


Greater than

result := (a > b);


Less than

result := (a < b);


>=

Greater than or equal to

result := (a >= b);


<=

Less than or equal to

result := (a <= b);

Logical

AND

Logical AND

result := (a > b) AND (c < a);


OR

Logical OR

result := (a > b) OR (b > 5);


NOT

Logical NOT

result := NOT (a > b);

Concatenation

`


`

Assignment

:=

Assignment

a := 10;

 Example 1:

DECLARE

a NUMBER := 10;

b NUMBER := 5;

c NUMBER;

d NUMBER;

e NUMBER;

f NUMBER;

BEGIN

c := a + b;

d := a - b;

e := a * b;

f := a / b;

DBMS_OUTPUT.PUT_LINE('Addition: ' || c);

DBMS_OUTPUT.PUT_LINE('Subtraction: ' || d);

DBMS_OUTPUT.PUT_LINE('Multiplication: ' || e);

DBMS_OUTPUT.PUT_LINE('Division: ' || f);

END;

Example 2:

DECLARE

-- Constants and variables

pi CONSTANT NUMBER := 3.14159;

radius NUMBER := 7;

height NUMBER := 10;

area NUMBER;

volume NUMBER;

result BOOLEAN;

first_name VARCHAR2(50) := 'John';

last_name VARCHAR2(50) := 'Doe';

full_name VARCHAR2(100);

BEGIN

-- Arithmetic operations

area := pi * radius * radius;  -- Area of a circle

volume := pi * radius * radius * height;  -- Volume of a cylinder

-- Relational operations

result := (area > 100);  -- Check if the area is greater than 100

-- Logical operations

result := (area > 100) AND (volume < 1000);  -- Check if area > 100 and volume < 1000

-- Concatenation

full_name := first_name || ' ' || last_name;  -- Combine first and last names

-- Output results

DBMS_OUTPUT.PUT_LINE('Pi: ' || pi);

DBMS_OUTPUT.PUT_LINE('Radius: ' || radius);

DBMS_OUTPUT.PUT_LINE('Height: ' || height);

DBMS_OUTPUT.PUT_LINE('Area of Circle: ' || area);

DBMS_OUTPUT.PUT_LINE('Volume of Cylinder: ' || volume);

DBMS_OUTPUT.PUT_LINE('Area > 100: ' || result);

DBMS_OUTPUT.PUT_LINE('Full Name: ' || full_name);

END;

Conditional Statements:

·        IF-THEN

·        IF-THEN-ELSE

·        IF-THEN-ELSIF-ELSE

·        CASE

IF-THEN Example

DECLARE

salary NUMBER := 4000;

BEGIN

IF salary > 3000 THEN

DBMS_OUTPUT.PUT_LINE('Salary is greater than 3000.');

END IF;

END;

IF-THEN-ELSE Example

DECLARE

salary NUMBER := 2500;

BEGIN

IF salary > 3000 THEN

DBMS_OUTPUT.PUT_LINE('Salary is greater than 3000.');

ELSE

DBMS_OUTPUT.PUT_LINE('Salary is 3000 or less.');

END IF;

END;

IF-THEN-ELSIF-ELSE Example

DECLARE

salary NUMBER := 2500;

BEGIN

IF salary > 5000 THEN

DBMS_OUTPUT.PUT_LINE('Salary is greater than 5000.');

ELSIF salary > 3000 THEN

DBMS_OUTPUT.PUT_LINE('Salary is between 3001 and 5000.');

ELSE

DBMS_OUTPUT.PUT_LINE('Salary is 3000 or less.');

END IF;

END;

CASE Statement Example

The CASE statement works like a switch-case structure found in other languages

DECLARE

grade CHAR(1) := 'B';

BEGIN

CASE grade

WHEN 'A' THEN

DBMS_OUTPUT.PUT_LINE('Excellent');

WHEN 'B' THEN

DBMS_OUTPUT.PUT_LINE('Good');

WHEN 'C' THEN

DBMS_OUTPUT.PUT_LINE('Average');

ELSE

DBMS_OUTPUT.PUT_LINE('Invalid grade');

END CASE;

END;

Loop Structures:

loops are used to execute a block of statements repeatedly. 

  • BASIC Loop
  • WHILE Loop
  • FOR Loop

Basic Loop Example

DECLARE

counter NUMBER := 1;

BEGIN

LOOP

DBMS_OUTPUT.PUT_LINE('Counter: ' || counter);

counter := counter + 1;

 IF counter > 5 THEN

EXIT;  -- Exit the loop when counter is greater than 5

END IF;

END LOOP;

END;

While Loop Example:

DECLARE

counter NUMBER := 1;

BEGIN

WHILE counter <= 5 LOOP

DBMS_OUTPUT.PUT_LINE('Counter: ' || counter);

counter := counter + 1;

END LOOP;

END;

For Loop Example:

BEGIN

FOR i IN 1..5 LOOP

DBMS_OUTPUT.PUT_LINE('Value of i: ' || i);

END LOOP;

END;

DML in PLSQL Example

BEGIN

-- Insert a new employee record

INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES (1003, 'Alice', 'Johnson', 5500);

-- Update the salary of the inserted employee

UPDATE employees SET salary = salary + 500 WHERE employee_id = 1003;

-- Delete the employee record

DELETE FROM employees WHERE employee_id = 1003;

-- Commit the transaction to make changes permanent

COMMIT;

DBMS_OUTPUT.PUT_LINE('DML operations completed successfully.');

END;

DRL in PLSQL Example:

DECLARE

-- Variables for direct assignment and column datatype

v_employee_salary employees.salary%TYPE;

v_employee_name employees.first_name%TYPE;

-- Variable for row datatype

v_employee_record employees%ROWTYPE;

BEGIN

-- Direct Assignment: Retrieve a single column value

SELECT salary   INTO v_employee_salary  FROM employees   WHERE employee_id = 1001;

-- Output the retrieved salary

DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_employee_salary);

-- Retrieve another column value using direct assignment

SELECT first_name INTO v_employee_name FROM employees  WHERE employee_id = 1001;

-- Output the retrieved first name

DBMS_OUTPUT.PUT_LINE('Employee First Name: ' || v_employee_name);

-- Row Datatype: Retrieve an entire row

SELECT *  INTO v_employee_record FROM employees  WHERE employee_id = 1001;

-- Output the retrieved row data

DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employee_record.employee_id);

DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_record.first_name || ' ' || v_employee_record.last_name);

DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || v_employee_record.salary);

END;


Comments

Popular posts from this blog

Oracle Database 19C Performance Tunning - PART 1

Advantages: 1. Improved Query Performance •    Optimized SQL execution plans lead to faster query response times. •    Reduces unnecessary full table scans and improves indexing strategies. •    Parallel execution tuning speeds up large data processing tasks. 2. Better Resource Utilization •    Efficient use of CPU, memory, disk I/O, and network resources. •    Reduces contention on Redo Logs, Undo Tablespaces, and Buffer Cache. •    Helps in load balancing across multiple instances in RAC (Real Application Clusters). 3. Increased System Scalability •    Ensures that the database can handle a growing number of users and transactions. •    Proper tuning allows scaling without degrading performance. •    Optimized parallel processing ensures better performance on multi-core servers. 4. Lower Infrastructure Costs •    Reduces the need for add...

Oracle RMAN Backup And Restore

RMAN: (Oracle 8) RMAN (Recovery Manager) is a utility provided by Oracle Database to perform backup, restore, and recovery operations. It is a command line tool. Features of RMAN in Oracle 19c Comprehensive Backup Capabilities: Full and incremental backups. Block-level backups for efficient data storage. Archived redo log backups. Fast Recovery Area (FRA) integration for centralized backup storage. Efficient Recovery Options: Point-in-time recovery (PITR). Complete and incomplete recovery. Flashback database capabilities for quick undo of changes. Multitenant Database Support: RMAN fully supports container databases (CDBs) and pluggable databases (PDBs). Provides flexibility to back up and recover individual PDBs or entire CDBs. Automatic Space Management: Manages disk space in the FRA. Automatically deletes obsolete backups and archived logs. Data Deduplication and Compression: Backup optimization through block-level deduplication. Built-in compression algorithms to reduce storage req...

Oracle 19c Database Software Installation in OEL8

 Pre-requisites for OS level:            Set the static IP Address     Disable the Firewall (systemctl stop firewalld & systemctl disable firewalld)     set SELINUX=permissive on /etc/selinux/config  ##Need to restart the server use init 6 Oracle Installation Pre-requisites Methods     Automatic Setup     Manual Setup      Automatic requisites Setup: (avoid step 1 to step 5): dnf install -y oracle-database-preinstall-19c Install the dependencies: curl -o oracle-database-preinstall-19c-1.0-2.el8.x86_64.rpm https://yum.oracle.com/repo/OracleLinux/OL8/appstream/x86_64/getPackage/oracle-database-preinstall-19c-1.0-2.el8.x86_64.rpm dnf -y localinstall oracle-database-preinstall-19c-1.0-2.el8.x86_64.rpm Manual Setup: step 1: Add the karenl parameters and values vi /etc/sysctl.conf     fs.file-max = 6815744 kernel.sem = 250 32000 100 128 kernel....