Build Your Own AI Assistant Part 1 - Creating the Assistant
116820 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
114678 Views
Control Arduino with Python using Firmata / PyFirmata
87081 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
57314 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
53588 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
52067 Views
LidarBot
Snaszy NAS a 3D printed NAS for Raspberry Pi
Waveshare CM5 boards
The Best Arduino Robot for Beginners
SMARS Lab upgrade with PyCharm
Chicken Nugget Piano
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
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 >