Default column values in MySQL
By default, MySQL will not set default values for your column entries. Refer here for how to add a default value for a column in MySQL.
To create a simple table with two columns
CREATE TABLE students (name VARCHAR(30),age INT);
To then insert a record into the above table:
INSERT INTO students (name)VALUES ('bob');
Note that we have not included any value for the
age. Nonetheless, the code is perfectly valid. Now, let’s see how the table looks like:
SELECT * FROM students;+------+------+| name | age |+------+------+| bob | NULL |+------+------+
Since we have not included the value for
age, MySQL inserts a
NULL value. For those who are familiar with programming languages like Java, you may have expected the
age to be set to
0 since we have set the data type for the age column to be
INT. However, this is clearly not the case for MySQL.