In MySQL, you can use the TINYINT data type to store boolean values. Typically, boolean values are represented as 0 for false and 1 for true in MySQL using the TINYINT(1) data type. Here's an example:
sql
-- Create a table with a boolean column
CREATE TABLE users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50),
is_active TINYINT(1)
);
-- Insert data into the table
INSERT INTO users (username, is_active)
VALUES
('user1', 1), -- true
('user2', 0); -- false
In this example:
We create a table named
userswith three columns:user_id,username, andis_active.The
is_activecolumn is of typeTINYINT(1), which is used to store boolean values. It can store0forfalseor1fortrue.We insert two rows into the
userstable, where theis_activecolumn represents boolean values.'user1'is active (1ortrue), and'user2'is not active (0orfalse).
You can use the TINYINT(1) data type to efficiently store boolean values in MySQL while using minimal storage space. When you query the database, you can treat 0 as false and 1 as true to work with boolean data effectively.
Comments
Post a Comment