MySQL建表設置默認值的取值范圍
mysql建表設置默認值取值范圍
設置默認值
設置默認值采用default,如代碼所示:
設置取值范圍
設置取值范圍采用check,如代碼所示:
create table student( id int not null primary key auto_increment, name varchar(32) not null, sex varchar(8) default('男'), #設置默認值 age int not null check (age between 10 and 50), #設置取值范圍 class_id int not null references class(id), stu_num varchar(32), constraint stu_u unique(stu_num) );
mysql字段默認值設置
在 MySQL 中,我們可以為表字段設置默認值,在表中插入一條新記錄時,如果沒有為某個字段賦值,系統(tǒng)就會自動為這個字段插入默認值。
關于默認值,有些知識還是需要了解的,我們一起來學習下字段默認值相關知識。
默認值相關操作
我們可以用 DEFAULT 關鍵字來定義默認值,默認值通常用在非空列,這樣能夠防止數據表在錄入數據時出現錯誤。
創(chuàng)建表時,我們可以給某個列設置默認值,具體語法格式如下:
# 格式模板 <字段名> <數據類型> 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)
通過以上實驗可以看出,當該字段設置默認值后,插入數據時,若不指定該字段的值,則以默認值處理。
關于默認值,還有其他操作,例如修改默認值,增加默認值,刪除默認值等。一起來看下這些應該如何操作。
# 添加新字段 并設置默認值 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';
幾點使用建議
其實不止非空字段可以設置默認值,普通字段也可以設置默認值,不過一般推薦字段設為非空。
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)
在項目開發(fā)中,有些默認值字段還是經常使用的,比如默認為當前時間、默認未刪除、某狀態(tài)值默認為 1 等等。
簡單通過下表展示下常用的一些默認值字段。
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;
這里也要提醒下,默認值一定要和字段類型匹配,比如說某個字段表示狀態(tài)值,可能取值 1、2、3… 那這個字段推薦使用 tinyint 類型,而不應該使用 char 或 varchar 類型。
筆者結合個人經驗,總結下關于默認值使用的幾點建議:
- 非空字段設置默認值可以預防插入報錯。
- 默認值同樣可設置在可為 null 字段。
- 一些狀態(tài)值字段最好給出備注,標明某個數值代表什么狀態(tài)。
- 默認值要和字段類型匹配。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Mysql中between...and引起的索引失效問題及解決
這篇文章主要介紹了Mysql中between...and引起的索引失效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Linux下MYSQL 5.7 找回root密碼的問題(親測可用)
這篇文章主要介紹了Linux下MYSQL 5.7 找回root密碼的問題(親測可用),通過 --skip-grant-tables 找回,新增完成后,:wq 保存退出,重啟mysqld服務,具體內容詳情跟隨小編一起看看吧2021-10-10MYSQL ERROR 1045 (28000): Access denied for user (using pass
Mysql中添加用戶之后可能出現登錄時提示ERROR 1045 (28000): Access denied for user的錯誤.2009-07-07windows下mysql 8.0.16 安裝配置方法圖文教程
這篇文章主要為大家詳細介紹了windows下mysql 8.0.16 安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05