
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can I create a MySQL boolean column and assign value 1 while altering the same column?
To assign value 1 while altering, use the MySQL DEFAULT. This will itself enter 1 if nothing is inserted in the same column while using the INSERT command.
Let us first create a table −
mysql> create table DemoTable ( isAdult int ); Query OK, 0 rows affected (1.39 sec)
Following is how you can assign value 1 default to the already created column −
mysql> alter table DemoTable CHANGE isAdult isAdult BOOLEAN DEFAULT '1' NOT NULL; Query OK, 0 rows affected (1.18 sec) Records: 0 Duplicates: 0 Warnings: 0
Insert some records in the table using insert command −
mysql> insert into DemoTable values(); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+---------+ | isAdult | +---------+ | 1 | | 1 | | 0 | +---------+ 3 rows in set (0.00 sec)
- Related Articles
- How to create boolean column in MySQL with false as default value?
- While fetching the data as output, how can I use multiple conditions on same column?
- On inserting ‘NULL’, ‘0’ or No Value to the column, will MySQL assign sequence number for AUTO_INCREMENT column?
- Count the same value of each row in a MySQL column?
- Select data and set value to boolean based on timestamp column in MySQL
- How can we create MySQL views with column list?
- How do I set the default value for a column in MySQL?
- How can I create a MySQL table with a column with only 3 possible given values?
- Getting maximum from a column value and set it for all other values in the same column with MySQL?
- Can we skip a column name while inserting values in MySQL?
- Find rows that have the same value on a column in MySQL?
- How can I insert a value in a column at the place of NULL using MySQL COALESCE() function?
- How can I drop an existing column from a MySQL table?
- How can I remove every column in a table in MySQL?
- How can we create MySQL views without any column list?

Advertisements