在MySQL中,DEFAULT
关键字用于为列指定一个默认值,当插入一条记录时,如果没有为该列提供值,则会使用该默认值。DEFAULT
可以用于不同的数据类型,包括数值类型、字符串类型、日期和时间类型等。
CREATE TABLE table_name (
column_name data_type DEFAULT default_value
);
TRUE
或 FALSE
作为默认值。以下是一些示例代码,展示了如何使用 DEFAULT
指定默认值创建表,并插入和检索数据。
创建一个包含数值类型列的表:
CREATE DATABASE test_default_db;
USE test_default_db;
CREATE TABLE employees (
emp_id INT AUTO_INCREMENT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL,
emp_age INT DEFAULT 30, -- 默认年龄为30
emp_salary DECIMAL(10, 2) DEFAULT 5000.00 -- 默认工资为5000.00
);
插入数据:
-- 插入数据,不提供默认值的列
INSERT INTO employees (emp_name) VALUES
('Alice'),
('Bob');
检索数据:
SELECT emp_id, emp_name, emp_age, emp_salary FROM employees;
检索结果:
+--------+----------+---------+------------+
| emp_id | emp_name | emp_age | emp_salary |
+--------+----------+---------+------------+
| 1 | Alice | 30 | 5000.00 |
| 2 | Bob | 30 | 5000.00 |
+--------+----------+---------+------------+
创建一个包含字符串类型列的表:
CREATE TABLE products (
product_id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(100) NOT NULL,
product_description TEXT DEFAULT 'No description available' -- 默认描述为 'No description available'
);
插入数据:
-- 插入数据,不提供默认值的列
INSERT INTO products (product_name) VALUES
('Laptop'),
('Smartphone');
检索数据:
SELECT product_id, product_name, product_description FROM products;
检索结果:
+-----------+-------------+---------------------------+
| product_id| product_name| product_description |
+-----------+-------------+---------------------------+
| 1 | Laptop | No description available |
| 2 | Smartphone | No description available |
+-----------+-------------+---------------------------+
创建一个包含日期和时间类型列的表:
CREATE TABLE orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
customer_id INT NOT NULL,
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- 默认时间为当前时间
delivery_date DATE DEFAULT '2023-12-31' -- 默认交货日期为2023-12-31
);
插入数据:
-- 插入数据,不提供默认值的列
INSERT INTO orders (customer_id) VALUES
(1),
(2);
检索数据:
SELECT order_id, customer_id, order_date, delivery_date FROM orders;
检索结果:
+---------+-------------+---------------------+--------------+
| order_id| customer_id | order_date | delivery_date|
+---------+-------------+---------------------+--------------+
| 1 | 1 | 2023-10-05 16:30:00 | 2023-12-31 |
| 2 | 2 | 2023-10-05 16:35:00 | 2023-12-31 |
+---------+-------------+---------------------+--------------+
创建一个包含布尔类型列的表:
CREATE TABLE tasks (
task_id INT AUTO_INCREMENT PRIMARY KEY,
task_name VARCHAR(100) NOT NULL,
is_completed BOOLEAN DEFAULT FALSE -- 默认未完成
);
插入数据:
-- 插入数据,不提供默认值的列
INSERT INTO tasks (task_name) VALUES
('Write documentation'),
('Fix bug');
检索数据:
SELECT task_id, task_name, is_completed FROM tasks;
检索结果:
+--------+--------------------+--------------+
| task_id| task_name | is_completed |
+--------+--------------------+--------------+
| 1 | Write documentation| 0 |
| 2 | Fix bug | 0 |
+--------+--------------------+--------------+
以下是一个更复杂的示例,展示了如何在用户管理系统中使用 DEFAULT
。
CREATE TABLE users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
signup_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- 默认注册时间为当前时间
status ENUM('active', 'inactive', 'pending') DEFAULT 'pending', -- 默认状态为 pending
login_attempts INT DEFAULT 0 -- 默认登录尝试次数为0
);
-- 插入用户数据,不提供默认值的列
INSERT INTO users (username, password) VALUES
('john_doe', 'password123'),
('jane_smith', 'securepassword');
-- 检索用户数据
SELECT user_id, username, signup_date, status, login_attempts FROM users;
+---------+-------------+---------------------+----------+----------------+
| user_id | username | signup_date | status | login_attempts |
+---------+-------------+---------------------+----------+----------------+
| 1 | john_doe | 2023-10-05 16:40:00 | pending | 0 |
| 2 | jane_smith | 2023-10-05 16:45:00 | pending | 0 |
+---------+-------------+---------------------+----------+----------------+
ENUM
)的可选值范围内。NULL
,这可能会导致数据完整性问题,因此建议明确指定默认值。DEFAULT
关键字:用于为列指定默认值,当插入记录时如果没有为该列提供值,则使用该默认值。通过理解 DEFAULT
关键字的用途和特点,可以更好地设计和优化数据库表,确保数据插入操作符合业务需求。上述示例展示了如何在实际应用中使用 DEFAULT
关键字设计数据库表,并插入和检索数据。