Roles, queries, and transactions

Roles around a database, queries vs statements, DDL DML DCL, the data dictionary, transactions, ACID, and knowledge bases — with the usual note-taking shortcuts corrected.

Beginner 32 min read SQLDBAQuery
Lesson 2 / 7 0%
View path

Roles, SQL, and transactions

If you connect roles + SQL commands + transactions + ACID correctly, the rest of SQL gets much easier.

Along the way we flag where lecture notes oversimplify or are not scientifically precise — so you are ready for the exam and do not learn the standard terms wrong.

1. Build the big picture first

In the previous lesson we had:

text
User / Application → DBMS → Database

Now the questions are:

  • Who decides what data we should have?
  • Who manages the database?
  • How does an application ask the DBMS for something?
  • What happens when several operations must run together?

This lesson answers exactly those questions.

2. Who works with the database?

Imagine a university system.

Students, teachers, the registrar, and finance all use it — but they do not share one role.

RoleFull nameSimple job
End UserEnd userUses the system
DAData AdministratorSets data policy and needs
DBADatabase AdministratorManages the database technically
DB Developer / ProgrammerDatabase programmerImplements code, queries, and needed structures
3. What is an end user?

End user uses the system but does not need to know SQL or database design.

A registrar clicks “enroll student” and may never see what runs behind that button:

sql
INSERT INTO students ...

So the end user works with the system, not necessarily with the database directly.

Examples: a student, a bank teller, a registrar, a shop clerk.

4. What is a DA?

DA = Data Administrator

This role focuses on data policy and management at the organization level.

A university must decide:

  • What student information do we keep?
  • Do we also need medical data?
  • Who may see grades?
  • How many years should we retain records?
text
student_id
name
national_code
major
phone

These are organizational decisions more than technical ones.

The DA says what data we have and under what policy it is managed.

In real organizations the DA/DBA boundary is not always the same, and a separate DA job title may not exist.

For the course model, remember:

DA = data policy maker
5. What is a DBA?

DBA = Database Administrator — the technical manager of the database.

Suppose the organization decided: “A student must not see another student’s grade.” That is an organizational need. Someone must implement it technically.

A DBA may own:

  • User management
  • Permissions
  • Backup
  • Recovery
  • Performance
  • Security
  • Monitoring
  • Database maintenance
The DA decides data policy; the DBA manages the database environment technically.
6. What is a DBP?

The notes say DBP = Database Programmer. In today’s market you more often see Database Developer or SQL Developer.

They write queries, build procedures, work with tables, and develop the database side of the application.

sql
SELECT *
FROM students
WHERE major = 'Computer';

Or they design a stored procedure to place an order.

Mental model:

text
DA
↓ data policy

DBA
↓ technical database management

Database Developer
↓ implementation and queries

End User
↓ uses the system

In a real project the jobs often overlap.

7. What is a query?

This part matters a lot.

A query is a request to retrieve or inspect information from the database.

Example: show every purchase made by Ali.

sql
SELECT *
FROM purchases
WHERE customer_name = 'Ali';

That means: from the purchases table, return rows whose customer_name is Ali.

8. Is every SQL command a query?

In everyday programming we often call every SQL statement a query. More precisely:

sql
SELECT * FROM students;

is clearly a query — it asks a question of the data. But:

sql
CREATE TABLE students (...);

is more an SQL statement than a query in the narrow sense.

The broader term is SQL statement.

9. Why is SQL declarative?

SQL is mainly a declarative language.

You tell the DBMS what you want, not exactly how to find it.

sql
SELECT *
FROM students
WHERE city = 'Tehran';

You did not say “start at the first record, then read memory…”. You only said you want students in Tehran.

The DBMS chooses how to run it. That choice is usually made by a query optimizer.

10. What is a host language?

Suppose the app is written in C#. That program may run SQL inside it.

C# is the main application language; SQL is used to talk to the database.

Older sources call C# or Java the host language.

“SQL is the guest” is a teaching metaphor, not a term you must memorize.

11. Now DDL, DML, and DCL

This part is often exam material. A useful metaphor:

Treat the database as a building.

  • DDL builds the building.
  • DML changes what is inside.
  • DCL decides who may enter and act.
12. What is DDL?

DDL = Data Definition Language. It works with database structure.

sql
CREATE TABLE students (
    id INT,
    name VARCHAR(100)
);

That creates the table.

ALTER changes an existing structure:

sql
ALTER TABLE students
ADD city VARCHAR(50);

The table is still students, but it gained a column.

DROP removes a database object:

sql
DROP TABLE students;

The table itself is gone.

For now, remember CREATE, ALTER, DROP as DDL.

13. What is DML?

DML = Data Manipulation Language. We are not building table structure; we work with the rows inside.

Insert:

sql
INSERT INTO students (id, name)
VALUES (1, 'Sara');

Update:

sql
UPDATE students
SET city = 'Isfahan'
WHERE id = 1;

Delete data:

sql
DELETE FROM students
WHERE id = 1;

And reading data:

sql
SELECT *
FROM students;

Many courses teach SELECT as DML. Some taxonomies split it out as DQL = Data Query Language.

If your instructor said SELECT → DML, answer that way in that class — and know the DQL split also exists.

14. DELETE vs DROP

A classic exam question. Suppose students is a table.

If you say:

sql
DELETE FROM students;

the rows are removed, but the table remains. But:

sql
DROP TABLE students;

the table itself is removed.

text
DELETE → Data

DROP → Structure
15. What is DCL?

DCL = Data Control Language — control of access.

Give Ali SELECT permission:

sql
GRANT SELECT
ON students
TO ali;

To take it back:

sql
REVOKE SELECT
ON students
FROM ali;
text
GRANT  → give permission

REVOKE → take permission away
16. A table worth memorizing
GroupMain jobExamples
DDLStructureCREATE, ALTER, DROP
DMLDataINSERT, UPDATE, DELETE, usually SELECT
DCLPermissionGRANT, REVOKE

A memory hook:

  • DDL → Design
  • DML → Modify data
  • DCL → Control

Not etymologically exact, but useful for recall.

17. Data dictionary and metadata

The DBMS must know which tables exist, which columns they have, each column’s type, which users exist, and which permissions they hold.

That information about the database is metadata.

students.name → VARCHAR(100) is about structure. “Sara” is data; the fact that name is VARCHAR(100) is metadata.

The DBMS keeps this metadata in a data dictionary or system catalog.

The data dictionary is the set of metadata about database structure and objects.

For example: table name, column name, data type, constraints, indexes, users, privileges — and more, depending on the DBMS.

18. What is a transaction?

This is the most important part of the lesson.

A transaction is a set of operations the system treats as one logical unit of work.

Classic example: transfer 1,000,000. Two main operations:

text
Account A → -1,000,000

Account B → +1,000,000

These two operations are not independent. They must happen together.

19. Where is the problem?

Suppose the first step ran:

text
A - 1,000,000

Then power failed, and this never ran:

text
B + 1,000,000

Money left A but never reached B. The system is now wrong.

So these two operations are one transaction: both succeed, or neither does.

20. What is COMMIT?

If every operation in the transaction succeeded:

sql
COMMIT;

Conceptually: make the changes final.

text
A = -1M
B = +1M

COMMIT

The transaction has finished successfully.

21. What is ROLLBACK?

If something goes wrong:

sql
ROLLBACK;

Undo the changes made by this transaction.

text
A = -1M

ERROR!

ROLLBACK

A returns to its previous state.

Some notes say ABORT. Abort means the transaction failed or was cancelled. In SQL the command you usually use is ROLLBACK.

text
COMMIT   → make final

ROLLBACK → undo
22. Now ACID

The four main transaction properties are remembered as ACID:

  • A → Atomicity
  • C → Consistency
  • I → Isolation
  • D → Durability

Do not only memorize them — understand them.

23. A — Atomicity

Atomicity = all or nothing

A transaction cannot be left half-done.

In a transfer, debit ✔ and credit ✘ is not acceptable.

Either both succeed, or neither does.

All or nothing
24. C — Consistency

Consistency means a transaction must take the database from one valid state to another valid state.

Example rule: balance >= 0

If an account has 500,000, a 1,000,000 transfer must not leave balance = -500,000 when the rules forbid it.

A transaction must not break the database’s valid rules.
25. I — Isolation

Isolation keeps concurrent transactions from interfering incorrectly.

Two people withdraw from the same account at once. Balance is 1,000,000. A wants 700,000 and B wants 700,000 at the same time.

If the DBMS does not control concurrency, both may see 1,000,000 and both withdraw. That is a concurrency problem.

Isolation stops concurrent transactions from leaving a wrong combined result.

26. Does isolation always mean locking?

No. This is a place many notes oversimplify.

Locking is one technique. DBMS products can also use MVCC — Multi-Version Concurrency Control.

Isolation is the result we want; locking is one technique for getting there.
27. D — Durability

Durability means if the DBMS said the commit succeeded, the change must not vanish a few seconds later — even after a crash.

Does durability mean mirroring? No. Notes that say that are imprecise.

Mirrors or replication can help, but they are not the definition. A DBMS can guarantee durability with a transaction log, write-ahead logging, recovery, and persistent storage.

Durability: a committed change must survive a crash.
28. Learn ACID with one full transfer

Suppose:

text
A = 5,000,000
B = 2,000,000

We transfer 1,000,000. Afterward:

text
A = 4,000,000
B = 3,000,000
  • Atomicity — A must not be debited alone.
  • Consistency — database rules must still hold.
  • Isolation — other transactions must not see a half-finished state.
  • Durability — after COMMIT, a restart must not erase the transfer.

If this example is clear, you essentially understand ACID.

29. Are integrity and consistency the same?

They are not identical, though they are closely related.

Data integrity is the broader idea that data stays correct and valid.

Consistency in ACID says a transaction must preserve the database’s constraints and rules.

So consistency is one of the things that helps keep the database valid.

30. “Internal and external integrity” in the notes

You may see “internal vs external integrity” in a university handout. It is not a standard split used the same way in all database texts.

For a solid foundation, focus on the well-known integrity kinds:

  • Entity integrity
  • Referential integrity
  • Domain integrity
  • User-defined constraints

Grade between 0 and 20: domain constraint. A registration needs a real student: referential integrity. A unique primary key: entity integrity.

These become much more important later.

31. Does every task go straight to the OS?

The note that “every task goes to the OS but a transaction goes to the DBMS” is oversimplified. Do not memorize it.

The application usually talks to the DBMS. The DBMS itself talks to the operating system for disk, memory, file I/O, and more.

A better model:

text
Application
     ↓
    DBMS
     ↓
Operating System
     ↓
Storage
32. What is a knowledge base?

A database is mainly for storing and managing structured data. For example:

text
Teacher: Ali

Skills:
CCNA
CCNP

Those are facts.

A knowledge base may also keep rules, semantic relations, and inference structure. For example:

text
CCNP → Advanced Networking Skill

Advanced Networking Skill
→ Eligible for Advanced Network Course

The system can infer from what it already knows.

The claim that a knowledge base “must use AI and NLP” is false. A knowledge base may be used in AI, but it does not require AI or NLP. Expert systems used knowledge bases long before language models.

33. Database vs knowledge base

Simply:

  • Database: What data do we have?
  • Knowledge Base: What do we know, and what relationships or rules exist?

A database might store:

text
Sara
age = 22
major = Computer

A knowledge base may add rules and semantic relations on top of those facts.

34. The whole lesson on one map

Roles and the data path:

text
                     Organization
                         │
                         ▼
                        DA
               data policy and needs
                         │
                         ▼
                        DBA
            technical database management
                         │
                         ▼
               Database Developer
                 SQL / implementation
                         │
                         ▼
                       DBMS
                         │
                         ▼
                     Database

The end user uses an application:

text
End User
   ↓
Application
   ↓
DBMS
   ↓
Database

And to talk to the DBMS:

text
SQL
├── DDL → Structure
├── DML → Data
└── DCL → Permissions

And when several operations are one unit of work:

text
Transaction
    ↓
   ACID
35. Know this table well for the exam
QuestionAnswer
Who uses the system?End user
Who sets data policy?DA
Who manages the database technically?DBA
Who writes queries and implements the DB?Database developer / DBP
What is CREATE?DDL
What is ALTER?DDL
What is DROP?DDL
What is INSERT?DML
What is UPDATE?DML
What is DELETE?DML
What is SELECT?Usually DML; sometimes DQL
What is GRANT?DCL
What is REVOKE?DCL
What is information about database structure?Metadata
Where is metadata kept?Data dictionary / system catalog
Finalize a transaction?COMMIT
Undo a transaction?ROLLBACK
All or nothing?Atomicity
Preserve database rules?Consistency
Control concurrent transactions?Isolation
Keep the result after commit?Durability

Next session is the relational model, tables, and keys: entity, attribute, primary key, foreign key, and relationship types. After that we move to ER diagrams.