PageSourceSearch

https://postgresqltutorial.dev/_next/static/chunks/pages/postgresql-insert.ja-JP-7900540ae3fc9f42.js

js postgresqltutorial.dev collected 2026-10-08 16:57:19 UTC 57,535 bytes, 1 lines download raw bytes

1(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[4003],{1515:function(s,e,l){(window.__NEXT_P=window.__NEXT_P||[]).push(["/postgresql-insert.ja-JP",function(){return l(3023)}])},3023:function(s,e,l){"use strict";l.r(e),l.d(e,{__toc:function(){return c}});var r=l(5893),n=l(2673),o=l(9298),i=l(8426);l(9128);var t=l(2643);let c=[{depth:2,value:"INSERT 文の概要",id:"insert-文の概要"},{depth:3,value:"基本構文",id:"基本構文"},{depth:2,value:"PostgreSQL テーブルへのデータ挿入",id:"postgresql-テーブルへのデータ挿入"},{depth:3,value:"1. 単一の行を挿入する",id:"1-単一の行を挿入する"},{depth:3,value:"2. 複数の行を挿入する",id:"2-複数の行を挿入する"},{depth:3,value:"3. 特定のカラムにデータを挿入する",id:"3-特定のカラムにデータを挿入する"},{depth:3,value:"4. デフォルト値を使用する",id:"4-デフォルト値を使用する"},{depth:3,value:"5. 別のテーブルからデータを挿入する",id:"5-別のテーブルからデータを挿入する"},{depth:3,value:"6. 挿入されたデータを返す",id:"6-挿入されたデータを返す"},{depth:3,value:"7. ON CONFLICT を使用して競合を処理する (アップサート)",id:"7-on-conflict-を使用して競合を処理する-アップサート"},{depth:3,value:"8. サブクエリを使用した INSERT",id:"8-サブクエリを使用した-insert"},{depth:3,value:"9. JSON データの挿入",id:"9-json-データの挿入"},{depth:3,value:"10. テーブル間でデータをコピーする",id:"10-テーブル間でデータをコピーする"},{depth:2,value:"結論",id:"結論"},{depth:2,value:"Schema Initialization",id:"schema-initialization"}];function _createMdxContent(s){let e=Object.assign({h1:"h1",p:"p",strong:"strong",h2:"h2",h3:"h3",pre:"pre",code:"code",span:"span",ul:"ul",li:"li",a:"a",hr:"hr"},(0,t.a)(),s.components);return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(e.h1,{children:"PostgreSQL INSERT 文: テーブルにデータを追加する"}),"\n",(0,r.jsxs)(e.p,{children:["私たちの PostgreSQL チュートリアルシリーズへようこそ!このレッスンでは、データベースのテーブルに新しいデータを追加するために欠かせない、",(0,r.jsx)(e.strong,{children:"INSERT"})," 文に焦点を当てます。INSERT 文を効果的に使用することにより、テーブルに意味のあるデータを投入できるようになります。"]}),"\n",(0,r.jsx)(e.h2,{id:"insert-文の概要",children:"INSERT 文の概要"}),"\n",(0,r.jsxs)(e.p,{children:["PostgreSQL の ",(0,r.jsx)(e.strong,{children:"INSERT"})," 文は、テーブルに新しい行を追加するために使用します。すべてのカラムにデータを挿入したり、特定のカラムに挿入したり、複数の行を一度に挿入したり、他のテーブルからデータを挿入することも可能です。"]}),"\n",(0,r.jsx)(e.h3,{id:"基本構文",children:"基本構文"}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"sql","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INSERT INTO"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" table_name (column1, column2, ...)"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VALUES"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" (value1, value2, ...);"})]})]})}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.code,{children:"INSERT INTO"}),": データを挿入するテーブルを指定します。"]}),"\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.code,{children:"VALUES"}),": カラムに挿入するデータをリストアップします。"]}),"\n"]}),"\n",(0,r.jsx)(e.h2,{id:"postgresql-テーブルへのデータ挿入",children:"PostgreSQL テーブルへのデータ挿入"}),"\n",(0,r.jsx)(e.p,{children:"ライブラリや映画データベースを使用した実用的な例を通じて、INSERT 文のさまざまな使い方を見てみましょう。"}),"\n",(0,r.jsx)(e.h3,{id:"1-単一の行を挿入する",children:"1. 単一の行を挿入する"}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"例: books テーブルに新しい本を追加"})}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"sql","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INSERT INTO"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" books (title, author, price, pages)"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VALUES"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Brave New World'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Aldous Huxley'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"9"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"99"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"268"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})]})]})}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.a,{href:"#schema-initialization",children:"スキーマ初期化を見る"})}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"解説:"})}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:"『Brave New World』"})," というタイトルの本が、指定された著者、価格、ページ数で ",(0,r.jsx)(e.code,{children:"books"})," テーブルに追加されます。"]}),"\n"]}),"\n",(0,r.jsx)(e.h3,{id:"2-複数の行を挿入する",children:"2. 複数の行を挿入する"}),"\n",(0,r.jsx)(e.p,{children:"単一の文で複数の行を挿入することができます。値のセットをカンマで区切ります。"}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"例: authors テーブルに複数の著者を追加"})}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"sql","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INSERT INTO"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" authors ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"name"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:")"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VALUES"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Aldous Huxley'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"       ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Ray Bradbury'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"       ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Kurt Vonnegut'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})]})]})}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"解説:"})}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:["3 人の新しい著者が ",(0,r.jsx)(e.code,{children:"authors"})," テーブルに追加されます。"]}),"\n"]}),"\n",(0,r.jsx)(e.h3,{id:"3-特定のカラムにデータを挿入する",children:"3. 特定のカラムにデータを挿入する"}),"\n",(0,r.jsxs)(e.p,{children:["すべてのカラムに値を挿入せず、特定のカラムにのみデータを挿入することもできます。指定されていないカラムにはデフォルト値または ",(0,r.jsx)(e.code,{children:"NULL"})," が設定されます。"]}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"例: 補足情報を指定せずに映画を挿入"})}),"\n",(0,r.jsxs)(e.p,{children:[(0,r.jsx)(e.code,{children:"movies"})," テーブルには ",(0,r.jsx)(e.code,{children:"title"})," カラムしかないので、そのカラムにのみデータを挿入します。"]}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"sql","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INSERT INTO"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" movies (title)"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VALUES"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Blade Runner'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})]})]})}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"解説:"})}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:"『Blade Runner』"})," というタイトルの新しい映画が ",(0,r.jsx)(e.code,{children:"movies"})," テーブルに追加されます。"]}),"\n"]}),"\n",(0,r.jsx)(e.h3,{id:"4-デフォルト値を使用する",children:"4. デフォルト値を使用する"}),"\n",(0,r.jsxs)(e.p,{children:["デフォルト値が定義されたカラムに対しては、",(0,r.jsx)(e.code,{children:"DEFAULT"})," キーワードを使用して明示的にデフォルト値を挿入することが可能です。"]}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"例: デフォルト価格を使用して本を挿入"})}),"\n",(0,r.jsxs)(e.p,{children:["まず、",(0,r.jsx)(e.code,{children:"books"})," テーブルにデフォルト価格が設定されているかを確認し、なければ追加します。"]}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"sql","data-theme":"default",children:[(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-comment)"},children:"-- books テーブルにデフォルト価格を設定"})}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"ALTER"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"TABLE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" books "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"ALTER"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" COLUMN price "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"SET"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"DEFAULT"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"9"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"99"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:";"})]}),"\n",(0,r.jsx)(e.span,{className:"line",children:" "}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-comment)"},children:"-- デフォルト価格を使用して本を挿入"})}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INSERT INTO"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" books (title, author, pages)"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VALUES"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Fahrenheit 451'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Ray Bradbury'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"194"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})]})]})}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"解説:"})}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:"『Fahrenheit 451』"})," がデフォルト価格 ",(0,r.jsx)(e.strong,{children:"$9.99"})," で追加されます。"]}),"\n"]}),"\n",(0,r.jsx)(e.h3,{id:"5-別のテーブルからデータを挿入する",children:"5. 別のテーブルからデータを挿入する"}),"\n",(0,r.jsxs)(e.p,{children:[(0,r.jsx)(e.code,{children:"INSERT INTO ... SELECT"})," 文を使用して、別のテーブルから選択されたデータを挿入できます。"]}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"例: 著者を新しいテーブルにコピーする"})}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"sql","data-theme":"default",children:[(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-comment)"},children:"-- クラシック著者用の新しいテーブルを作成"})}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"CREATE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"TABLE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"classic_authors"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"name"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VARCHAR"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"255"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"));"})]}),"\n",(0,r.jsx)(e.span,{className:"line",children:" "}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-comment)"},children:"-- authors テーブルから classic_authors に著者を挿入"})}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INSERT INTO"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" classic_authors ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"name"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"
1)"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"SELECT"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"name"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"FROM"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" authors"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"WHERE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"name"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"IN"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'George Orwell'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Jane Austen'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Leo Tolstoy'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})]})]})}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"解説:"})}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:["新しいテーブル ",(0,r.jsx)(e.code,{children:"classic_authors"})," が作成されます。"]}),"\n",(0,r.jsxs)(e.li,{children:["指定された名前に一致する著者が ",(0,r.jsx)(e.code,{children:"classic_authors"})," にコピーされます。"]}),"\n"]}),"\n",(0,r.jsx)(e.h3,{id:"6-挿入されたデータを返す",children:"6. 挿入されたデータを返す"}),"\n",(0,r.jsxs)(e.p,{children:[(0,r.jsx)(e.code,{children:"RETURNING"})," 句を使用すると、挿入された行のデータを返すことができます。"]}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"例: 本を挿入し、そのタイトルと価格を返す"})}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"sql","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INSERT INTO"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" books (title, author, price, pages)"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VALUES"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Slaughterhouse-Five'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Kurt Vonnegut'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"8"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"99"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"275"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:")"})]}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"RETURNING title, price;"})})]})}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"解説:"})}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:["本が挿入され、挿入された行の ",(0,r.jsx)(e.code,{children:"title"})," と ",(0,r.jsx)(e.code,{children:"price"})," が返されます。"]}),"\n"]}),"\n",(0,r.jsx)(e.h3,{id:"7-on-conflict-を使用して競合を処理する-アップサート",children:"7. ON CONFLICT を使用して競合を処理する (アップサート)"}),"\n",(0,r.jsxs)(e.p,{children:[(0,r.jsx)(e.code,{children:"ON CONFLICT"})," 句を使用すると、一意制約違反が発生した場合に代替のアクションを指定できます。"]}),"\n",(0,r.jsx)(e.p,{children:"まずは、関連するカラムに一意制約を設定します。"}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"例: 著者を挿入するか、既に存在する場合は何もしない"})}),"\n",(0,r.jsxs)(e.p,{children:["まず、",(0,r.jsx)(e.code,{children:"name"})," カラムに一意制約を設定します。"]}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsx)(e.code,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"ALTER"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"TABLE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" authors "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"ADD"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"CONSTRAINT"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" unique_name "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"UNIQUE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"name"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})]})})}),"\n",(0,r.jsxs)(e.p,{children:["次に ",(0,r.jsx)(e.code,{children:"ON CONFLICT"})," を使用します:"]}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"sql","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INSERT INTO"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" authors ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"name"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:")"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VALUES"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'George Orwell'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:")"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"ON"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" CONFLICT ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"name"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:") DO NOTHING;"})]})]})}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"解説:"})}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:"『George Orwell』"})," の挿入を試みます。"]}),"\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.strong,{children:"『George Orwell』"})," が既に存在する場合、挿入がスキップされます。"]}),"\n"]}),"\n",(0,r.jsx)(e.h3,{id:"8-サブクエリを使用した-insert",children:"8. サブクエリを使用した INSERT"}),"\n",(0,r.jsx)(e.p,{children:"サブクエリを使用して、挿入する値を生成することができます。"}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"例: authors テーブルに存在しない書籍の著者を挿入する"})}),"\n",(0,r.jsxs)(e.p,{children:["いくつかの書籍には、",(0,r.jsx)(e.code,{children:"authors"})," テーブルにまだ登録されていない著者がいるかもしれません。"]}),"\n",(0,r.jsxs)(e.p,{children:["まず、",(0,r.jsx)(e.code,{children:"books"})," にいるが ",(0,r.jsx)(e.code,{children:"authors"})," にいない著者を見つけ、それを ",(0,r.jsx)(e.code,{children:"authors"})," に追加します。"]}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"sql","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INSERT INTO"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" authors ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"name"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:")"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"SELECT DISTINCT"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" author"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"FROM"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" books"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"WHERE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" author "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"NOT"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"IN"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"SELECT"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"name"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"FROM"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" authors);"})]})]})}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"解説:"})}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.code,{children:"books"})," テーブルの著者で、まだ ",(0,r.jsx)(e.code,{children:"authors"})," テーブルに存在しない著者が ",(0,r.jsx)(e.code,{children:"authors"})," に挿入されます。"]}),"\n",(0,r.jsx)(e.li,{children:"スキーマ上では、すべての著者は既に存在しているため、新しい著者は追加されませんが、新しい本に新しい著者が追加された場合は挿入されます。"}),"\n"]}),"\n",(0,r.jsx)(e.h3,{id:"9-json-データの挿入",children:"9. JSON データの挿入"}),"\n",(0,r.jsx)(e.p,{children:"現在のスキーマには JSON カラムを持つテーブルはありませんが、新しく作成することができます。"}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"例: テーブルに JSON オブジェクトを挿入"})}),"\n",(0,r.jsx)(e.p,{children:"まず、JSON カラムを持つテーブルを作成します:"}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"sql","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"CREATE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"TABLE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"book_info"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"    id "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"SERIAL"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"PRIMARY KEY"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:","})]}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"    info JSONB"})}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})})]})}),"\n",(0,r.jsx)(e.p,{children:"JSON データを挿入します:"}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"sql","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INSERT INTO"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" book_info (info)"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VALUES"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:'\'{"title": "Dune", "author": "Frank Herbert", "price": 10.99, "pages": 412}\''}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})]})]})}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"解説:"})}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:["書籍の詳細を含む JSON オブジェクトが ",(0,r.jsx)(e.code,{children:"book_info"})," テーブルに挿入されます。"]}),"\n"]}),"\n",(0,r.jsx)(e.h3,{id:"10-テーブル間でデータをコピーする",children:"10. テーブル間でデータをコピーする"}),"\n",(0,r.jsx)(e.p,{children:"テーブル間でデータをコピーすることができ、変換も可能です。"}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"例: 割引価格付きの discounted_books テーブルに書籍をコピーする"})}),"\n",(0,r.jsxs)(e.p,{children:[(0,r.jsx)(e.code,{children:"discounted_books"})," テーブルを作成します:"]}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"sql","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"CREATE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"TABLE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"discounted_books"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"    title "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VARCHAR"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"255"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"    author "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VARCHAR"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"255"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.j
1sxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"    discounted_price "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"NUMERIC"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"10"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"2"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:")"})]}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})})]})}),"\n",(0,r.jsx)(e.p,{children:"変換された値と共にデータを挿入します:"}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"sql","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INSERT INTO"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" discounted_books (title, author, discounted_price)"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"SELECT"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" title, author, price "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"*"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"0"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"9"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"FROM"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" books;"})]})]})}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"解説:"})}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:["すべての書籍が ",(0,r.jsx)(e.code,{children:"discounted_books"})," にコピーされ、価格に ",(0,r.jsx)(e.strong,{children:"10%"})," の割引が適用されます。"]}),"\n"]}),"\n",(0,r.jsx)(e.h2,{id:"結論",children:"結論"}),"\n",(0,r.jsx)(e.p,{children:"PostgreSQL の INSERT 文は、テーブルにデータを追加するための柔軟なツールです。単一の行、複数の行、他のテーブルからのデータを挿入する際でも、INSERT 文を習得することは効果的なデータベース管理において重要です。"}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"重要なポイント:"})}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:["新しいデータを追加するには、",(0,r.jsx)(e.code,{children:"INSERT INTO ... VALUES"})," を使用します。"]}),"\n",(0,r.jsx)(e.li,{children:"複数の行を挿入するには複数の値セットを指定します。"}),"\n",(0,r.jsx)(e.li,{children:"特定のカラムにデータを挿入するには、カラムを指定します。"}),"\n",(0,r.jsxs)(e.li,{children:["挿入された行の情報を取得するには ",(0,r.jsx)(e.code,{children:"RETURNING"})," を活用します。"]}),"\n",(0,r.jsxs)(e.li,{children:["競合を防ぐためには ",(0,r.jsx)(e.code,{children:"ON CONFLICT"})," を使用します。"]}),"\n",(0,r.jsxs)(e.li,{children:["別のテーブルからデータを挿入するには ",(0,r.jsx)(e.code,{children:"INSERT INTO ... SELECT"})," を使用します。"]}),"\n",(0,r.jsx)(e.li,{children:"PostgreSQL の JSON データ型サポートを活用します。"}),"\n"]}),"\n",(0,r.jsx)(e.hr,{}),"\n",(0,r.jsx)(e.h2,{id:"schema-initialization",children:"Schema Initialization"}),"\n",(0,r.jsx)(e.pre,{"data-language":"sql","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"sql","data-theme":"default",children:[(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-comment)"},children:"-- Create the books table"})}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"CREATE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"TABLE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"books"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"    title "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VARCHAR"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"255"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"    author "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VARCHAR"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"255"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"    price "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"NUMERIC"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"10"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"2"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"    pages "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INTEGER"})]}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})}),"\n",(0,r.jsx)(e.span,{className:"line",children:" "}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-comment)"},children:"-- Insert sample data into the books table"})}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INSERT INTO"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" books (title, author, price, pages) "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VALUES"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'The Great Gatsby'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'F. Scott Fitzgerald'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"10"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"99"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"180"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'To Kill a Mockingbird'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Harper Lee'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"7"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"99"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"281"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'1984'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'George Orwell'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"8"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"99"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"328"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Pride and Prejudice'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Jane Austen'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"6"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"99"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"279"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'The Catcher in the Rye'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'J.D. Salinger'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"9"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"99"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"214"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Moby-Dick'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Herman Melville'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"11"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"99"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"635"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'War and Peace'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Leo Tolstoy'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"12"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"99"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:", "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"1225"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})]}),"\n",(0,r.jsx)(e.span,{className:"line",children:" "}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-comment)"},children:"-- Create the authors table"})}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"CREATE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"TABLE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"authors"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"    "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"name"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VARCHAR"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"255"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:")"})]}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})}),"\n",(0,r.jsx)(e.span,{className:"line",children:" "}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-comment)"},children:"-- Insert sample data into the authors table"})}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INSERT INTO"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" authors ("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"name"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:") "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VALUES"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'F. Scott Fitzgerald'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Harper Lee'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n"
1,(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'George Orwell'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Jane Austen'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'J.D. Salinger'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Herman Melville'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Leo Tolstoy'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})]}),"\n",(0,r.jsx)(e.span,{className:"line",children:" "}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-comment)"},children:"-- Create the movies table"})}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"CREATE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"TABLE"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"movies"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" ("})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"    title "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VARCHAR"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"255"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:")"})]}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})}),"\n",(0,r.jsx)(e.span,{className:"line",children:" "}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-comment)"},children:"-- Insert sample data into the movies table"})}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"INSERT INTO"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" movies (title) "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"VALUES"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'The Shawshank Redemption'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'The Godfather'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'The Dark Knight'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'Pulp Fiction'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:"'The Lord of the Rings'"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:");"})]})]})}),"\n",(0,r.jsx)(e.hr,{}),"\n",(0,r.jsx)(e.p,{children:"このスキーマを使って、INSERT 文の練習をして PostgreSQL の理解を深めましょう!Happy coding!"})]})}let a={MDXContent:function(){let s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},{wrapper:e}=Object.assign({},(0,t.a)(),s.components);return e?(0,r.jsx)(e,{...s,children:(0,r.jsx)(_createMdxContent,{...s})}):_createMdxContent(s)},pageOpts:{filePath:"pages/postgresql-insert.ja-JP.mdx",route:"/postgresql-insert",timestamp:1730457028e3,pageMap:[{kind:"Meta",locale:"ja-JP",data:{index:{title:"PostgreSQLとは?"},"postgresql-select":{title:"SELECT: データのクエリ"},"postgresql-where":{title:"WHERE: データのフィルタリング"},"postgresql-join":{title:"JOIN: テーブルデータの結合"},"postgresql-group-by":{title:"GROUP BY: データの集約"},"postgresql-insert":{title:"INSERT: 新しいデータの追加"},"postgresql-update":{title:"UPDATE: データの修正"}}},{kind:"MdxPage",name:"index",route:"/",locale:"ja-JP"},{kind:"MdxPage",name:"postgresql-group-by",route:"/postgresql-group-by",locale:"ja-JP"},{kind:"MdxPage",name:"postgresql-insert",route:"/postgresql-insert",locale:"ja-JP"},{kind:"MdxPage",name:"postgresql-join",route:"/postgresql-join",locale:"ja-JP"},{kind:"MdxPage",name:"postgresql-select",route:"/postgresql-select",locale:"ja-JP"},{kind:"MdxPage",name:"postgresql-update",route:"/postgresql-update",locale:"ja-JP"},{kind:"MdxPage",name:"postgresql-where",route:"/postgresql-where",locale:"ja-JP"}],flexsearch:{codeblocks:!0},title:"PostgreSQL INSERT 文: テーブルにデータを追加する",headings:c},pageNextRoute:"/postgresql-insert.ja-JP",nextraLayout:o.ZP,themeConfig:i.Z};e.default=(0,n.j)(a)},8426:function(s,e,l){"use strict";var r=l(5893);l(7294);let n={logo:(0,r.jsx)("span",{children:"PostgreSQL Tutorial"}),useNextSeoProps:()=>({titleTemplate:"%s – postgresqltutorial.dev"}),editLink:{text:null},toc:{extraContent:!1},feedback:{content:null},footer:{text:(0,r.jsxs)("span",{children:["\xa9"," ",(0,r.jsx)("a",{href:"https://postgresqltutorial.dev",children:"postgresqltutorial.dev"})]})},i18n:[{locale:"en-US",text:"\uD83C\uDDFA\uD83C\uDDF8 English"},{locale:"es-ES",text:"\uD83C\uDDEA\uD83C\uDDF8 Spanish"},{locale:"fr-FR",text:"\uD83C\uDDEB\uD83C\uDDF7 French"},{locale:"de-DE",text:"\uD83C\uDDE9\uD83C\uDDEA German"},{locale:"zh-CN",text:"\uD83C\uDDE8\uD83C\uDDF3 Chinese (Simplified)"},{locale:"pt-BR",text:"\uD83C\uDDE7\uD83C\uDDF7 Portuguese (Brazil)"},{locale:"nl-NL",text:"\uD83C\uDDF3\uD83C\uDDF1 Dutch"}
1,{locale:"ko-KR",text:"\uD83C\uDDF0\uD83C\uDDF7 Korean"},{locale:"tr-TR",text:"\uD83C\uDDF9\uD83C\uDDF7 Turkish"},{locale:"vi-VN",text:"\uD83C\uDDFB\uD83C\uDDF3 Vietnamese"},{locale:"it-IT",text:"\uD83C\uDDEE\uD83C\uDDF9 Italian"},{locale:"pl-PL",text:"\uD83C\uDDF5\uD83C\uDDF1 Polish"},{locale:"uk-UA",text:"\uD83C\uDDFA\uD83C\uDDE6 Ukrainian"},{locale:"ru-RU",text:"\uD83C\uDDF7\uD83C\uDDFA Russian"},{locale:"ja-JP",text:"\uD83C\uDDEF\uD83C\uDDF5 Japanese"},{locale:"hi-IN",text:"\uD83C\uDDEE\uD83C\uDDF3 Hindi"},{locale:"bn-IN",text:"\uD83C\uDDEE\uD83C\uDDF3 Bengali"},{locale:"ta-IN",text:"\uD83C\uDDEE\uD83C\uDDF3 Tamil"},{locale:"id-ID",text:"\uD83C\uDDEE\uD83C\uDDE9 Indonesian"},{locale:"th-TH",text:"\uD83C\uDDF9\uD83C\uDDED Thai"},{locale:"sv-SE",text:"\uD83C\uDDF8\uD83C\uDDEA Swedish"},{locale:"cs-CZ",text:"\uD83C\uDDE8\uD83C\uDDFF Czech"}]};e.Z=n}},function(s){s.O(0,[9774,2977,2888,179],function(){return s(s.s=1515)}),_N_E=s.O()}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.