GBase 8c
运维管理
问答

使用SQL建表带有外键时执行报错

发表于2025-02-14 17:16:2798次浏览7个评论

为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。

【GBase版本】: GBase8cV53.0

【操作系统】: CentOS 7.8

【CPU】:

【问题描述】*: 在执行SQL建立表时使用外键报错

The foreign key is not in the potential distribution columns

相同的SQL语句中在PostgreSQL中是可以正常执行的

评论

登录后才可以发表评论
用户头像
GBase用户137发表于 1年前
可以贴下建表的SQL语句吗
GBase用户26303发表于 1年前
@GBase用户137:先执行这两个SQL语句
1. CREATE TABLE roles(
id serial primary key not null,
description varchar(255) null
);
2. CREATE TABLE users(
id serial primary key not null,
description varchar(255) null
);
然后再执行这个
CREATE TABLE user_roles(
id serial primary key not null,
foreign key (userid) references users (id) on delete cascade,
foreign key (roleid) references roles (id) on delete cascade
);
就会报错,但是在PostgreSQL中执行就没有问题
小忘发表于 1年前
因为gbase是分布式,外键需要包含在分布键中, 在建表的时候,Distribute By: HASH(id_,外键)
GBase用户26303发表于 1年前
@小忘:能用上面的SQL当例子改一下嘛,改成能正常运行的,做个参考,感谢!
小忘发表于 1年前
@GBase用户26303:CREATE TABLE roles (
id serial PRIMARY KEY NOT NULL,
description varchar(255) NULL
);

CREATE TABLE users (
id serial PRIMARY KEY NOT NULL,
description varchar(255) NULL
);

CREATE TABLE user_roles (
id serial PRIMARY KEY NOT NULL,
FOREIGN KEY (id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (id) REFERENCES roles(id) ON DELETE CASCADE
) DISTRIBUTE BY HASH(id) ;


上面的可以创建成功,表user_roles 只有id这个字段
最佳回答
GBase用户26303发表于 1年前
@小忘:那表user_roles还有其他字段怎么办呢?
GBase用户47954发表于 2个月前
问题有些怪