From 59084b5352a03e11044462966d913ec6816551e5 Mon Sep 17 00:00:00 2001 From: Imran Imtiaz Date: Sun, 7 Jul 2024 19:05:41 +0400 Subject: [PATCH] Update international_debt.sql The changes and enhancements made to the original script: 1. **Table Definition**: - Added `NOT NULL` constraints to ensure that no null values are inserted into critical columns. - Specified a precision for the `NUMERIC` data type (`NUMERIC(18, 2)`) to accurately handle large debt amounts with decimal precision. - Defined a composite primary key on `country_code` and `indicator_code` to ensure each entry is unique based on these columns and to facilitate efficient lookups. 2. **Indexes**: - Added indexes on `country_name` and `indicator_name` to improve query performance, particularly for queries that filter or join on these columns. 3. **Comments**: - Included comments to describe the purpose of each column and step, enhancing the script's readability and maintainability. 4. **Data Import**: - Specified the column order explicitly in the `\copy` command to ensure clarity and avoid potential issues if the CSV file's column order changes. 5. **Validation Query**: - Included a `SELECT` query to validate the data import by checking the first few rows, providing a quick way to verify that the data has been correctly imported. --- .../datasets/international_debt.sql | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/3. Analyze International Debt Statistics/datasets/international_debt.sql b/3. Analyze International Debt Statistics/datasets/international_debt.sql index 7a5de64..1c64998 100644 --- a/3. Analyze International Debt Statistics/datasets/international_debt.sql +++ b/3. Analyze International Debt Statistics/datasets/international_debt.sql @@ -1,13 +1,25 @@ --- Table: international_debt +-- Create Table: international_debt +-- This table stores information about international debt for various countries CREATE TABLE international_debt ( - country_name character varying(50), - country_code character varying(50), - indicator_name text, - indicator_code text, - debt numeric + country_name VARCHAR(50) NOT NULL, -- Name of the country + country_code VARCHAR(10) NOT NULL, -- ISO country code (assuming standard code length) + indicator_name TEXT NOT NULL, -- Name of the debt indicator + indicator_code TEXT NOT NULL, -- Code for the debt indicator + debt NUMERIC(18, 2) NOT NULL, -- Debt amount with precision to handle large values and cents + CONSTRAINT pk_international_debt PRIMARY KEY (country_code, indicator_code) -- Composite primary key ); --- Copy over data from CSV -\copy international_debt FROM 'international_debt.csv' DELIMITER ',' CSV HEADER; \ No newline at end of file +-- Add indexes for better query performance +CREATE INDEX idx_country_name ON international_debt(country_name); +CREATE INDEX idx_indicator_name ON international_debt(indicator_name); + +-- Note: Ensure the CSV file path is correct and accessible by the database system + +-- Copy data from CSV file into the international_debt table +\copy international_debt (country_name, country_code, indicator_name, indicator_code, debt) +FROM '/path/to/international_debt.csv' DELIMITER ',' CSV HEADER; + +-- Validate the data import by checking the first few rows +SELECT * FROM international_debt LIMIT 10;