Help me design a realistic database for a demand side platform (DSP) in the ad space. Consider security and access control. Write sql statements to create the 10-20 essential tables in the database. Add sql comments to explain each column. Specify the table CREATE statements in the correct order, so that foreign-key relationships are respected. Start with tables that have no foreign-key relationships.
Your response should consist of executable sql code and sql comments only, compatible with mysql.
Assistant
sql
-- Create the roles table
CREATE TABLE roles (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE COMMENT 'Name of the role, e.g., admin, advertiser, etc.'
) ENGINE=InnoDB COMMENT='User roles for access control';
-- Create the users table
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE COMMENT 'Unique username for login',
password_hash VARCHAR(255) NOT NULL COMMENT 'Hashed password for authentication',
email VARCHAR(255) NOT NULL UNIQUE COMMENT 'User email address',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Account creation timestamp',
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Last account update timestamp',
is_active BOOLEAN DEFAULT TRUE COMMENT 'Flag indicating if the account is active'
) ENGINE=InnoDB COMMENT='User accounts for the platform';
-- Create the advertisers table
CREATE TABLE advertisers (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL COMMENT 'Name of the advertiser company',
contact_email VARCHAR(255) NOT NULL COMMENT 'Contact email for the advertiser',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Advertiser creation timestamp',
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Last advertiser update timestamp',
status ENUM('active','inactive') DEFAULT 'active' COMMENT 'Advertiser status'
) ENGINE=InnoDB COMMENT='Advertisers on the platform';
-- Create the ad_exchanges table
CREATE TABLE ad_exchanges (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL COMMENT 'Name of the ad exchange',
api_endpoint VARCHAR(255) COMMENT 'API endpoint URL for integration',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Ad exchange creation timestamp',
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Last ad exchange update timestamp',
is_active BOOLEAN DEFAULT TRUE COMMENT 'Flag indicating if the ad exchange is active'
) ENGINE=InnoDB COMMENT='Ad exchanges where ads are bought and sold';
-- Create the user_roles table
CREATE TABLE user_roles (
user_id INT NOT NULL COMMENT 'Foreign key to users(id)',
role_id INT NOT NULL COMMENT 'Foreign key to roles(id)',
PRIMARY KEY (user_id, role_id),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE
) ENGINE=InnoDB COMMENT='Mapping of users to roles for access control';
-- Create the campaigns table
CREATE TABLE campaigns (
id INT AUTO_INCREMENT PRIMARY KEY,
advertiser_id INT NOT NULL COMMENT 'Foreign key to advertisers(id)',
name VARCHAR(255) NOT NULL COMMENT 'Name of the campaign',
start_date DATE NOT NULL COMMENT 'Campaign start date',
end_date DATE NOT NULL COMMENT 'Campaign end date',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Campaign creation timestamp',
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Last campaign update timestamp',
status ENUM('active','paused','completed') DEFAULT 'active' COMMENT 'Campaign status',
FOREIGN KEY (advertiser_id) REFERENCES advertisers(id) ON DELETE CASCADE
) ENGINE=InnoDB COMMENT='Advertising campaigns';
-- Create the creatives table
CREATE TABLE creatives (
id INT AUTO_INCREMENT PRIMARY KEY,
campaign_id INT NOT NULL COMMENT 'Foreign key to campaigns(id)',
creative_type ENUM('image','video','html') NOT NULL COMMENT 'Type of creative',
creative_url VARCHAR(255) NOT NULL COMMENT 'URL to the creative asset',
click_url VARCHAR(255) NOT NULL COMMENT 'URL where the user is directed after clicking the ad',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Creative creation timestamp',
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Last creative update timestamp',
status ENUM('active','paused','disapproved') DEFAULT 'active' COMMENT 'Creative status',
FOREIGN KEY (campaign_id) REFERENCES campaigns(id) ON DELETE CASCADE
) ENGINE=InnoDB COMMENT='Ad creatives associated with campaigns';
-- Create the budgets table
CREATE TABLE budgets (
id INT AUTO_INCREMENT PRIMARY KEY,
campaign_id INT NOT NULL UNIQUE COMMENT 'Foreign key to campaigns(id)',
daily_budget DECIMAL(10,2) NOT NULL COMMENT 'Daily budget for the campaign in USD',
total_budget DECIMAL(12,2) NOT NULL COMMENT 'Total budget for the campaign in USD',
currency VARCHAR(3) DEFAULT 'USD' COMMENT 'Currency code',
FOREIGN KEY (campaign_id) REFERENCES campaigns(id) ON DELETE CASCADE
) ENGINE=InnoDB COMMENT='Budget settings for campaigns';
-- Create the targeting_criteria table
CREATE TABLE targeting_criteria (
id INT AUTO_INCREMENT PRIMARY KEY,
campaign_id INT NOT NULL COMMENT 'Foreign key to campaigns(id)',
criteria_type ENUM('geo','device','os','browser','interest','age','gender') NOT NULL COMMENT 'Type of targeting criteria',
criteria_value VARCHAR(255) NOT NULL COMMENT 'Value for the criteria',
FOREIGN KEY (campaign_id) REFERENCES campaigns(id) ON DELETE CASCADE
) ENGINE=InnoDB COMMENT='Targeting criteria for campaigns';
-- Create the bids table
CREATE TABLE bids (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
campaign_id INT NOT NULL COMMENT 'Foreign key to campaigns(id)',
ad_exchange_id INT NOT NULL COMMENT 'Foreign key to ad_exchanges(id)',
bid_amount DECIMAL(10,4) NOT NULL COMMENT 'Bid amount per impression in USD',
bid_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Timestamp when bid was placed',
FOREIGN KEY (campaign_id) REFERENCES campaigns(id) ON DELETE CASCADE,
FOREIGN KEY (ad_exchange_id) REFERENCES ad_exchanges(id) ON DELETE CASCADE
) ENGINE=InnoDB COMMENT='Bids placed on ad exchanges for impressions';
-- Create the impressions table
CREATE TABLE impressions (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
bid_id BIGINT NOT NULL COMMENT 'Foreign key to bids(id)',
creative_id INT NOT NULL COMMENT 'Foreign key to creatives(id)',
ad_exchange_id INT NOT NULL COMMENT 'Foreign key to ad_exchanges(id)',
impression_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Timestamp when impression was served',
user_id VARCHAR(255) COMMENT 'Anonymous user ID',
device_type VARCHAR(50) COMMENT 'Type of device',
os VARCHAR(50) COMMENT 'Operating system',
browser VARCHAR(50) COMMENT 'Browser used',
geo_location VARCHAR(100) COMMENT 'Geographical location',
FOREIGN KEY (bid_id) REFERENCES bids(id) ON DELETE CASCADE,
FOREIGN KEY (creative_id) REFERENCES creatives(id) ON DELETE CASCADE,
FOREIGN KEY (ad_exchange_id) REFERENCES ad_exchanges(id) ON DELETE CASCADE
) ENGINE=InnoDB COMMENT='Ad impressions served';
-- Create the clicks table
CREATE TABLE clicks (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
impression_id BIGINT NOT NULL UNIQUE COMMENT 'Foreign key to impressions(id)',
click_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Timestamp when click occurred',
FOREIGN KEY (impression_id) REFERENCES impressions(id) ON DELETE CASCADE
) ENGINE=InnoDB COMMENT='Clicks recorded for impressions';
-- Create the audit_logs table
CREATE TABLE audit_logs (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
user_id INT NULL COMMENT 'Foreign key to users(id)',
action VARCHAR(255) NOT NULL COMMENT 'Action performed',
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Timestamp of the action',
details TEXT COMMENT 'Additional details about the action',
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
) ENGINE=InnoDB COMMENT='Audit logs for tracking user actions';