KevsRobots Learning Platform
30% Percent Complete
By Kevin McAleer, 6 Minutes
In this lesson, we’ll explore how to structure data in tables and choose appropriate data types for each column. You’ll also learn about primary keys and constraints, essential concepts for organizing and securing data in a relational database.
In a relational database, data is organized into tables. A table represents a specific entity or concept, like “Users” or “Products.” Each row in the table represents a unique instance of that entity (e.g., a specific user), while each column represents a characteristic or attribute of that entity (e.g., name, age, or email).
Here’s an example of a simple table called users:
| id | name | age | email |
|----|------------|-----|--------------------|
| 1 | Alice | 30 | [email protected] |
| 2 | Bob | 25 | [email protected] |
In this table:
CREATE TABLEThe CREATE TABLE statement defines the structure of a new table, specifying each column’s name and data type.
CREATE TABLE table_name (
column1 data_type constraints,
column2 data_type constraints,
...
);
users TableHere’s how to create the users table with columns for id, name, age, and email.
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER,
email TEXT
);
PRIMARY KEY.NOT NULL), ensuring each user has a name.Note: Constraints like
PRIMARY KEYandNOT NULLhelp ensure data integrity. We’ll discuss them in more detail below.
SQL provides various data types to represent different types of data. Choosing the correct data type for each column is essential for efficient storage and data integrity.
| Type | Description | Example |
|---|---|---|
| INTEGER | Whole numbers without decimal places | 0, 1, 2 ,3 |
| REAL | Floating-point numbers | 3.14, 2.718 |
| TEXT | Alphanumeric characters and strings | "Alice" |
| BLOB | Binary Large Object, for storing binary data like images or files | [0x00,0x01,x010] |
| DATE | Stores date values | 2023-07-14 |
| BOOLEAN | True or false values | 1 for true, 0 for false |
age.name or email.price or salary.birth_date or hire_date.Tip: Choose the most specific data type to save space and improve performance.
Constraints are rules applied to columns to enforce data integrity. They help ensure that the data is accurate, consistent, and meaningful.
| Constraint | Description |
|---|---|
| PRIMARY KEY | Uniquely identifies each row in a table. Often set on an id column. |
| NOT NULL | Ensures a column cannot be empty. Useful for mandatory fields like name. |
| UNIQUE | Ensures all values in a column are unique. Useful for fields like email or username. |
| DEFAULT | Sets a default value for a column if no value is specified. |
| FOREIGN KEY | Links one table to another, establishing relationships between tables. |
id INTEGER PRIMARY KEY
name TEXT NOT NULL
email TEXT UNIQUE
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER DEFAULT 18,
email TEXT UNIQUE
);
In this example:
id is a primary key, ensuring each user has a unique identifier.name cannot be null, so every user must have a name.age defaults to 18 if no value is provided.email must be unique across all users.AUTO_INCREMENT for Primary Keys (MySQL) or AUTOINCREMENT (SQLite)In some databases, you can set a primary key column to automatically generate a unique number for each row. In SQLite, this is achieved with AUTOINCREMENT.
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE
);
Now, each new row added to the users table will automatically assign a unique id.
ALTER TABLEOnce a table is created, you can modify its structure using the ALTER TABLE command. Common modifications include adding, dropping, or renaming columns.
ALTER TABLE users
ADD COLUMN phone TEXT;
This adds a phone column to the users table.
If your database supports it (e.g., MySQL), you can remove columns.
ALTER TABLE users
DROP COLUMN phone;
products with the following columns:
product_id: integer, primary key with auto-increment.product_name: text, not null.price: real.stock: integer with a default value of 0.CREATE TABLE products (
product_id INTEGER PRIMARY KEY AUTOINCREMENT,
product_name TEXT NOT NULL,
price REAL,
stock INTEGER DEFAULT 0
);
.schema products
This will display the products table’s structure if you’re using SQLite.
| Command | Description |
|---|---|
CREATE TABLE |
Defines a new table and its columns. |
ALTER TABLE |
Modifies an existing table by adding or removing columns. |
INTEGER, TEXT, etc. |
Data types used for defining columns. |
PRIMARY KEY, NOT NULL |
Constraints to enforce data integrity. |
AUTOINCREMENT |
Automatically increments a primary key value for each new row. |
With this knowledge, you’re ready to create well-structured tables and define columns with data types and constraints. In the next lesson, we’ll cover how to add, modify, and delete data in your tables.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments