MySQL is a relational database management system that is often used for web application development and data storage. When we need to import data into MySQL, we can use the following steps.
Step 1: Create a database
First, we need to create a database in MySQL. We can create a database through the command line or tools such as MySQL Workbench.
The command line operation to create a database is as follows:
CREATE DATABASE databasename;
We can replace "databasename" with the database name of our choice.
Step 2: Create a table
Next, we need to create a table to store the data. We can use tools such as MySQL Workbench to create tables, or we can create them through the command line. If we want to create a table through the command line, the steps are as follows:
USE databasename; CREATE TABLE tablename ( column1 datatype, column2 datatype, column3 datatype, ..... );
In the above command, we need to replace "databasename" with the name of the database we created and "tablename" with the name of the database we want to create Table name, and specify the column name to be created and the corresponding data type through "column1", "column2", etc.
Step 3: Prepare data
Before importing data into MySQL, we need to prepare the data first. Data can include text files, Excel files and other formats.
In order to successfully import data into MySQL, we need to save the data in .csv or .txt format. First open the Excel table, click "File" - "Save As" - save the file format as ".csv" or ".txt" format.
Step 4: Import data
We can import data into MySQL in the following ways:
Open MySQL Workbench, select "Server" - "Data Import" - "Import from Self-Contained File" - select the CSV file to be imported - select the target database and table - click "Start Import".
The operation of using the command line to import data is as follows:
LOAD DATA LOCAL INFILE 'filepath/filename.csv' INTO TABLE tablename FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY ' ';
In the above command, we need to change "filepath/filename. csv" is replaced with the path and name of the CSV file we want to import, "tablename" is replaced with the name of the table to which the data is to be imported, where "FIELDS TERMINATED BY" and "LINES TERMINATED BY" are used to specify the separators of columns and rows and Terminator at the end of the line.
In PHPMyAdmin, select the target database and table, click "Import" - select the CSV file to be imported - set options - click " Import".
Summary
The above are the steps for importing data into MySQL. We can choose a method that suits us to import data according to our needs. When importing data, you need to pay attention to the format, path, name, delimiter, etc. of the data to ensure that the data can be correctly imported into MySQL. At the same time, we need to create the database and tables before importing data to ensure that the data can be stored correctly.
The above is the detailed content of How to import mysql. For more information, please follow other related articles on the PHP Chinese website!