I ran the following in my sql database:
update table1 SET car_color_code="red metalic" where car_model="335i";
and it said:
Query OK, 1 row affected (0.06 sec) Rows matched: 1 Changed: 1
Warnings: 0
However, I dont see the change in the table
The table looks like this:
------------------------------------------------
| ID | model | manufacturer | car_color_code |
| 1 | 335i | BMW | NULL |
------------------------------------------------
So the null from before should be replaced with "red metalic" but it stays NULL.
However when i try to insert again it says:
Rows matched: 1 Changed: 0 Warnings: 0
And when i try to select again I still dont see the change in car_color_code column...
My table description is
+------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| model | varchar(255) | NO | | NULL | |
| manufacturer | varchar(255) | NO | | NULL | |
| car_color_code | varchar(255) | NO | | NULL | |
+------------------+--------------+------+-----+---------+----------------+
You might have forgot to commit. It is possible that you have exited out of MySQL server and tried the select statement after relogging in.
Make sure you commit after you update.
I had two tables running in 2 different databases but the structure is identical. I want to import data of one table into the other but the id of the rows was autoincrement. This causes id's in both tables to have the same value but their content is different.
How do I insert the content of table1 into table 2 and auto update the id to a value that doesnt exist yet?
Because the table contains around 1000 rows I can't manually change the numbers or declare each individual row.
Something like ON DUPLICATE 'id' AUTO INCREMENT 'id'
?
this could be the way
Hitesh> desc test;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| name | varchar(200) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
Hitesh> desc test_new;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| name | varchar(200) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
Hitesh> insert into test_new(name) select name from test;
Query OK, 9 rows affected (0.03 sec)
Records: 9 Duplicates: 0 Warnings: 0
Hitesh> select * from test_new;
+-------------------------+----+
| name | id |
+-------------------------+----+
| i am the boss | 1 |
| You will get soon | 2 |
| Happy birthday bro | 3 |
| the beautiful girl | 4 |
| oyee its sunday | 5 |
| cat and dog in a park | 6 |
| dog and cat are playing | 7 |
| cat | 8 |
| dog | 9 |
+-------------------------+----+
9 rows in set (0.00 sec)
INSERT INTO new_db.new_tbl SELECT * FROM old_db.old_tbl;
Above will not generate new ids for new_tbl.
Let me explain it a little further, we consider you have both tables with id as auto increment enabled.
Override the auto increments
insert into B select * from A;
If you insert a value into new_tbl's (B) id column. i.e. if you select all columns, This will override the auto increment for the new table.
Activate the auto increment
insert into B (col1, col2) select col1, col2 from A;
insert into B select 0, col1, col2 from A;
If you want activate the auto increment on new_tbl (B) you can not pass ids to the insert stmnt, so you will need to skip the id (chose the columns you want to migrate without id column) or send DEFAULT/NULL/0 for the id.
I am trying to insert into mysql table from a csv file using the following command in linux:
LOAD DATA LOCAL INFILE '\/home\/abc\/mapping.csv' INTO TABLE db1.ackno_rollno_mapping FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
csv file is like:
166 D/O-5
208 W/O-8
230
231
236 W/O-9
245 W/O-10
8604 P/O-142
8623 W/O-730
8629 W/O-731
Table structure is:
mysql> desc db1.ackno_rollno_mapping;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| ackNo | varchar(45) | NO | PRI | NULL | |
| rollNo | varchar(45) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
The records are inserted, but some parts of both ackNo and rollNo are missing.
Like:
| 58 | W/O-728
| 67 | W/O-729
| 04 | P/O-142
| 23 | W/O-730
| 29 | W/O-731
I tried changing the datatype of ackNo from varchar(45) to integer, but still the same issue...
Am i missing something??
Thanks in Advance
--------EDIT---------
my csv file is like:
13,W/O-1
14,W/O-2
20,P/O-1
60,D/O-1
61,W/O-3
62,W/O-4
I tried below query also (removing the ENCLOSED BY '"')
LOAD DATA LOCAL INFILE '\/home\/abc\/ackno_rollno_mapping.csv' INTO TABLE db1.ackno_rollno_mapping FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
But the issue still is same. I noticed that the first digit of ackNo is removed in the table.
mysql> select * from db1.ackno_rollno_mapping;
+-------+-----------+
| ackNo | rollNo |
+-------+-----------+
| 3 | W/O-1
| 4 | W/O-2
| 0 | P/O-1
| 0 | D/O-1
| 1 | W/O-3
| 2 | W/O-4
Please advice.
Thanks
I was trying to measure the difference between TINYINT and INT when I came across something interesting. For tables with small numbers of columns, the choice of data type does not seem to affect the size of the table.
Server version: 5.1.41-3ubuntu12.10 (Ubuntu)
Example:
mysql> describe tinyint_test;
+----------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| test_int | tinyint(4) | YES | | NULL | |
+----------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> describe tinyint_id_test;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | tinyint(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> describe int_test;
+--------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| not_id | int(11) | YES | | NULL | |
+--------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> select * from tinyint_test;
+------+----------+
| id | test_int |
+------+----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 127 |
| 10 | 50 |
+------+----------+
4 rows in set (0.00 sec)
mysql> select * from tinyint_id_test;
+------+
| id |
+------+
| 1 |
| 2 |
| 127 |
| 50 |
+------+
4 rows in set (0.00 sec)
mysql> select * from int_test;
+--------+
| not_id |
+--------+
| 1 |
| 2 |
| 127 |
| 50 |
+--------+
4 rows in set (0.00 sec)
mysql> SELECT TABLE_NAME, DATA_LENGTH FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA like '%test%';
+-----------------+-------------+
| TABLE_NAME | DATA_LENGTH |
+-----------------+-------------+
| int_test | 28 |
| tinyint_id_test | 28 |
| tinyint_test | 28 |
+-----------------+-------------+
3 rows in set (0.00 sec)
I vaguely suspect that there might be an internal column in each row, or that the minimum data size for a given row must be at least the size of a full INT, but neither of these suspicions really account for what's happening here. What could be the case is my choice of DATA_LENGTH is the incorrect tool for measuring the true size of the tables, in which case an acceptable answer would point me in the right direction for actually measuring these tables.
EDIT:
I can generate a table of a different size by using two INTs:
mysql> describe int_id_test;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| test_int | int(11) | YES | | NULL | |
+----------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> select * from int_id_test;
+------+----------+
| id | test_int |
+------+----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 127 |
| 10 | 50 |
+------+----------+
4 rows in set (0.00 sec)
mysql> SELECT TABLE_NAME, DATA_LENGTH FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA like '%test%';
+-----------------+-------------+
| TABLE_NAME | DATA_LENGTH |
+-----------------+-------------+
| int_id_test | 36 |
| int_test | 28 |
| tinyint_id_test | 28 |
| tinyint_test | 28 |
+-----------------+-------------+
4 rows in set (0.01 sec)
the data_length column is how much hard drive space the operating system allocates
for a table.
mysql database page sizes configurable default is 16KB, the three table's data may used same pages, so the data_length are same!!
edit:
innodb engine default page size is 16KB, i don't know this size for other engines
I have found a work around for this problem as well as something of an explanation.
After looking at the table structure in a hex editor (on my linux machines these were located in /var/lib/mysql/[DATABASE NAME]/[TABLE NAME].MYD), I found that in all cases the records were created using a minimum of 7 bytes for a row, regardless of the actual data types involved. Any extra bytes that were not used by the table were zeroed out.
Here is an example with a smaller data set to illustrate:
mysql> describe int_test_2;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> select * from int_test_2;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
Looking at this guy in a hex editor, we see:
fd01 0000 0000 00fd 0200 0000 0000
Using information from Neo's link, I was able to decode this row:
fd Record header bits.
01000000 Integer value "1" (little endian)
0000 Wasted Space!
fd Record header bits.
02000000 Integer value "2" (little endian)
0000 Wasted Space!
However, notice the following:
mysql> alter table int_test_2 MAX_ROWS=50000000, AVG_ROW_LENGTH=4;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
Now, the MYD file looks like this:
fd01 0000 00fd 0200 0000
That is, it uses the correct sizes.
One thing to note is that the number in brackets does not effect the size of that column, i.e an INT(4) is the same size as an INT(11) in terms of storage, all the number in brackets does is pad the returned value with spaces so that it fills 11 or 4 characters.
I suspect if you trully want to work out the size of the tables, you will need to look in the MySQL file itself and see how they are stored. All the data is stored in /var/lib/mysql/ - ibdata & ib_logfile are the main files. Open this in a text editor (Caution - this file may be HUGE depending on the sizes of your databases.. also DO NOT modify this file!!)
All the tables and cells are stored in here, however they are not delimeted, so its very difficult to see where one column ends and the next begins - it is all based on the data size which you are trying to establish. If you know the data in the table you should be able to work out the structure.
Edit: I think some of the data in these files may be stored in hex, so if it doesnt immediately make sense, try a hex editor.
I have a table called city:
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| country_id | mediumint(9) | NO | MUL | NULL | |
| region_id | bigint(20) | NO | MUL | NULL | |
| city | varchar(45) | NO | | NULL | |
| latitude | float(18,2) | NO | | NULL | |
| longitude | float(18,2) | NO | | NULL | |
| timezone | varchar(10) | NO | | NULL | |
| dma_id | mediumint(9) | YES | | NULL | |
| code | varchar(4) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
I have a simple file (just a test file) to import:
"id","country_id","region_id","city","latitude","longitude","timezone","dma_id","code"
42231,1,833,"Herat","34.333","62.2","+04:30",0,"HERA"
5976,1,835,"Kabul","34.517","69.183","+04:50",0,"KABU"
42230,1,852,"Mazar-e Sharif","36.7","67.1","+4:30",0,"MSHA"
42412,2,983,"Korce","40.6162","20.7779","+01:00",0,"KORC"
5977,2,1011,"Tirane","41.333","19.833","+01:00",0,"TIRA"
5978,3,856,"Algiers","36.763","3.051","+01:00",0,"ALGI"
5981,3,858,"Skikda","36.879","6.907","+01:00",0,"SKIK"
5980,3,861,"Oran","35.691","-0.642","+01:00",0,"ORAN"
I run this command:
LOAD DATA LOCAL INFILE 'cities_test.txt' INTO TABLE city FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;
Output:
Query OK, 0 rows affected (0.00 sec)
Records: 0 Deleted: 0 Skipped: 0 Warnings: 0
No records are inserted and I don't know why.
Any ideas?
Thanks!
Jamie
Worked it out. Silly mistake.
Had to change this:
LINES TERMINATED BY '\r\n'
To this:
LINES TERMINATED BY '\n'
:-)
I had the same problem, but I try this, erase the first row
`("id","country_id","region_id","city,"latitude","longitude",
"timezone","dma_id","code")` in your file to import.
Now when you run the comand write like this
mysql> LOAD DATA LOCAL
INFILE 'cities_test.txt'
INTO TABLE city FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';
And that is all.
It worked for me :D
I had same issue on mac,
Try this if you are using mac
LOAD DATA INFILE 'sqlScript1.txt' INTO TABLE USER
FIELDS TERMINATED BY ',' LINES STARTING BY '\r';
For me, what worked on a mac was
LOAD DATA LOCAL
INFILE 'cities_test.txt'
INTO TABLE city FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r';
Since Macs use carriage return for its line break you must use '/r'