mysql query duplicate urls associated with other indexes - mysql

I have this table that lacks a unique key and want to find out how many duplicate urls are associated with other indexes. Is possible to query this?
| URL | index |
| -------- | --------------|
| /page1 | 1 |
| /page1 | 1 |
| /page1 | 2 |
| /page2 | 1 |
| /page2 | 1 |
| /page3 | 1 |
| /page3 | 2 |
| /page3 | 2 |
| /page3 | 3 |
I want to return
| URL | count |
| -------- | --------------|
| /page1 | 2 |
| /page3 | 3 |
to essentially find out which indexs have a duplicate url associated with another index.

A GROUP BY and a COUNT will in cobination with HAVING give your anted result
CREATE TABLE table1
(`URL` varchar(8), `index` varchar(14))
;
INSERT INTO table1
(`URL`, `index`)
VALUES
('/page1', '1'),
('/page1', '1'),
('/page1', '2'),
('/page2', '1'),
('/page2', '1'),
('/page3', '1'),
('/page3', '2'),
('/page3', '2'),
('/page3', '3')
;
Records: 9 Duplicates: 0 Warnings: 0
SELECT
`URL`, COUNT(DISTINCT`index`) as count_
FROM table1
GROUP BY `URL`
HAVING count_ > 1
URL
count_
/page1
2
/page3
3
fiddle

Related

MYSQL filled some rows, but was't suppost to

So I made a basic struct of a database, and mysql filled some rows on a table and I don't know why.
Maybe I can explain myself with the tables
mysql> show fields
-> from products;
+--------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| ID_PRODUCT | decimal(8,0) | NO | PRI | NULL | |
| ID_CATEGORY | decimal(8,0) | NO | MUL | NULL | |
| NAME | text | NO | | NULL | |
| BRAND | text | NO | | NULL | |
| PICTURE | longblob | YES | | NULL | |
| OBSERVATIONS | text | YES | | NULL | |
| QUANT_UNIT | text | NO | | NULL | |
+--------------------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
So this is a table with products, and I have one other table with the prices so I can save the history of prices
mysql> show fields from prices;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| ID_PRODUCT | decimal(8,0) | NO | MUL | NULL | |
| PRICE | text | NO | | NULL | |
| TIME | timestamp | NO | | NULL | |
+------------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
And if I insert the values into the product and prices tables
mysql> insert into products (id_product, id_category, name, brand, quant_unit)
-> values (1, 0, 'rice', 'XPTO', 'kg');
Query OK, 1 row affected (0.01 sec)
mysql> select * from products;
+------------+--------------+--------+----------+----------------+-------------+--------------------+
| ID_PRODUCT | ID_CATEGORY | NAME | BRAND | PICTURE | OBSERVATION | QUANT_UNIT |
+------------+--------------+--------+----------+----------------+-------------+--------------------+
| 0 | 0 | mug | no_brand | 0x | favorite | un |
| 1 | 0 | rice | xpto | 0x | NULL | kg |
+------------+--------------+--------+----------+----------------+-------------+--------------------+
2 rows in set (0.00 sec)
mysql> insert into prices (id_product, price, time)
-> values (0, '5', CURRENT_TIMESTAMP);
Query OK, 1 row affected (0.01 sec)
mysql> select * from prices;
+------------+-------+---------------------+
| ID_PRODUCT | PRICE | TIME |
+------------+-------+---------------------+
| 0 | 7.30 | 2020-11-27 23:25:34 |
| 1 | 5 | 2020-11-27 23:29:12 |
+------------+-------+---------------------+
2 rows in set (0.00 sec)
all fine so far, but if I do a select with both of the tables
mysql> select pro.id_product, pro.id_category, pro.name, pro.brand, pro.quant_unit, pre.price
-> from products pro, prices pri;
+------------+--------------+--------+----------+-------------+-------+
| id_product | id_category | name | brand | quant_unit | price |
+------------+--------------+--------+----------+-------------+-------+
| 0 | 0 | mug | no_brand | un | 7.30 |
| 1 | 0 | rice | xpto | kg | 7.30 |
| 0 | 0 | mug | no_brand | un | 5 |
| 1 | 0 | rice | xpto | kg | 5 |
+------------+--------------+--------+----------+-------------+-------+
4 rows in set (0.00 sec)
those are my constraints to connect the tables
alter table PRICES add constraint FK_PRICES_COST_PRODUCTS foreign key (ID_PRODUCT)
references PRODUCTS (ID_PRODUCT) on delete restrict on update restrict;
alter table PRODUCTS add constraint FK_PRODUCTS_PERTENCE_CATEGORY foreign key (ID_CATEGORY)
references CATEGORY (ID_CATEGORY) on delete restrict on update restrict;
I can't even search the problem because it doesn't make sense to me... But I'm new at SQL, so I think I'm doing something wrong...
I will appreciate any feedback, thanks!
I advise against using comma-join but in your query you didn't specify what to match between those two tables. You can simply fix that by adding WHERE in your query like this:
select pro.id_product, pro.id_category, pro.name, pro.brand, pro.quant_unit, pre.price
from products pro, prices pri
where pro.id_product=pri.id_product;
But nowadays, most people uses JOIN instead of comma-join, hence:
select pro.id_product, pro.id_category, pro.name, pro.brand, pro.quant_unit, pre.price
from products pro JOIN prices pri
ON pro.id_product=pri.id_product;
There's a lot of JOIN types in MySQL and you can refer to this documentation.

How to set UNIQUE constraint to multiple columns MySQL?

I have a table
table location_category {
id,
location_id,
category_id,
is_primary
}
What I want is to set a UNIQUE constraint for the combination location_id and is_primary.
I get that using this will make a multi column UNIQUE constraint
ALTER TABLE `votes` ADD UNIQUE `unique_index`(`location_id`, `is_primary`);
But my concern is that we can have multiple categories for a location but only set 1 category as primary.
For example:
| id | location_id | category_id | is_primary |
| 1 | 1 | 1 | 0 |
| 2 | 1 | 2 | 0 |
| 3 | 1 | 3 | 1 |
| 4 | 1 | 4 | 0 |
Will this violate the UNIQUE contraint? Since I have multiple instances of location_id = 1 and is_primary = 0?
Just trying to figure this out. Thank you for helping.
There is no need to change anything, UNIQUE allows multiple NULL values
CREATE TABLE `votes` (
`id` INTEGER,
`location_id` INTEGER,
`category_id` INTEGER,
`is_primary` INTEGER
);
ALTER TABLE `votes` ADD UNIQUE `unique_index`(`location_id`, `is_primary`);
INSERT INTO `votes`
(`id`, `location_id`, `category_id`, `is_primary`)
VALUES
('1', '1', '1', NULL),
('2', '1', '2', NULL),
('3', '1', '3', '1'),
('4', '1', '4', NULL);
SELECT * from `votes`
id | location_id | category_id | is_primary
-: | ----------: | ----------: | ---------:
1 | 1 | 1 | null
2 | 1 | 2 | null
3 | 1 | 3 | 1
4 | 1 | 4 | null
db<>fiddle here
So you can only have one location with is primary 1

Magento causes deadlock in mariadb

My Magento(Vesion 1.8.0.0) website was running without any problem when I was using MySQL5.5 database for years. After moving to mariadb-server-10.2 recently(exported data using sqldump and then imported on new mariadb server), many times PHP child process stays in waiting states until max_execution_time expires and finally php-fpm stops responding.
On investigation I found that all the php child processes are waiting on database queries. I checked the processlist on mariadb when I encountered the problem recently and output is below. Any idea how I could fix this? Thanks in advance.
MariaDB [(none)]> show processlist;
+--------+-------------+-------------------------------+---------+---------+------+------------------------------+---------------------------------------------------------------------- --------------------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+--------+-------------+-------------------------------+---------+---------+------+------------------------------+---------------------------------------------------------------------- --------------------------------+----------+
| 1 | system user | | NULL | Daemon | NULL | | NULL | 0.000 |
| 2 | system user | | NULL | Daemon | NULL | | NULL | 0.000 |
| 4 | system user | | NULL | Daemon | NULL | | NULL | 0.000 |
| 3 | system user | | NULL | Daemon | NULL | | NULL | 0.000 |
| 5 | system user | | NULL | Daemon | NULL | InnoDB shutdown handler | NULL | 0.000 |
| 109822 | catalog | mymagentositexxxxxx.com:56405 | catalog | Query | 985 | update | INSERT INTO `cataloginventory_stock_status` (`product_id`,`website_id `,`stock_id`,`qty`,`stock_statu | 0.000 |
| 109850 | catalog | mymagentositexxxxxx.com:56436 | catalog | Query | 985 | update | INSERT INTO `cataloginventory_stock_status` (`product_id`,`website_id `,`stock_id`,`qty`,`stock_statu | 0.000 |
| 109859 | catalog | mymagentositexxxxxx.com:56448 | catalog | Query | 985 | update | INSERT INTO `cataloginventory_stock_status` (`product_id`,`website_id `,`stock_id`,`qty`,`stock_statu | 0.000 |
| 109931 | catalog | mymagentositexxxxxx.com:56554 | catalog | Query | 986 | Waiting for table level lock | DELETE FROM `catalogsearch_fulltext` WHERE (store_id=1) AND (product_ id IN ('26515')) | 0.000 |
| 109966 | catalog | mymagentositexxxxxx.com:56606 | catalog | Query | 984 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 33218 AS `query_id`, `s`.`p roduct_id`, 0 AS `relevance` FR | 0.000 |
| 109993 | catalog | mymagentositexxxxxx.com:56645 | catalog | Query | 934 | update | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat ed_at`, `product_id`, `store_id | 0.000 |
| 109997 | catalog | mymagentositexxxxxx.com:56650 | catalog | Query | 987 | Sending data | INSERT INTO `catalogsearch_result` SELECT 33215 AS `query_id`, `s`.`p roduct_id`, 0 AS `relevance` FR | 0.000 |
| 110040 | catalog | mymagentositexxxxxx.com:56709 | catalog | Query | 983 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 33215 AS `query_id`, `s`.`p roduct_id`, 0 AS `relevance` FR | 0.000 |
| 110218 | catalog | mymagentositexxxxxx.com:56925 | catalog | Query | 940 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 6656 AS `query_id`, `s`.`pr oduct_id`, 0 AS `relevance` FRO | 0.000 |
| 110248 | catalog | mymagentositexxxxxx.com:56956 | catalog | Query | 932 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 33220 AS `query_id`, `s`.`p roduct_id`, 0 AS `relevance` FR | 0.000 |
| 110380 | catalog | mymagentositexxxxxx.com:57095 | catalog | Query | 906 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 33214 AS `query_id`, `s`.`p roduct_id`, 0 AS `relevance` FR | 0.000 |
| 110392 | catalog | mymagentositexxxxxx.com:57107 | catalog | Query | 905 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 33215 AS `query_id`, `s`.`p roduct_id`, 0 AS `relevance` FR | 0.000 |
| 110810 | catalog | mymagentositexxxxxx.com:57554 | catalog | Query | 825 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 6656 AS `query_id`, `s`.`pr oduct_id`, 0 AS `relevance` FRO | 0.000 |
| 110865 | catalog | mymagentositexxxxxx.com:57616 | catalog | Query | 808 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 21581 AS `query_id`, `s`.`p roduct_id`, 0 AS `relevance` FR | 0.000 |
| 110968 | catalog | mymagentositexxxxxx.com:57782 | catalog | Query | 48 | update | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat ed_at`, `product_id`, `store_id | 0.000 |
| 110969 | catalog | mymagentositexxxxxx.com:57783 | catalog | Query | 48 | update | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat ed_at`, `product_id`, `store_id | 0.000 |
| 110989 | catalog | mymagentositexxxxxx.com:57803 | catalog | Query | 39 | update | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat ed_at`, `product_id`, `store_id | 0.000 |
| 110999 | catalog | mymagentositexxxxxx.com:57813 | catalog | Query | 34 | update | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat ed_at`, `product_id`, `store_id | 0.000 |
| 111012 | catalog | mymagentositexxxxxx.com:57867 | catalog | Query | 32 | update | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat ed_at`, `product_id`, `store_id | 0.000 |
| 111067 | catalog | mymagentositexxxxxx.com:57959 | catalog | Query | 18 | update | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat ed_at`, `product_id`, `store_id | 0.000 |
| 111077 | catalog | mymagentositexxxxxx.com:57974 | catalog | Query | 15 | update | INSERT INTO `sales_flat_quote_item` (`quote_id`, `created_at`, `updat ed_at`, `product_id`, `store_id | 0.000 |
| 111078 | root | localhost | NULL | Query | 0 | init | show processlist | 0.000 |
| 111117 | catalog | mymagentositexxxxxx.com:58015 | catalog | Query | 10 | Waiting for table level lock | INSERT INTO `catalogsearch_result` SELECT 13226 AS `query_id`, `s`.`p roduct_id`, 0 AS `relevance` FR | 0.000 |
| 111147 | moodle | mymagentositexxxxxx.com:58046 | moodle | Sleep | 0 | | NULL | 0.000 |
+--------+-------------+-------------------------------+---------+---------+------+------------------------------+---------------------------------------------------------------------- --------------------------------+----------+
29 rows in set (0.00 sec)
#update2
'Show create table output' of table's in waiting state is as below
MariaDB [catalog]> SHOW CREATE TABLE catalogsearch_fulltext;
+------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| catalogsearch_fulltext | CREATE TABLE `catalogsearch_fulltext` (
`product_id` int(10) unsigned NOT NULL COMMENT 'Product ID',
`store_id` smallint(5) unsigned NOT NULL COMMENT 'Store ID',
`data_index` longtext DEFAULT NULL COMMENT 'Data index',
`fulltext_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Entity ID',
PRIMARY KEY (`fulltext_id`),
UNIQUE KEY `UNQ_CATALOGSEARCH_FULLTEXT_PRODUCT_ID_STORE_ID` (`product_id`,`store_id`),
FULLTEXT KEY `FTI_CATALOGSEARCH_FULLTEXT_DATA_INDEX` (`data_index`)
) ENGINE=MyISAM AUTO_INCREMENT=912741 DEFAULT CHARSET=utf8 COMMENT='Catalog search result table' |
+------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [catalog]> SHOW CREATE TABLE catalogsearch_result;
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| catalogsearch_result | CREATE TABLE `catalogsearch_result` (
`query_id` int(10) unsigned NOT NULL COMMENT 'Query ID',
`product_id` int(10) unsigned NOT NULL COMMENT 'Product ID',
`relevance` decimal(20,4) NOT NULL DEFAULT 0.0000 COMMENT 'Relevance',
PRIMARY KEY (`query_id`,`product_id`),
KEY `IDX_CATALOGSEARCH_RESULT_QUERY_ID` (`query_id`),
KEY `IDX_CATALOGSEARCH_RESULT_PRODUCT_ID` (`product_id`),
CONSTRAINT `FK_CATALOGSEARCH_RESULT_QUERY_ID_CATALOGSEARCH_QUERY_QUERY_ID` FOREIGN KEY (`query_id`) REFERENCES `catalogsearch_query` (`query_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_CATSRCH_RESULT_PRD_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY (`product_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Catalog search result table' |
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
The process with the most time (#109997) is touching that MyISAM table? After doing SHOW FULL PROCESSLIST, I see
INSERT
INTO `catalogsearch_result`
SELECT 11474 AS `query_id`, `s`.`product_id`, 0 AS `relevance`
FROM `catalogsearch_fulltext` AS `s`
INNER JOIN `catalog_product_entity` AS `e` ON e.entity_id = s.product_id
WHERE (s.store_id = 1)
AND ((`s`.`data_index` LIKE '%Informaatika%'
OR `s`.`data_index` LIKE '%8.c%')
) ON DUPLICATE KEY
UPDATE `relevance` = VALUES(`relevance`)
That is a slow SELECT that only recently got started. Since we are talking about MyISAM, and there writes going on, the queries will happen one at a time (mostly).
It is slow because of both the OR and the leading wildcard in the LIKE test. Do you have any way to avoid both of those issues? With them there, the SELECT must scan the entire table, which apparently takes minutes. (How big is the table?)
Changing timeouts may get you past one instance, but then simply keep you in a never-ending sequence of queries waiting for other queries to finish.
The best solution is to get away from MyISAM. More tips on that: http://mysql.rjweb.org/doc.php/myisam2innodb
(Even after converting, there could be other issues.)

multi-column index and ORDER BY

I have a mysql database with 7 million rows hosted on an aws rds micro instance.
I am doing this sort of queries:
SELECT
`id`, `address`, `property_type`, `rooms`,
`published`, `size`, `net_rent`, `gross_rent`,
`purchase_price`, `original_id`
FROM (`properties`)
WHERE
`postcode` IN ('1000', '1001', '1002', '1003',
'1004', '1005', '1006', '1007',
'1010', '1011', '1012', '1014',
'1015', '1017', '1018', '1019')
AND `published` < '2013-01-09'
AND `property_type` IN ('Apartment', 'Apartment with terrace',
'Attic', 'Attic flat / penthouse',
'Loft', 'Maisonette', 'Studio')
AND `sale_or_rent` = 'rent'
LIMIT 50
And thus I created an index like that:
| properties | 1 | listing | 1 | postcode | A | 25091 | NULL | NULL | YES | BTREE | | |
| properties | 1 | listing | 2 | published | A | 2333545 | NULL | NULL | YES | BTREE | | |
| properties | 1 | listing | 3 | property_type | A | 3500318 | NULL | NULL | YES | BTREE | | |
| properties | 1 | listing | 4 | sale_or_rent | A | 3500318 | NULL | NULL | YES | BTREE | | |
Everything is fine so far. The problems arrise when I try to add ORDER BY published DESC (30"+ for one query).
Is there anyway to have an efficient ORDER BY published DESC knowing that I also need published to be in a WHERE clause?
I'd try putting 'published' first in the index

MySQL - retrospectively add ID that AUTO_INCREMENTs

I have table with lots of rows, and there is no id column. I'd like to go back and:
add an ID column as PRIMARY KEY with AUTO_INCREMENT
more importantly, retrospectively add an ID for all the existing rows, from oldest to newest (there is an 'updatetime' column).
Any suggestions?
Let's consider the following example:
CREATE TABLE your_table (some_value int, updatetime datetime);
INSERT INTO your_table VALUES (100, '2010-08-11 12:09:00');
INSERT INTO your_table VALUES (300, '2010-08-11 12:08:00');
INSERT INTO your_table VALUES (200, '2010-08-11 12:07:00');
INSERT INTO your_table VALUES (400, '2010-08-11 12:06:00');
INSERT INTO your_table VALUES (600, '2010-08-11 12:05:00');
INSERT INTO your_table VALUES (500, '2010-08-11 12:04:00');
INSERT INTO your_table VALUES (800, '2010-08-11 12:03:00');
First we can add the id column:
ALTER TABLE your_table ADD id int unsigned;
Now the table looks like this:
SELECT * FROM your_table;
+------------+---------------------+------+
| some_value | updatetime | id |
+------------+---------------------+------+
| 100 | 2010-08-11 12:09:00 | NULL |
| 300 | 2010-08-11 12:08:00 | NULL |
| 200 | 2010-08-11 12:07:00 | NULL |
| 400 | 2010-08-11 12:06:00 | NULL |
| 600 | 2010-08-11 12:05:00 | NULL |
| 500 | 2010-08-11 12:04:00 | NULL |
| 800 | 2010-08-11 12:03:00 | NULL |
+------------+---------------------+------+
7 rows in set (0.00 sec)
Then we can UPDATE the id column with the row number when the result set is ordered by the updatetime column:
SET #row_number := 0;
UPDATE your_table
SET your_table.id = (#row_number := #row_number + 1)
ORDER BY your_table.updatetime;
Now the table looks like this:
SELECT * FROM your_table ORDER BY id;
+------------+---------------------+----+
| some_value | updatetime | id |
+------------+---------------------+----+
| 800 | 2010-08-11 12:03:00 | 1 |
| 500 | 2010-08-11 12:04:00 | 2 |
| 600 | 2010-08-11 12:05:00 | 3 |
| 400 | 2010-08-11 12:06:00 | 4 |
| 200 | 2010-08-11 12:07:00 | 5 |
| 300 | 2010-08-11 12:08:00 | 6 |
| 100 | 2010-08-11 12:09:00 | 7 |
+------------+---------------------+----+
Then we can set the id column as the primary key, and make it NOT NULL and AUTO_INCREMENT:
ALTER TABLE your_table
MODIFY id int unsigned NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (id);
This is the new description of the table:
DESCRIBE your_table;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| some_value | int(11) | YES | | NULL | |
| updatetime | datetime | YES | | NULL | |
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
+------------+------------------+------+-----+---------+----------------+
3 rows in set (0.04 sec)
We can now try to INSERT a new row in the table to confirm that AUTO_INCREMENT is working as expected:
INSERT INTO your_table (some_value, updatetime)
VALUES (900, '2010-08-11 12:10:00');
SELECT * FROM your_table ORDER BY id;
+------------+---------------------+----+
| some_value | updatetime | id |
+------------+---------------------+----+
| 800 | 2010-08-11 12:03:00 | 1 |
| 500 | 2010-08-11 12:04:00 | 2 |
| 600 | 2010-08-11 12:05:00 | 3 |
| 400 | 2010-08-11 12:06:00 | 4 |
| 200 | 2010-08-11 12:07:00 | 5 |
| 300 | 2010-08-11 12:08:00 | 6 |
| 100 | 2010-08-11 12:09:00 | 7 |
| 900 | 2010-08-11 12:10:00 | 8 |
+------------+---------------------+----+
8 rows in set (0.00 sec)
I'm not sure if there is an easier way to tackle this, but this approach seems to do the job.