Hi
What's the best/quickest way to insert large amounts of data (say, countries) into a MYSQL database?
Something like:
INSERT INTO Country VALUES (1,'Afghanistan'),(2,'Brazil'),.....Z ?
Or is there anything faster?
Thanks.
Mak.
| SitePoint Sponsor |


Hi
What's the best/quickest way to insert large amounts of data (say, countries) into a MYSQL database?
Something like:
INSERT INTO Country VALUES (1,'Afghanistan'),(2,'Brazil'),.....Z ?
Or is there anything faster?
Thanks.
Mak.
Theres only one way to get info into an SQL server, and thats to INSERT it one row at a time.... So no, there isnt any faster way.
THE INSTRUCTIONS BELOW ARE OLD AND MAY BE INACCURATE.
THIS INSTALL METHOD IS NOT RECOMMENDED, IT MAY RUN
OVER YOUR DOG. <-- MediaWiki installation guide


Hi
I don't suppose anyone has countries/occupations (etc.) already typed up into SQL inserts? They are quite common so it would be a shame to re-do all that typing. If not, guess I will have to!
Thanks.
Mak.
Why are some people too lazy to use Google?
this came up as the first result with "country code iso mysql insert"
http://27.org/isocountrylist/


Hey
Thanks for that.
I did use google.. not lazy to use it, just incompetent at using it, perhaps :P. Thanks for that.
The person who did this went about it in the following way:
----
# Create table for ISO country list
CREATE TABLE IF NOT EXISTS country (
id INT(11) NOT NULL auto_increment,
name VARCHAR(80) NOT NULL,
iso CHAR(2) NOT NULL,
PRIMARY KEY (id),
INDEX by_name (name,iso),
INDEX by_iso (iso,name),
);
# Data for ISO country list
INSERT INTO country values(NULL,"Afghanistan","AF");
----
I was wondering if someone could explain the following to me:
1. How the INDEX by is used?
2. Why is the first auto_increment value set to NULL instead of 1,2..n. Is that 'cos it is 'auto_increment' so why not let SQL fill it in?
Thanks.
Mak.
Moved to a more appropriate forum
Sean![]()
Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
I can answear th is part....Originally posted by mak-uk
2. Why is the first auto_increment value set to NULL instead of 1,2..n. Is that 'cos it is 'auto_increment' so why not let SQL fill it in?
NULL is the same as leaving it blank, but since you're doing values, doing this:
Would put ID as "Afghanistan".PHP Code:INSERT INTO country values("Afghanistan","AF");
So you can do one of 2 things:
OrPHP Code:INSERT INTO country values("","Afghanistan","AF");
Null is easier, IMO, it'll still put the right ID in there.PHP Code:INSERT INTO country values(NULL,"Afghanistan","AF");
It's becuase when you use VALUES() it doesn't know Afghanistan is supposed to be a country, it'll think if it's first it shouild be the ID
Hope I helped!
Post back if you still don't get it
Thanks,
~someonewhois
- Nathan


Hi
What about if instead of putting:
INSERT INTO country values(NULL,"Afghanistan","AF");
You put:
INSERT INTO country values(1,"Afghanistan","AF");
INSERT INTO country values(2,"Brazil","BR");
... down to 300 or something
Would that work the same? If it does, obviously it's easier putting NULL instead as it will ensure that u dont have any errors in numbering and it will automatically do the work for u... right?(I think!).
Thanks.
Mak.
Why not let the ID auto-increment like it is designed to do instead of entering values for it? If you specify the column names as part of the insert command you should be able to skip the ID column. It should look like this:Originally posted by mak-uk
What about if instead of putting:
INSERT INTO country values(NULL,"Afghanistan","AF");
You put:
INSERT INTO country values(1,"Afghanistan","AF");
INSERT INTO country values(2,"Brazil","BR");
... down to 300 or something
INSERT INTO country NAME, ISO values ("Afghanistan", "AF");
...
Last edited by haironfire; Aug 11, 2002 at 10:01.





That sql would screw it up. DoCode:INSERT INTO country (name, iso) VALUES("Afghanistan", "AF");
Right. Forgot to show the () around the column names. But the point remains the same.


Hi
Would this work as well:
INSERT INTO country (name, iso) VALUES('Afghanistan', 'AF');
i.e. single quotes instead of speech marks.
Are single quotes just used in queries within PHP?
Thanks.
Mak.
Either is fine. I prefer using "" as a rule. If you have a string embedded within a string, use "" on the outside. However, if you only have one string either way is fine.Originally posted by mak-uk
Are single quotes just used in queries within PHP?
You could just play around with this to find workable combinations.
yeah, it's designed to auto increment if you want it to! dumps always have an AUTO_INCREMENT column's values explicitly in the dump file. you certainly DO NOT want the numbers to be generated for you with existing data! if numbers were deleted, they will get reused when renumbered sequentially. if you had references to a certain id in another table, which you most likely did, guess what? it will be pointing to the wrong thing now.Originally posted by haironfire
Why not let the ID auto-increment like it is designed to do instead of entering values for it?
since these AUTO_INCREMENT columns usually serve as the PRIMARY KEY, its values should stay the same. there's a good chance that won't happen if you "let the ID auto-increment like it is designed to do..."
- Matt
Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
"Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR
Thanks for the heads up.Originally posted by DR_LaRRY_PEpPeR since these AUTO_INCREMENT columns usually serve as the PRIMARY KEY, its values should stay the same. there's a good chance that won't happen if you "let the ID auto-increment like it is designed to do..."
I thought he was trying to insert fresh data, not a dump so it seems like it should be OK.
I am pretty new at this, but could you do the following:
1. create a new database in mysql.
2. compile the list of NAME, ISO values as a text file with each entry enclosed by quotes and a open / close paren around a whole line.
for example, ("Afghanistan", "AF")
3. Search and replace for the open paren to add the INSERT command
INSERT INTO county (names, iso) VALUE (
4. batch insert the resulting SQL file using:
mysql databasename < name_iso.sql
Does that make sense?


Hi
Hmm.. I am confused now.
What I am trying to do is write a PHP script to create my database, tables and put the values in there that I require.
So.. is it ok to do:
Where '...' is not part of the query but all the countries in the middle.PHP Code:$sql8-1 = mysql_query("INSERT INTO Country (countryName) VALUES ('Afghanistan'),...('Zimbabwe')");
if (mysql_query($sql8-1)) {
echo("<P>Query 8.1 OK: INSERT INTO Country</P>");
}
else {
echo("<P>Query 8.1 FAIL: INSERT INTO Country: " . mysql_error() . "</P>");
}
Thanks.
Mak.
yeah, that should work fine. the only problem is that $sql8-1 isn't a valid variable name in PHP.![]()


Hi
Silly me. What would you suggest as a valid alternative to express '8.1' in PHP?
Thanks.
Mak.![]()
$sql8_1![]()
Bookmarks