如何通过liquibase更改自动增量列的起始值?

问题描述 投票:0回答:3

我的数据库使用 MySql。我已经找到了如何在创建表时设置列的起始自动增量值,但我需要知道如何为现有列设置新的起始值。执行此操作的 liquibase 脚本是什么样的?

mysql liquibase
3个回答
1
投票

MySQL 语法非常简单:

ALTER TABLE mytable AUTO_INCREMENT = val ;

(注意,这实际上是一个表属性,而不是列属性。表中只能有一个列声明为 AUTO_INCRMENT。)

SQL Server 或 Oracle 不支持此语法;除了 SEQUENCE 对象和 TRIGGER 之外,Oracle 甚至没有“auto_increment”列的概念。 SQL Server 将其称为 IDENTITY 属性。所以我不知道这个语句如何用“liquibase”语法表示,除了指定这个语句是本机 MySQL 语法。


0
投票

您可以使用

addAutoIncrement
(http://www.liquibase.org/documentation/changes/add_auto_increment.html) 更改现有的 AUTO_INCRMENT 列。

不要忘记在

columnDataType
中指定
addAutoIncrement

我昨天在我们的项目中使用了它并且它有效(对于 MySQL)。


0
投票

在 Liquibase 4+ alter table 可以添加到变更集中,下面是changelog.xml

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
    <changeSet author="shriksha (generated)" id="1692968391660-1">
        <createTable catalogName="appledb" tableName="apple">
            <column autoIncrement="true" name="apple_id" type="BIGINT">
                <constraints nullable="false" primaryKey="true" />
            </column>
            <column name="apple_name" type="VARCHAR(255)" />
            <column defaultValueComputed="CURRENT_TIMESTAMP" name="current_date_time" type="datetime" />
        </createTable>
    </changeSet>
    <changeSet author="shriksha (generated)" id="1692968322140-2">
        <addAutoIncrement columnDataType="int" schemaName="appleDb"  columnName="apple_id" startWith="10" tableName="apple" />
    </changeSet>
    <changeSet author="shriksha (generated)" id="1692968322140-3">
        <insert catalogName="appledb" tableName="apple">
            <column name="apple_name" value="Macintosh" />
            <column name="current_date_time" valueDate="2023-08-25T18:27:47" />
        </insert>
    </changeSet>
</databaseChangeLog>

注意:注意msql不支持incrementBy

找到解决方案这里

© www.soinside.com 2019 - 2024. All rights reserved.