JSqlParser生成修改表定义SQL语句

依赖

xml 复制代码
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>4.3</version>
        </dependency>

删除表

java 复制代码
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.drop.Drop;

public class DropTableDemo {
    public static void main(String[] args) {
        Drop drop = new Drop();
        drop.setIfExists(true);
        drop.setType("TABLE");
        Table table = new Table("t_one");
        drop.setName(table);
        System.out.println(drop);
    }
}

语句:

sql 复制代码
DROP TABLE IF EXISTS t_one

修改字段定义

java 复制代码
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterExpression;
import net.sf.jsqlparser.statement.alter.AlterOperation;
import net.sf.jsqlparser.statement.create.table.ColDataType;

import java.util.Collections;

public class AlterTableDemo {
    public static void main(String[] args)throws Exception {
        Alter alter = new Alter();
        alter.setTable(new Table("t_one"));
        // 修改姓名
        ColDataType varchar = new ColDataType("VARCHAR");
        varchar.setArgumentsStringList(Collections.singletonList("128"));
        AlterExpression.ColumnDataType name = new AlterExpression.ColumnDataType("name", false, varchar, Collections.singletonList("COMMENT '道号'"));
        AlterExpression alterExpression = new AlterExpression();
        alterExpression.setOperation(AlterOperation.MODIFY);
        alterExpression.addColDataType(name);
        alter.addAlterExpression(alterExpression);
        // 修改仙龄
        ColDataType anInt = new ColDataType("INT");
        anInt.setArgumentsStringList(Collections.singletonList("10"));
        AlterExpression.ColumnDataType age = new AlterExpression.ColumnDataType("age", false, anInt, Collections.singletonList("COMMENT '仙龄'"));
        AlterExpression ageAlterexpression = new AlterExpression();
        ageAlterexpression.setOperation(AlterOperation.MODIFY);
        ageAlterexpression.addColDataType(age);
        alter.addAlterExpression(ageAlterexpression);
        System.out.println(alter);
    }
}

语句:

sql 复制代码
ALTER TABLE t_one MODIFY name VARCHAR (128) COMMENT '道号', MODIFY age INT (10) COMMENT '仙龄'

新增字段

java 复制代码
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterExpression;
import net.sf.jsqlparser.statement.alter.AlterOperation;
import net.sf.jsqlparser.statement.create.table.ColDataType;

import java.util.Collections;

public class AddColumnDemo {
    public static void main(String[] args) {
        Alter alter = new Alter();
        alter.setTable(new Table("t_one"));
        AlterExpression alterExpression = new AlterExpression();
        alterExpression.setOperation(AlterOperation.ADD);
        ColDataType bigint = new ColDataType("BIGINT");
        bigint.setArgumentsStringList(Collections.singletonList("20"));
        AlterExpression.ColumnDataType fightingCapacity = new AlterExpression.ColumnDataType("fighting_capacity", false, bigint, Collections.singletonList("COMMENT '战力'"));
        alterExpression.addColDataType(fightingCapacity);
        alter.addAlterExpression(alterExpression);
        System.out.println(alter);
    }
}

语句:

sql 复制代码
ALTER TABLE t_one ADD fighting_capacity BIGINT (20) COMMENT '战力'

删除字段

java 复制代码
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.alter.AlterExpression;
import net.sf.jsqlparser.statement.alter.AlterOperation;


public class DropColumnDemo {
    public static void main(String[] args)throws Exception {
        Alter alter = new Alter();
        alter.setTable(new Table("t_one"));
        AlterExpression alterExpression = new AlterExpression();
        alterExpression.setOperation(AlterOperation.DROP);
        alterExpression.setColumnName("fighting_capacity");
        alter.addAlterExpression(alterExpression);
        System.out.println(alter);
    }
}

语句:

sql 复制代码
ALTER TABLE t_one DROP fighting_capacity
相关推荐
新手小袁_J15 分钟前
JDK11下载安装和配置超详细过程
java·spring cloud·jdk·maven·mybatis·jdk11
呆呆小雅16 分钟前
C#关键字volatile
java·redis·c#
Monly2116 分钟前
Java(若依):修改Tomcat的版本
java·开发语言·tomcat
阳冬园18 分钟前
mysql数据库 主从同步
数据库·主从同步
Ttang2319 分钟前
Tomcat原理(6)——tomcat完整实现
java·tomcat
钱多多_qdd29 分钟前
spring cache源码解析(四)——从@EnableCaching开始来阅读源码
java·spring boot·spring
XiaoH23329 分钟前
培训机构Day15
sql·mysql
waicsdn_haha31 分钟前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
Q_192849990642 分钟前
基于Spring Boot的摄影器材租赁回收系统
java·spring boot·后端
Code_流苏44 分钟前
VSCode搭建Java开发环境 2024保姆级安装教程(Java环境搭建+VSCode安装+运行测试+背景图设置)
java·ide·vscode·搭建·java开发环境