Database design: keys, relationships and normalization
Design tables that avoid duplicated data: primary keys, foreign keys, one-to-many and many-to-many relationships.
Read exactly the rows you need, in the order you need.
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(80) NOT NULL,
category VARCHAR(30) NOT NULL,
price DECIMAL(8,2) NOT NULL
);
INSERT INTO products (name, category, price) VALUES
('Pen', 'office', 1.50), ('Notebook', 'office', 4.00),
('Mouse', 'tech', 15.00), ('Keyboard', 'tech', 30.00);SELECT name, price FROM products WHERE category = 'tech';
SELECT * FROM products WHERE price BETWEEN 2 AND 20;
SELECT name FROM products WHERE name LIKE 'K%';SELECT name, price FROM products ORDER BY price DESC LIMIT 2;SELECT name FROM products
WHERE category = 'tech' AND price < 20 OR category = 'office';Use brackets when you mix AND and OR, because AND is evaluated first.
SELECT *.ORDER BY, the order of rows is not guaranteed.Design tables that avoid duplicated data: primary keys, foreign keys, one-to-many and many-to-many relationships.
A folder layout and a few habits that keep a growing PHP site tidy, secure and easy to maintain.