jOOQ - DDL 語句



什麼是 DDL 語句?

DDL 代表資料定義語言。雖然 DML 語句用於查詢給定資料庫表中的資料,但 DDL 語句用於定義、修改或刪除資料庫物件,例如表、檢視、索引和模式。最常見的 DDL 操作是

  • CREATE:它建立一個新的表、檢視或索引。

  • ALTER:此操作修改現有表。

  • DROP:它用於刪除資料庫物件,例如表、檢視或索引。

  • TRUNCATE:刪除表中的所有行。

jOOQ 中的 DDL 語句

在 jOOQ 中,使用 DSL 類的以下方法建立 DDL 語句:

序號 方法和描述
1. createTable(String name)

它開始建立具有指定名稱的表。

2. column(String name, DataType<?> type)

它用於在表中定義具有指定名稱和資料型別的列。

3. alterTable(String tableName)

開始修改現有表的過程。

4. addColumn(String columnName, DataType<?> type)

此方法向現有表新增新列。

5. dropColumn(String columnName)

此方法用於從現有表中刪除列。

6. renameColumn(String oldName, String newName)

重新命名現有列。

7. dropTable(String name)

從資料庫中刪除現有表。

8. truncateTable(String name)

刪除表中的所有行,而不會刪除表本身。

jOOQ 中 DDL 語句示例

在本例中,我們將執行ALTER TABLE操作以將“ID”重新命名為“Rank”。我們將使用以下表進行此操作:

ID 姓名 職位
1 Aman 技術作家
2 Shriansh 軟體工程師
4 Vivek 開發人員

對於 ALTER TABLE 操作,在src -> main -> java -> com.example.demo內建立一個包。com.example.demo資料夾的名稱將取決於專案的名稱。在此包內建立一個 Java 類。

我們將包命名為service,Java 類命名為EmployeeService.java。您可以根據自己的選擇命名。

在 jOOQ 中,我們使用alterTable()方法修改表結構,並使用renameColumn()方法重新命名列。將以下程式碼片段複製並貼上到EmployeeService.java檔案中。

package com.example.demo.service;

import org.jooq.*;
import org.jooq.Record;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;

@Service
public class EmployeeService {

   private final DataSource dataSource;

   @Autowired
   public EmployeeService(DataSource dataSource) {
      this.dataSource = dataSource;
   }

   public void run() {
      // Create a DSLContext using the DataSource
      DSLContext create = DSL.using(dataSource, SQLDialect.MYSQL);

      // Rename the column 'id' to 'rank'
      create.alterTable("employee")
         .renameColumn("id").to("rank")
         .execute();

      // Manually define the table and columns after renaming
      Table<?> employee = DSL.table("employee");
      Field<Integer> rank = DSL.field("rank", Integer.class);  
      Field<String> name = DSL.field("name", String.class);
      Field<String> jobTitle = DSL.field("job_title", String.class);

      // Fetch the values from the employee table
      Result<Record3<Integer, String, String>> result = create.select(rank, name, jobTitle)
         .from(employee)
         .fetch();

      // Print the results
      for (Record record : result) {
         Integer employeeRank = record.get(rank);  
         String employeeName = record.get(name);
         String employeeJobTitle = record.get(jobTitle);

         System.out.println("Rank: " + employeeRank + ", Name: " + employeeName + ", Job Title: " + employeeJobTitle);
      }
   }
}

現在,導航到com.example.demo資料夾內的DemoApplication.java檔案。並編寫以下程式碼:

package com.example.demo;

import com.example.demo.service.EmployeeService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

   private final EmployeeService demoApplication;
      
   public DemoApplication(EmployeeService demoApplication) {
      this.demoApplication = demoApplication;
   }
      
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
      
   @Override
   public void run(String... args) throws Exception {
      demoApplication.run();
   }
}

執行此程式碼時,它會將“id”列更改為“Rank”,如下所示:

Rank: 1, Name: Aman, Job Title: Technical Writer
Rank: 2, Name: Shriansh, Job Title: Software Engineer
Rank: 4, Name: Vivek, Job Title: Developer
廣告

© . All rights reserved.