MySQL DEFAULT Constraint
The DEFAULT
constraint provides a default value for a column when no value is specified.
Example with Tamil Kings
CREATE TABLE tamil_kings_default (
id INT AUTO_INCREMENT PRIMARY KEY,
king_name VARCHAR(100),
kingdom VARCHAR(100) DEFAULT 'Chola'
);
Code Explanation: If no value is provided for the kingdom
column, it will default to 'Chola'.
Best Practices
- Use default values for columns that frequently have the same value.
- Ensure default values make sense for the data context.
Key Takeaways
- The
DEFAULT
constraint simplifies data entry by providing default values. - It helps in maintaining data consistency when certain columns are commonly filled with the same value.