How can I set default values for data in MySQL?
In MySQL, default values can be set for data by specifying them when creating a table. Here is how you can set default values:
- Specify default values when creating a table.
CREATE TABLE table_name (
column_name data_type DEFAULT default_value,
...
);
For example, create a table named users and set the default value for the status column to active.
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(50),
status VARCHAR(50) DEFAULT 'active'
);
- Change the structure of the table.
ALTER TABLE table_name
ALTER COLUMN column_name SET DEFAULT default_value;
For example, adding a default value of active for the status column in the users table.
ALTER TABLE users
ALTER COLUMN status SET DEFAULT 'active';
You can set default values for data in MySQL using the methods mentioned above.