MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
The DEFAULT
keyword in MySQL is used to set a default value for a column in a table. This default value is used when an INSERT
statement is executed without specifying a value for this column.
Prerequisites:
Tutorial:
To start the mysql
command-line client, open a terminal or command prompt, and enter:
mysql -u [username] -p
Replace [username]
with your MySQL username and enter your password when prompted.
Select the database where you want to create your table:
USE [database_name];
Replace [database_name]
with the name of your database.
Suppose you want to create a users
table with a status
column that defaults to 'active'
:
CREATE TABLE users ( id INT AUTO_INCREMENT, username VARCHAR(255), status VARCHAR(50) DEFAULT 'active', PRIMARY KEY (id) );
In this example, if you don't specify a value for the status
column when inserting a new user, it will be set to 'active'
.
When you insert data into the table, MySQL will use the default value for any column that is not specified in the INSERT
statement:
INSERT INTO users (username) VALUES ('johndoe');
If you select this user with SELECT * FROM users;
, you will see that the status
for this user is 'active'
.
You can change the default value for a column using the ALTER TABLE
statement:
ALTER TABLE users MODIFY status VARCHAR(50) DEFAULT 'inactive';
After running this statement, any new users that are inserted without a specified status
will be set to 'inactive'
.
EXIT;
By using the DEFAULT
keyword in MySQL, you can ensure that your columns always have a value, even if one is not specified when inserting data. This can make your data more consistent and your queries more predictable.
Setting default values in MySQL tables:
CREATE TABLE users ( user_id INT PRIMARY KEY, username VARCHAR(50) DEFAULT 'guest', email VARCHAR(255) DEFAULT 'user@example.com', registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
How to use DEFAULT in MySQL columns:
CREATE TABLE products ( product_id INT PRIMARY KEY, product_name VARCHAR(100) DEFAULT 'Untitled', price DECIMAL(10, 2) DEFAULT 0.00 );
MySQL DEFAULT and NULL values:
CREATE TABLE orders ( order_id INT PRIMARY KEY, order_status VARCHAR(20) DEFAULT 'Pending' NOT NULL );
Specifying default values for different data types in MySQL:
CREATE TABLE preferences ( user_id INT PRIMARY KEY, preferred_color VARCHAR(20) DEFAULT 'Blue', notification_enabled BOOLEAN DEFAULT true );
DEFAULT values in CREATE TABLE statement in MySQL:
CREATE TABLE tasks ( task_id INT PRIMARY KEY, task_name VARCHAR(100) DEFAULT 'Untitled', completion_status BOOLEAN DEFAULT false );
Changing default values for existing columns in MySQL:
ALTER TABLE your_table MODIFY COLUMN column_name datatype DEFAULT new_default_value;
MySQL DEFAULT expression examples:
CREATE TABLE logs ( log_id INT PRIMARY KEY, log_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, log_message VARCHAR(255) DEFAULT 'No message' );
Using DEFAULT in INSERT statements in MySQL:
INSERT INTO users (user_id, username, email) VALUES (1, DEFAULT, 'newuser@example.com');
Setting default values for TIMESTAMP and DATETIME in MySQL:
CREATE TABLE events ( event_id INT PRIMARY KEY, event_name VARCHAR(100) DEFAULT 'Unnamed Event', event_date DATETIME DEFAULT CURRENT_TIMESTAMP );
MySQL DEFAULT for auto-incremented columns:
CREATE TABLE messages ( message_id INT PRIMARY KEY AUTO_INCREMENT, sender_id INT, message_text VARCHAR(255) DEFAULT 'No message' );
Setting default values for ENUM and SET types in MySQL:
CREATE TABLE survey_responses ( response_id INT PRIMARY KEY, user_id INT, rating ENUM('Poor', 'Average', 'Excellent') DEFAULT 'Average' );
MySQL DEFAULT and foreign key constraints:
CREATE TABLE invoices ( invoice_id INT PRIMARY KEY, customer_id INT, total_amount DECIMAL(10, 2) DEFAULT 0.00, FOREIGN KEY (customer_id) REFERENCES customers(customer_id) );
Handling DEFAULT values in UPDATE statements in MySQL:
UPDATE your_table SET column_name = DEFAULT WHERE your_condition;