114532 Views
101685 Views
86270 Views
54891 Views
51137 Views
49962 Views
Level Up your CAD Skills
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
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:
users
| id | name | age | email | |----|------------|-----|--------------------| | 1 | Alice | 30 | [email protected] | | 2 | Bob | 25 | [email protected] |
In this table:
CREATE TABLE
The 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, ... );
Here’s how to create the users table with columns for id, name, age, and email.
id
name
age
email
CREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER, email TEXT );
PRIMARY KEY
NOT NULL
Note: Constraints like PRIMARY KEY and NOT NULL help 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.
0
1
2
3
3.14
2.718
"Alice"
[0x00,0x01,x010]
2023-07-14
price
salary
birth_date
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.
username
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:
AUTO_INCREMENT
AUTOINCREMENT
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 TABLE
Once 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.
phone
If your database supports it (e.g., MySQL), you can remove columns.
ALTER TABLE users DROP COLUMN phone;
products
product_id
product_name
stock
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.
INTEGER
TEXT
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.
< Previous Next >