LoginSignup
1
1

More than 5 years have passed since last update.

初めてのSQL(Postgres)

Last updated at Posted at 2016-12-30
  • 検索
  • データの一部書き換え

の2つだけ、初めてSQLを書きました。
あとトランザクションエラーも発生したときの解決法も。

データを検索(抽出)

「user_table から new_flg が1のものを検索したい」

SELECT * FROM user_table WHERE new_flg = 1

「*」 は「全件」を表す。

検索結果の一部を置換する

「user_table の gender を 'man' から 'woman' にした場合の 結果を表示したい

SELECT REPLACE(gender, 'man', 'woman') FROM user_table;

SELECT FROM 間の表記は、「検索結果として何を表示するか」の指定らしく、DBが書き換えられるわけではない!

データを更新

「user_table の gender を 'man' から 'woman' に書き換えたい」

UPDATE user_table SET gender=REPLACE(gender, 'man', 'woman');

WHERE も追加可能。

UPDATE user_table SET gender=REPLACE(gender, 'man', 'woman') WHERE new_flg = 1;

トランザクションブロック内でエラーが発生

[2016-12-22 15:49:29] [25P02] ERROR: current transaction is aborted, commands ignored until end of transaction block

解決策

ロールバックした。

ROLLBACK
1
1
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
1
1