What are the benefits of normalizing data in a database rather than storing values as comma-separated strings?

Normalizing data in a database involves breaking down data into separate tables to reduce redundancy and improve data integrity. Storing values as comma-separated strings can lead to data duplication, inconsistency, and difficulty in querying and updating the data. By normalizing the data, we can ensure that each piece of information is stored only once and relationships between data are properly maintained.

// Example of normalizing data in a database using PHP and MySQL

// Create a table for storing users
CREATE TABLE users (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    email VARCHAR(50)
);

// Create a table for storing user roles
CREATE TABLE user_roles (
    user_id INT,
    role VARCHAR(20),
    FOREIGN KEY (user_id) REFERENCES users(id)
);

// Insert data into the users table
INSERT INTO users (id, name, email) VALUES (1, 'John Doe', 'john@example.com');

// Insert data into the user_roles table
INSERT INTO user_roles (user_id, role) VALUES (1, 'admin');