-- Create the roles table
CREATE TABLE roles (
role_id INT PRIMARY KEY AUTO_INCREMENT,
role_name VARCHAR(50) NOT NULL COMMENT 'Name of the role, e.g., admin, advertiser, manager',
description VARCHAR(255) COMMENT 'Description of the role'
);
-- Create the permissions table
CREATE TABLE permissions (
permission_id INT PRIMARY KEY AUTO_INCREMENT,
permission_name VARCHAR(50) NOT NULL COMMENT 'Name of the permission e.g., view_campaign, edit_campaign',
description VARCHAR(255) COMMENT 'Description of the permission'
);
-- Create the role_permissions table
CREATE TABLE role_permissions (
role_id INT NOT NULL COMMENT 'Foreign key to roles table',
permission_id INT NOT NULL COMMENT 'Foreign key to permissions table',
PRIMARY KEY (role_id, permission_id),
FOREIGN KEY (role_id) REFERENCES roles(role_id),
FOREIGN KEY (permission_id) REFERENCES permissions(permission_id)
);
-- Create the advertisers table
CREATE TABLE advertisers (
advertiser_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL COMMENT 'Name of the advertiser',
address VARCHAR(255) COMMENT 'Mailing address of the advertiser',
contact_name VARCHAR(100) COMMENT 'Contact person at the advertiser',
contact_email VARCHAR(100) COMMENT 'Contact email at the advertiser',
contact_phone VARCHAR(20) COMMENT 'Contact phone number at the advertiser'
);
-- Create the users table
CREATE TABLE users (
user_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL UNIQUE COMMENT 'Unique username for login',
password_hash VARCHAR(255) NOT NULL COMMENT 'Password hash for authentication',
role_id INT NOT NULL COMMENT 'Foreign key to roles table',
advertiser_id INT COMMENT 'Foreign key to advertisers table if user is associated with an advertiser',
first_name VARCHAR(50) COMMENT 'First name of the user',
last_name VARCHAR(50) COMMENT 'Last name of the user',
email VARCHAR(100) NOT NULL COMMENT 'Email address of the user',
active BOOLEAN DEFAULT TRUE COMMENT 'Status of the user account',
FOREIGN KEY (role_id) REFERENCES roles(role_id),
FOREIGN KEY (advertiser_id) REFERENCES advertisers(advertiser_id)
);
-- Create the campaigns table
CREATE TABLE campaigns (
campaign_id INT PRIMARY KEY AUTO_INCREMENT,
advertiser_id INT NOT NULL COMMENT 'Foreign key to advertisers table',
name VARCHAR(100) NOT NULL COMMENT 'Name of the campaign',
budget DECIMAL(15,2) NOT NULL COMMENT 'Budget allocated for the campaign',
start_date DATE NOT NULL COMMENT 'Start date of the campaign',
end_date DATE NOT NULL COMMENT 'End date of the campaign',
status VARCHAR(20) NOT NULL COMMENT 'Status of the campaign e.g., active, paused, completed',
FOREIGN KEY (advertiser_id) REFERENCES advertisers(advertiser_id)
);
-- Create the targeting_criteria table
CREATE TABLE targeting_criteria (
targeting_criteria_id INT PRIMARY KEY AUTO_INCREMENT,
description VARCHAR(255) NOT NULL COMMENT 'Description of the targeting criteria e.g., age group, location'
);
-- Create the bids table
CREATE TABLE bids (
bid_id INT PRIMARY KEY AUTO_INCREMENT,
campaign_id INT NOT NULL COMMENT 'Foreign key to campaigns table',
bid_amount DECIMAL(10,4) NOT NULL COMMENT 'Bid amount for the impression in the currency unit per mille',
targeting_criteria_id INT COMMENT 'Foreign key to targeting_criteria table',
FOREIGN KEY (campaign_id) REFERENCES campaigns(campaign_id),
FOREIGN KEY (targeting_criteria_id) REFERENCES targeting_criteria(targeting_criteria_id)
);
-- Create the creatives table
CREATE TABLE creatives (
creative_id INT PRIMARY KEY AUTO_INCREMENT,
campaign_id INT NOT NULL COMMENT 'Foreign key to campaigns table',
name VARCHAR(100) NOT NULL COMMENT 'Name of the creative',
creative_type VARCHAR(20) NOT NULL COMMENT 'Type of creative e.g., image, video, text',
creative_url VARCHAR(255) NOT NULL COMMENT 'URL where the creative is hosted',
status VARCHAR(20) NOT NULL COMMENT 'Status of the creative e.g., active, paused',
FOREIGN KEY (campaign_id) REFERENCES campaigns(campaign_id)
);
-- Create the impressions table
CREATE TABLE impressions (
impression_id BIGINT PRIMARY KEY AUTO_INCREMENT,
bid_id INT NOT NULL COMMENT 'Foreign key to bids table',
timestamp DATETIME NOT NULL COMMENT 'Timestamp of the impression',
win_price DECIMAL(10,4) NOT NULL COMMENT 'Price at which the impression was won',
ad_served BOOLEAN DEFAULT TRUE COMMENT 'Whether the ad was successfully served',
click BOOLEAN DEFAULT FALSE COMMENT 'Whether the ad was clicked',
conversion BOOLEAN DEFAULT FALSE COMMENT 'Whether the ad led to a conversion',
FOREIGN KEY (bid_id) REFERENCES bids(bid_id)
);