hsql中的大写

hmmo2u0o  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(313)

有没有什么方法可以为hsql的create表中的元素创建默认的大写值

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

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

wfypjpf4

wfypjpf41#

对于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")
6psbrbz9

6psbrbz92#

可以使用计算/生成列:

-- 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演示

相关问题