From 750d33b97672104e6ff300f9f95f2da99e358b90 Mon Sep 17 00:00:00 2001 From: Leonard Excoffier <48970393+excoffierleonard@users.noreply.github.com> Date: Thu, 29 Aug 2024 21:39:57 -0400 Subject: [PATCH] feat: added stuff and corrected the schema creation. --- db_schema.sql | 10 +++++----- stockdb.session.sql | 29 +++++++++++++++++++++++++++++ write_to_db.py | 1 + 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 stockdb.session.sql diff --git a/db_schema.sql b/db_schema.sql index a5836b9..5ae1bfc 100644 --- a/db_schema.sql +++ b/db_schema.sql @@ -1,17 +1,17 @@ -CREATE TABLE entities ( +CREATE TABLE IF NOT EXISTS entities ( cik INT PRIMARY KEY, -- CIK is now the primary key, ensuring uniqueness name VARCHAR(255) NOT NULL -- Name of the company ); -CREATE TABLE facts ( +CREATE TABLE IF NOT EXISTS facts ( id VARCHAR(255) PRIMARY KEY, -- Unique identifier for the fact taxonomy VARCHAR(255), -- Taxonomy of the fact label VARCHAR(255), -- Label of the fact description TEXT, -- Description of the fact - unit VARCHAR(255), -- Unit of the fact + unit VARCHAR(255) -- Unit of the fact ); -CREATE TABLE data ( +CREATE TABLE IF NOT EXISTS data ( cik INT, -- CIK of the company fact_id VARCHAR(255), start DATE, -- Start date of the fact @@ -23,7 +23,7 @@ CREATE TABLE data ( form VARCHAR(255), filed DATE, frame VARCHAR(255), - PRIMARY KEY (cik, label) + PRIMARY KEY (cik, fact_id), FOREIGN KEY (cik) REFERENCES entities(cik), FOREIGN KEY (fact_id) REFERENCES facts(id) ); diff --git a/stockdb.session.sql b/stockdb.session.sql new file mode 100644 index 0000000..5ae1bfc --- /dev/null +++ b/stockdb.session.sql @@ -0,0 +1,29 @@ +CREATE TABLE IF NOT EXISTS entities ( + cik INT PRIMARY KEY, -- CIK is now the primary key, ensuring uniqueness + name VARCHAR(255) NOT NULL -- Name of the company +); + +CREATE TABLE IF NOT EXISTS facts ( + id VARCHAR(255) PRIMARY KEY, -- Unique identifier for the fact + taxonomy VARCHAR(255), -- Taxonomy of the fact + label VARCHAR(255), -- Label of the fact + description TEXT, -- Description of the fact + unit VARCHAR(255) -- Unit of the fact +); + +CREATE TABLE IF NOT EXISTS data ( + cik INT, -- CIK of the company + fact_id VARCHAR(255), + start DATE, -- Start date of the fact + end DATE, + val INT, + accn VARCHAR(255), + fy INT, + fp VARCHAR(255), + form VARCHAR(255), + filed DATE, + frame VARCHAR(255), + PRIMARY KEY (cik, fact_id), + FOREIGN KEY (cik) REFERENCES entities(cik), + FOREIGN KEY (fact_id) REFERENCES facts(id) +); diff --git a/write_to_db.py b/write_to_db.py index 95bd095..70a45f8 100644 --- a/write_to_db.py +++ b/write_to_db.py @@ -1,3 +1,4 @@ +import mariadb import json # Step 1: Load the JSON data from a file