主键定义上的 HSQLDB 错误
Posted
技术标签:
【中文标题】主键定义上的 HSQLDB 错误【英文标题】:HSQLDB error on primary key definition 【发布时间】:2014-12-07 02:43:04 【问题描述】:我在本地 HSQLDB 2.3.2 数据库中运行以下 SQL:
CREATE TABLE IF NOT EXISTS countries (
country_id PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) NOT NULL,
country_version INTEGER NOT NULL,
country_name NVARCHAR(100) NOT NULL,
country_label NVARCHAR(100) NOT NULL,
country_description NVARCHAR(500) NOT NULL
country_code NVARCHAR(10) NOT NULL,
CONSTRAINT uc_countries UNIQUE (country_id, country_version, country_label, country_description, country_code)
);
CREATE TABLE IF NOT EXISTS states (
state_id PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) NOT NULL,
state_version INTEGER NOT NULL
state_name NVARCHAR(100) NOT NULL,
state_label NVARCHAR(100) NOT NULL,
state_description NVARCHAR(500) NOT NULL,
country_id INT NOT NULL,
FOREIGN KEY (country_id) REFERENCES countries(country_id),
CONSTRAINT uc_states UNIQUE (state_id, state_version, state_label, state_description, country_id)
);
CREATE TABLE IF NOT EXISTS addresses (
address_id PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) NOT NULL,
address_version INTEGER NOT NULL,
address_line_1 NVARCHAR(500) NOT NULL,
address_line_2 NVARCHAR(500),
address_line_3 NVARCHAR(500),
address_city NVARCHAR(100) NOT NULL,
state_id INTEGER NOT NULL,
address_postal_code INTEGER NOT NULL,
FOREIGN KEY (state_id) REFERENCES states(state_id),
CONSTRAINT uc_addresses UNIQUE (address_line_1, address_city, state_id)
);
我的PRIMARY KEY
s 的目标是让它们自动递增(开始:1,逐步:1)不能为 NULL 的整数。
当我执行这个时,我得到:
Error: unexpected token: PRIMARY: line: 2
SQLState: 42581
ErrorCode: -5581
这里发生了什么?我的表/约束设置还有什么不好的地方吗?
【问题讨论】:
我不知道在HSQL中定义表的细节,但是在其他数据库中你需要一个类型。比如:country_id int not null primary key . . .
.
【参考方案1】:
您需要该列的数据类型,加上 primary key
关键字到末尾 (as documented in the manual)
country_id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) NOT NULL PRIMARY KEY
countries.country_description
和 states.state_version
中的列定义后还缺少逗号
【讨论】:
以上是关于主键定义上的 HSQLDB 错误的主要内容,如果未能解决你的问题,请参考以下文章