Home > Database > Mysql Tutorial > body text

How to set the default value to 0 in mysql

下次还敢
Release: 2024-05-01 21:24:48
Original
617 people have browsed it

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;

How to set the default value to 0 in mysql

How to set the default value to 0 in MySQL

In MySQL , you can set the default value of a column to 0 by using the DEFAULT keyword when creating or modifying a table.

Set default value when creating 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>
Copy after login

For example:

<code class="sql">CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255),
  age INT DEFAULT 0
);</code>
Copy after login

Set default values ​​when modifying tables

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>
Copy after login

For example:

<code class="sql">ALTER TABLE users ALTER COLUMN age SET DEFAULT 0;</code>
Copy after login

Important notes

  • Default values ​​can only be set on the following data types: INTEGER, DECIMAL, FLOAT, DOUBLE , CHAR, VARCHAR, BINARY, and VARBINARY.
  • If you do not set a default value explicitly, the default value for a numeric column will be NULL.
  • Even if a default value is specified, the value can still be overridden when inserting records.

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!