首页 小组 文章 相册 留言本 用户 搜索 我的社区 在线学堂 商城 购物车 支付钱包

[打卡]自增长列!!!

2024-05-28 14:49:15
0
178

#自增长列 AUTO_INCREMENT

CREATE DATABASE dbtest;

use dbtest;

SELECT DATABASE();

#在 CREATE TABLE时添加

CREATE table test1(

id int PRIMARY key auto_increment,

last_name VARCHAR(15)


);

desc test1;

INSERT into test1
(last_name)
VALUES('Sam');

select * from test1;

INSERT into test1
(id,last_name)
VALUES(0,'Tom');

INSERT into test1
(id,last_name)
VALUES(null,'Lili');

#结论:当我们向主键(含有增长列)的字段上增加0或null,实际上会自动网上添加制定的字段的数值

INSERT into test1
(id,last_name)
VALUES(30,'Myy');

INSERT into test1
(id,last_name)
VALUES(-30,'Myy');

select * from test1;

#结论:开发中一旦主键作用的字段上声明有(增长列)
#则我们再添加数据时,就不要给主键对应的字段去赋值

#在AUTO TABLE 时添加(很少)

CREATE table test4(

id int PRIMARY key,

last_name VARCHAR(10)
);

desc test4;

ALTER table test4
modify id int auto_increment;

#在AUTO TABLE 时删除

ALTER table test4
modify id int ;

desc test4;

评论
意见反馈