Home >Database >Mysql Tutorial >Boolean vs. tinyint(1) in MySQL: Which Should I Use for Boolean Values?

Boolean vs. tinyint(1) in MySQL: Which Should I Use for Boolean Values?

Susan Sarandon
Susan SarandonOriginal
2024-12-30 06:51:10324browse

Boolean vs. tinyint(1) in MySQL: Which Should I Use for Boolean Values?

Boolean vs tinyint(1) for Boolean Values in MySQL

When working with boolean values in MySQL, the choice between using the boolean data type and the tinyint(1) data type can arise. While they may seem like different options, they are essentially equivalent.

MySQL recognizes tinyint(1) as a boolean datatype. Both boolean and tinyint(1) can store values of 0 (false) and 1 (true). They can be used interchangeably, as MySQL automatically converts between them.

Usage Example:

To illustrate this, consider the following example:

CREATE TABLE my_table (
  is_active boolean,
  is_admin tinyint(1)
);

INSERT INTO my_table (is_active, is_admin) VALUES (true, 1);
SELECT * FROM my_table WHERE is_active = is_admin;

In this example, the columns is_active and is_admin are both used to store boolean values. The INSERT statement inserts a new row with is_active set to true (1) and is_admin set to 1 (true). The SELECT statement retrieves the row where is_active and is_admin are equal, which will return the row just inserted.

Conclusion:

When storing boolean values in MySQL, both boolean and tinyint(1) can be used effectively. As they are synonyms, choosing between them is a matter of personal preference.

The above is the detailed content of Boolean vs. tinyint(1) in MySQL: Which Should I Use for Boolean Values?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn