Six advantages come up most often, and each one has a cost attached to it.
Integrity. Constraints such as primary keys, foreign keys and NOT NULL are checked by the engine on every write. No application code can forget to check. The cost is that schema changes need migrations.
Concurrency. A transaction manager lets many people work at once without corrupting each other's changes. The cost is lock waits and the occasional deadlock.
Recovery. A transaction log and regular backups let the engine restore a consistent state after a crash. The cost is write overhead and the work of testing a restore.
Query power. You can ask new questions without writing new code. Joins, aggregation and indexes answer reports that files answer slowly or not at all. The cost is that a careless query can still crawl on a large table.
Data independence. You can change how the data is stored without changing how applications ask for it. The cost is one more layer to design and debug.
Security. Permissions let one user read a report while another can change the rows. The cost is managing roles, and it never replaces network security.
None of this is free, and a DBMS is not automatically faster than a file. The advantage is that it makes correct behaviour the default, not something every program must remember on its own.
Constraints move data quality checks out of application code and into the engine. Once a rule is declared, every writer obeys it. That writer can be your service, a report script, or a person at a terminal.
PRIMARY KEY makes each row unique and gives it a stable identity. UNIQUE does the same for a column such as an email address. NOT NULL forbids a missing value. FOREIGN KEY makes sure a referenced row really exists.
The script below declares all four and then tries to break one of them.
PRAGMA foreign_keys = ON;
CREATE TABLE department (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL UNIQUE
);
CREATE TABLE employee (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
dept_id INTEGER,
FOREIGN KEY (dept_id) REFERENCES department(id)
);
INSERT INTO department (id, name) VALUES (1, 'Engineering');
INSERT INTO department (id, name) VALUES (2, 'Support');
INSERT INTO employee (id, name, email, dept_id)
VALUES (1, 'Asha', '[email protected]', 1);
-- The UNIQUE rule on email rejects this row.
INSERT OR IGNORE INTO employee (id, name, email, dept_id)
VALUES (2, 'Bilal', '[email protected]', 1);
SELECT id, name, email FROM employee ORDER BY id;
SELECT e.name, d.name AS department
FROM employee e
LEFT JOIN department d ON d.id = e.dept_id
ORDER BY e.name;
The first employee insert succeeds. The second one repeats an email that is already taken, so the UNIQUE rule rejects it. The INSERT OR IGNORE clause tells SQLite to skip that row rather than abort the statement, so the script keeps running and the employee table still holds one row.
One honest detail. SQLite checks foreign keys only when you turn them on for the connection with PRAGMA foreign_keys = ON. That line sits at the top of the script. Without it, a row pointing at a missing department would be accepted silently.
The last query joins employees to departments. That join doubles as a check, because an employee without a department would show a null department name.
A DBMS has real running costs, and on a small project they often outweigh the benefits.
The first is resource use. The engine holds a buffer pool in memory, keeps indexes on disk, and needs a process or service that stays alive. A file library costs almost nothing.
The second is administration. Somebody has to take backups, apply upgrades, watch disk space and answer the pager when a connection pool runs out. A single file has none of that.
The third is schema rigidity. Every change to the structure needs a migration, and old versions of your program must keep working while the new one rolls out. With files you can often change the format in place.
The fourth is the central point of failure. One database server serves everyone, so when it is down everything is down. Files can be copied and read on their own.
The fifth is scale and tuning. Queries fine at a thousand rows can crawl at ten million, and then you learn about indexes and query plans.
The last one is lock in. Moving away later means rewriting the data access layer and the schema. The rule of thumb is simple. Use a DBMS when several people rely on the same data, or when the data has to stay correct. For one program and one file, keep it simple.