How to insert multiple rows in mysql - mysql

I have a table name :: users with id and name in mysql , the id is primary key with auto increment and name is varchar(50) and now i have a bunch of unique names almost 70 , how should i insert it in bulk , i know basic statements but i didn't found something best related to this problem
mysql> desc users;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+

The MySQL docs says:
INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of comma-separated column values, with lists enclosed within parentheses and separated by commas. Example:
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
Also you can use the Insert ... Select ... syntax:
INSERT INTO tbl_name (a,b,c)
SELECT a,b,c
FROM ...

Sorry man for me it was simple only insert into users (name) values ("name1"),("name2");

If you have all the information you want to insert inside file you might use LOAD DATA INFILE
LOAD DATA INFILE 'InfoToBeInsertedInsideDB.csv'
INTO TABLE tbl_name
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
The csv looks like:

Related

LOAD DATA LOCAL INFILE fails only on the first record's key (auto_increment) -- bug?

Here's the content of a very simple text file to show my problem. Somehow, I end up using "|" as the separator. But that's not the problem...
10|Antonio
11|Carolina
12|Diana
13|Alejandro
Here is the code I use to create that very simple table and to load the file into it.
CREATE TABLE IF NOT EXISTS names
(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100)
);
LOAD DATA LOCAL INFILE
'C:\\Users\\Carolina\\Desktop\\Tmp\\TablasCSV\\names.csv'
INTO TABLE names
CHARACTER SET 'utf8'
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\r\n';
Here is the result of a simple select:
mysql> SELECT * FROM names;
+----+-----------+
| id | name |
+----+-----------+
| 1 | Antonio |
| 11 | Carolina |
| 12 | Diana |
| 13 | Alejandro |
+----+-----------+
4 rows in set (0.00 sec)
It has worked fine for me until I encounter the case, in which the first record value for the "id" was not "1".
I always get "1" after the load.
Any one has noticed this problem?
Is this a bug?
So far, I am fixing the record using an UPDATE command after the load, but I don't like it!!!!

How to insert data into a table using SQL?

Insert into the App.settings table the following values:
(99, DEFAULT, "horizontal", "2015-09-15 04:01:04")
I have a DATABASE called App with a settings table. I am trying to insert into the table but I can not seem to get it right.
My statement:
INSERT INTO App.settings
VALUES(99, DEFAULT, "horizontal", "2015-09-15 04:01:04");
Am I doing it right? It says my answer is wrong.
mysql> DESC App.settings;
+-----------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+-------+
| user_id | int(7) | NO | | NULL | |
| email_frequency | tinyint(2) unsigned | YES | | NULL | |
| layout | varchar(70) | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+-----------------+---------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
When you use insert, always list the columns in the table. Second, the default string delimiter is the single quote in SQL rather than the double quote.
So I would expect to see:
INSERT INTO App.settings (col1, col3, col4) -- your real column names here
VALUES (99, 'horizontal', '2015-09-15 04:01:04');
Note that col2 was removed from the INSERT and the VALUES because you seem to want a DEFAULT value. Not all databases support that syntax.
you are sure that all the fields of the tuple are there and if so, they are in the correct order if you are not entering all the fields of the tuple you should use the following form: INSERT INTO App.settings(value, config, position, date ) VALUES(99, "DEFAULT", "horizontal", "2015-09-15 04:01:04");
In the same order
this is only an example i dont know the fields names you must be change for the you are using
Like #GordonLinoff said, you should include the columns in your query. That way, you can also skip entering the DEFAULT value for email_frequency.
As for the mistake, it's most likely the double quotes that you're currently using instead of single quotes.
Try the following:
INSERT INTO App.settings (user_id, layout, updated_at)
VALUES (99, 'horizontal', '2015-09-15 04:01:04');

Insert random string into field with MySQL UNI key

The task is to fill the table with N rows of random unique data.
I have the next MySQL table structure:
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(20) | NO | UNI | NULL | |
+----------+--------------+------+-----+---------+----------------+
Username field has string type but if the script will insert numbers its OK.
Theres is dirty solution with INSERT IGNORE, that can make 1000 random rows with endless cycle.
INSERT IGNORE INTO table (id,username) VALUES ('', 1 + ceil(rand() * 1000));
Also, I can use ON DUPLICATE KEY structure, but this 2 solutions are not OK.
I want to make the query, that generate unique username which will be unique and will be inserted from the first time.
So, I tell the script to add 1m of rows and it will insert 1m of unique data without any infelicities.
Any ideas? Thanks.
You can use UUID() which will give you a random string which would fit in your field, guaranteed to be unique with slim chance of collisions.

Bulk insert skips one of the fields in the iinput file

Working here on bulk insert, which skips 2 records. Explanation below:
My table (works fine, Auto-incrementing Job_Id):
create table avjobs ( Job_Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
Job_Name varchar(255), Job_Seq varchar(255), Job_Date varchar(255),
Start_Time time, End_Time time, Runtime time, Status varchar(255) );
Here is my csv file:
JOB1A|0029|20140506|14:01:05|15:00:01|0:59:45|FINISHED
JOB2B|0030|20140506|15:01:05|16:00:01|0:59:55|INITIATED
Here is the BULK INSERT that I am using:
LOAD DATA LOCAL INFILE '/tmp/jobs.csv' INTO TABLE avjobs FIELDS TERMINATED BY '|' LINES TERMINATED BY '\n';
Query OK, 3 rows affected, 9 warnings (0.00 sec)
Here is output for select:
mysql> select * from avjobs;
+--------+----------+----------+----------+------------+----------+----------+--------+
| Job_Id | Job_Name | Job_Seq | Job_Date | Start_Time | End_Time | Runtime | Status |
+--------+----------+----------+----------+------------+----------+----------+--------+
| 1 | 0029 | 20140506 | 14:01:05 | 15:00:01 | 00:23:55 | 00:00:00 | NULL |
| 2 | 0030 | 20140506 | 15:01:05 | 16:00:01 | 00:59:55 | 00:00:00 | NULL |
+--------+----------+----------+----------+------------+----------+----------+--------+
The Bulk insert skips, somehow, the job name as well as status.
Can you please advise what is wrong in syntax?
You must specify your columns:
LOAD DATA LOCAL INFILE '/tmp/jobs.csv'
INTO TABLE avjobs (Job_Name, Job_Seq, Job_Date, Start_Time, End_Time, Runtime, Status)
FIELDS TERMINATED BY '|' LINES TERMINATED BY '\n';
because your import file doesn't contain your Job_ID, see manual
LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata; By default,
when no column list is provided at the end of the LOAD DATA INFILE
statement, input lines are expected to contain a field for each table
column. If you want to load only some of a table's columns, specify a
column list:

MySQL Insert data to table using column number not name

Do I have to know the name of the column so to be able to insert data ? because I need to insert data using column number (first column is 1...)
Instead of using this :
insert into my_table (column1, column2)
values (value1, value2)
Just use that :
insert into my_table values (value1, value2)
i.e. don't specify the list of columns.
Of course, this will only work if you pass data for the columns in the order they are defined in the table, though.
if you have a table like (for example) :
CREATE TABLE test (
id INT(10),
field1 VARCHAR(16),
field2 TINYINT(1)
);
you can execute the query
INSERT INTO test VALUES (1, 'field1 value', 0);
The insert values must be in the same order as the declared columns. And as Galz mentioned it, you must set all fields, even the nullable or the auto incrementing ones. In fact, for the latter, you have to pass a null value.
For example, if id were to be a primary key set to "auto_increment", this query
INSERT INTO test VALUES (null, 'foo', 1);
SELECT * FROM test;
Would return
+----+--------------+--------+
| id | field1 | field2 |
+----+--------------+--------+
| 1 | field1 value | 0 |
| 2 | foo | 1 |
+----+--------------+--------+
On another subject, if you don't know beforehand what is the order of the fields, you can execute this query :
show columns from <table name>;
which, in this case would return
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| field1 | varchar(16) | YES | | NULL | |
| field2 | tinyint(1) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
Thus telling you that id is the first field, field1 is the second, and field2 is third.