📜  mysql add column tinyint default 0 - SQL (1)

📅  最后修改于: 2023-12-03 15:17:45.582000             🧑  作者: Mango

MySQL Add Column Tinyint Default 0 - SQL

Introduction

In MySQL, adding a column to a table can be done using the ALTER TABLE statement. The TINYINT data type is an 8-bit integer and can be used to store binary flags, boolean values, or small numbers.

The DEFAULT clause is used to specify the default value for the column when a new row is inserted into the table and the value for the column is not specified.

In this article, we will discuss how to add a TINYINT column with a default value of 0 to an existing MySQL table using SQL.

Syntax

The syntax for adding a TINYINT column with a default value of 0 to an existing MySQL table is as follows:

ALTER TABLE table_name ADD COLUMN column_name TINYINT DEFAULT 0;

table_name is the name of the table you want to modify.

column_name is the name of the column you want to add.

Example

Let's say we have a table named employees with three columns: id, name, and salary. We want to add a new column, active, which stores whether an employee is currently active or not. We will set the default value of this column to 0.

The SQL command to add the new column would be:

ALTER TABLE employees ADD COLUMN active TINYINT DEFAULT 0;

After executing this command, the employees table will have four columns: id, name, salary, and active. The active column will have a default value of 0 for all existing and new rows.

Conclusion

Adding a TINYINT column to a MySQL table with a default value of 0 can be done using the ALTER TABLE statement in SQL. This is useful for storing binary flags, boolean values, or small numbers. Remember to specify the table name, column name, data type, and default value in your SQL command.