同事問道乙個有趣的mysql問題,在乙個有資料的表中,如何修改自增id值從一開始?我的第一反應就是不能無法修改,不能實現。在有表的資料中怎麼能實現自增id值從一開始,邏輯上就行不通。做個試驗:
建自增表
mysql> create table `tb1` (
-> `id` int(11) not null auto_increment,
-> `col1` int(11) default null,
-> primary key (`id`)
-> ) engine=myisam auto_increment=1 default charset=utf8;
query ok, 0 rows affected (0.01 sec)
mysql> insert into tb1(col1) value(5);
query ok, 1 row affected (0.00 sec)
mysql> insert into tb1(col1) value(5);
query ok, 1 row affected (0.00 sec)
mysql> insert into tb1(col1) value(5);
query ok, 1 row affected (0.00 sec)
mysql> insert into tb1(col1) value(5);
query ok, 1 row affected (0.00 sec)
mysql> select * from tb1;
+----+------+
| id | col1 |
+----+------+
| 1 | 5 |
| 2 | 5 |
| 3 | 5 |
| 4 | 5 |
+----+------+
4 rows in set (0.00 sec)
清除自增值為1、2的資料
mysql> delete from tb1 where id in(1,2);
query ok, 2 rows affected (0.00 sec)
mysql> show create table tb1 \g
*************************** 1. row ***************************
table: tb1
create table: create table `tb1` (
`id` int(11) not null auto_increment,
`col1` int(11) default null,
primary key (`id`)
) engine=myisam auto_increment=5 default charset=utf8
1 row in set (0.00 sec)
修改從1開始,我認為這步應該邏輯上應該報錯的,竟然沒報錯!?
mysql> alter table tb1 auto_increment=1 default charset=utf8;
query ok, 2 rows affected (0.00 sec)
records: 2 duplicates: 0 warnings: 0
雖然沒報錯,但是自增值還是從當前最大值開始,如下
mysql> show create table tb1 \g
*************************** 1. row ***************************
table: tb1
create table: create table `tb1` (
`id` int(11) not null auto_increment,
`col1` int(11) default null,
primary key (`id`)
) engine=myisam auto_increment=5 default charset=utf8
1 row in set (0.00 sec)
mysql> select * from tb1;
+----+------+
| id | col1 |
+----+------+
| 3 | 5 |
| 4 | 5 |
+----+------+
2 rows in set (0.00 sec)
mysql> insert into tb1(col1) value(6);
query ok, 1 row affected (0.00 sec)
mysql> insert into tb1(col1) value(6);
query ok, 1 row affected (0.00 sec)
mysql> insert into tb1(col1) value(6);
query ok, 1 row affected (0.00 sec)
mysql> insert into tb1(col1) value(6);
query ok, 1 row affected (0.00 sec)
mysql> select * from tb1;
+----+------+
| id | col1 |
+----+------+
| 3 | 5 |
| 4 | 5 |
| 5 | 6 |
| 6 | 6 |
| 7 | 6 |
| 8 | 6 |
+----+------+
6 rows in set (0.00 sec)
mysql> insert into tb1(id,col1) value(1,6);
query ok, 1 row affected (0.00 sec)
mysql> show create table tb1 \g
*************************** 1. row ***************************
table: tb1
create table: create table `tb1` (
`id` int(11) not null auto_increment,
`col1` int(11) default null,
primary key (`id`)
) engine=myisam auto_increment=9 default charset=utf8
1 row in set (0.00 sec)
mysql> insert into tb1(id,col1) value(11,6);
query ok, 1 row affected (0.00 sec)
mysql> select * from tb1;
+----+------+
| id | col1 |
+----+------+
| 3 | 5 |
| 4 | 5 |
| 5 | 6 |
| 6 | 6 |
| 7 | 6 |
| 8 | 6 |
| 1 | 6 |
| 11 | 6 |
+----+------+
8 rows in set (0.00 sec)
看來mysql有些邏輯還是有點不夠嚴謹,從執行alter table tb1 auto_increment=1這步來看
乙個有趣的問題
今早朋友圈某人以100軟妹幣求助這樣乙個問題 概率論是學的一塌糊塗,但是突然想起類似用蒙特卡洛方法可以模擬出來概率。於是向著這100軟妹幣出發了。但是首先遇到了第乙個問題。陣列b的亂序排列感覺有點棘手。首先的第一反應是 迴圈隨機產生1 100的隨機數,判斷陣列中是否已經有該數,若已存在,則重新生成隨...
乙個有趣的指標問題
是從網上看到的乙個例子 struct s int i int p void main struct s s int p s.i p 0 4 p 1 3 s.p p s.p 1 1 s.p 0 2 問程式會在哪一行死掉 解答 程式執行到最後一行就會報出異常,死掉.具體解答為 首先需要說明的是結構體s,...
乙個有趣的SQL問題。
有朋友近來要我幫忙解決乙個 問題,問題描述 有表,表有3個字段 f1,f2,f3,其中,每個欄位中都可能出現1 9之間的9個數字,現要統計出整個表中1 9各出現的次數。如 f1 f2f311 2123 212在上例中 1 出現了4次,2 出現了4次,3 出現了1次。當然,這個 問題是要求盡可能的用 ...