Get ready for Schema Optimizer, where you’ll design and secure an ML database schema using DDL, DCL, and Triggers! This project builds a robust structure for model data, perfect for your ML-Interview-Preparation-Hub portfolio to show scalable design. 🚀
- Scenario: You’re a database architect for an ML platform. Your task is to create tables for models and predictions, secure access, and automate logging.
- Goals: Build tables, set permissions, add triggers, and document schema.
- SQL Skills: DDL (CREATE, ALTER), DCL (GRANT), Triggers.
- Create
models(model_id INT, trained_date DATE) andpredictionstables. - Add constraints (e.g., primary keys, foreign keys).
- Grant read-only access to analysts using DCL.
- Create a trigger to log prediction inserts.
- Create a log table for trigger data.
- Test the schema with sample inserts.
- Document for portfolio use.
-- Task 1 & 2: Create Tables with Constraints
CREATE TABLE models (
model_id INT PRIMARY KEY,
trained_date DATE NOT NULL
);
CREATE TABLE predictions (
prediction_id INT PRIMARY KEY,
model_id INT,
score FLOAT,
prediction_date DATE,
model_name VARCHAR(50),
FOREIGN KEY (model_id) REFERENCES models(model_id)
);
-- Task 3: Grant Permissions
CREATE ROLE analyst;
GRANT SELECT ON models, predictions TO analyst;
-- Task 4 & 5: Create Log Table and Trigger
CREATE TABLE prediction_log (
log_id SERIAL PRIMARY KEY,
prediction_id INT,
insert_time TIMESTAMP
);
CREATE OR REPLACE FUNCTION log_prediction()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO prediction_log (prediction_id, insert_time)
VALUES (NEW.prediction_id, CURRENT_TIMESTAMP);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER prediction_insert
AFTER INSERT ON predictions
FOR EACH ROW EXECUTE FUNCTION log_prediction();
-- Task 6: Test Schema
INSERT INTO models VALUES (101, '2025-04-01');
INSERT INTO predictions VALUES (1, 101, 0.85, '2025-04-15', 'BERT_v1');
SELECT * FROM prediction_log;
-- Task 7: Document (See Portfolio Tips)Explanation:
- DDL defines tables with constraints.
- DCL secures access for analysts.
- Triggers automate logging of inserts.
- Tests verify schema integrity.
- A secure, functional ML schema.
- A log table tracking prediction inserts.
- A portfolio-ready schema design doc.
- Add schema SQL to
irohanportfolio.netlify.app. - Write a README explaining design and ML use (e.g., model tracking).
- Diagram the schema with tools like Lucidchart.
- Tag skills: DDL, DCL, Triggers.
Add a TCL transaction to ensure atomic inserts across tables.