What are the best practices for normalizing database structures to optimize data retrieval and manipulation in PHP?

Normalizing database structures involves organizing data into separate tables to reduce redundancy and improve data integrity. This optimization allows for efficient data retrieval and manipulation in PHP by reducing the need to store duplicate information and ensuring consistent data across the database.

// Example of normalizing a database structure in PHP

// Create a users table
CREATE TABLE users (
    id INT PRIMARY KEY,
    username VARCHAR(50) UNIQUE,
    email VARCHAR(100) UNIQUE
);

// Create a posts table
CREATE TABLE posts (
    id INT PRIMARY KEY,
    user_id INT,
    title VARCHAR(100),
    content TEXT,
    FOREIGN KEY (user_id) REFERENCES users(id)
);

// Retrieve user information and their posts
SELECT users.username, posts.title
FROM users
JOIN posts ON users.id = posts.user_id
WHERE users.id = 1;