是否可以在postgreSQL中将表A ID索引为表B userId

Is it possible to index table A ID to table B userId in postgreSQL?

本文关键字:ID 索引 userId postgreSQL 是否      更新时间:2023-09-26

是否可以在postgreSQL中将表A ID索引到表B userId?所以我会通过id或类似的东西加入两个表。

为了更好地解释我的问题,这里有一个mongoDB和mongoose的例子:

const Billing = new Schema({
  account:Number,
  user: {
    type: Schema.Types.ObjectId,
    ref: 'user',
    required: true
  }
});
// later in the code i can do something like 
Billing.findOne().populate('user');

这将在向用户计费之间创建虚拟关系。

能用postgreSQL 做这样的事情吗

我正在使用sequelize ORM。

听起来像是在谈论外键http://www.postgresql.org/docs/8.3/static/tutorial-fk.html

在你的情况下,它应该是这样的:

create table B (
  userid varchar(80) primary key,
  <other fields>
);
create table A (
  id varchar(80) references B(userid),
  <other fields>
);

类型可以不同,因为您需要