MySQL字段默認值設置詳解

前言:mysql

在 MySQL 中,咱們能夠爲表字段設置默認值,在表中插入一條新記錄時,若是沒有爲某個字段賦值,系統就會自動爲這個字段插入默認值。關於默認值,有些知識仍是須要了解的,本篇文章咱們一塊兒來學習下字段默認值相關知識。sql

1.默認值相關操做

咱們能夠用 DEFAULT 關鍵字來定義默認值,默認值一般用在非空列,這樣可以防止數據表在錄入數據時出現錯誤。ide

建立表時,咱們能夠給某個列設置默認值,具體語法格式以下:學習

# 格式模板
<字段名> <數據類型> DEFAULT <默認值>

# 示例
mysql> CREATE TABLE `test_tb` (
    ->   `id` int NOT NULL AUTO_INCREMENT,
    ->   `col1` varchar(50) not null DEFAULT 'a',
    ->   `col2` int not null DEFAULT 1,
    ->   PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.06 sec)

mysql> desc test_tb;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| col1  | varchar(50) | NO   |     | a       |                |
| col2  | int(11)     | NO   |     | 1       |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> insert into test_tb (col1) values ('fdg');
Query OK, 1 row affected (0.01 sec)

mysql> insert into test_tb (col2) values (2);
Query OK, 1 row affected (0.03 sec)

mysql> select * from test_tb;
+----+------+------+
| id | col1 | col2 |
+----+------+------+
|  1 | fdg  |    1 |
|  2 | a    |    2 |
+----+------+------+
2 rows in set (0.00 sec)

經過以上實驗能夠看出,當該字段設置默認值後,插入數據時,若不指定該字段的值,則以默認值處理。code

關於默認值,還有其餘操做,例如修改默認值,增長默認值,刪除默認值等。一塊兒來看下這些應該如何操做。開發

# 添加新字段 並設置默認值
alter table `test_tb` add column `col3` varchar(20) not null DEFAULT 'abc';

# 修改原有默認值
alter table `test_tb` alter column `col3` set default '3a';
alter table `test_tb` change column `col3` `col3` varchar(20) not null DEFAULT '3b';
alter table `test_tb` MODIFY column `col3` varchar(20) not null DEFAULT '3c';

# 刪除原有默認值
alter table `test_tb` alter column `col3` drop default;

# 增長默認值(和修改相似)
alter table `test_tb` alter column `col3` set default '3aa';

2.幾點使用建議

其實不止非空字段能夠設置默認值,普通字段也能夠設置默認值,不過通常推薦字段設爲非空。rem

mysql> alter table `test_tb` add column `col4` varchar(20) DEFAULT '4a';
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>  desc test_tb;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| col1  | varchar(50) | NO   |     | a       |                |
| col2  | int(11)     | NO   |     | 1       |                |
| col3  | varchar(20) | NO   |     | 3aa     |                |
| col4  | varchar(20) | YES  |     | 4a      |                |
+-------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

在項目開發中,有些默認值字段仍是常常使用的,好比默認爲當前時間、默認未刪除、某狀態值默認爲 1 等等。簡單經過下表展現下經常使用的一些默認值字段。it

CREATE TABLE `default_tb` (
  `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主鍵',
  ...
  `country` varchar(50) not null DEFAULT '中國',
  `col_status` tinyint not null DEFAULT 1 COMMENT '1:表明啥 2:表明啥...',
  `col_time` datetime NOT NULL DEFAULT '2020-10-01 00:00:00' COMMENT '什麼時間',
  `is_deleted` tinyint not null DEFAULT 0 COMMENT '0:未刪除 1:刪除',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

這裏也要提醒下,默認值必定要和字段類型匹配,好比說某個字段表示狀態值,可能取值 一、二、3... 那這個字段推薦使用 tinyint 類型,而不該該使用 char 或 varchar 類型。table

筆者結合我的經驗,總結下關於默認值使用的幾點建議:模板

  • 非空字段設置默認值能夠預防插入報錯。
  • 默認值一樣可設置在可爲 null 字段。
  • 一些狀態值字段最好給出備註,標明某個數值表明什麼狀態。
  • 默認值要和字段類型匹配。

總結:

本篇文章主要講述 MySQL 字段默認值相關知識,比較簡單易懂,但願各位有所收穫。

相關文章
相關標籤/搜索