Home >Database >Mysql Tutorial >How to set the default value to 0 in mysql
Setting the default value of a column to 0 in MySQL can be achieved by the following methods: use the DEFAULT keyword when creating a table, for example: CREATE TABLE user (id INT DEFAULT 0); use the ALTER TABLE statement when modifying the table , for example: ALTER TABLE user ALTER COLUMN age SET DEFAULT 0;
In MySQL , you can set the default value of a column to 0 by using the DEFAULT
keyword when creating or modifying a table.
When creating table, you can use the following syntax to set the default value of a column to 0:
<code class="sql">CREATE TABLE table_name ( column_name DATATYPE DEFAULT 0 );</code>
For example:
<code class="sql">CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT DEFAULT 0 );</code>
To modify the default values of columns of an existing table, you can use the following syntax:
<code class="sql">ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT 0;</code>
For example:
<code class="sql">ALTER TABLE users ALTER COLUMN age SET DEFAULT 0;</code>
INTEGER
, DECIMAL
, FLOAT
, DOUBLE
, CHAR
, VARCHAR
, BINARY
, and VARBINARY
. NULL
. The above is the detailed content of How to set the default value to 0 in mysql. For more information, please follow other related articles on the PHP Chinese website!