简体   繁体   English

使用jOOQ创建包含主键的表

[英]Create table with primary key using jOOQ

jOOQ has CREATE TABLE syntax as stated in the documentation : jOOQ具有文档中所述的 CREATE TABLE语法:

create.createTable(AUTHOR)
      .column(AUTHOR.ID, SQLDataType.INTEGER)
      .column(AUTHOR.FIRST_NAME, SQLDataType.VARCHAR.length(50))
      .column(AUTHOR_LAST_NAME, SQLDataType.VARCHAR.length(50))
      .execute();

I'm wondering how to define which column belongs to the primary key? 我想知道如何定义哪个列属于主键? So is there a way in jOOQ to create a CREATE TABLE statement with PRIMARY KEY information? 那么在jOOQ中有没有办法用PRIMARY KEY信息创建一个CREATE TABLE语句?

I'm specifically interested in a solution for SQLite, which doesn't have syntax to add the primary key afterwards, so I think in worst case I have to go to a DB specific solution? 我对SQLite的解决方案特别感兴趣,后来没有语法来添加主键,所以我认为在最坏的情况下我必须转到特定于DB的解决方案?

This feature has been implemented for jOOQ 3.8: #4050 . 此功能已针对jOOQ 3.8实施: #4050

create.createTable(AUTHOR)
      .column(AUTHOR.ID, SQLDataType.INTEGER)
      .column(AUTHOR.FIRST_NAME, SQLDataType.VARCHAR.length(50))
      .column(AUTHOR_LAST_NAME, SQLDataType.VARCHAR.length(50))
      .constraints(
           constraint("PK_AUTHOR").primaryKey(AUTHOR.ID)
      )
      .execute();

Since jOOQ 3.6 ( #3338 ), you can also use the ALTER TABLE statement to add a constraint after creating the table: 从jOOQ 3.6( #3338 )开始,您还可以在创建表后使用ALTER TABLE语句添加约束:

create.alterTable(AUTHOR)
      .add(constraint("PK_AUTHOR").primaryKey(AUTHOR.ID))
      .execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM