HSQL中的大写

时间:2018-06-12 18:59:45

标签: mysql scala hsqldb

我们有什么方法可以为HSQL的create table中的元素创建UPPER大小写的默认值

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

现在我需要每次都将jobName存储为大写。

2 个答案:

答案 0 :(得分:1)

您可以使用计算/生成列:

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

答案 1 :(得分:1)

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