GENERATED ALWAYS AS IDENTITY produces incorrect table schema with UCanAccess

↘锁芯ラ 提交于 2019-12-11 04:05:46

问题


I'm writing a program using JDBC/UCanAccess and I'm finding when I create one of the tables, instead of creating four columns with the DateTime format, it also applies the same format to the next column, displacing the other formats:

CREATE TABLE Person (
    Id INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), 
    Name VARCHAR(40), 
    Surname VARCHAR(40), 
    Card VARCHAR(9), 
    Email VARCHAR(30)
);

CREATE TABLE Place (
    Id INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), 
    Name VARCHAR(40)
);


CREATE TABLE Activity (
    Id INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), 
    Name VARCHAR(40) NOT NULL, 
    Description1 VARCHAR(500), 
    Description2 VARCHAR(500), 
    Id_Person INT, 
    Hour_Start DATETIME, 
    Hour_End DATETIME, 
    Date_Plan_Start DATETIME, 
    Date_Plan_End DATETIME, 
    Cost CURRENCY, 
    Sale CURRENCY, 
    Id_Place INT, 
    CONSTRAINT fk_person_activity FOREIGN KEY (Id_Person) REFERENCES Person (Id), 
    CONSTRAINT fk_place_activity FOREIGN KEY (Id_Place) REFERENCES Place (Id)
);

It seems to do everything as it should up to Date_Plan_End, but the created table has Cost as a DateTime, and Id_Place as Currency. If anyone could tell me why this happens I'd appreciate it.

EDIT: The error stills happens inserting only this:

CREATE TABLE Activity (
    Id INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), 
    Name VARCHAR(40) NOT NULL, 
    Date_Plan_End DATETIME, 
    Cost CURRENCY, 
    Sale CURRENCY, 
    Id_Place INT, 
    PRIMARY KEY (Id)
);

SQL and outcome

The problem seems to be related to the GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1). Its removal stops the displacement from happening, although I don't know why.


回答1:


The problem seems to be related to the GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1). Its removal stops the displacement from happening

That's because ... GENERATED ALWAYS AS IDENTITY ... is HSQLDB DDL syntax, and UCanAccess uses Access SQL DDL syntax. Therefore instead of

CREATE TABLE Activity (
    Id INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), 
    Name VARCHAR(40) NOT NULL, 
    Date_Plan_End DATETIME, 
    Cost CURRENCY, 
    Sale CURRENCY, 
    Id_Place INT, 
    PRIMARY KEY (Id)
);

you should use

CREATE TABLE Activity (
    Id COUNTER PRIMARY KEY, 
    Name VARCHAR(40) NOT NULL, 
    Date_Plan_End DATETIME, 
    Cost CURRENCY, 
    Sale CURRENCY, 
    Id_Place INT
);


来源:https://stackoverflow.com/questions/48865778/generated-always-as-identity-produces-incorrect-table-schema-with-ucanaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!