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