Skip to content

Chapter 2 of 8

Tables, Keys, and Data Types

Inside a relational database, the table is the primary unit of structured storage. A table is composed of rows, which are individual records, and columns, which are named attributes. Every column has a defined data type that determines what kind of values it can hold, and together the tables are linked to one another through keys.

The primary key is the unique identifier for each row in a table. It guarantees that no two records are identical and makes lookups efficient. A primary key may consist of a single column or several columns combined into a composite key. To model relationships between tables, a foreign key is used: it is a column, or set of columns, in one table that points to the primary key of another. For example, an orders.customer_id foreign key can be linked to customers.id, enforcing referential integrity so that orders cannot reference customers that do not exist.

SQL provides a rich set of data types that vary slightly between systems. Numeric types include INT and FLOAT, string types include VARCHAR and TEXT, date and time types include DATE and TIMESTAMP, and boolean values are represented with BOOLEAN. Beyond data types, constraints shape what values a column can accept. A CHECK constraint enforces domain integrity by restricting values, for example requiring age >= 18. A UNIQUE constraint guarantees that all values in a column are distinct, which is useful for fields like email addresses; unlike PRIMARY KEY, UNIQUE allows NULL values.

All chapters
  1. 1Database Fundamentals and SQL
  2. 2Tables, Keys, and Data Types
  3. 3Defining Database Structure with DDL
  4. 4Querying and Modifying Data with DML
  5. 5Aggregates, Joins, and Subqueries
  6. 6Indexes, Views, and Advanced Features
  7. 7Transactions, ACID, and Normalization
  8. 8SQL versus NoSQL and Database Security

Drill it

Reading is not remembering. These come from the SQL And Databases deck:

Q

What is a database?

A database is an organized collection of structured data stored and accessed electronically from a computer system. Databases allow efficient storage, retrieval...

Q

What is a Relational Database Management System (RDBMS)?

An RDBMS is a software system that manages relational databases, organizing data into tables with rows and columns related by keys. It supports SQL for querying...

Q

What are the main types of databases?

Databases are broadly classified into relational (SQL-based, like MySQL) and non-relational (NoSQL, like MongoDB for documents or Cassandra for wide-column stor...

Q

What is SQL?

SQL stands for Structured Query Language, a standard language for managing and manipulating relational databases. It includes sublanguages like DDL for schema d...