CSV to SQL Converter




CSV Preview

SQL Output


              
          
Advertisement




CSV to SQL

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.

Creating a Database and Table

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
);
    

Importing CSV into SQL

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;
    

Sample CSV File

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
    

Verifying the Data

Once the data is imported, you can verify it using a simple SQL query:

SELECT * FROM Countries;
    

Conclusion

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.