如何设置mysql 自动插入id

2025-03-04 11:33:13
推荐回答(5个)
回答1:

设置mysql 自动插入id,需要设置id为整形自动增长的主键。下面介绍设置方法,

通过一个例子说明:

创建一张表student

create table student(

      id int(5) not null auto_increatment,

     name varchar(20) not null,

    age int(3) not null,

     primary key(id));

在插入数据可以不用插入id,可以这样:

insert into student(name,age)  values('xiaobai',12);第一条插入数据id默认是1

insert into student (name,age) values('xiaoming',11);第二条插入数据id默认是2,第三条,第四条,······,依次自动增加。

由以上例子可知表插入数据的时候,无需插入id,即可自动插入。

回答2:

通过mysql助手,在创建设计表的时候,创建主键的时候,字段设置有个选项,自动增加,勾选上就好了.

回答3:

假设你的数据表字段为:
id name sex age
这样写sql语句就可以了:
insert into 数据表名(name,sex,age) values('val1',val2'','val3');
id 字段省去就可以了。

回答4:

create table cdat
(
localt char(20) not null,
cd char(5) not null,
snosat char(2) not null,
rnorec char(3) not null,
id INT(20) not null AUTO_INCREMENT,
primary key (id)
);
注:只有int类型且为primary key 才可以使用auto_increment.

回答5:

建表的时候设置自动递增就行了呀