604. Oracle and DeveloperNotes
604. Oracle and DeveloperNotes
SAHIL S 1
SQL Constraints
Constraints ensure data integrity in tables.
Example:
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
salary DECIMAL(10,2) CHECK (salary > 0)
);
SQL Joins
Joins combine records from multiple tables
SAHIL S 2
SQL Subqueries & Views
• Subquery: A query inside another query.
SELECT name FROM students WHERE age = (SELECT MAX(age) FROM students);
• View: A virtual table based on a query.
CREATE VIEW student_view AS SELECT name, age FROM students;
Example:
CREATE TABLE employees (
emp_id INT,
salary NUMBER(10,2),
bonus DECIMAL(8,2)
);
SAHIL S 3
Example:
CREATE TABLE students (
student_id INT,
name VARCHAR(50),
remarks TEXT
);
Example:
CREATE TABLE orders (
order_id INT,
order_date DATE,
delivery_time TIMESTAMP
);
4. Other SQL Data Types
SAHIL S 4
Types of SQL Operators
SAHIL S 5
Operator Precedence (Execution Order)
Operators follow a specific order of execution, similar to BODMAS (Brackets, Orders,
Division, Multiplication, Addition, Subtraction) in Mathematics.
SAHIL S 6
PL/SQL
PL/SQL (Procedural Language/SQL) is an extension of SQL.
Allows procedural programming using loops, conditions, and exception handling.
PL/SQL is used for writing stored procedures, triggers, functions, and packages.
Features:
1. Supports procedural programming (IF, LOOP, CASE).
2. Allows error handling using EXCEPTION
3. Enhances performance by reducing multiple SQL calls.
4. Supports modular programming with procedures and functions.
5. Ensures data security with stored procedures.
PL/SQL block structure:
DECLARE -- (Optional) Declare variables
BEGIN -- (Mandatory) Execution section
-- SQL and PL/SQL statements
EXCEPTION -- (Optional) Error handling
-- Error-handling code
END;
/
SAHIL S 7
DBMS_OUTPUT.PUT_LINE('High Salary');
END IF;
IF-THEN-ELSE
IF salary > 50000 THEN
DBMS_OUTPUT.PUT_LINE('High Salary');
ELSE
DBMS_OUTPUT.PUT_LINE('Low Salary');
END IF;
CASE Statement
CASE
WHEN salary > 50000 THEN DBMS_OUTPUT.PUT_LINE('High Salary');
WHEN salary > 30000 THEN DBMS_OUTPUT.PUT_LINE('Medium Salary');
ELSE DBMS_OUTPUT.PUT_LINE('Low Salary');
END CASE;
Loops in PL/SQL
SAHIL S 8
SQL *PLUS
SQL*Plus is an interactive tool provided by Oracle for executing SQL commands and PL/SQL
blocks. t supports both command-line and GUI versions.
SQL*Plus Commands
1. Environment Commands
2. Formatting Commands
SAHIL S 9
Running SQL Scripts in SQL*Plus
1. Open Notepad or any text editor.
2. Write SQL commands and save as myscript.sql:
SELECT * FROM employees;
3. Run in SQL*Plus:
START myscript.sql;
SAHIL S 10
Stored Procedures
A Stored Procedure is a precompiled set of SQL statements stored in the database. It
executes as a single unit
Key Characteristics
Advanatges-
1.Because it is precompiled, it saves execution time.
2. Code Reusability
3. Centralized logic ensures uniform business rules across application
SAHIL S 11
Stored Procedure vs. Regular SQL Queries
SAHIL S 12
Functions in SQL
SQL Functions are built-in operations that perform calculations, modify data, and return
results.
Types of SQL Functions
SQL functions are categorized into two main types:
SAHIL S 13
C. Date & Time Functions (Work with date and time values)
SAHIL S 14
TRIGGER
A Trigger is a stored PL/SQL block that automatically executes when a specified event
occurs in the database. Triggers can be set to fire BEFORE or AFTER an event.
Advantages of Triggers
SAHIL S 15
CURSOR
A cursor in PL/SQL is a pointer to a result set of a SQL query. It is used to fetch and process
multiple rows from a database. Cursors are essential for handling query results row by row.
Advantages of Cursors
SAHIL S 16
JOIN
Joins in SQL are used to combine data from two or more tables based on a related column
SAHIL S 17
1.Inner join:
SELECT Students.student_name, Courses.course_name
FROM Students
ON Students.course_id = Courses.course_id;
2.Left join:
SELECT Students.student_name, Courses.course_name
FROM Students
ON Students.course_id = Courses.course_id;
3.Right join:
SELECT Students.student_name, Courses.course_name
FROM Students
ON Students.course_id = Courses.course_id;
SAHIL S 18
4.Full join:
SELECT Students.student_name, Courses.course_name
FROM Students
ON Students.course_id = Courses.course_id;
5.Cross join:
SELECT Students.student_name, Courses.course_name
FROM Students
SAHIL S 19
CLAUSES
SQL clause helps us to retrieve a set of records from the table by specifying a condition on
the columns or records of a table.
FROM Students
GROUP BY course_id
SAHIL S 20
(C) ORDER BY Clause
SAHIL S 21
(D) GROUP BY Clause
SAHIL S 22
CONSTRAINTS
SQL Constraints are rules applied to table columns to ensure data integrity and accuracy.
They prevent invalid, duplicate, or inconsistent data in the database.
Constraints are applied during table creation using the CREATE TABLE or later with
ALTER TABLE.
Key Points:
• Each student_id must be unique and cannot be NULL.
• A table can have only one PRIMARY KEY, but it can consist of multiple columns
(Composite Key).
Ensures referential integrity by linking a column to the PRIMARY KEY of another table.
Prevents invalid data entry in child tables.
CREATE TABLE Courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(50)
);
SAHIL S 23
student_name VARCHAR(50),
course_id INT,
FOREIGN KEY (course_id) REFERENCES Courses(course_id)
);
Key Points:
• course_id in Students must match a valid course_id in Courses.
• If a course_id does not exist in Courses, it cannot be inserted in Students.
Key Points:
• Each email must be unique but can be NULL.
• A table can have multiple UNIQUE columns.
Key Points:
• emp_name and salary must have values (cannot be NULL).
• Useful for mandatory fields like Name, Email, etc.
SAHIL S 24
(E) CHECK Constraint
Key Points:
• The price must be greater than 0.
• Helps enforce valid data ranges (e.g., age must be ≥18).
Key Points:
• If no value is inserted in order_status, it defaults to 'Pending'.
• Useful for setting default values like 0 for quantity, CURRENT_TIMESTAMP for dates,
etc.
SAHIL S 25
Transaction Control & Concurrency Control
1. Transaction Control in SQL
What is a Transaction?
SAHIL S 26
SAHIL S 27
FORMS
1. What is a Form in Oracle?
Forms in Oracle are graphical user interfaces (GUI) that allow users to view, insert,
update, and delete data from a database easily.
They provide an interactive way to interact with database tables without writing SQL
queries manually.
Used in Oracle Forms Builder, a tool in Oracle Developer Suite.
SAHIL S 28
REPORTS
1. What is a Report in Oracle?
Reports in Oracle are formatted documents that present database data in a structured
way.
Used for data analysis, summaries, invoices, charts, and printable documents.
Created using Oracle Reports Builder, part of Oracle Developer Suite.
Data Model → Defines the data source (SQL query, table, view).
Layout Model → Controls the report structure and formatting.
Parameter Form → Allows user input to filter data dynamically.
Triggers → PL/SQL code executed before/after the report runs.
Report Output → Can be displayed as PDF, HTML, Excel, or printed documents.
SAHIL S 29
SET OPERATORS
SET operators are special type of operators which are used to combine the result of two
queries. The queries must have the same number of columns.
Types of Set Operators in SQL
SAHIL S 30
Result of UNION
SAHIL S 31
ATTRIBUTE DECLARATIONS (%TYPE, %ROWTYPE)
Attribute declarations in PL/SQL are placeholders used to declare variables dynamically
based on existing database structures.
They ensure data consistency and reduce maintenance efforts by automatically
adapting to changes in database schema.
The two main types of attribute declarations in PL/SQL are:
• %TYPE → Inherits the data type of a specific column.
• %ROWTYPE → Inherits the structure (all columns) of a table or cursor.
%TYPE Attribute
Definition:
The %TYPE attribute is used to declare a variable that inherits the data type of a specific
column in a table or another variable.
This ensures that if the column’s data type changes, the variable automatically adapts.
Syntax:
variable_name table_name.column_name%TYPE;
Advantages of %TYPE:
%ROWTYPE Attribute
Definition:
The %ROWTYPE attribute is used to declare a record variable that inherits the structure
of an entire table row.
This means the record will have all columns and their respective data types from the
table.
Syntax:
record_name table_name%ROWTYPE;
SAHIL S 32
Advantages of %ROWTYPE:
SAHIL S 33
LIBRARY, ALERTS and LOV IN DEVELOPER 2000
1. Library
2. Alerts
What is an Alert?
List of Values (LOV) is a popup window that displays a list of valid values for a field in an
Oracle Form.
It helps users select values easily instead of manually entering them.
SAHIL S 34
Types of LOV in Oracle Forms
SAHIL S 35