DDL FUNC
DDL FUNC
🔍 PART 1: Views
✅ What is a View?
● A view is like a virtual table based on the result of a SELECT statement.
● It doesn't store data — it just shows data from one or more tables.
✅ Key Characteristics
Feature Explanation
Read-Only (mostly) Usually can’t insert/update through it unless certain conditions are
met.
An updatable view is a view that allows you to perform INSERT, UPDATE, and DELETE
operations on the underlying base table via the view itself.
So, instead of going directly to the table, you can modify the data through the view — which
can simplify your queries and add extra layers of abstraction.
○ DISTINCT keyword
○ Joins
○ GROUP BY
● For instance, if your view combines sales data and customer info from two tables,
and you try to update the customer_name directly via the view — it won’t work.
UPDATE employee_salary
SET salary = 3800
WHERE emp_id = 101;
🔒 2. Limitations of Views
While views are super useful, they come with some limitations:
🚧 Performance:
● Views are not always fast, especially for complex queries. They have to re-run the
underlying query every time you access them.
● If your view involves large joins or aggregations, it might slow down performance.
🔄 Updatability:
● Some views are not updatable (especially complex ones), so you might need to work
directly with the base tables.
🔑 Security:
● Views can expose sensitive data. If you're not careful, you might accidentally allow
users to access too much data (like via a SELECT * view).
🔄 Read-only:
● Views are read-only if they’re created from multiple tables or contain aggregation
operations. You can’t directly modify the underlying data via them.
🧠 Think of it like:
“Run this heavy query once, store the result, and use it many times fast.”
This is very much like querying a view, but with dynamic filtering thanks to parameters.
So instead:
👉 That’s why people informally call them parameterized views — but in SQL terms, they’re
functions.
✅ Key Characteristics
Feature Explanation
SQL-only logic You can only use SQL statements (no IFs, loops, etc.).
Returns values Must have a RETURNS clause — can return a single value (e.g.,
INT) or a table.
Real-time execution Function runs when called, just like a view does.
Can accept You define inputs in the function signature.
parameters
🏢 Real-Life Example
🛒 Scenario: E-Commerce Company
You work in an e-commerce company. The business team frequently asks:
🧠 Problem:
Running this query every time with different customer IDs is repetitive.
SELECT get_monthly_order_count(105);
Instead of writing a full SELECT COUNT(*) FROM orders... query each time.
ChatGPT сказал:
Great question! The short answer is: No, you can’t run IF or WHILE loops directly in plain
SQL — they only work inside a procedural function or block.
● FUNCTIONs
● PROCEDUREs
Example:
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM users WHERE email = '[email protected]') THEN
RAISE NOTICE 'User exists!';
END IF;
END;
$$ LANGUAGE plpgsql;
If you want logic and control flow like a real program, you need to write it inside
a procedural function.
PART 3. Procedural Language Function — Theory Summary
✅ Definition:
A Procedural Language Function is a user-defined function written using a
programming-style language like PL/pgSQL (in PostgreSQL). It allows you to perform
step-by-step operations, use control flow (like IF, WHILE, LOOP), and handle errors — all
from within the database.
🎯 Purpose:
Used to perform complex business logic that:
🧩 Key Features:
Feature Explanation
Accepts parameters You can pass inputs like numbers, strings, etc.
🏦 Real-Life Example:
Bank Transfer Function
🧠 Simple Analogy:
It’s like a recipe you follow step-by-step in the kitchen, including what to do if
you run out of eggs (error handling).
○ Variables
“Hey, this isn’t just plain SQL — it’s SQL with programming logic.”
✅ First — What is DDL?
DDL (Data Definition Language) is the part of SQL used to define and manage the structure
of database objects — not the data itself.
● CREATE
● ALTER
● DROP
● TRUNCATE
These affect how the database is structured, not what data is inside.
📌 You are defining a new object (a view) in the database — so it's a DDL action.
Example:
When you create a function, you're also defining a new object (the function itself), which
becomes part of the database's schema.
📌 Since you're creating something that changes the structure of the database (by adding a
function object), it's DDL.
Example:
Again — you’re not inserting, updating, or selecting data — you're defining logic structure.
🎯 Why It Matters
Even though views and functions use SELECT or DML-like logic inside them, the CREATE
action is what classifies them under DDL.
It's not what's inside the function or view — it's the fact that you're defining a named object
in the schema.
🧠 Quick Recap:
Feature Is It DDL? Why?
CREATE
TABLE
✅ Defines a new table structure
CREATE
FUNCTION
✅ Defines a new logical object
SELECT,
INSERT
❌ These are DML (Data Manipulation) — they deal with data,
not structure
🔍 LANGUAGE SQL vs LANGUAGE plpgsql
Feature LANGUAGE SQL LANGUAGE plpgsql
Query Language Functions are best when I need to return results from a simple SQL
query, like generating a monthly sales report, getting top customers, or filtering data
based on parameters like dates or product categories. They’re written using
LANGUAGE SQL, and they don’t support loops or conditional logic. I think of them like
"smart, reusable queries" — almost like parameterized views.
On the other hand, Procedural Language Functions (like LANGUAGE plpgsql) are
useful when I need to handle complex business logic — for example, applying bonuses
based on performance, looping through customers to apply updates, or using IF,
WHILE, and exception handling. These functions behave more like mini-programs
inside the database, with full control flow support.
🔹 For example, if I need to return top 10 selling categories in a given quarter — I’d use
🔸
a Query Language Function.
But if I need to check each employee’s sales and calculate bonuses differently —
I’d use a Procedural Function with IF logic.
In short, I choose based on the complexity of logic. If it's just one SQL query: use SQL
function. If it needs step-by-step logic or loops: go with procedural.