Good evening, I am trying to create an ERP, which is a bit pretentious to call it that because the name is too big for it and there is a lot of work to be done to even start calling it that. LOL
Well, getting to the point, I was wondering if anyone could help or correct this database schema and see if it looks correct or how it could be improved. Thanks in advance for any response. Long live StackOverflow and the people who selflessly help others by contributing their grain or ton of sand XD.
-- Table Company
CREATE TABLE Company (
company_id INT AUTO_INCREMENT PRIMARY KEY,
company_name VARCHAR(255) NOT NULL
);
-- Table Plant
CREATE TABLE Plant (
plant_id INT AUTO_INCREMENT PRIMARY KEY,
plant_name VARCHAR(255) NOT NULL,
company_id INT,
country_id INT,
address VARCHAR(255),
city VARCHAR(255),
state VARCHAR(255),
FOREIGN KEY (company_id) REFERENCES Company(company_id)
);
-- Table Location
CREATE TABLE Location (
location_id INT AUTO_INCREMENT PRIMARY KEY,
location_name VARCHAR(255) NOT NULL,
plant_id INT,
FOREIGN KEY (plant_id) REFERENCES Plant(plant_id)
);
-- Table Employee
CREATE TABLE Employee (
employee_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
date_of_birth DATE,
hire_date DATE,
department_id INT,
location_id INT,
FOREIGN KEY (department_id) REFERENCES Department(department_id),
FOREIGN KEY (location_id) REFERENCES Location(location_id)
);
-- Table Asset
CREATE TABLE Asset (
asset_id INT AUTO_INCREMENT PRIMARY KEY,
asset_name VARCHAR(255) NOT NULL,
asset_type VARCHAR(255),
purchase_date DATE,
value DECIMAL(10, 2),
location_id INT,
FOREIGN KEY (location_id) REFERENCES Location(location_id)
);
-- Table Department
CREATE TABLE Department (
department_id INT AUTO_INCREMENT PRIMARY KEY,
department_name VARCHAR(255) NOT NULL,
plant_id INT,
manager_id INT,
FOREIGN KEY (plant_id) REFERENCES Plant(plant_id),
FOREIGN KEY (manager_id) REFERENCES Employee(employee_id)
);
-- Table External_Company
CREATE TABLE External_Company (
external_company_id INT AUTO_INCREMENT PRIMARY KEY,
company_name VARCHAR(255) NOT NULL,
country_id INT,
address VARCHAR(255),
city VARCHAR(255),
state VARCHAR(255),
contact_person VARCHAR(255),
phone VARCHAR(20),
email VARCHAR(255)
);
-- Table External_Technician
CREATE TABLE External_Technician (
technician_id INT AUTO_INCREMENT PRIMARY KEY,
technician_name VARCHAR(255) NOT NULL,
external_company_id INT,
specialty VARCHAR(255),
availability VARCHAR(255),
FOREIGN KEY (external_company_id) REFERENCES External_Company(external_company_id)
);
-- Table Plant_External_Company (Intermediary table for many-to-many relationship)
CREATE TABLE Plant_External_Company (
plant_id INT,
external_company_id INT,
PRIMARY KEY (plant_id, external_company_id),
FOREIGN KEY (plant_id) REFERENCES Plant(plant_id),
FOREIGN KEY (external_company_id) REFERENCES External_Company(external_company_id)
);