Skip to content
VJ Classes

One place to learn science, commerce, arts, computers and life skills

Foundations: Data, Tables and Keys

Why Databases? Files, Codd and the Relational Model

Key points

  • A database is an organised, shared collection of data; a DBMS is the software that manages it.
  • Plain files cause redundancy, inconsistency, clashes and weak safety; a DBMS prevents them with rules, controls and recovery.
  • In 1970 E. F. Codd proposed the relational model: data in simple tables, with users saying what they want and the system deciding how.
  • SQL is the standard language of relational databases, and MySQL is the free, popular RDBMS used in this course.

Tables, Keys and Relationships

Key points

  • A table holds one kind of thing: rows are items, columns are properties with data types, and NULL means no value.
  • A primary key is unique and never NULL; a foreign key points to the primary key of another table.
  • Use a foreign key on the “many” side for one-to-many, and a junction table for many-to-many.
  • Referential integrity stops rows from pointing at something that does not exist.

Working with SQL in MySQL

SQL Basics in MySQL: Create, Insert, Select, Update, Delete

Key points

  • CREATE TABLE sets column names, data types and constraints; INSERT adds rows.
  • SELECT ... FROM ... WHERE ... ORDER BY ... LIMIT reads, filters, sorts and trims rows.
  • Use IS NULL, not = NULL, and use brackets when AND and OR are mixed.
  • UPDATE and DELETE need a WHERE clause; test it with SELECT first.

Aggregates, GROUP BY and HAVING

Key points

  • Aggregate functions (COUNT, SUM, AVG, MAX, MIN) turn many rows into one value and ignore NULLs, except COUNT(*).
  • GROUP BY produces one result row per group; every selected column must be grouped or aggregated.
  • WHERE filters rows before grouping; HAVING filters groups after it.
  • MySQL evaluates FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY and LIMIT, in that order.

Joins: Combining Tables in the School Database

Key points

  • A join matches rows of two tables on a condition, normally foreign key = primary key; aliases and qualified names keep it readable.
  • INNER JOIN keeps only matches; LEFT JOIN keeps every left row and fills gaps with NULL.
  • After a left join, count a right-hand column, not COUNT(*), and use IS NULL to find the unmatched rows.
  • A join without a proper ON condition multiplies the rows; check the row count.

Design, Speed and Safety

Normalisation: 1NF, 2NF, 3NF and BCNF

Key points

  • Bad design causes update, insertion and deletion anomalies; normalisation removes them by splitting tables.
  • 1NF: single values in cells. 2NF: no partial dependency on part of a key. 3NF: no non-key column depends on another non-key column.
  • BCNF demands that the left side of every dependency be a candidate key.
  • Check a split by joining the pieces back; you must get exactly the original rows.

Indexes, Transactions and ACID

Key points

  • An index is a sorted B-tree that lets MySQL find rows without a full scan; it costs space and slows writes, and EXPLAIN shows whether it is used.
  • A function wrapped around an indexed column in WHERE usually prevents the index from being used.
  • A transaction groups statements: COMMIT keeps all of them, ROLLBACK undoes all of them.
  • ACID stands for atomicity, consistency, isolation and durability.

Views, Subqueries and Prepared Statements

Key points

  • A view is a saved query; a subquery is a SELECT inside another statement, returning a value, a list or a table.
  • SQL injection happens when user input is glued into SQL text and changes its meaning.
  • A prepared statement sends the SQL with ? placeholders and the values separately, so input is never run as code.
  • Placeholders cannot stand in for table or column names; also use least-privilege accounts and hash passwords.

Important terms

  • aggregate function
  • alias
  • Alternate key
  • anomalies
  • backup and recovery
  • Book
  • Candidate key
  • Clashes
  • column
  • Composite key
  • concurrency control
  • constraints
  • Data
  • data independence
  • data type
  • database
  • DBMS
  • Deletion anomaly
  • Edgar F. Codd
  • First normal form (1NF
  • Foreign key
  • functional dependency
  • Hard questions
  • Inconsistency
  • inner join
  • Insertion anomaly
  • integrity rules
  • join
  • left join
  • lossless
  • MariaDB
  • MySQL
  • natural key
  • No rules
  • normal forms
  • Normalisation
  • NoSQL
  • NULL
  • partial dependency
  • Primary key
  • qualified column
  • query language
  • RDBMS
  • Redundancy
  • Referential integrity
  • relational database
  • relations
  • row
  • Second normal form (2NF
  • security
  • SQL
  • Super key
  • surrogate key
  • table
  • table alias
  • Third normal form (3NF
  • transitive dependency
  • Update anomaly
  • Weak safety
  • XAMPP
Enrol to read the full lessons. This page is a summary. The lessons explain every point with examples, and enrolling also gives you the full notes PDF and your progress. See the course