0

我正在从 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
) 

我不确定这一点。

4

1 回答 1

3

您需要一个单独的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

于 2013-04-15T21:19:40.393 回答