HSQL 中的大写

Upper case in HSQL

有什么方法可以在 create table for HSQL

中为元素创建大写默认值
CREATE TABLE IF NOT EXISTS  "job" (
   "id" INTEGER IDENTITY,
   "jobId" BIGINT ,
   "jobName" VARCHAR(100)  ,
   PRIMARY KEY ("jobName")
);

现在我的需求是每次都以大写形式存储jobName。

您可以使用 computed/generated 列:

-- MySQL
CREATE TABLE job (
   id INT AUTO_INCREMENT,
   jobid INT ,
   jobName VARCHAR(100)  ,
   jobNameU VARCHAR(100) GENERATED ALWAYS AS (UPPER(jobName)) STORED,
   PRIMARY KEY (jobNameU),
   KEY(id)
);

INSERT INTO job(id, jobid, jobName)
VALUES (1,2,'aaa');

INSERT INTO job( jobid, jobName)
VALUES (2,'aaA')
--  Duplicate entry 'AAA' for key 'PRIMARY'

DBFiddle Demo

对于 HSQLDB,生成列的解决方案(类似于 MySQL 解决方案)是:

CREATE TABLE IF NOT EXISTS  "job" (
 "id" INTEGER IDENTITY,
 "jobId" BIGINT ,
 "jobName" VARCHAR(100)  ,
 "jobNameU" VARCHAR(100) GENERATED ALWAYS AS (UPPER("jobName")),   
 PRIMARY KEY ("jobNameU")
);

有一个更好的解决方案,使用 BEFORE INSERT 触发器,无需额外生成的列即可工作:

CREATE TABLE IF NOT EXISTS  "job" (
   "id" INTEGER IDENTITY,
   "jobId" BIGINT ,
   "jobName" VARCHAR(100)  ,
   PRIMARY KEY ("jobName")
);

CREATE TRIGGER makeUpper BEFORE INSERT ON "job" REFERENCING NEW ROW AS NEW
  FOR EACH ROW
  SET NEW."jobName" = UPPER(NEW."jobName")