简体   繁体   English

自动递增栏

[英]Auto increment column

I want to make an AUTO_INCREMENT column in a database table,here is the syntax i write: 我想在数据库表中创建一个AUTO_INCREMENT列,这是我写的语法:

create table comments
(
     name varchar(20),
     mail varchar(30),
     comment varchar(100),
     com_no int auto_increment
);

and i get the following error: 我得到以下错误:

ERROR 1075 (42000): Incorrect table definition; 错误1075(42000):错误的表格定义; there can be only one auto column and it must be defined as a key 只能有一个自动列,并且必须将其定义为键

then i made it as a primary key: 然后我将其作为主键:

create table comments
(
    name varchar(20),
    mail varchar(30),
    comment varchar(100),
    com_no int primary_key auto_increment
);

and i get the following error: 我得到以下错误:

ERROR 1064 (42000): You have an error in your SQL syntax; 错误1064(42000):您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'primary_key auto_increment,name varchar(20),mail varchar(30),comment varchar(100' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的'primary_key auto_increment,name varchar(20),mail varchar(30),comment varchar(100')附近使用

what is wrong??? 怎么了???

It is PRIMARY KEY without the underscore. 它是没有下划线的PRIMARY KEY

create table comments
(
    name varchar(20),
    mail varchar(30),
    comment varchar(100),
    com_no int primary key auto_increment
);

or 要么

create table comments
(
    name varchar(20),
    mail varchar(30),
    comment varchar(100),
    com_no int auto_increment,
    primary key(`com_no`)
);
 create table comments(
    name varchar(20), 
    mail varchar(30),
    comment varchar(100),
    com_no int auto_increment,
    PRIMARY KEY (com_no)
  );

(as per on-line MySQL manual). (根据在线MySQL手册)。

create table comments
(
    name varchar(20),
    mail varchar(30),
    comment varchar(100),
    com_no int NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (com_no)
);

ref . 参考

使用primary key代替primary_key

The proper syntax goes like this for example: 正确的语法如下所示:

CREATE TABLE `admin` (
    `id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
    `userid` VARCHAR(50) NULL DEFAULT '0',
    `pwd` VARCHAR(50) NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)

In MySQL , there can be only one auto increment column (that is generally known as identity column ) and it should be also defined as a unique key . MySQL中 ,只能有一个自动增量列(通常称为标识列 ),并且还应将其定义为唯一键 For example: 例如:

create table comments
(
  com_no int NOT NULL AUTO_INCREMENT,
  name varchar(20),
  mail varchar(30),
  comment varchar(100),
  PRIMARY KEY (com_no)
);

Please see MySQL auto increment documentation for more details. 请参阅MySQL自动递增文档以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM