Home >Database >Mysql Tutorial >How to set the default value to 1 in mysql
In MySQL, the default value of a column can be set to 1 using the ALTER TABLE statement and the DEFAULT keyword. The steps are as follows: 1. Determine the table name and column name; 2. Write the ALTER TABLE statement; 3. Execute the statement.
In MySQL, you can pass the ALTER TABLE
statement and ## The #DEFAULT keyword sets the column's default value to 1.
<code class="sql">ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT 1;</code>
1. Determine the table name and column name
First, you need to know the table name to be updated Name and the name of the column for which you want to set the default value.2. Write the ALTER TABLE statement
ALTER TABLE statement, specify the table name, column name and default value.
3. Execute statement
ExecuteALTER TABLE statement in MySQL command line or client:
<code class="sql">mysql> ALTER TABLE my_table ALTER COLUMN my_column SET DEFAULT 1;</code>ExampleSuppose you have a column named
my_column in a table named
my_table, and you want to set the default value of the column to 1. Then, you need to execute the following statement:
<code class="sql">ALTER TABLE my_table ALTER COLUMN my_column SET DEFAULT 1;</code>After executing this statement, the
my_column column will default to 1 without explicitly specifying a value.
statement will overwrite it.
constraint to enforce a default value.
The above is the detailed content of How to set the default value to 1 in mysql. For more information, please follow other related articles on the PHP Chinese website!