Tipo booleano - 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à.

Tipo booleano

Usa valori di dati BOOLEAN per memorizzare valori true e false in una colonna a byte singolo. La tabella seguente descrive i tre possibili stati per un valore booleano e i valori letterali che risultano in quello stato. Indipendentemente dalla stringa di input, una colonna booleana memorizza e restituisce "t" per true e "f" per false.

Stato Valori letterali validi Storage
True TRUE 't' 'true' 'y' 'yes' '1' 1 byte
False FALSE 'f' 'false' 'n' 'no' '0' 1 byte
Sconosciuto NULL 1 byte

È possibile utilizzare un confronto IS per controllare il valore Boolean solo come predicato nella clausola WHERE. Non è possibile utilizzare un confronto IS con un valore Boolean nell'elenco SELECT.

Esempi

È possibile usare una colonna BOOLEAN per memorizzare uno stato "Attivo/Inattivo" per ciascun cliente in una tabella CUSTOMER.

create table customer( custid int, active_flag boolean default true);
insert into customer values(100, default);
select * from customer; custid | active_flag -------+-------------- 100 | t

Se non viene specificato alcun valore predefinito (true o false) nell'istruzione CREATE TABLE, inserire un valore predefinito significa inserire un valore null.

In questo esempio, la query seleziona dalla tabella USERS utenti a cui piacciono gli sport ma non il teatro:

select firstname, lastname, likesports, liketheatre from users where likesports is true and liketheatre is false order by userid limit 10; firstname | lastname | likesports | liketheatre ----------+------------+------------+------------- Lars | Ratliff | t | f Mufutau | Watkins | t | f Scarlett | Mayer | t | f Shafira | Glenn | t | f Winifred | Cherry | t | f Chase | Lamb | t | f Liberty | Ellison | t | f Aladdin | Haney | t | f Tashya | Michael | t | f Lucian | Montgomery | t | f (10 rows)

Il seguente esempio seleziona dalla tabella USERS gli utenti per i quali non si sa se gradiscono la musica rock.

select firstname, lastname, likerock from users where likerock is unknown order by userid limit 10; firstname | lastname | likerock ----------+----------+---------- Rafael | Taylor | Vladimir | Humphrey | Barry | Roy | Tamekah | Juarez | Mufutau | Watkins | Naida | Calderon | Anika | Huff | Bruce | Beck | Mallory | Farrell | Scarlett | Mayer | (10 rows)

L'esempio seguente restituisce un errore perché utilizza un confronto IS nell'elenco SELECT.

select firstname, lastname, likerock is true as "check" from users order by userid limit 10; [Amazon](500310) Invalid operation: Not implemented

L'esempio seguente ha esito positivo perché utilizza un confronto uguale ( = ) nell'elenco SELECT invece del confronto IS.

select firstname, lastname, likerock = true as "check" from users order by userid limit 10; firstname | lastname | check ----------+-----------+------ Rafael | Taylor | Vladimir | Humphrey | Lars | Ratliff | true Barry | Roy | Reagan | Hodge | true Victor | Hernandez | true Tamekah | Juarez | Colton | Roy | false Mufutau | Watkins | Naida | Calderon |