Okay, you're looking to normalize your PostgreSQL schema by moving the awards, contact_persons, and programs data out of arrays within the Product_Maker table into their own separate, related tables. This is a great move for data integrity, query performance, and flexibility.
Here’s a breakdown of the changes and the updated SQL:
- Remove array-based composite types from
Product_Maker: The columns awards, contact_persons, and programs will be removed from the Product_Maker table.
- Create new tables:
Awards table: Will store individual award details, linked to Product_Maker via a foreign key.
Contact_Persons table: Will store individual contact person details, linked to Product_Maker via a foreign key.
Programs table: Will store individual program details, linked to Product_Maker via a foreign key.
- Adjust composite types: The composite types
award_type, contact_person_type, and program_type can now be dropped if they are not used elsewhere in your schema. The address_type and telephone_type composite types can remain as they are still used in the Product_Maker table.
Revised Schema Definition
First, let's define the new tables and adjust the Product_Maker table.
(Assumption: You have a TSI_User_Info table with a TSI_User_Info_id column.)
-- --- Composite Type Definitions (Keep if still needed elsewhere) ---
-- Keep address_type as it's still used in Product_Maker
CREATE TYPE address_type AS (
street TEXT,
city TEXT,
state TEXT,
postal_code TEXT,
country TEXT
);
-- Keep telephone_type if you might use it elsewhere, otherwise it can be dropped
CREATE TYPE telephone_type AS (
country_code TEXT,
area_code TEXT,
telephone_number TEXT
);
-- --- Drop composite types that will no longer be used in arrays ---
-- You can run these DROP TYPE commands AFTER creating the new tables and
-- ensuring these types aren't used elsewhere.
-- DROP TYPE award_type;
-- DROP TYPE contact_person_type;
-- DROP TYPE program_type;
-- --- Product Makers Table (Revised) ---
CREATE TABLE Product_Maker (
Product_Maker_id SERIAL PRIMARY KEY,
business_name TEXT NOT NULL,
business_registration_path TEXT NOT NULL,
year_incorporated TEXT NOT NULL, -- Consider INTEGER or DATE if appropriate
founder_CEO TEXT NOT NULL, -- Consider FK to a Person table if CEO is a person record
website TEXT, -- Making website and category nullable as they might not always be provided. Adjust if they must be NOT NULL.
category TEXT,
office_address address_type,
warehouse_address address_type,
-- Removed 'telephone telephone_type[]' - Assuming phone numbers are now managed with contacts or a separate phone table if needed.
validation_status BOOLEAN NOT NULL,
last_validation_date DATE, -- Made nullable, as it might not have been validated yet.
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_by TEXT NOT NULL, -- Consider FK to a User table if applicable
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
TSI_User_Info_id TEXT REFERENCES TSI_User_Info(TSI_User_Info_id) -- Consider making nullable if not always assigned
);
-- --- Awards Table ---
CREATE TABLE Awards (
award_id SERIAL PRIMARY KEY,
product_maker_id INTEGER NOT NULL REFERENCES Product_Maker(Product_Maker_id) ON DELETE CASCADE,
award_name TEXT NOT NULL,
year INTEGER, -- Year can be optional
issuing_organization TEXT, -- Organization can be optional
award_path TEXT -- Path to award details/certificate
);
-- --- Contact Persons Table ---
CREATE TABLE Contact_Persons (
contact_person_id SERIAL PRIMARY KEY,
product_maker_id INTEGER NOT NULL REFERENCES Product_Maker(Product_Maker_id) ON DELETE CASCADE,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
email TEXT, -- Email can be optional, or add UNIQUE constraint for product_maker_id if needed
phone TEXT, -- Assuming one primary phone per contact. Use a separate table for multiple phones.
role TEXT
);
-- --- Programs Table ---
CREATE TABLE Programs (
program_id SERIAL PRIMARY KEY, -- Using SERIAL for PK, as is standard.
product_maker_id INTEGER NOT NULL REFERENCES Product_Maker(Product_Maker_id) ON DELETE CASCADE,
program_identifier TEXT, -- To store the original TEXT Program_id if it's an external reference
program_name TEXT NOT NULL,
program_status TEXT -- Consider using an ENUM or a separate Status table for consistency
);
-- --- Optional: Add Indexes for performance ---
-- Index on foreign keys for faster joins
CREATE INDEX idx_awards_product_maker_id ON Awards(product_maker_id);
CREATE INDEX idx_contact_persons_product_maker_id ON Contact_Persons(product_maker_id);
CREATE INDEX idx_programs_product_maker_id ON Programs(product_maker_id);
-- Consider unique constraints if applicable
-- For example, if each contact person should have a unique email per product maker:
-- ALTER TABLE Contact_Persons ADD CONSTRAINT uq_contact_persons_email_product_maker UNIQUE (product_maker_id, email);
-- Or if program names must be unique per product maker:
-- ALTER TABLE Programs ADD CONSTRAINT uq_programs_name_product_maker UNIQUE (product_maker_id, program_name);