Import Only Second Column of CSV into MySQL - mysql

I want to import the second column of a CSV file into MySQL. Here's the CSV file:
Name,Source,Follows
John,Youtube,Y
Kat,FB,N
Jacob,Twitter,N
Here's the code I have so far:
DROP TABLE temp;
CREATE TABLE temp
(ID INT AUTO_INCREMENT primary key,
sn VARCHAR(50)
);
DESCRIBE temp;
LOAD DATA LOCAL INFILE '/Users/...temp.csv' INTO TABLE Person
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
IGNORE 1 LINES
(#col2) set sn=#col2;
SELECT * FROM temp;
However, I get that there is an empty set.

Try:
File: /path/to/temp.csv:
Name,Source,Follows
John,Youtube,Y
Kat,FB,N
Jacob,Twitter,N
MySQL Command-Line:
mysql> DROP TABLE IF EXISTS `temp`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `temp` (
-> `ID` INT AUTO_INCREMENT PRIMARY KEY,
-> `sn` VARCHAR(50)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> LOAD DATA LOCAL INFILE '/path/to/temp.csv'
-> INTO TABLE `temp`
-> FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
-> IGNORE 1 LINES
-> (#`null`, `sn`, #`null`);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
mysql> SELECT `ID`, `sn`
-> FROM `temp`;
+----+---------+
| ID | sn |
+----+---------+
| 1 | Youtube |
| 2 | FB |
| 3 | Twitter |
+----+---------+
3 rows in set (0.00 sec)
UPDATE
mysql> LOAD DATA LOCAL INFILE '/tmp/temp.csv'
-> INTO TABLE `temp`
-> FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
-> IGNORE 1 LINES
-> (#`null`, `sn`);
Query OK, 3 rows affected, 3 warnings (0.00 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 3
mysql> SHOW WARNINGS;
+---------+------+---------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------+
| Warning | 1262 | Row 1 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 2 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 3 was truncated; it contained more data than there were input columns |
+---------+------+---------------------------------------------------------------------------+
3 rows in set (0.00 sec)
mysql> SELECT `ID`, `sn`
-> FROM `temp`;
+----+---------+
| ID | sn |
+----+---------+
| 1 | Youtube |
| 2 | FB |
| 3 | Twitter |
+----+---------+
3 rows in set (0.00 sec)

Related

How to modify a column to insert consequtive numbers?

So I have a table where a column that was given an auto_increment value accidentally got started form 300 instead of 1,2,3,4......i'm a beginner and i do not know how to change it back to 1,2,3,4......screenshot of table
how to change the 307, 308 to 1,2,3,4...?
I tried to update the table but that did not work.
Step-1) First take backup of your table data.
Step-2) Truncate the table by using the below SQL query.
TRUNCATE TABLE [Your_Table_Name];
Step-3) then again insert the into your table using backup data.
Alter table to drop the auto_increment, update, alter table to add the auto_increment
drop table if exists t;
create table t
( id int auto_increment primary key, val int);
insert into t values
(307,1),(308,1),(309,1),(310,1),(311,1);
alter table t
modify column id int;
#drop primary key;
show create table t;
update t
set id = id - 306;
alter table t
modify column id int auto_increment;
show create table t;
https://dbfiddle.uk/eBQh6cj8
With MySQL 8.0 you can use a window function to calculate the row numbers and then update the table:
mysql> select * from t;
+-----+------+
| id | val |
+-----+------+
| 307 | 1 |
| 308 | 1 |
| 309 | 1 |
| 310 | 1 |
| 311 | 1 |
+-----+------+
mysql> with cte as ( select id, row_number() over () as rownum from t )
-> update t join cte using (id) set id = rownum;
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5 Changed: 5 Warnings: 0
mysql> select * from t;
+----+------+
| id | val |
+----+------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
+----+------+
Then make sure the next id won't be a high value:
mysql> alter table t auto_increment=1;
You can try to set the auto_increment to 1, MySQL will automatically advances that to the highest id value in the table, plus 1.
Be aware that this doesn't guarantee subsequent rows will use consecutive values. You can get non-consecutive values if:
You insert greater values explicitly, overriding the auto-increment.
You roll back transactions. Id values generated by auto-increment are not recycled if you roll back.
You delete rows.
Occasionally InnoDB will skip a number anyway. It does not guarantee consecutive values — it only guarantees unique values. You should not rely on the auto-increment to be the same as a row number.
Here is a one approach to your problem.
Please take note of the following points before proceeding:
Take backup of your table in-case things do not go as expected.
Below test case has been performed on MySQL 5.7 and MyISAM Engine.
Step1: Generating dummy test table as per your test case.
mysql> CREATE TABLE t (
-> `Id` int(11) NOT NULL AUTO_INCREMENT,
-> `product_id` int(11) DEFAULT 0,
-> PRIMARY KEY (`Id`)
-> ) ENGINE=MyISAM;
Query OK, 0 rows affected (0.03 sec)
-- Inserting dummy data
mysql> INSERT INTO t VALUES (300,1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO t VALUES (302,1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO t VALUES (305,1);
Query OK, 1 row affected (0.00 sec)
-- Checking auto_increment value
mysql> show create table t;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t | CREATE TABLE `t` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) DEFAULT '0',
PRIMARY KEY (`Id`)
) ENGINE=MyISAM AUTO_INCREMENT=306 DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> INSERT INTO t (product_id) VALUES (2);
Query OK, 1 row affected (0.01 sec)
-- Below is the resultant table for which we need Id starting from 1,2,3 and so on...
mysql> SELECT * FROM t;
+-----+------------+
| Id | product_id |
+-----+------------+
| 300 | 1 |
| 302 | 1 |
| 305 | 1 |
| 306 | 2 |
+-----+------------+
4 rows in set (0.00 sec)
Step2: Remove AUTO_INCREMENT for the column and set the Ids manually.
-- Remove AUTO_INCREMENT
mysql> ALTER TABLE t MODIFY COLUMN Id int(11) NOT NULL;
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
-- Set the Id manually starting from 1
mysql> SET #i = 0;UPDATE t SET id = #i :=#i +1;
Query OK, 0 rows affected (0.00 sec)
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5 Changed: 5 Warnings: 0
-- Below is the updated table with Id starting from 1,2,3 and so on...
mysql> SELECT * FROM t;
+----+------------+
| Id | product_id |
+----+------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
+----+------------+
5 rows in set (0.00 sec)
Step3: Enable AUTO_INCREMENT again for future record insertions.
-- Enable AUTO_INCREMENT again for future record insertions.
mysql> ALTER TABLE t MODIFY COLUMN Id int(11) NOT NULL AUTO_INCREMENT;
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
-- Set the AUTO_INCREMENT value to continue from highest value of id in the table.
mysql> SELECT MAX(id+1) FROM t;
+-----------+
| MAX(id+1) |
+-----------+
| 6 |
+-----------+
1 row in set (0.00 sec)
mysql> ALTER TABLE t AUTO_INCREMENT=6;
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
-- Table is successfully modified and will have future records inserted with no gaps in Id's
mysql> INSERT INTO t (product_id) VALUES (5);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM t;
+----+------------+
| Id | product_id |
+----+------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 5 |
+----+------------+
6 rows in set (0.00 sec)
The DBCC CHECKIDENT management command is used to reset identity counter
DBCC CHECKIDENT (table_name [, { NORESEED | { RESEED [, new_reseed_value]}}])
[ WITH NO_INFOMSGS ]
EXample:
DBCC CHECKIDENT ('TestTable', RESEED, 0)
GO
many times we need to just reseed to next Id available
declare #max int
select #max=max([Id]) from [TestTable]
if #max IS NULL --check when max is returned as null
SET #max = 0
DBCC CHECKIDENT ('[TestTable]', RESEED, #max)
This will check the table and reset to the next ID.
You can get help from the link below:
Reset identity seed after deleting records in SQL Server
My mother says: the mountain that can be seen is not far away, don't stop trying

importing csv file with custom delimiter in mysql

I'm doing mysql in my terminal as well as in my cmd.
I have a table :
mysql> create table stu (sno int, name char(10));
It's CSV file (b.csv):
sno, name
1,a
2,b
3,"c,d"
I imported that in mysql :
mysql> load data local infile 'd://b.csv' into table stu fields terminated by ',' enclosed by '"' lines terminated by '\n' ignore 1 rows;
Query OK, 3 rows affected (0.05 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from stu;
+------+------+
| sno | name |
+------+------+
| 1 | a |
| 2 | b |
| 3 | c,d |
+------+------+
3 rows in set (0.00 sec)
When I change the default delimiter alone :
I have another file with a custom delimiter and a custom quotechar (a.csv):
*rno*-*name*
*1*-*ron*
*3*-*vince*
*5*-*Abi-nav*
when I import this into mysql,
mysql> load data local infile 'd://a.csv' into table stu1 fields terminated by "-"
enclosed by "*" lines terminated by '\n' ignore 1 rows;
Query OK, 2 rows affected, 1 warning (0.05 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 1
mysql> show warnings;
+---------+------+---------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------+
| Warning | 1262 | Row 1 was truncated; it contained more data than there were input columns |
+---------+------+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from stu1;
+------+------------+
| sno | name |
+------+------------+
| 1 | ron*
*3 |
| 5 | *Abi-nav*
+------+------------+
2 rows in set (0.00 sec)
I get this problem only when I change the default quotechar from " into other characters in both Ubuntu and in Windows.
Why I get this warning and this output??? How to solve this?
Thanks in advance
EDIT :
I tried as said by in the answer:
mysql> select version();
+-------------------------+
| version() |
+-------------------------+
| 5.7.31-0ubuntu0.16.04.1 |
+-------------------------+
1 row in set (0.04 sec)
mysql> load data local infile '/home/data.csv' into table stu fields terminated by "-" enclosed by '*' lines terminated by '\n' ignore 1 rows;
Query OK, 3 rows affected (0.19 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from stu;
+------+---------+
| sno | name |
+------+---------+
| 1 | ron |
| 3 | vince |
| 5 | Abi-nav |
+------+---------+
3 rows in set (0.00 sec)
Then here you go :
mysql> load data local infile '/home/a.csv' into table stu fields terminated by "-" enclosed by '|' lines terminated by '\n' ignore 1 rows;
Query OK, 2 rows affected, 1 warning (0.09 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 1
mysql> show warnings;
+---------+------+---------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------+
| Warning | 1262 | Row 1 was truncated; it contained more data than there were input columns |
+---------+------+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from stu;
+------+------------+
| sno | name |
+------+------------+
| 1 | ron|
|3 |
| 5 | |Abinav|
|
+------+------------+
2 rows in set (0.00 sec)
Again I got the warning. Why that happens???
*-*name*
*1*-*ron*
*3*-*vince*
*5*-*Abi-nav*
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.20 |
+-----------+
1 row in set (0.00 sec)
mysql> create table stu1 (sno int, name char(10));
Query OK, 0 rows affected (1.23 sec)
mysql> load data local infile '/data/data.csv' into table stu1 fields terminated by "-" enclosed by "*" lines terminated by '\n' ignore 1 rows;
Query OK, 3 rows affected (0.07 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from stu1;
+------+---------+
| sno | name |
+------+---------+
| 1 | ron |
| 3 | vince |
| 5 | Abi-nav |
+------+---------+
3 rows in set (0.00 sec)
it is working in mysql version - 8.0.20
Under windows i needed to change termination. so that it runs
MySQL 8.0.22
Load data infile 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/a.csv'
into table stu
fields terminated by "-"
enclosed by "*"
lines terminated by '\r\n'
ignore 1 LINES;
The main problem that i see, is that we can't see your csv file further you seem to switch between operating system Windows and linux.
Like i showed in my example the csv was produced in windows and had so \r\n as line termination, and this is what the MySQL interpreter had problems with, when i used \n\allone.
It had nothing to do with field termination or enclosure.
Your problem can be due to line termination (on Windows \r\n, on Unix/Linux \n)
You can try with a program like Notepad++, set the line termination to \n.
After delete all line break and recreate the line break.
If the load statement works, that was the issue.

what syntax to use to update a SET column in mysql?

I created a column called oilcompany that has SET data (Hunt, Pioneer, Chevron, BP)
I can enter any one of those into the oilcompany column and change from one to another one but I can not figure out how to change from one oilcompany to multiple oilcompany (eg. Hunt and BP)... any suggestion?
In the MySQL documentation there are not examples for UPDATE statements, but I normally use two ways to update these kind of columns:
Using text values
Using numeric values
Creating the test environment
mysql> CREATE TABLE tmp_table(
-> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> oilcompany SET('Hunt', 'Pioneer', 'Chevron', 'BP')
-> );
Query OK, 0 rows affected (0.54 sec)
mysql> INSERT INTO tmp_table(oilcompany) VALUES ('Hunt'), ('Pioneer');
Query OK, 2 rows affected (0.11 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM tmp_table;
+----+------------+
| id | oilcompany |
+----+------------+
| 1 | Hunt |
| 2 | Pioneer |
+----+------------+
2 rows in set (0.00 sec)
Alternative#1: Using Text Values
As a SET is a collection of ENUM elements, and any ENUM element can be treated as a string, then we can do things like:
mysql> UPDATE tmp_table
-> SET oilcompany = 'Hunt,BP'
-> WHERE id = 1;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM tmp_table;
+----+------------+
| id | oilcompany |
+----+------------+
| 1 | Hunt,BP |
| 2 | Pioneer |
+----+------------+
2 rows in set (0.00 sec)
Alternative#2: Using Numeric Values
Any SET element is stored internally as a 64bit number containing the combination of the bits that represent each SET element.
In our table: 'Hunt'=1, 'Pioneer'=2, 'Chevron'=4, 'BP'=8.
Also, mysql allows to use these numbers instead of text values. If we need to see the numeric value in the select, we need to use the SET column inside a numeric expression (E.g. adding zero).
Let's see the current values:
mysql> SELECT id, oilcompany+0, oilcompany FROM tmp_table;
+----+--------------+------------+
| id | oilcompany+0 | oilcompany |
+----+--------------+------------+
| 1 | 9 | Hunt,BP |
| 2 | 2 | Pioneer |
+----+--------------+------------+
2 rows in set (0.00 sec)
Here 9 = 'Hunt' (1) + 'BP' (8) and 2 = 'Pioneer' (2).
Now, let's change the Pioneer to 'Hunt' (1) + 'Chevron' (4):
mysql> UPDATE tmp_table
-> SET oilcompany = 5
-> WHERE id = 2;
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT id, oilcompany+0, oilcompany FROM tmp_table;
+----+--------------+--------------+
| id | oilcompany+0 | oilcompany |
+----+--------------+--------------+
| 1 | 9 | Hunt,BP |
| 2 | 5 | Hunt,Chevron |
+----+--------------+--------------+
2 rows in set (0.00 sec)

LOAD DATA INFILE to load data into different tables

I have two tables:
Company(id int, varchar(name), primary key(id));
Product (id int, c_id int, varchar(name), foreign key(c_id), references Company(id));
Table 'Company' stores a list of company names and 'Product' stores a list of product names, and one company can have multiple products.
If I have data file like this, tab delimited:
1 Apple iPhone
2 Apple iPad
3 Apple iMac
4 Google Gmail
5 Google Google Search
6 Amazon Kindle
Is it possible to use "Load DATA INFILE" to load this file into two tables, where the first column goes to table Company and second coloumn goes to Product? The question is how to load selected fields to a particular table, instead of loading the full record into one table.
You can try an approach like this, with the premise of using a temporary table. I hope you find it useful.
/path/to/file/file.csv
1,Apple,iPhone
2,Apple,iPad
3,Apple,iMac
4,Google,Gmail
5,Google,Google Search
6,Amazon,Kindle
mysql> DELIMITER //
mysql> DROP TRIGGER IF EXISTS `from_load_data`//
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP TABLE IF EXISTS `temp_company_product`//
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP TABLE IF EXISTS `product`//
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP TABLE IF EXISTS `company`//
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CREATE TABLE `company` (
-> `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
-> `name` VARCHAR(25),
-> UNIQUE KEY `unique_name` (`name`)
-> )//
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE `product` (
-> `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
-> `c_id` INT UNSIGNED NOT NULL,
-> `name` VARCHAR(25),
-> FOREIGN KEY (`c_id`) REFERENCES `company`(`id`)
-> ON UPDATE CASCADE ON DELETE CASCADE
-> )//
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE `temp_company_product` (
-> `id` INT UNSIGNED PRIMARY KEY,
-> `company_name` VARCHAR(25),
-> `product_name` VARCHAR(25)
-> )//
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TRIGGER `from_load_data` AFTER INSERT ON `temp_company_product`
-> FOR EACH ROW
-> BEGIN
-> INSERT INTO `company` (`name`) VALUES (NEW.`company_name`)
-> ON DUPLICATE KEY UPDATE `name` = VALUES(`name`);
-> INSERT INTO `product` (`c_id`, `name`)
-> SELECT `id`, NEW.`product_name`
-> FROM `company`
-> WHERE `name` = NEW.`company_name`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> LOAD DATA INFILE '/path/to/file/file.csv'
-> INTO TABLE `temp_company_product`
-> FIELDS TERMINATED BY ',' ENCLOSED BY '"'
-> LINES TERMINATED BY '\r\n'
-> (`id`, `company_name`, `product_name`)//
Query OK, 6 rows affected (0.00 sec)
Records: 6 Deleted: 0 Skipped: 0 Warnings: 0
mysql> SELECT
-> `id`,
-> `company_name`,
-> `product_name`
-> FROM
-> `temp_company_product`//
+----+--------------+---------------+
| id | company_name | product_name |
+----+--------------+---------------+
| 1 | Apple | iPhone |
| 2 | Apple | iPad |
| 3 | Apple | iMac |
| 4 | Google | Gmail |
| 5 | Google | Google Search |
| 6 | Amazon | Kindle |
+----+--------------+---------------+
6 rows in set (0.00 sec)
mysql> SELECT
-> `id`,
-> `name`
-> FROM
-> `company`//
+----+--------+
| id | name |
+----+--------+
| 6 | Amazon |
| 1 | Apple |
| 4 | Google |
+----+--------+
3 rows in set (0.00 sec)
mysql> SELECT
-> `id`,
-> `c_id`,
-> `name`
-> FROM
-> `product`//
+----+------+---------------+
| id | c_id | name |
+----+------+---------------+
| 1 | 1 | iPhone |
| 2 | 1 | iPad |
| 3 | 1 | iMac |
| 4 | 4 | Gmail |
| 5 | 4 | Google Search |
| 6 | 6 | Kindle |
+----+------+---------------+
6 rows in set (0.00 sec)
mysql> DROP TRIGGER IF EXISTS `from_load_data`//
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TABLE IF EXISTS `temp_company_product`//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
I don't believe that LOAD DATA is flexible enough to allow to you selectively load certain columns into two different already-existing tables. It was designed to be a fast work horse, but was not designed to be particularly flexible. An alternative would be to load your data into a temporary table, and then INSERT INTO...SELECT the data into your two tables.
CREATE TABLE temp(id int, company varchar(55), product varchar(55));
LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE temp
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(id, company, product);
And then just use INSERT INTO...SELECT to get the data into your two tables which already exist:
INSERT INTO Company (name)
SELECT company
FROM temp
INSERT INTO Product (name)
SELECT product
FROM temp

Use of UNION when creating tables

Database-1
create table sample (
id INT,
nm VARCHAR(10)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
UNION=(for tables from another databases);
So, when we do union what actually it meance?
Please explain, I am getting confusing for this type of UNION.
That looks close to the syntax for creating a merge table, but it has the engine type wrong. Your statement will ignore the union clause and simply create a new, empty table. In order to create merge table you need to specify ENGINE=MERGE.
14.3 The MERGE Storage Engine
The MERGE storage engine, also known as the MRG_MyISAM engine, is a
collection of identical MyISAM tables that can be used as one.
The tables you specify in the UNION clause there, must all be identical - ie, having the same index and column specification, and they must all be in the same order in each table.
After that, can you query your merge table and access the data from all of the tables that form it.
You can also insert into your merge table, which is something you cannot do with a view:
You can optionally specify an INSERT_METHOD option to control how
inserts into the MERGE table take place. Use a value of FIRST or LAST
to cause inserts to be made in the first or last underlying table,
respectively. If you specify no INSERT_METHOD option or if you specify
it with a value of NO, inserts into the MERGE table are not permitted
and attempts to do so result in an error.
Anyway, the doco has the rest of the information if you want to peruse more - I've never felt the need to use this type of table.
Example:
mysql>
mysql> create table t2 (
-> id integer primary key auto_increment,
-> val char(20)
-> ) engine=myisam;
Query OK, 0 rows affected (0.05 sec)
mysql>
mysql> insert into t1(val) values ('table1 a'), ('table1 b');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into t2(val) values ('table2 a'), ('table2 b');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql>
mysql>
mysql> create table mt (
-> id integer primary key auto_increment,
-> val char(20)
-> ) engine=merge union=(t1,t2) insert_method=last;
Query OK, 0 rows affected (0.04 sec)
mysql>
mysql> select * from mt;
+----+----------+
| id | val |
+----+----------+
| 1 | table1 a |
| 2 | table1 b |
| 1 | table2 a |
| 2 | table2 b |
+----+----------+
4 rows in set (0.00 sec)
mysql> insert into mt(val) values ('12345');
Query OK, 1 row affected (0.00 sec)
mysql> select * from mt;
+----+----------+
| id | val |
+----+----------+
| 1 | table1 a |
| 2 | table1 b |
| 1 | table2 a |
| 2 | table2 b |
| 3 | 12345 |
+----+----------+
5 rows in set (0.01 sec)
mysql> select * from t2;
+----+----------+
| id | val |
+----+----------+
| 1 | table2 a |
| 2 | table2 b |
| 3 | 12345 |
+----+----------+
3 rows in set (0.00 sec)