1. 建表
<changeSet id="createTable t_book" author="xiegb">
<!-- 判断表是否存在 -->
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="t_book"/>
</not>
</preConditions>
<!-- comment 不能放在 preConditions 之前,会报错 -->
<comment>创建新表</comment>
<!-- createTable 是用于创建新表, createIndex 用于创建索引 -->
<createTable tableName="t_book" remarks="书">
<column name="id" type="char(32)" remarks="主键">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(200)" remarks="书名"/>
<column name="page" type="integer" remarks="书页" defaultValueNumeric="0"/>
<column name="publish_time" type="timestamp without time zone" remarks="发布时间" defaultValueComputed="now()"/>
<column name="remark" type="text" remarks="备注"/>
<column name="is_edit" type="boolean" remarks="true-可编辑,false-不可编辑" defaultValueBoolean="true"/>
<column name="id_del" type="varchar(3)" remarks="删除标记(1-已删除,0-未删除)" defaultValue="0"/>
</createTable>
<createIndex tableName="t_book" indexName="idx_book_name">
<column name="name"/>
</createIndex>
</changeSet>
2. 修改字段类型
<changeSet id="modifyDataType t_user sex" author="xiegb">
<preConditions onFail="MARK_RAN">
<columnExists tableName="t_user" columnName="sex"/>
</preConditions>
<modifyDataType tableName="t_user" columnName="sex" newDataType="varchar(100)" />
</changeSet>
3. 修改字段注释
<changeSet id="setColumnRemarks t_user isDelete" author="xiegb">
<preConditions onFail="MARK_RAN">
<columnExists tableName="t_user" columnName="isDelete"/>
</preConditions>
<setColumnRemarks tableName="t_user" columnName="isDelete" remarks="是否删除" ></setColumnRemarks>
</changeSet>
4. 删除字段
<changeSet id="dropColumn t_user address" author="xiegb">
<dropColumn tableName="t_user" columnName="address" ></dropColumn>
</changeSet>
5. 添加字段
<changeSet id="addColumn t_user age" author="xiegb">
<!-- 判断字段是否存在 -->
<preConditions onFail="MARK_RAN">
<not>
<columnExists tableName="t_user" columnName="age"/>
</not>
</preConditions>
<comment>增加字段age</comment>
<addColumn tableName="t_user">
<column name="age" type="int(2)" remarks="年龄">
<!-- 不为空,如果原数据表有数据,增加字段时设置不能为空会报错 -->
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>
<changeSet id="addColumn t_user city" author="xiegb">
<!-- 判断字段是否存在 -->
<preConditions onFail="MARK_RAN">
<not>
<columnExists tableName="t_user" columnName="city"/>
</not>
</preConditions>
<comment>增加字段city</comment>
<addColumn tableName="t_user">
<column name="city" type="varchar(20)" remarks="城市" defaultValue="西安"/>
</addColumn>
</changeSet>
6. 修改字段名称
<changeSet id="renameColumn t_user user_name" author="xiegb">
<preConditions onFail="MARK_RAN">
<columnExists tableName="t_user" columnName="user_name"/>
</preConditions>
<renameColumn tableName="t_user" oldColumnName="user_name" newColumnName="name"/>
</changeSet>
7. 创建唯一约束
<changeSet id="addUniqueConstraint t_user authentication_id" author="xiegb">
<addUniqueConstraint tableName="t_user" columnNames="authentication_id" />
</changeSet>
8. 创建复合索引
<changeSet id="createIndex t_user u_index_name_age" author="xiegb">
<createIndex tableName="t_user" indexName="u_index_name_age" unique="true">
<column name="name"/>
<column name="age"/>
</createIndex>
</changeSet>
9. 创建全局序列号
<changeSet id="createSequence user_sequence" author="xiegb">
<createSequence sequenceName="user_sequence" startValue="1" />
</changeSet>
10. 插入数据
<changeSet id="insert t_user" author="xiegb">
<!-- 判断数据是否存在 -->
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">
select count(id) from t_user where id = '3'
</sqlCheck>
</preConditions>
<comment>增加数据</comment>
<insert tableName="t_user">
<column name="id" value="3" />
<column name="name" value="王五" />
<column name="age" value="12" />
</insert>
</changeSet>
11. sql语句
<!-- runOnChange:每个 changeSet 不能修改,只有引用文件时可以修改文件内容,当 sql 文件发生变化时,执行这些变化 -->
<changeSet id="sqlFile initUser.sql" author="xiegb" runOnChange="true">
<comment>初始化用户数据</comment>
<sql>update t_user set active = 1;<sql>
<sqlFile path="classpath:liquibase/sql/initUser.sql"/>
</changeSet>
sql文件
DELETE FROM t_user WHERE id != 0;
INSERT INTO t_user ("id", "name") VALUES (1, 'xiegb');
网友评论