MySQL 字段默認(rèn)值該如何設(shè)置
在 MySQL 中,我們可以為表字段設(shè)置默認(rèn)值,在表中插入一條新記錄時,如果沒有為某個字段賦值,系統(tǒng)就會自動為這個字段插入默認(rèn)值。關(guān)于默認(rèn)值,有些知識還是需要了解的,本篇文章我們一起來學(xué)習(xí)下字段默認(rèn)值相關(guān)知識。
1.默認(rèn)值相關(guān)操作我們可以用 DEFAULT 關(guān)鍵字來定義默認(rèn)值,默認(rèn)值通常用在非空列,這樣能夠防止數(shù)據(jù)表在錄入數(shù)據(jù)時出現(xiàn)錯誤。
創(chuàng)建表時,我們可以給某個列設(shè)置默認(rèn)值,具體語法格式如下:
# 格式模板<字段名> <數(shù)據(jù)類型> DEFAULT <默認(rèn)值># 示例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)
通過以上實驗可以看出,當(dāng)該字段設(shè)置默認(rèn)值后,插入數(shù)據(jù)時,若不指定該字段的值,則以默認(rèn)值處理。
關(guān)于默認(rèn)值,還有其他操作,例如修改默認(rèn)值,增加默認(rèn)值,刪除默認(rèn)值等。一起來看下這些應(yīng)該如何操作。
# 添加新字段 并設(shè)置默認(rèn)值alter table `test_tb` add column `col3` varchar(20) not null DEFAULT ’abc’;# 修改原有默認(rèn)值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’;# 刪除原有默認(rèn)值alter table `test_tb` alter column `col3` drop default;# 增加默認(rèn)值(和修改類似)alter table `test_tb` alter column `col3` set default ’3aa’; 2.幾點使用建議
其實不止非空字段可以設(shè)置默認(rèn)值,普通字段也可以設(shè)置默認(rèn)值,不過一般推薦字段設(shè)為非空。
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: 0mysql> 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)
在項目開發(fā)中,有些默認(rèn)值字段還是經(jīng)常使用的,比如默認(rèn)為當(dāng)前時間、默認(rèn)未刪除、某狀態(tài)值默認(rèn)為 1 等等。簡單通過下表展示下常用的一些默認(rèn)值字段。
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 ’創(chuàng)建時間’, `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ’修改時間’, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
這里也要提醒下,默認(rèn)值一定要和字段類型匹配,比如說某個字段表示狀態(tài)值,可能取值 1、2、3... 那這個字段推薦使用 tinyint 類型,而不應(yīng)該使用 char 或 varchar 類型。
筆者結(jié)合個人經(jīng)驗,總結(jié)下關(guān)于默認(rèn)值使用的幾點建議:
非空字段設(shè)置默認(rèn)值可以預(yù)防插入報錯。
默認(rèn)值同樣可設(shè)置在可為 null 字段。
一些狀態(tài)值字段最好給出備注,標(biāo)明某個數(shù)值代表什么狀態(tài)。
默認(rèn)值要和字段類型匹配。
總結(jié):本篇文章主要講述 MySQL 字段默認(rèn)值相關(guān)知識,比較簡單易懂,希望各位有所收獲。
以上就是MySQL 字段默認(rèn)值該如何設(shè)置的詳細(xì)內(nèi)容,更多關(guān)于MySQL 字段默認(rèn)值的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. DB2比較常用與實用sql語句總結(jié)2. db2v8的pdf文檔資料3. MySQL分支選擇參考:Percona還是MariaDB4. short int、long、float、double使用問題說明5. MySQL數(shù)據(jù)庫基礎(chǔ)學(xué)習(xí)之JSON函數(shù)各類操作詳解6. 恢復(fù)從 Access 2000、 Access 2002 或 Access 2003 中數(shù)據(jù)庫刪除表的方法7. db2 導(dǎo)入導(dǎo)出單個表的操作詳解8. MySQL導(dǎo)入sql文件的三種方法小結(jié)9. 關(guān)于MariaDB安裝問題小記(CMake Error at)10. Oracle中分割字符串的方法實例代碼
