当前位置: 动力学知识库 > 问答 > 编程问答 >

database - Index inside a table postgreSQL

问题描述:

I am transferring from mySQL to postgreSQL and wondering how can I add index inside the table of 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

)

FROM

 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

)

I am not sure on this.

网友答案:

You need a separate create index statement and you need to define a game_id column:

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);

More details about the create table statement are in the manual: http://www.postgresql.org/docs/current/static/sql-createtable.html

And more details about the create index statement: http://www.postgresql.org/docs/current/static/sql-createindex.html

分享给朋友:
您可能感兴趣的文章:
随机阅读: