

 Amazon Redshift は、パッチ 198 以降、新しい Python UDF の作成をサポートしなくなります。既存の Python UDF は、2026 年 6 月 30 日まで引き続き機能します。詳細については、[ブログ記事](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)を参照してください。

# INSERT の例
<a name="c_Examples_of_INSERT_30"></a>

TICKIT データベースの CATEGORY テーブルには、次の行が含まれています。

```
 catid | catgroup |  catname  |                  catdesc
-------+----------+-----------+--------------------------------------------
     1 | Sports   | MLB       | Major League Baseball
     2 | Sports   | NHL       | National Hockey League
     3 | Sports   | NFL       | National Football League
     4 | Sports   | NBA       | National Basketball Association
     5 | Sports   | MLS       | Major League Soccer
     6 | Shows    | Musicals  | Musical theatre
     7 | Shows    | Plays     | All non-musical theatre
     8 | Shows    | Opera     | All opera and light opera
     9 | Concerts | Pop       | All rock and pop music concerts
    10 | Concerts | Jazz      | All jazz singers and bands
    11 | Concerts | Classical | All symphony, concerto, and choir concerts
(11 rows)
```

 CATEGORY テーブルと同様のスキーマを持つ CATEGORY\$1STAGE テーブルを作成します。ただし、列のデフォルト値を定義します。

```
create table category_stage
(catid smallint default 0,
catgroup varchar(10) default 'General',
catname varchar(10) default 'General',
catdesc varchar(50) default 'General');
```

次の INSERT ステートメントでは、CATEGORY テーブルのすべての行を選択し、CATEGORY\$1STAGE テーブルに挿入します。

```
insert into category_stage
(select * from category);
```

クエリを囲むかっこはオプションです。

このコマンドは、CATEGORY\$1STAGE テーブルに新しい行を挿入します。各列には順番に値が指定されます。

```
insert into category_stage values
(12, 'Concerts', 'Comedy', 'All stand-up comedy performances');
```

特定の値とデフォルトの値を組み合わせた新しい行を挿入できます。

```
insert into category_stage values
(13, 'Concerts', 'Other', default);
```

次のクエリを実行して、挿入される行を返します。

```
select * from category_stage
where catid in(12,13) order by 1;

 catid | catgroup | catname |             catdesc
-------+----------+---------+----------------------------------
    12 | Concerts | Comedy  | All stand-up comedy performances
    13 | Concerts | Other   | General
(2 rows)
```

次の例は、複数行の INSERT VALUES ステートメントを示しています。最初の例では、2 行について特定の CATID 値を挿入し、両方の行の他の列にはデフォルト値を挿入します。

```
insert into category_stage values
(14, default, default, default),
(15, default, default, default);

select * from category_stage where catid in(14,15) order by 1;
 catid | catgroup | catname | catdesc
-------+----------+---------+---------
    14 | General  | General | General
    15 | General  | General | General
(2 rows)
```

次の例では、特定の値とデフォルトの値の多様な組み合わせを使用して 3 つの行を挿入します。

```
insert into category_stage values
(default, default, default, default),
(20, default, 'Country', default),
(21, 'Concerts', 'Rock', default);

select * from category_stage where catid in(0,20,21) order by 1;
 catid | catgroup | catname | catdesc
-------+----------+---------+---------
     0 | General  | General | General
    20 | General  | Country | General
    21 | Concerts | Rock    | General
(3 rows)
```

この例の最初にある一連の VALUES は、単一列の INSERT ステートメントに DEFAULT VALUES を指定した場合と同じ結果を提供します。

次の例では、テーブルに IDENTITY 列がある場合の INSERT の動作を示します。まず、CATEGORY テーブルの新しいバージョンを作成してから、CATEGORY よりこのテーブルに行を挿入します。

```
create table category_ident
(catid int identity not null,
catgroup varchar(10) default 'General',
catname varchar(10) default 'General',
catdesc varchar(50) default 'General');


insert into category_ident(catgroup,catname,catdesc)
select catgroup,catname,catdesc from category;
```

CATID IDENTITY 列には、整数値を指定して挿入できないことに注意してください。IDENTITY 列の値は、自動的に生成されます。

次の例は、複数行の INSERT VALUES ステートメントでは、式としてサブクエリを使用できないことを示しています。

```
insert into category(catid) values
((select max(catid)+1 from category)),
((select max(catid)+2 from category));

ERROR: can't use subqueries in multi-row VALUES
```

次の例は、`WITH SELECT` 句を使用して `venue` テーブルからのデータが入力されたテンポラリテーブルへの挿入を示しています。`venue` テーブルの詳細については、「[サンプルデータベース](c_sampledb.md)」を参照してください。

まず、テンポラリテーブル `#venuetemp` を作成します。

```
CREATE TABLE #venuetemp AS SELECT * FROM venue;
```

`#venuetemp` テーブル内の行を一覧表示します。

```
SELECT * FROM #venuetemp ORDER BY venueid;
         
venueid | venuename                | venuecity  | venuestate| venueseats
--------+--------------------------+------------+-----------+------------
1        Toyota Park                Bridgeview   IL          0	
2        Columbus Crew Stadium      Columbus     OH          0	
3        RFK Stadium                Washington   DC          0	
4        CommunityAmerica Ballpark  Kansas City  KS          0	
5        Gillette Stadium           Foxborough   MA          68756	
...
```

`WITH SELECT` 句を使用して `#venuetemp` テーブルに重複する行を 10 行挿入します。

```
INSERT INTO #venuetemp (WITH venuecopy AS (SELECT * FROM venue) SELECT * FROM venuecopy ORDER BY 1 LIMIT 10);
```

`#venuetemp` テーブル内の行を一覧表示します。

```
SELECT * FROM #venuetemp ORDER BY venueid;
         
venueid | venuename                | venuecity  | venuestate| venueseats
--------+--------------------------+------------+-----------+------------
1        Toyota Park                Bridgeview   IL          0	
1        Toyota Park                Bridgeview   IL          0	
2        Columbus Crew Stadium      Columbus     OH          0	
2        Columbus Crew Stadium      Columbus     OH          0	
3        RFK Stadium                Washington   DC          0
3        RFK Stadium                Washington   DC          0	
4        CommunityAmerica Ballpark  Kansas City  KS          0	
4        CommunityAmerica Ballpark  Kansas City  KS          0	
5        Gillette Stadium           Foxborough   MA          68756
5        Gillette Stadium           Foxborough   MA          68756
...
```