Efficient CSV Data Import into Employees Table
Import Data from a CSV File using LOAD DATA INFILE
Write a MySQL query to import data from a CSV file into the "Employees" table.
Solution:
-- This command imports data from a CSV file into the Employees table
LOAD DATA INFILE '/tmp/Employees_backup.csv'
-- Specifies the target table as Employees
INTO TABLE Employees
-- Indicates that fields in the CSV are separated by commas
FIELDS TERMINATED BY ','
-- Specifies that field values are enclosed in double quotes
ENCLOSED BY '"'
-- Indicates that each record ends with a newline character
LINES TERMINATED BY '\n';
Explanation:
- Purpose of the Query:
- To restore table data from a CSV backup file.
- Demonstrates the LOAD DATA INFILE statement for importing CSV data.
- Key Components:
- Matches the field and line terminators used during export.
- Targets the "Employees" table for data insertion.
- Real-World Application:
- Facilitates fast data restoration or migration from CSV backups.
Note:
- Ensure that the CSV file path is correct and accessible by MySQL.
- Verify that the table structure matches the CSV file format.
For more Practice: Solve these Related Problems:
- Write a SQL query to import data from a CSV file into the "Employees" table with fields terminated by a semicolon.
- Write a SQL query to import data into the "Orders" table from a CSV file while skipping the header row.
- Write a SQL query to import data into the "Products" table from a CSV file and update existing records on duplicate keys.
- Write a SQL query to import data from a CSV file into the "Customers" table while handling NULL values for missing fields.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous MySQL Exercise: Export Data to a CSV File Using SELECT INTO OUTFILE.
Next MySQL Exercise: Backup User Privileges from the mysql Database.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics