Multiple uses of MySQL Load Data1. LOAD Basic Background
2. LOAD basic parameters
-- Export basic parameters select * into outfile '/data/mysql/3306/tmp/employees.txt' character set utf8mb4 fields terminated by ',' enclosed by '"' lines terminated by '\n' from employees.employees limit 10; -- Import basic parameters load data infile '/data/mysql/3306/tmp/employees.txt' replace into table demo.emp character set utf8mb4 fields terminated by ',' enclosed by '"' lines terminated by '\n' ... 3. LOAD sample data and sample table structure
--Exported file data content [root@10-186-61-162 tmp]# cat employees.txt "10001","1953-09-02","Georgi","Facello","M","1986-06-26" "10002","1964-06-02","Bezalel","Simmel","F","1985-11-21" "10003","1959-12-03","Parto","Bamford","M","1986-08-28" "10004","1954-05-01","Christian","Koblick","M","1986-12-01" "10005","1955-01-21","Kyoichi","Maliniak","M","1989-09-12" "10006","1953-04-20","Anneke","Preusig","F","1989-06-02" "10007","1957-05-23","Tzvetan","Zielinski","F","1989-02-10" "10008","1958-02-19","Saniya","Kalloufi","M","1994-09-15" "10009","1952-04-19","Sumant","Peac","F","1985-02-18" "10010","1963-06-01","Duangkaew","Piveteau","F","1989-08-24" -- Sample table structure SQL > desc demo.emp; +-------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------------+------+-----+---------+-------+ | emp_no | int | NO | PRI | NULL | | | birth_date | date | NO | | NULL | | | first_name | varchar(16) | NO | | NULL | | | last_name | varchar(16) | NO | | NULL | | | fullname | varchar(32) | YES | | NULL | | -- A new field in the table, which does not exist in the exported data file | gender | enum('M','F') | NO | | NULL | | | hire_date | date | NO | | NULL | | | modify_date | datetime | YES | | NULL | | -- A new field is added to the table, which does not exist in the exported data file| delete_flag | char(1) | YES | | NULL | | -- A new field is added to the table, which does not exist in the exported data file+-------------+---------------+------+-----+---------+-------+ -- The corresponding relationship between the exported data and fields emp_no birth_date first_name last_name gender hire_date "10001" "1953-09-02" "Georgi" "Facello" "M" "1986-06-26" "10002" "1964-06-02" "Bezalel" "Simmel" "F" "1985-11-21" "10003" "1959-12-03" "Parto" "Bamford" "M" "1986-08-28" "10004" "1954-05-01" "Christian" "Koblick" "M" "1986-12-01" "10005" "1955-01-21" "Kyoichi" "Maliniak" "M" "1989-09-12" "10006" "1953-04-20" "Anneke" "Preusig" "F" "1989-06-02" "10007" "1957-05-23" "Tzvetan" "Zielinski" "F" "1989-02-10" "10008" "1958-02-19" "Saniya" "Kalloufi" "M" "1994-09-15" "10009" "1952-04-19" "Sumant" "Peac" "F" "1985-02-18" "10010" "1963-06-01" "Duangkaew" "Piveteau" "F" "1989-08-24" 4. LOAD scenario exampleScenario 1. There are more fields in the LOAD file than in the data table
-- Temporarily create a table structure with two fields SQL > create table emp_tmp select emp_no,hire_date from emp; SQL > desc emp_tmp; +-----------+------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+------+------+-----+---------+-------+ | emp_no | int | NO | | NULL | | | hire_date | date | NO | | NULL | | +-----------+------+------+-----+---------+-------+ -- Import data statement load data infile '/data/mysql/3306/tmp/employees.txt' replace into table demo.emp_tmp character set utf8mb4 fields terminated by ',' enclosed by '"' lines terminated by '\n' (@C1,@C2,@C3,@C4,@C5,@C6) -- This part corresponds to the 6 columns of data in the employees.txt file -- Only the 2 columns specified in the exported data are matched with the fields in the table. The order specified by the mapping relationship does not affect the import result. set hire_date=@C6, emp_no=@C1; -- Import data result example SQL > select * from emp_tmp; +--------+------------+ |emp_no |hire_date | +--------+------------+ | 10001 | 1986-06-26 | | 10002 | 1985-11-21 | | 10003 | 1986-08-28 | | 10004 | 1986-12-01 | | 10005 | 1989-09-12 | | 10006 | 1989-06-02 | | 10007 | 1989-02-10 | | 10008 | 1994-09-15 | | 10009 | 1985-02-18 | | 10010 | 1989-08-24 | +--------+------------+ 10 rows in set (0.0016 sec) Scenario 2. There are fewer fields in the LOAD file than in the data table
-- Import data statement load data infile '/data/mysql/3306/tmp/employees.txt' replace into table demo.emp character set utf8mb4 fields terminated by ',' enclosed by '"' lines terminated by '\n' (@C1,@C2,@C3,@C4,@C5,@C6) -- This part corresponds to the 6 columns of data in the employees.txt file -- Map the fields in the file to the fields in the table, and do not process the extra fields in the table. set emp_no=@C1, birth_date=@C2, first_name=@C3, last_name=@C4, gender=@C5, hire_date=@C6; Scenario 3. LOAD generates custom field data
-- Import data statement load data infile '/data/mysql/3306/tmp/employees.txt' replace into table demo.emp character set utf8mb4 fields terminated by ',' enclosed by '"' lines terminated by '\n' (@C1, @C2, @C3, @C4, @C5, @C6) -- This part corresponds to the 6 columns of data in the employees.txt file -- The following part clearly maps the fields in the table to the fields in the data file. Non-existent data is generated through function processing (it can also be set to a fixed value) set emp_no=@C1, birth_date=@C2, first_name=upper(@C3), -- Convert the imported data to uppercase last_name=lower(@C4), -- Convert the imported data to lowercase fullname=concat(first_name,' ',last_name), -- Concatenate first_name and last_name gender=@C5, hire_date=@C6 , modify_date=now(), -- Generate current time data delete_flag=if(hire_date<'1988-01-01','Y','N'); -- Perform conditional operations on the values to be generated based on a certain column Scenario 4. LOAD fixed-length data
SQL > select c1 as sample_data, substr(c1,1,3) as c1, substr(c1,4,3) as c2, substr(c1,7,2) as c3, substr(c1,9,5) as c4, substr(c1,14,3) as c5, substr(c1,17,3) as c6 from t1 *************************** 1. row *************************** sample_data: ABC Yu Zhenxing CD MySQL EF G database c1: ABC c2: Yu Zhenxing c3: CD c4: MySQL c5: EFG c6: Database
-- Generate fixed-length data SQL > select concat(rpad(emp_no,10,' '), rpad(birth_date,19,' '), rpad(first_name,14,' '), rpad(last_name,16,' '), rpad(gender,2,' '), rpad(hire_date,19,' ')) as fixed_length_data from employees.employees limit 10; +--------------------------------------------------------------------------------------------------+ | fixed_length_data | +--------------------------------------------------------------------------------------------------+ | 10001 1953-09-02 Georgi Facello M 1986-06-26 | | 10002 1964-06-02 Bezalel Simmel F 1985-11-21 | | 10003 1959-12-03 Parto Bamford M 1986-08-28 | | 10004 1954-05-01 Christian Koblick M 1986-12-01 | | 10005 1955-01-21 Kyoichi Maliniak M 1989-09-12 | | 10006 1953-04-20 Anneke Preusig F 1989-06-02 | | 10007 1957-05-23 Tzvetan Zielinski F 1989-02-10 | | 10008 1958-02-19 Saniya Kalloufi M 1994-09-15 | | 10009 1952-04-19 Sumant Peac F 1985-02-18 | | 10010 1963-06-01 Duangkaew Piveteau F 1989-08-24 | +--------------------------------------------------------------------------------------------------+ -- Export fixed-length data select concat(rpad(emp_no,10,' '), rpad(birth_date,19,' '), rpad(first_name,14,' '), rpad(last_name,16,' '), rpad(gender,2,' '), rpad(hire_date,19,' ')) as fixed_length_data into outfile '/data/mysql/3306/tmp/employees_fixed.txt' character set utf8mb4 lines terminated by '\n' from employees.employees limit 10; --Export data example [root@10-186-61-162 tmp]# cat employees_fixed.txt 10001 1953-09-02 Georgi Facello M 1986-06-26 10002 1964-06-02 Bezalel Simmel F 1985-11-21 10003 1959-12-03 Parto Bamford M 1986-08-28 10004 1954-05-01 Christian Koblick M 1986-12-01 10005 1955-01-21 Kyoichi Maliniak M 1989-09-12 10006 1953-04-20 Anneke Preusig F 1989-06-02 10007 1957-05-23 Tzvetan Zielinski F 1989-02-10 10008 1958-02-19 Saniya Kalloufi M 1994-09-15 10009 1952-04-19 Sumant Peac F 1985-02-18 10010 1963-06-01 Duangkaew Piveteau F 1989-08-24 -- Import fixed-length data load data infile '/data/mysql/3306/tmp/employees_fixed.txt' replace into table demo.emp character set utf8mb4 fields terminated by ',' enclosed by '"' lines terminated by '\n' (@row) -- Define a row of data as a whole set emp_no = trim(substr(@row,1,10)), -- Use substr to get the first 10 characters and remove the leading and trailing spaces birth_date = trim(substr(@row,11,19)), -- The subsequent fields are similar first_name = trim(substr(@row,30,14)), last_name = trim(substr(@row,44,16)), fullname = concat(first_name,' ',last_name), -- concatenate first_name and last_name gender = trim(substr(@row,60,2)), hire_date = trim(substr(@row,62,19)), modify_date = now(), delete_flag = if(hire_date<'1988-01-01','Y','N'); -- Perform conditional operations on the values to be generated based on a certain column 5. LOAD Summary 1. By default, the import order is 2. If the table structure and text data are inconsistent, it is recommended to number the columns in the text file in sequence and establish a mapping relationship with the fields in the table to prevent data from being imported into the wrong field. 3. For scenarios where the text file to be imported is large, it is recommended to 4. After importing the file, it is recommended to execute the following statement to verify whether the imported data has
5. If there is a huge difference between the text file data and the table structure or the data needs to be cleaned and converted, it is recommended to use a professional ETL tool or roughly import it into MySQL before processing and converting it. The above is the detailed content of the various uses of MySQL Load Data. For more information about the usage of MySQL Load Data, please pay attention to other related articles on 123WORDPRESS.COM! , I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of Docker compose orchestration tool
>>: This article will show you what Vite does to the browser's request
MySQL row to column operation The so-called row-t...
Nginx is a high-performance website server and re...
In actual work or interviews, we often encounter ...
In many cases, arrays are often used when writing...
Table of contents Defining the HTML structure Inp...
Preface There are often some articles on the Inte...
In our daily development work, text overflow, tru...
Vim is a text editor that we use very often in Li...
Sometimes we build a file server through nginx, w...
Table of contents Import on demand: Global Import...
Table of contents 1. Compiler code format specifi...
Table of contents Step 1: Build the framework Ste...
1. Use Docker Compose to configure startup If you...
Table of contents 1. What is a subquery? 2. Self-...
I searched a lot online and found that many of th...