Home >Daily Programming >Mysql Knowledge >How to write the default value male in mysql
In MySQL, you can specify a default value for the gender column by setting the DEFAULT keyword. DEFAULT 'male' sets the default value for the gender column to "male" when creating the table, which is automatically applied when new rows are inserted if no gender value is provided explicitly.
How to use MySQL to set the default gender value to "Male"
In MySQL, for the column in the table Setting default values is very simple. For the gender column, you can set it to "Male" or "Female", depending on the needs of your application.
Step 1: Create a table
First, you need to create a table with a gender column. The following SQL statement will create a table named people
that contains a column named gender
with a default value of "Male":
<code class="sql">CREATE TABLE people ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, gender VARCHAR(10) NOT NULL DEFAULT 'male' );</code>
Step 2: Understand the DEFAULT keyword
DEFAULT
keyword is used to specify the default value used when no value is specified for the column. In the example above, the default value for the gender
column is set to "male". This means that when a new row is inserted into the people
table, if a value for the gender
column is not explicitly specified, the column will automatically be set to "male".
Step 3: Insert data
You can insert data with gender "male" into the people
table, as shown below:
<code class="sql">INSERT INTO people (name) VALUES ('John Doe');</code>
After querying the table, you will find that the value of the gender
column is "male" because the default value of the column has been applied.
<code class="sql">SELECT * FROM people WHERE name = 'John Doe';</code>
Note:
ALTER TABLE
statement to modify the default value of an existing table. NULL
as the default value, indicating that there is no valid value in the column. The above is the detailed content of How to write the default value male in mysql. For more information, please follow other related articles on the PHP Chinese website!