mysql数据库设置了一个id字段是自增的,但是插入数据时还是要手动插入id,不插入id就会出错。
我在mysql数据库设置了一个id字段是自增的,但是插入数据时还是要手动插入id,不插入id就会出错。,那设置的id主键自增还有什么意义呢,我是想用不插入id的方法,然后...
我在mysql数据库设置了一个id字段是自增的,但是插入数据时还是要手动插入id,不插入id就会出错。,那设置的id主键自增还有什么意义呢,我是想用不插入id的方法,然后用户每插入一条数据,id就自动增加1,该怎么做?
展开
展开全部
成功的执行例子如下,仅供参考.
mysql> CREATE TABLE test_create_tab2 (
-> id INT AUTO_INCREMENT,
-> val VARCHAR(10),
-> PRIMARY KEY (id)
-> );
Query OK, 0 rows affected (0.09 sec)
mysql> INSERT INTO test_create_tab2(val) VALUES ('NO id');
Query OK, 1 row affected (0.03 sec)
mysql> select last_insert_id() as id;
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
mysql> INSERT INTO test_create_tab2(val) VALUES ('NO id 2');
Query OK, 1 row affected (0.03 sec)
mysql> select last_insert_id() as id;
+----+
| id |
+----+
| 2 |
+----+
1 row in set (0.00 sec)
mysql> select * from test_create_tab2;
+----+---------+
| id | val |
+----+---------+
| 1 | NO id |
| 2 | NO id 2 |
+----+---------+
2 rows in set (0.00 sec)
mysql> CREATE TABLE test_create_tab2 (
-> id INT AUTO_INCREMENT,
-> val VARCHAR(10),
-> PRIMARY KEY (id)
-> );
Query OK, 0 rows affected (0.09 sec)
mysql> INSERT INTO test_create_tab2(val) VALUES ('NO id');
Query OK, 1 row affected (0.03 sec)
mysql> select last_insert_id() as id;
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
mysql> INSERT INTO test_create_tab2(val) VALUES ('NO id 2');
Query OK, 1 row affected (0.03 sec)
mysql> select last_insert_id() as id;
+----+
| id |
+----+
| 2 |
+----+
1 row in set (0.00 sec)
mysql> select * from test_create_tab2;
+----+---------+
| id | val |
+----+---------+
| 1 | NO id |
| 2 | NO id 2 |
+----+---------+
2 rows in set (0.00 sec)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询