Схема в итоге получилась следующая (буду рад любым замечаниям):
CREATE TABLE IF NOT EXISTS user (
id int unsigned AUTO_INCREMENT,
regdate timestamp,
lastvisit timestamp,
email varchar(100),
password varchar(128),
salt varchar(20),
status tinyint,
level tinyint,
balance decimal(10,2),
PRIMARY KEY (id),
UNIQUE KEY (email)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS user_profile (
id int unsigned AUTO_INCREMENT,
uid int unsigned NOT NULL,
firstname timestamp,
PRIMARY KEY (id),
UNIQUE KEY (uid),
FOREIGN KEY (uid) REFERENCES user (id) ON DELETE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS user_activation (
id int unsigned AUTO_INCREMENT,
uid int unsigned NOT NULL,
expire timestamp,
hash varchar(20),
PRIMARY KEY (id),
FOREIGN KEY (uid) REFERENCES user (id) ON DELETE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS user_reset (
id int unsigned AUTO_INCREMENT,
uid int unsigned NOT NULL,
expire timestamp,
hash varchar(20),
PRIMARY KEY (id),
FOREIGN KEY (uid) REFERENCES user (id) ON DELETE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS user_session (
id int unsigned AUTO_INCREMENT,
uid int unsigned NOT NULL,
expire timestamp,
hash varchar(20),
ip varchar(39),
agent varchar(200),
PRIMARY KEY (id),
FOREIGN KEY (uid) REFERENCES user (id) ON DELETE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS product_category (
id int unsigned AUTO_INCREMENT,
name varchar(100),
url varchar(100),
PRIMARY KEY (id),
UNIQUE KEY (name),
UNIQUE KEY (url)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS product_brand (
id int unsigned AUTO_INCREMENT,
name varchar(100),
url varchar(100),
PRIMARY KEY (id),
UNIQUE KEY (name),
UNIQUE KEY (url)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS product (
id int unsigned AUTO_INCREMENT,
created timestamp,
edited timestamp,
name varchar(100),
description text,
category_id int unsigned NULL,
brand_id int unsigned NULL,
url varchar(100),
cost decimal(10,2),
quantity int unsigned,
PRIMARY KEY (id),
FOREIGN KEY (category_id) REFERENCES product_category (id) ON DELETE SET NULL,
FOREIGN KEY (brand_id) REFERENCES product_brand (id) ON DELETE SET NULL
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS product_photo (
id int unsigned AUTO_INCREMENT,
product_id int unsigned NOT NULL,
url varchar(100),
order_id int unsigned,
PRIMARY KEY (id),
UNIQUE KEY (url),
FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE CASCADE
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS product_tag (
id int unsigned AUTO_INCREMENT,
name varchar(100),
PRIMARY KEY (id),
UNIQUE KEY (name)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS product_tag_relation (
id int unsigned AUTO_INCREMENT,
product_id int unsigned NOT NULL,
tag_id int unsigned NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (product_id) REFERENCES product (id) ON DELETE CASCADE,
FOREIGN KEY (tag_id) REFERENCES product_tag (id) ON DELETE CASCADE
) ENGINE=InnoDB;