58
40

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PostgreSQLでMySQLのAUTO_INCREMENTを使う

Last updated at Posted at 2019-04-09

AUTO_INCREMENT属性とSERIAL型

AUTO_INCREMENT属性はMySQL用なので、PostgreSQLでは使用できません。

PostgreSQLでは、SERIAL型を使います。

MySQLの場合
CREATE TABLE tasks (
    id INT(10) NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    created_at TIMESTAMP,
    updated_at TIMESTAMP,
    PRIMARY KEY (id)
);
PostgreSQLの場合
CREATE TABLE tasks (
    id SERIAL NOT NULL,
    name VARCHAR(255) NOT NULL,
    created_at TIMESTAMP,
    updated_at TIMESTAMP,
    PRIMARY KEY (id)
);

メモ

  • MySQLではAUTO_INCREMENT属性を、PostgreSQLではSERIAL型を使う
  • SERIAL型は1から2,147,483,647の値を取り扱う
  • なお、PostgreSQLではint(10)のように、INT型に桁数の指定はできない
  • PostgreSQLのINT型は-2,147,483,648から2,147,483,647の整数を取り扱う

参考

58
40
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
58
40

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?