-- SQL script to create essential tables for a Demand Side Platform (DSP) database in the ad space.
-- Create table advertisers
CREATE TABLE advertisers (
advertiser_id SERIAL PRIMARY KEY, -- Unique ID for each advertiser
name VARCHAR(255) NOT NULL, -- Name of the advertiser
contact_email VARCHAR(255), -- Contact email for the advertiser
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -- Timestamp when the advertiser was created
);
-- Create table campaigns
CREATE TABLE campaigns (
campaign_id SERIAL PRIMARY KEY, -- Unique ID for each campaign
advertiser_id INTEGER NOT NULL, -- Foreign key to advertisers
name VARCHAR(255) NOT NULL, -- Name of the campaign
budget DECIMAL(18,2) NOT NULL, -- Total budget allocated to the campaign
daily_budget DECIMAL(18,2), -- Optional daily budget
start_date DATE, -- Campaign start date
end_date DATE, -- Campaign end date
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- Timestamp when the campaign was created
CONSTRAINT fk_campaign_advertiser FOREIGN KEY (advertiser_id) REFERENCES advertisers (advertiser_id)
);
-- Create table creatives
CREATE TABLE creatives (
creative_id SERIAL PRIMARY KEY, -- Unique ID for each creative
campaign_id INTEGER NOT NULL, -- Foreign key to campaigns
type VARCHAR(50) NOT NULL, -- Creative type (e.g., image, video, text)
content_url VARCHAR(1024) NOT NULL, -- URL to the creative content
mime_type VARCHAR(255), -- MIME type of the creative content
width INTEGER, -- Width of the creative in pixels
height INTEGER, -- Height of the creative in pixels
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- Timestamp when the creative was created
CONSTRAINT fk_creative_campaign FOREIGN KEY (campaign_id) REFERENCES campaigns (campaign_id)
);
-- Create table users
CREATE TABLE users (
user_id BIGSERIAL PRIMARY KEY, -- Unique ID for each user
device_id VARCHAR(255), -- Device identifier
cookie_id VARCHAR(255), -- Cookie identifier
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -- Timestamp when the user was created
);
-- Create table bids
CREATE TABLE bids (
bid_id BIGSERIAL PRIMARY KEY, -- Unique ID for each bid
campaign_id INTEGER NOT NULL, -- Foreign key to campaigns
creative_id INTEGER NOT NULL, -- Foreign key to creatives
bid_amount DECIMAL(18,6) NOT NULL, -- Bid amount in CPM (cost per mille)
bid_timestamp TIMESTAMP NOT NULL, -- Timestamp when the bid was made
targeting_data JSONB, -- JSON data containing targeting parameters
CONSTRAINT fk_bid_campaign FOREIGN KEY (campaign_id) REFERENCES campaigns (campaign_id),
CONSTRAINT fk_bid_creative FOREIGN KEY (creative_id) REFERENCES creatives (creative_id)
);
-- Create table impressions
CREATE TABLE impressions (
impression_id BIGSERIAL PRIMARY KEY, -- Unique ID for each impression
bid_id BIGINT NOT NULL, -- Foreign key to bids
user_id BIGINT, -- Optional foreign key to users
impression_timestamp TIMESTAMP NOT NULL, -- Timestamp when the impression occurred
CONSTRAINT fk_impression_bid FOREIGN KEY (bid_id) REFERENCES bids (bid_id),
CONSTRAINT fk_impression_user FOREIGN KEY (user_id) REFERENCES users (user_id)
);
-- Create table clicks
CREATE TABLE clicks (
click_id BIGSERIAL PRIMARY KEY, -- Unique ID for each click
impression_id BIGINT NOT NULL, -- Foreign key to impressions
user_id BIGINT, -- Optional foreign key to users
click_timestamp TIMESTAMP NOT NULL, -- Timestamp when the click occurred
CONSTRAINT fk_click_impression FOREIGN KEY (impression_id) REFERENCES impressions (impression_id),
CONSTRAINT fk_click_user FOREIGN KEY (user_id) REFERENCES users (user_id)
);
-- Create table targeting_criteria
CREATE TABLE targeting_criteria (
criterion_id SERIAL PRIMARY KEY, -- Unique ID for each targeting criterion
campaign_id INTEGER NOT NULL, -- Foreign key to campaigns
criterion_type VARCHAR(50) NOT NULL, -- Type of targeting (e.g., geo, device, interest)
criterion_value VARCHAR(255) NOT NULL, -- Value of the targeting criterion
CONSTRAINT fk_targeting_campaign FOREIGN KEY (campaign_id) REFERENCES campaigns (campaign_id)
);