SQL Boolean type

Source: Internet
Author: User

About MySQL for Boolean and tinyint (1)

Boolean type
MySQL saves a Boolean value with 1 for true,0 representing False,boolean in MySQL with type tinyint (1),
There are four constants in MySQL: True,false,true,false, which represent 1,0,1,0 respectively,
Mysql> Select True,false,true,false;
+------+-------+------+-------+
| TRUE | FALSE | TRUE | FALSE |
+------+-------+------+-------+
|     1 |    0 |     1 | 0 |
+------+-------+------+-------+
You can insert a Boolean value as follows: INSERT INTO [xxxx (XX)] VALUES (true), and of course values (1);
Examples are as follows:
Mysql> ALTER TABLE Test add IsOk boolean;
Query OK
mysql> desc test;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| ID | Int (11) | NO | PRI | NULL | auto_increment |
| IsOk | tinyint (1) |     YES | |                NULL | |
+-------+-------------+------+-----+---------+----------------+
mysql> INSERT INTO Test (ISOK) values (true);
Query OK
Mysql> select IsOk from test;
+------+
| IsOk |
+------+
| 1 |
+------+
=================

MySQL does not have a Boolean type. This is also a very strange phenomenon. Cases:
CREATE TABLE XS
(
ID int PRIMARY KEY,
BL Boolean
)
This is a good way to create a success, but looking at the statement after the build, you will find that MySQL replaced it with tinyint (1). In other words, MySQL has boolean=tinyint, but what type of Pojo class to define?
Because of inertial thinking, it is also defined as type in the Java class. Then use the <s:check/> tag in struts. This creates a serious problem. <s:check> is a Boolean, and Pojo is defined as byte. This data can never be submitted, was blocked by struts intercept. The workaround is to define a Boolean in the Pojo class, defined in MySQL as tinyint (1). ------TINYINT (1) or ENUM (' true ', ' false ')-------Boolean and TINYINT about MySQL (1) One, Oracle itself does not have a Boolean type, that is, the database-related types do not include Boolean, which is typically implemented with number (1) and char (1).
So "You cannot insert the values TRUE and FALSE into a database column. Also, you cannot select or Fetch column values into a BOOLEAN variable. "
Plsql in order to implement structured programming, the Boolean type is supported, so the possible problem is that the return parameter of a stored procedure or function is Boolean, but the return parameter of the Boolean type cannot be registered at call this procedure. Execution throws a "wrong parameter type" exception, and the solution is to replace the Boolean parameter with a different type.

It is not clear why Oracle does not support Boolean types.

Second, has been the Oracle has no Boolean type, there are two solutions on the net, one is with number (1), the second is with char (1), strengths, personal comparison like the number method to solve, the reason is very simple, because it starts from the C language, which conforms to the C language habit. A few days ago I had the opportunity to consult with an Oracle consultant who provided the solution to Boolean with Char (1), but there were also areas to note: char (1) is a better choice than number (1) If it is a specific Boolean type. Because the former uses less storage space than the latter, but these two in the query when the storage space savings will provide check efficiency, but note that with char (1) When the field can not be allowed to be empty, must have default, otherwise the query efficiency will be reduced


Three There is a Boolean type in PL/SQL and a null type
There are boolean types in PL/SQL that can only take 2 values: true and false;
The variables in the stored procedure also support the Boolean type;
However, there is no Boolean type in the database.


Iv. in the stored procedure:
Declare
V1 Boolean;
Begin
v1:=1>2;
if (v1) Then
Dbms_output.put_line (' True
');
Else
Dbms_output.put_line (' False
');
End If;
End

Print: False
----------------------------------------
Declare
V1 Boolean;
Begin
v1:=1>2;
Dbms_output.put_line (v1);
End;


Will error. The runtime gets an error message: The number of arguments or the type error when calling ' Put_Line '. This is because the Boolean type value cannot be printed directly in the script.


Five:
Oracle does not have boolean,mysql with bit (1) and Oracle can use CHAR (1) Check (... (0,1)) Field
Such as:
CREATE Table A (a char (1) Check (a in (0,1)))
JDBC then uses Getboolean () to return the correct result.

JDBC, I use Ojdbc14.jar.

PS: The above content is quoted from the network, please respect the original author, here is only for learning. Boolean type (decode&case) sql> show err is not supported in Oracle SQL statements;
Errors for FUNCTION is1gt0:

Line/col ERROR
-------- -----------------------------------------------------------------
5/3 Pl/sql:statement ignored
5/10 pls-00306:wrong number or types of arguments in call to ' DECODE '
Sql>

Case Perfect through:
sql> CREATE OR REPLACE FUNCTION is1gt0
RETURN VARCHAR2
Is
BEGIN
RETURN Case 1 > 0
When TRUE
Then ' true '
ELSE ' false '
END;
END;
/

Function created.

Sql> Show err;
No errors.
Sql> select is1gt0 from dual;

Is1gt0
--------------------------------------------------------------------------------
True

Sql>


Summary:
1. Boolean types are not supported in Oracle SQL statements;
2. Decode is unique to Oracle, and case is used by standard sql,mysql and SQL Server, and case can also output Boolean conversions.

SQL Boolean type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.