23 lines
828 B
SQL
23 lines
828 B
SQL
CREATE TABLE customer_points (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
customer_id UUID NOT NULL,
|
|
balance BIGINT DEFAULT 0 NOT NULL,
|
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
|
|
|
CONSTRAINT fk_customer_points_customer
|
|
FOREIGN KEY (customer_id)
|
|
REFERENCES customers(id)
|
|
ON DELETE CASCADE,
|
|
|
|
CONSTRAINT chk_customer_points_balance_non_negative
|
|
CHECK (balance >= 0)
|
|
);
|
|
|
|
-- Create indexes
|
|
CREATE INDEX idx_customer_points_customer_id ON customer_points(customer_id);
|
|
CREATE INDEX idx_customer_points_updated_at ON customer_points(updated_at);
|
|
|
|
-- Create unique constraint to ensure one point record per customer
|
|
CREATE UNIQUE INDEX idx_customer_points_unique_customer ON customer_points(customer_id);
|