我在数据库中创建表时有一个简单的问题。这是我的代码:
$query="CREATE TABLE users (
userid int(5) not null AUTO_INCREMENT,
firstname varchar(20),
lastname varchar(20),
username varchar(30),
password varchar(32),
email varchar(50),
age int(2),
PRIMARY KEY (userid)
)";
我希望USERID AUTO INCREMENT
以SPECIFIC NUMBER
开头。
例如,从99001开始......
我该怎么做?
答案 0 :(得分:11)
您有两种方法可以设置AUTO_INCREMENT
字段的起始值。如果您的表已经存在,您可以调用以下ALTER TABLE
命令:
ALTER TABLE users AUTO_INCREMENT = 99001;
否则您也可以直接在CREATE TABLE
命令中使用设置起始值,如下所示:
CREATE TABLE users (
userid int(5) not null AUTO_INCREMENT,
firstname varchar(20),
lastname varchar(20),
username varchar(30),
password varchar(32),
email varchar(50),
age int(2),
PRIMARY KEY (userid)
) AUTO_INCREMENT = 99001;
测试用例:
CREATE TABLE users (
userid int(5) not null AUTO_INCREMENT,
firstname varchar(20),
lastname varchar(20),
username varchar(30),
password varchar(32),
email varchar(50),
age int(2),
PRIMARY KEY (userid)
);
Query OK, 0 rows affected (0.04 sec)
ALTER TABLE users AUTO_INCREMENT = 99001;
Query OK, 0 rows affected (0.05 sec)
INSERT INTO users (firstname, lastname, username, password)
VALUES ('a', 'b', 'c', 'd');
Query OK, 1 row affected (0.00 sec)
SELECT * FROM users;
+--------+-----------+----------+----------+----------+-------+------+
| userid | firstname | lastname | username | password | email | age |
+--------+-----------+----------+----------+----------+-------+------+
| 99001 | a | b | c | d | NULL | NULL |
+--------+-----------+----------+----------+----------+-------+------+
1 row in set (0.00 sec)
答案 1 :(得分:1)
您需要在创建表格时使用AUTO_INCREMENT
关键字:
$query="CREATE TABLE users (
userid int(5) not null AUTO_INCREMENT,
firstname varchar(20),
lastname varchar(20),
username varchar(30),
password varchar(32),
email varchar(50),
age int(2),
PRIMARY KEY (userid)
) AUTO_INCREMENT=99001";
修改强>:
更正语法。