PostgreSQL

ubuntu 20.04 に postgres postgresql を install する

何も考えずに ローカルユーザーネームと root のロールで postgres が動く環境を入れたい場合。 $ sudo apt update $ sudo apt-get install postgresql $ sudo su - postgres @postgres> createuser YOUR_USERNAME @postgres> createdb YOUR_USERNAME @postg…

自己結合はいつ使うか

自テーブル各レコードの特性を 単純な射影や選択では比較できない場合に使う ここでは CASE, 相関サブクエリ, 内部/外部結合などに言及しないとする (Web 記事や書籍を眺めてみたのだが、なかなか扱いがよくわからん構文 勉強しきれてないが、これ使うのであ…

相関サブクエリはいつ(一番)使われるか

(一番)使われるのは「レコードの存在チェック」 Web 記事を色々眺めて、私なりの答え (業務システムの SQL を見ていると「このオペレーションは、いつ、何のために使うのか」 を見失うので一回整理したい) 例 利用者が存在し、各属性を持つ 利用者 ID 年齢(0…

SQL の GROUP BY 句 演算目的と SELECT 句に含められる属性

プログラマのための SQL 第4版 より 自分用のまとめ GROUP BY 演算の目的 複数行を集約キーでグループごとにまとめ各グループの特性を示す行を返すこと 返ってきた行は そのグループの特性を示すものであり(*1) グループに存在する特定行の性質を示すもので…

SQL の CASE 使い方

(PostgreSQL 9.4) background reports (レポート提出管理)テーブルが次の通り定義されている CREATE TABLE reports( reportid INTEGER -- レポート番号 NOT NULL, submitter CHAR(16) -- 提出者名 NOT NULL, acceptflag CHAR(1) -- 承認フラグ '0':無効 '1':…

COALESCE

コウアレス (if objects or ideas coalesce, they combine to form one single group ( quoted from longman )) 引数を走査していき、NULL ではない初めの値を返す 引数が全て NULL の場合は NULL を返す CREATE TABLE staffs( id INTEGER NOT NULL, name CH…

SQL ALL

CREATE TABLE cardpoints ( id INTEGER NOT NULL, pointedlocation CHAR(32) NOT NULL, point INTEGER NOT NULL CONSTRAINT points_point_range CHECK(point BETWEEN 0 AND 15000), PRIMARY KEY(id, pointedlocation) ); INSERT INTO cardpoints (id, pointe…

Read View 作成

CREATE TABLE trial_table ( trial_id INTEGER NOT NULL, name CHAR(20) NOT NULL, age INTEGER NOT NULL CONSTRAINT trial_age_range CHECK(age BETWEEN 0 AND 200), PRIMARY KEY(trial_id) ); INSERT INTO trial_table ( trial_id, name, age ) VALUES ( 1…

interval, timestamp, date, time の違い

interval timestamp, date, time の時間を変動させる時に利用する関数 以下の sql と結果を参照 (RDBMS ごとに文法が違うので注意) timestamp 日付と時刻 SELECT (timestamp'2014-10-17 22:00:00') AS timestamp_col; timestamp_col --------------------- 2…

update or delete on table "foreigns" violates foreign key constraint "to_foreigns_to_foreigns_id_fkey" on table "to_foreigns"

CREATE TABLE foreigns ( foreigns_id INTEGER NOT NULL, PRIMARY KEY(foreigns_id) ); CREATE TABLE to_foreigns ( to_foreigns_id INTEGER NOT NULL REFERENCES foreigns(foreigns_id), PRIMARY KEY(to_foreigns_id) ); INSERT INTO foreigns (foreigns_id…

INTERVAL 型

型の一種(査読中) PostgreSQL で以下の動作確認 SELECT current_date; date ------------ 2015-10-15 (1 row) CREATE TABLE times ( test_date DATE NOT NULL ); INSERT INTO times VALUES ('2015-10-15'); INSERT INTO times VALUES ('2015-10-16'); INSERT…

PostgreSQL と Play Framwork を JDBC で連携接続する

環境 MacOSX 10.10.4 JDK 1.8.0_45 Play Framwork 2.4.2(Damiya) 前提 PlayFramework 初期状態から画面構築 に基づいて app 構築している PostgreSQL 構築 (brew だと、createuser 上手くいかんかったんでこっちで... brew はまた別途...) Postgres.app 公式…

CHECK 制約 と ALTER TABLE tablename ADD CONSTRAINT

ALTER TABLE tablename ADD CONSTRAINT を 新規テーブルへ実行 ALTER TABLE 可能 CREATE TABLE projects ( projectname text NOT NULL, membernum integer NOT NULL ); ALTER TABLE projects ADD CONSTRAINT projects_membernum_check CHECK(membernum BETWE…

CHECK 制約

「アプリケーション層の validation は、不正値入力の『通知』に留め」 「データの整合性 はデータ層が担保しよう」 って Dan Chak さんが言ってた(覚えがあるので要復習) CREATE TABLE users ( name text NOT NULL, receivemailflag INTEGER NOT NULL DEFAU…

外部参照 PostgreSQL

PostgreSQL 外部参照留め書き 参照する側 が 参照される側 に無い要素を insert, update をすることはできない https://www.postgresql.jp/document/8.1/html/tutorial-fk.html

NOT IN ; sql

check whether exists NULL when use NOT IN (related to: return value of IN predicate ; sql - Red > Green > Refactor > Red) create table member ( memberid serial, name text, age integer, home text, PRIMARY KEY(memberid) ); SELECT * FROM memb…

PostgreSQL's auto increment > serial

use serial official PostgreSQL: Documentation: 9.4: Numeric Types unofficial PostgreSQL - AUTO INCREMENTwww.tutorialspoint.com CREATE TABLE table_names ( id serial, name text, PRIMARY KEY(id) ); INSERT INTO table_names (name) VALUES ('taro…

順列テーブル

PostgreSQL 9.4.1 こんなテーブルから products name ---------------- cpu_heat mouse_cat keyboard_punch 以下順列テーブルを出す name | name ----------------+---------------- cpu_heat | cpu_heat cpu_heat | mouse_cat cpu_heat | keyboard_punch mo…

decimal (and numeric) - what is

what is decimal(p, s) p ... precision (式の桁数) p = 1 => 1 p = 2 => 10 p = 3 => 100 s ... scale (小数点以下の桁数) s = 1 => 0.1 s = 2 => 0.01 s = 3 => 0.001 PostgreSQL: Documentation: 9.4: Data Types diff (decimal and numeric) (continue t…

int, int4 and integer - PostgreSQL

int and int4 are allies for integer official PostgreSQL: Documentation: 9.4: Data Types unofficial SQL base types: integer vs int?stackoverflow.com

PostgreSQL - varchar and text

no difference varchar(n) and text official PostgreSQL: Documentation: 9.4: Character Types unofficial PostgreSQL data type text vs varchar without lengthdba.stackexchange.com note: PostgreSQL: Don't Use CHAR or VARCHAR