亲爱的MySQL专业人士:我想知道我做错了什么。我的代码就像:
use testdb;
drop table testtable;
create table testtable (
ID int NOT NULL,
lastn VARCHAR(20) NOT NULL,
firstn varchar(20));
Select * from testtable;
alter table testtable auto_increment = 7001;
insert into testtable (lastn,firstn) values('kim','jeff');
Select * from testtable;
insert into testtable (lastn,firstn) values('Lee','jim');
Select * from testtable;
表生成如下:(“alter”语句无效)
# ID, lastn, firstn '0', 'kim', 'jeff' '0', 'Lee', 'jim'
答案 0 :(得分:0)
ID
必须是AUTO_INCREMENT和PRIMARY KEY。 (这些“足够”但不完全“必要”。)
答案 1 :(得分:0)
更改CREATE TABLE命令以将ID字段设置为自动递增并将其初始化为:
create table testtable (
ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
lastn VARCHAR(20) NOT NULL,
firstn varchar(20))
AUTO_INCREMENT = 7001;
之后或改变表格:
create table testtable (
ID int NOT NULL,
lastn VARCHAR(20) NOT NULL,
firstn varchar(20));
ALTER TABLE testtable MODIFY COLUMN ID INT PRIMARY KEY AUTO_INCREMENT;
ALTER TABLE testtable AUTO_INCREMENT = 7001;