Lesson 9 of 9
Database Design Basics
Good database design saves time and prevents errors. Use Primary Keys to uniquely identify rows. Use Foreign Keys to link tables. Follow normalization rules to avoid duplicate data.
SQL
-- Create a table with primary key
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Foreign key example
CREATE TABLE orders (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT,
total DECIMAL(10,2),
FOREIGN KEY (user_id) REFERENCES users(id)
);