SQL for Data Science › Chapter 3

Chapter 3

Database theory

50:00 → 1:23:00Data vs informationFiles vs DBMSRelational modelIntegrity

Data vs information

Data

Raw facts, usually numeric, collected through observations. On its own it doesn't tell you much.

Information

Data that has been processed so it carries meaning and lets you make a decision.

Example: the marks of every student in a class are data. "Who topped?" or "What's the class average?" is information you create by processing that data. Data is the raw material; information is what you actually use.

Turn data into information
Here is raw data. Click a question to process it.
studentmarks
Amit72
Ankita91
Rahul58
Sneha84
Vikas33
Who topped? Class average? Who failed (< 40)? How many scored above average?
Pick a question.

What storing data well requires

Data is important, so wherever you store it, that storage should guarantee five properties:

1 · Integrity

The data stays accurate and consistent. What you stored is what you get back.

2 · Availability

It is there whenever someone needs it. A social network can't show a profile "only after 1 a.m."

3 · Security

Only the right people can read or change it.

4 · Independent of the application

Facebook's Android app, iOS app and website all read the same data. Your name must not differ between platforms.

5 · Concurrency

Many users must be able to access and change the data at the same time without breaking it.

Real databases are designed around requirements like these.

Data is costly

As a software developer, always remember that storing data, and especially reading and writing it, costs money. Whether you use AWS, Google Cloud, Azure or anything else, database bills in industry are significant. Nitish shared a story of a student who got a monthly bill of about ₹7 lakh after an app was shared with many people with every setting maxed out. Design with cost in mind.

Why not just store data in files?

If databases are costly, why not save data in a simple text file? Because files have serious limitations for an application's main data:

The program depends on the physical structure of the data. Suppose you store users in a text file as email,password per line. Your code has to know that a comma separates the two. Change the format (add a column, change the separator) and you must rewrite every piece of code that reads the file.

Retrieving data is a complex process. To find one user you open the file, read it line by line, split, compare… For every kind of question you write new code. A database answers with one query.

Concurrency problems. When many people use the same file at once, it misbehaves. A workaround is making multiple copies and splitting users between them, but then the copies go out of sync.

All-or-nothing access. Whoever can open the file can see and change everything. You can't create user-defined roles (this person may only read, that one may only edit some records).

Data redundancy. The same data gets repeated in many places, which wastes space and leads to inconsistencies.

…but files still have their uses

Offline data

Apps that must work without internet keep data on the device, e.g. WhatsApp stores chats locally so you can read them offline.

Fast-arriving, short-lived data

Sensors across a city send readings every few minutes. Once a new reading arrives the old one is useless; writing every reading to a costly DB and deleting it again isn't worth it.

Low-importance data

Log files: when a server crashes it writes some text about what happened. You don't need a database for that.

What is a database?

Database: a shared collection of logically related data, and a description of that data, designed to meet the information needs of an organisation.

In simple words: a database is a system used to store, organise and retrieve data.

What is a DBMS?

DBMS (Database Management System): software that enables users to define, create, maintain and control access to a database. Application programs interact with the database by sending requests to the DBMS.

Remember the layering below; it's always followed. The user never touches the database directly.

User ApplicationPython app, website DBMSMySQL, Oracle… Database Operating system Hardware
Hardware → OS → DBMS manages the database → applications talk to the DBMS → users use the application.

In Chapter 2: our Python program was the application, MySQL was the DBMS, and the users table lived in the database.

What a DBMS does for you

Data managementtap to flip
Store, retrieve and modify data (the CRUD operations).
Integritytap to flip
Keeps data accurate and consistent using rules (constraints), e.g. an INT column only accepts integers.
Concurrencytap to flip
Many users can read and write at the same time safely.
Transactionstap to flip
A group of changes either fully happens or doesn't happen at all, even if something fails midway.
Securitytap to flip
Users with passwords (like root) and permissions. phpMyAdmin → User accounts lets you add users.
Utilitiestap to flip
Import / export (e.g. to CSV), backups, logging.
Transactions: all or nothing
Transfer ₹500 from A to B. That's two steps: subtract from A, add to B. Turn on "crash midway" to see why a transaction must be all-or-nothing.
Account A₹1000
Account B₹1000

Nitish showed the same idea in phpMyAdmin: start an operation and cancel it halfway, and nothing is half-saved. We'll study transactions (TCL: COMMIT, ROLLBACK) later.

Types of databases

Data is stored in tables (rows and columns) with a fixed structure, and tables are related to each other. Queried with SQL. This is what most companies use and what this course is about.

Examples: MySQL (what we use), Oracle, Microsoft SQL Server, PostgreSQL, MariaDB, SQLite.

For data that doesn't fit neatly into fixed tables: unstructured or semi-structured data, where any record can have different fields. Think of a social networking site, where a post can be text, an image, a video, a poll…

Example: MongoDB (stores JSON-like documents). You may well work with it at a company.

There are many more kinds (key-value stores like Redis, column stores like Cassandra, graph databases like Neo4j…), but relational databases are by far the most common in data work.

Relational terminology

A relational database is a collection of tables. Each table has its own formal name. Click a term to highlight it in the table:

Explore the vocabulary
emp_idnamesalarydept
1Nitish50000CS
2Ankit42000IT
3Rahul61000CS
4Sneha55000ECE
Click a term.
Formal termEveryday term
RelationTable
TupleRow / record
AttributeColumn / field
CardinalityNumber of rows
DegreeNumber of columns
DomainSet of allowed values for a column

Data integrity and constraints

Data integrity means maintaining and assuring the accuracy and consistency of data over its entire life cycle: as long as the data is in the database, it should stay correct. Databases enforce it with constraints (rules), which we'll write ourselves in Chapter 7. There are three types:

Every table must have a primary key: a column (or set of columns) whose value is unique and never null, so every row can be identified. Like a roll number in college: two students can share a name, but never a roll number. In Chapter 2's users table, user_id played this role.

Every value in a column must come from that column's domain (its allowed set of values). When Nitish made a products table and chose INT for a column, only integers could go in; anything else is rejected. Data types and rules like CHECK enforce this.

Relationships between tables must stay valid. Example: a branch table (branch_id, name), e.g. CSE → Computer Science, and a students table where each student stores a branch_id.

If the branch code later changes from CSE to CS, every student's branch must change too; otherwise students point at a branch that no longer exists and the data is inconsistent. A foreign key enforces this relationship, which is the topic of the next chapter.

See referential integrity
Rename the branch code and choose whether a foreign key with ON UPDATE CASCADE protects the link.
branch
branch_idname
CSEComputer Science
ECEElectronics
students
idnamebranch_id
Both tables agree.

Conceptual questions

These check whether you understand why, not just what. Several go one step beyond the video.

Your Android app and website show different profile names for the same user. Which property of good data storage is violated?

All platforms should read the same data, so they never disagree.

Why is a database better than many copies of a text file when thousands of users write at once?

Concurrency control is one of the core DBMS functions.

A money transfer debits account A, then the server crashes before crediting B. Which DBMS property guarantees A gets its money back?

This is exactly the transaction demo above.

A salary column accepts the text 'ten thousand'. Which kind of integrity is missing?

Each value must come from the column's domain (here, numbers), enforced by the data type or a CHECK.

Which data is the best candidate to keep in plain files rather than a database?

Logs are low-importance, append-only text: a classic file use case from the chapter.