The DEFAULT
constraint specifies a default value for a column when no value is provided.
It helps ensure meaningful data and can simplify INSERT statements.
column_name data_type DEFAULT default_value
CREATE TABLE products ( product_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, price DECIMAL(10,2) NOT NULL DEFAULT 0.00, in_stock BOOLEAN NOT NULL DEFAULT TRUE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
ALTER TABLE users MODIFY COLUMN signup_date DATE NOT NULL DEFAULT '2025-01-01';
ALTER TABLE products ALTER COLUMN price DROP DEFAULT;
UPDATE
first if needed.
quantity DEFAULT 0
).is_active DEFAULT TRUE
).CURRENT_TIMESTAMP
for automatic creation times.Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!