database - 表 postgreSQL 中的索引

标签 database postgresql

我正在从 mySQL 转移到 postgreSQL,想知道如何在 postgreSQL 的表中添加索引

CREATE TABLE IF NOT EXISTS  game_history
(
 id SERIAL,
 PRIMARY KEY(id),
 INDEX fk_game_history_user_idx (game_id ASC) ,
 CONSTRAINT fk_game_history_user
 FOREIGN KEY (game_id )
 REFERENCES mydb.game (id)
 ON DELETE NO ACTION
 ON UPDATE NO ACTION
) 

来自

   INDEX fk_game_history_user_idx (game_id ASC) ,
 CONSTRAINT fk_game_history_user
 FOREIGN KEY (game_id )
 REFERENCES mydb.game (id)
 ON DELETE NO ACTION
 ON UPDATE NO ACTION
) 

我不确定。

最佳答案

您需要一个单独的create index 语句并且您需要定义一个game_id 列:

CREATE TABLE IF NOT EXISTS  game_history
(
 id      SERIAL,
 game_id integer not null, -- you need to define the column
                           -- otherwise you can't have a foreign key
 PRIMARY KEY(id),
 CONSTRAINT fk_game_history_user
   FOREIGN KEY (game_id)
   REFERENCES game (id)
   ON DELETE NO ACTION
   ON UPDATE NO ACTION
);

CREATE INDEX fk_game_history_user_idx
  ON game_history (game_id ASC);

关于 create table 语句的更多细节在手册中:http://www.postgresql.org/docs/current/static/sql-createtable.html

以及有关create index 语句的更多详细信息:http://www.postgresql.org/docs/current/static/sql-createindex.html

关于database - 表 postgreSQL 中的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16023745/

相关文章:

node.js - Sequelize 拥有并属于许多

php - 向表中添加额外的列

postgresql - 连接到我的 RoR 的 Heroku Postgres 数据库?

sql - 个人和团体注册的数据库设计

mysql - 在mysql中使用和搜索jsonb数据

android - AsyncTask,Insert,在 Android 中选择错误的执行顺序

mysql - 将 mySql2 迁移到 postgreSQL : query cache type error when mysql2psql

postgresql - 如何从 docker 容器向主机发送 tcp/ip 请求?

postgresql - 如何使用 Luminus 连接到 Heroku 中的 PostgreSQL?

node.js - 如何在 sequelize 中进行多个查询?