Explain DEFAULT constraint in MySQL
Explain DEFAULT constraint in MySQL
The DEFAULT constraint in MySQL is used to set a default value for a column in a table. This default value is automatically inserted into the column when a new record is added to the table without specifying a value for that column. The primary purpose of the DEFAULT constraint is to ensure that a column always has a value, even if no explicit value is provided during the insertion of a new record.
When defining a table, you can specify a DEFAULT constraint for a column by including the DEFAULT keyword followed by the desired default value in the column definition. The default value must be compatible with the data type of the column. For example, if the column is of type VARCHAR, the default value should be a string; if the column is of type INT, the default value should be an integer, and so on. It's important to note that the default value must be a literal constant, such as a number or a string, and cannot be a function or an expression, with the exception of the CURRENT_TIMESTAMP function, which can be used as a default value for TIMESTAMP and DATETIME columns[2].
The DEFAULT constraint can be added to a column at the time of table creation using the CREATE TABLE statement, or it can be added to an existing table using the ALTER TABLE statement. For example, when creating a new table, you might specify a default value for a "Country" column as 'INDIA', and for a "DateOfJoining" column as the current timestamp. This means that if a new record is inserted into the table without specifying values for these columns, 'INDIA' and...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào