SQL is widely used for managing relational databases. In this article, we will learn how to import a CSV file containing country data into an SQL database.
Before importing the CSV file, we need to create a database and a table to store the data. Here is an example SQL script:
CREATE DATABASE CountryDB;
USE CountryDB;
CREATE TABLE Countries (
Country VARCHAR(100),
Capital VARCHAR(100),
Population BIGINT
);
You can import a CSV file into an SQL database using the LOAD DATA INFILE statement in MySQL:
LOAD DATA INFILE 'countries.csv'
INTO TABLE Countries
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
Here is an example of what a countries.csv file might look like:
Country,Capital,Population
India,New Delhi,1393409038
USA,Washington D.C.,331449281
UK,London,67886011
Canada,Ottawa,37742154
Australia,Canberra,25499884
Once the data is imported, you can verify it using a simple SQL query:
SELECT * FROM Countries;
Using SQL, you can easily import and manage country data from a CSV file. This is useful for database management, data analysis, and reporting applications.