Esempi di INSERT - Amazon Redshift

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Esempi di INSERT

La tabella CATEGORY nel database TICKIT contiene le seguenti righe:

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)

Crea una tabella CATEGORY_STAGE con uno schema simile alla tabella CATEGORY, ma definisci i valori predefiniti per le colonne:

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

La seguente istruzione INSERT seleziona tutte le righe dalla tabella CATEGORY e le inserisce nella tabella CATEGORY_STAGE.

insert into category_stage (select * from category);

Le parentesi che racchiudono la query sono facoltative.

Questo comando inserisce una nuova riga nella tabella CATEGORY_STAGE con un valore specificato per ogni colonna nell'ordine:

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

Puoi anche inserire una nuova riga che combina valori specifici e valori predefiniti:

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

Esegui la seguente query per restituire le righe inserite:

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)

Gli esempi seguenti mostrano alcune istruzioni INSERT VALUES a più righe. Il primo esempio inserisce valori CATID specifici per due righe e valori predefiniti per le altre colonne in entrambe le righe.

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)

Il prossimo esempio inserisce tre righe con varie combinazioni di valori specifici e predefiniti:

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)

La prima serie di VALUES in questo esempio produce gli stessi risultati della specifica di DEFAULT VALUES per un'istruzione INSERT a riga singola.

I seguenti esempi mostrano il comportamento di INSERT quando una tabella ha una colonna IDENTITY. Innanzitutto, crea una nuova versione della tabella CATEGORY, quindi inserisci le righe in essa contenute da 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;

Non è possibile inserire valori interi specifici nella colonna CATID IDENTITY. I valori delle colonne IDENTITY vengono generati automaticamente.

L'esempio seguente dimostra che le sottoquery non possono essere utilizzate come espressioni in istruzioni INSERT VALUES a più righe:

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

L'esempio seguente mostra un inserimento in una tabella temporanea popolata con i dati della tabella venue utilizzando la clausola WITH SELECT. Per ulteriori informazioni sulla tabella venue, consulta Database di esempio.

Per prima cosa, crea la tabella temporanea #venuetemp.

CREATE TABLE #venuetemp AS SELECT * FROM venue;

Elenca le righe nella tabella #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 ...

Inserisci 10 righe duplicate nella tabella #venuetemp utilizzando la clausola WITH SELECT.

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

Elenca le righe nella tabella #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 ...