mysql序列是一组整数:1,2,3....,由于一张数据表只能有一个字段自增主键,如果你想实现其他字段自动增加,就可以使用mysql序列来实现。

使用auto_increment来定义列

drop table if EXISTS test_autoincrement ;
create table `test_autoincrement`(
`id` int UNSIGNED not null auto_increment,
PRIMARY KEY (id),
`name` varchar(20) not null,
`date` date not null,
`origin` varchar(30)
);
insert into test_autoincrement (name,date,origin) values
('housefly','2001-09-10','kitchen');
-- 使用last_insert_id() 可以查看当前操作的次数
select LAST_INSERT_ID();
select * from test_autoincrement;
insert into test_autoincrement values
(null,"millipede",'2001-09-10','driverway'),
(null,"grasshopper",'2001-09-10','front yard');
-- 即便插入多行,也只显示插入第一行时产生的值
select LAST_INSERT_ID();
select * from test_autoincrement;

重置序列

先删除列,再添加列

drop table if EXISTS test_autoincrement ;
create table `test_autoincrement`(
`id` int UNSIGNED not null auto_increment,
PRIMARY KEY (id),
`name` varchar(20) not null,
`date` date not null,
`origin` varchar(30)
);
insert into test_autoincrement (name,date,origin) values
('housefly','2001-09-10','kitchen'),
("millipede",'2001-09-10','driverway'),
("grasshopper",'2001-09-10','front yard');
alter table test_autoincrement drop id;
select * from test_autoincrement;
alter table test_autoincrement add id int(3) UNSIGNED not null auto_increment FIRST,
add primary key (id);
select * from test_autoincrement;

设置序列的开始值

建表时设置序列值

create table `test_autoincrement`
(`id` int(3) UNSIGNED not null auto_increment,
primary key (id),
`name` varchar(20) not null )
engine=innodb auto_increment=100 charset=utf8;
insert into test_autoincrement values
(null,"tom"),
(null,"jerry");
select * from test_autoincrement;

mysql-12序列使用的更多相关文章

  1. Mysql的序列

    Mysql的序列 Mysql自带的序列:字段设置为int,属性里面选上“自动增长”即可: 在插入数据的时候可以不插入该字段的值,mysql会自动处理:

  2. 11、可扩展MySQL+12、高可用

    11.1.扩展MySQL 静态分片:根据key取hash,然后取模: 动态分片:用一个表来维护key与分片id的关系: 11.2.负载均衡 12. 12.2导致宕机得原因: 35%环境+35%性能+2 ...

  3. Oracle 学习笔记 12 -- 序列、索引、同义词

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/Topyuluo/article/details/24232449 数据库的对象包含:表.视图.序列. ...

  4. Oracle与Mysql操作表序列

    一.Oracle添加表序列 CREATE SEQUENCE name -- 序列名 INCREMENT BY -- 每次加几个 START WITH -- 从几开始计数 MINVALUE --- 最小 ...

  5. MySQL --12 备份的分类

    目录 物理备份(Xtrabackup) 1.全量备份 2.增量备份及恢复 3.差异备份及恢复 4.实战:企业级增量恢复实战 物理备份(Xtrabackup) Xtrabackup安装 #下载epel源 ...

  6. mysql#自定义序列

    原文 mysql主键不用自增数字的时候,可以参考如下方式,我抄来的. -- 创建公共的序列表 DROP TABLE IF EXISTS t_common_sequence; CREATE TABLE ...

  7. mysql> 12 simple but staple commands

    Edit at:  2019-12-28 16:52:42 1.mysql -u+username -p+password  --> connect mysql 2.use databasena ...

  8. 【网站建设】Linux上安装MySQL - 12条命令搞定MySql

    从零开始安装mysql数据库 : 按照该顺序执行 :  a. 查看是否安装有mysql:yum list installed mysql*, 如果有先卸载掉, 然后在进行安装; b. 安装mysql客 ...

  9. oracle mysql的序列的新增、删除、修改及使用

    序列的使用  参考文献: https://blog.csdn.net/meijory/article/details/51891529 1.序列介绍 序列: 是 oracle 提供的用于产生一系列唯一 ...

  10. mysql 12小时和24小时转换

    1.12小时显示 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String ...

随机推荐

  1. python第三方库PIL安装的各种坑

    PIL python的图像处理库,pycharm安装屡次失败,始终报错 果断换依旧报错,查遍所有博客, 有给pip加参数的,pip install PIL --allow-external PIL - ...

  2. 2016"百度之星" - 资格赛(Astar Round1) A 逆元

    Problem A  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Others) Problem ...

  3. 用 SQL 对关系型数据库进行查询

    前面几节中,我们已经掌握了如何向 SQLite 数据库中写入数据.这一节,我们将学习如何根据需求对数据库进行查询,进而从中获取数据.接下来的例子中会使用 data/datasets.sqlite(之前 ...

  4. 使用 Redis

    Redis(https://redis.io/),既不像 SQLite 以表的形式存储数据,也不像 MongoDB 允许以嵌套结构存储和查询,它是一种内存数据库结构,即将数据缓存在内存中.它将键—值( ...

  5. 字符编码_Windows资料

    1. 1.1.MSDN 函数WideCharToMultiByte(...) https://msdn.microsoft.com/en-us/library/windows/desktop/dd37 ...

  6. python学习笔记(日志系统实现)

    博主今天在自己的接口自动化框架中添加了日志系统 基于python自带的logging库.包括日志主函数.生成日志文件: # -*- coding: utf-8 -*- # 日志系统 # 时间:2017 ...

  7. 何时使用MQ ?

    何时使用MQmq作为一种基础中间件在互联网项目中有着大量的使用. 一种技术的产生自然是为了解决某种需求,通常来说是以下场景: 需要跨进程通信:B系统需要A系统的输出作为输入参数.当A系统的输出能力远远 ...

  8. tcpdump 实现原理【整理】

    参考:http://blog.sina.com.cn/s/blog_523491650101au7f.html 一.tcpdump 对于本机中进程的系统行为调用跟踪,strace是一个很好的工具,而在 ...

  9. Alpha阶段第1周/共2周 Scrum立会报告+燃尽图 03

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2248 一.小组介绍 组长:刘莹莹 组员:朱珅莹 孙韦男 祝玮琦 王玉潘 ...

  10. specialized English for automation-Lesson 3 CMOS Logic Circuit

    CMOS logic is a newer technology, based on the use of complementary MOS transistors toperform logic ...