What a Database Is
💻 SQLUnderstand tables, rows, columns and the difference between a database and a spreadsheet.
A relational database stores data in tables. Each table has a fixed set of columns and any number of rows. SQL is how you ask questions of that data.
- Describe tables, rows and columns.
- Explain a primary key.
- Write your first SELECT statement.
Tables
A table models one kind of thing: students, orders or lessons. Each column has a name and a type. Each row is one record.
Keys
A primary key uniquely identifies a row. It is often an auto-incrementing number. A foreign key points to a primary key in another table, creating a relationship.
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
level VARCHAR(20) NOT NULL,
email VARCHAR(150) UNIQUE
);Your First Query
The asterisk means every column. Return only the columns you need in real code; it is faster and clearer.
SELECT id, name, level
FROM students;Practice
- List the columns you would use for a books table.
- Explain why an email column might be UNIQUE.
- Write a query that returns only names from students.
Hands-on exercise
Write SQL that creates a table called courses with:
1. An auto-incrementing id as primary key.
2. A title that cannot be empty.
3. A price stored as a decimal.
4. A created_at timestamp.
Recap
Tables hold rows and columns. Primary keys identify rows, foreign keys connect tables, and SELECT asks for the columns you want.
Example
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
level VARCHAR(20) NOT NULL,
email VARCHAR(150) UNIQUE
);
Exercises
1. An auto-incrementing id as primary key.
2. A title that cannot be empty.
3. A price stored as a decimal.
4. A created_at timestamp.
Expected Output
A courses table with four columns and a primary key.
Lesson Nav
SQL Course
0/9 lessons completed
- 1 What a Database Is Tutorial
- 2 Filtering and Sorting Tutorial
- 3 Aggregates and Grouping Tutorial
- 4 Joining Tables Tutorial
- 5 Subqueries and Common Table Expressions Tutorial
- 6 Inserting, Updating and Deleting Tutorial
- 7 Schemas, Keys and Normalisation Tutorial
- 8 Indexes and Query Performance Tutorial
- 9 Project: A Reporting Query Project