mysql序列怎么加长 mysql增加一列数据

mysql 序列功能mysql 里没有这个东西!
好像mysql序列怎么加长你在建表mysql序列怎么加长的时候可以这么写!
create table test(id int not null primary key auto_increment);
主要是auto_increment
mybatis如何实现序列自增长一、首先我们看对于同一张student表,对于mysql,sql server,oracle中它们都是怎样创建主键的
1、在mysql中
create table Student(
Student_IDint(6) NOT NULL PRIMARY KEY AUTO_INCREMENT,
Student_Name varchar(10) NOT NULL,
Student_Age int(2) NOT NULL
);
insert into student(student_name,student_age) values('zhangsan',20);
2、在sql server中
create table Student(
Student_IDint primary key identity(1,1),
Student_Name varchar2(10) NOT NULL,
Student_Age number(2) NOT NULL
);
insert into student(student_name,student_age) values('zhangsan',20);
3、在oracle中
create table Student(
Student_IDnumber(6) NOT NULL PRIMARY KEY,
Student_Name varchar2(10) NOT NULL,
Student_Age number(2) NOT NULL
);
而oracle如果想设置主键自增长,则需要创建序列
CREATE SEQUENCE student_sequence
INCREMENT BY 1
NOMAXVALUE
NOCYCLE
CACHE 10;
insert into Student values(student_sequence.nextval,'aa',20);
如果使用了触发器的话 , 就更简单了
create or replace trigger student_trigger
before insert on student
for each row
begin
select student_sequence.nextval into :new.student_id from dual;
end student_trigger;
此时插入的时候触发器会帮你插入id
insert into student(student_name,student_age) values('wangwu',20);
至此,mysql,sql server,oracle中怎样创建表中的自增长主键都已完成 。
看一看出oracle的主键自增较mysql和sql sever要复杂些,mysql,sqlserver配置好主键之后 , 插入时,字段和值一一对应即可 , 数据库就会完成你想做的 , 但是在oracle由于多了序列的概念,如果不使用触发器,oracle怎样实现主键自增呢?
insert id="add" parameterType="Student"
selectKey keyProperty="student_id" resultType="int" order="BEFORE"
select student_sequence.nextval from dual
/selectKey
insert into student(student_id,student_name,student_age) values(#{student_id},#{student_name},#{student_age})
/insert
或者
insert id="save" parameterType="com.threeti.to.ZoneTO"
selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER"
SELECT SEQ_ZONE.CURRVAL AS id from dual
/selectKey
insert into TBL_ZONE (ID, NAME ) values (SEQ_ZONE.NEXTVAL, #{name,jdbcType=VARCHAR})
/insert
二、MyBatis 插入时候获取自增主键方法有二
以MySQL5.5为例:
方法1:
insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id"
insert into person(name,pswd) values(#{name},#{pswd})
/insert
方法2:
insert id="insert" parameterType="Person"
selectKey keyProperty="id" resultType="long"
select LAST_INSERT_ID()
/selectKey
insert into person(name,pswd) values(#{name},#{pswd})
/insert
插入前实体id属性为0;
插入后实体id属性为保存后自增的id;
2020-01-16 控制MYSQL的自增长序列的起始值一、修改自增长序列的值
alter table table_name auto_increment=n;
注意:n只能大于已有的auto_increment的整数值,小于的值无效.
show table status like 'table_name' 的返回结果里的auto_increment列就是表的现有值.
二、控制主键的起点
create table 表名
(
......
) engine=INNODB auto_increment=1001 default charset=gbk;
三、自增主键归零
如果曾经的数据都不需要的话,可以直接清空所有数据,并将自增字段恢复从1开始计数

推荐阅读