|
| 1 | += Boolean data type |
| 2 | +:database-version: 23.2.0 |
| 3 | +:database-category: sql |
| 4 | + |
| 5 | +The `BOOLEAN` data type enables the storage and processing of `TRUE` and `FALSE` values. |
| 6 | + |
| 7 | +Boolean values can be used as table column values or inside SQL query expressions. |
| 8 | + |
| 9 | +To declare a table column of type `BOOLEAN`, either use the `BOOLEAN` or `BOOL` keyword: |
| 10 | + |
| 11 | +[source,sql] |
| 12 | +[subs="verbatim"] |
| 13 | +---- |
| 14 | +CREATE TABLE email_addresses |
| 15 | +( |
| 16 | + user_id NUMBER NOT NULL, |
| 17 | + email VARCHAR2(255) NOT NULL, |
| 18 | + active BOOLEAN NOT NULL, |
| 19 | + primary BOOL NOT NULL |
| 20 | +); |
| 21 | +---- |
| 22 | + |
| 23 | +.Example |
| 24 | +[source,sql] |
| 25 | +[subs="verbatim"] |
| 26 | +---- |
| 27 | +SQL> CREATE TABLE email_addresses |
| 28 | + 2 ( |
| 29 | + 3 user_id NUMBER NOT NULL, |
| 30 | + 4 email VARCHAR2(255) NOT NULL, |
| 31 | + 5 active BOOLEAN NOT NULL, |
| 32 | + 6 primary BOOL NOT NULL |
| 33 | + 7 ); |
| 34 | +
|
| 35 | +Table EMAIL_ADDRESSES created. |
| 36 | +
|
| 37 | +SQL> INSERT INTO email_addresses |
| 38 | + 2 (user_id, active, primary, email) |
| 39 | + 3 VALUES ( 1, true, true, 'jon.doe@example.com'), |
| 40 | + 4 ( 2, true, true, 'jane.smith@gmail.com'), |
| 41 | + 5 ( 2, false, false, 'jsmith@gmail.com'), |
| 42 | + 6 ( 3, true, true, 'max.well@example.com'), |
| 43 | + 7 ( 3, true, false, 'mwell@gmail.com'); |
| 44 | +
|
| 45 | +5 rows inserted. |
| 46 | +
|
| 47 | +SQL> COMMIT; |
| 48 | +
|
| 49 | +Commit complete. |
| 50 | +
|
| 51 | +SQL> -- Select all email addresses that are active |
| 52 | +SQL> SELECT email FROM email_addresses |
| 53 | + 2 WHERE active; |
| 54 | +
|
| 55 | +EMAIL |
| 56 | +-------------------- |
| 57 | +jon.doe@example.com |
| 58 | +jane.smith@gmail.com |
| 59 | +max.well@example.com |
| 60 | +mwell@gmail.com |
| 61 | + |
| 62 | +SQL> -- Select all email addresses that are active and primary |
| 63 | +SQL> SELECT email FROM email_addresses |
| 64 | + 2 WHERE active AND primary; |
| 65 | + |
| 66 | +EMAIL |
| 67 | +-------------------- |
| 68 | +jon.doe@example.com |
| 69 | +jane.smith@gmail.com |
| 70 | +max.well@example.com |
| 71 | +---- |
| 72 | + |
| 73 | +== Benefits |
| 74 | + |
| 75 | +The `BOOLEAN` data type standardizes the storage of "Yes" and "No" values. |
| 76 | + |
| 77 | +== Further information |
| 78 | + |
| 79 | +* Introduced: xref:versions:{database-version}/index.adoc[] |
| 80 | +* Availability: all editions |
| 81 | +* link:https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/Data-Types.html[Documentation] |
| 82 | +* link:https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/Data-Types.html[Example] |
0 commit comments