偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

MySQL字段默認(rèn)值設(shè)置詳解

數(shù)據(jù)庫 MySQL
在 MySQL 中,我們可以為表字段設(shè)置默認(rèn)值,在表中插入一條新記錄時(shí),如果沒有為某個(gè)字段賦值,系統(tǒng)就會(huì)自動(dòng)為這個(gè)字段插入默認(rèn)值。關(guān)于默認(rèn)值,有些知識(shí)還是需要了解的,本篇文章我們一起來學(xué)習(xí)下字段默認(rèn)值相關(guān)知識(shí)。

 前言:

在 MySQL 中,我們可以為表字段設(shè)置默認(rèn)值,在表中插入一條新記錄時(shí),如果沒有為某個(gè)字段賦值,系統(tǒng)就會(huì)自動(dòng)為這個(gè)字段插入默認(rèn)值。關(guān)于默認(rèn)值,有些知識(shí)還是需要了解的,本篇文章我們一起來學(xué)習(xí)下字段默認(rèn)值相關(guān)知識(shí)。

[[383574]]

1.默認(rèn)值相關(guān)操作

我們可以用 DEFAULT 關(guān)鍵字來定義默認(rèn)值,默認(rèn)值通常用在非空列,這樣能夠防止數(shù)據(jù)表在錄入數(shù)據(jù)時(shí)出現(xiàn)錯(cuò)誤。

創(chuàng)建表時(shí),我們可以給某個(gè)列設(shè)置默認(rèn)值,具體語法格式如下:

 

  1. # 格式模板 
  2. <字段名> <數(shù)據(jù)類型> DEFAULT <默認(rèn)值> 
  3.  
  4. # 示例 
  5. mysql> CREATE TABLE `test_tb` ( 
  6.     ->   `id` int NOT NULL AUTO_INCREMENT, 
  7.     ->   `col1` varchar(50) not null DEFAULT 'a'
  8.     ->   `col2` int not null DEFAULT 1, 
  9.     ->   PRIMARY KEY (`id`) 
  10.     -> ) ENGINE=InnoDB  DEFAULT CHARSET=utf8; 
  11. Query OK, 0 rows affected (0.06 sec) 
  12.  
  13. mysql> desc test_tb; 
  14. +-------+-------------+------+-----+---------+----------------+ 
  15. | Field | Type        | Null | Key | Default | Extra          | 
  16. +-------+-------------+------+-----+---------+----------------+ 
  17. | id    | int(11)     | NO   | PRI | NULL    | auto_increment | 
  18. | col1  | varchar(50) | NO   |     | a       |                | 
  19. | col2  | int(11)     | NO   |     | 1       |                | 
  20. +-------+-------------+------+-----+---------+----------------+ 
  21. rows in set (0.00 sec) 
  22.  
  23. mysql> insert into test_tb (col1) values ('fdg'); 
  24. Query OK, 1 row affected (0.01 sec) 
  25.  
  26. mysql> insert into test_tb (col2) values (2); 
  27. Query OK, 1 row affected (0.03 sec) 
  28.  
  29. mysql> select * from test_tb; 
  30. +----+------+------+ 
  31. | id | col1 | col2 | 
  32. +----+------+------+ 
  33. |  1 | fdg  |    1 | 
  34. |  2 | a    |    2 | 
  35. +----+------+------+ 
  36. rows in set (0.00 sec) 

通過以上實(shí)驗(yàn)可以看出,當(dāng)該字段設(shè)置默認(rèn)值后,插入數(shù)據(jù)時(shí),若不指定該字段的值,則以默認(rèn)值處理。

關(guān)于默認(rèn)值,還有其他操作,例如修改默認(rèn)值,增加默認(rèn)值,刪除默認(rèn)值等。一起來看下這些應(yīng)該如何操作。

 

  1. # 添加新字段 并設(shè)置默認(rèn)值 
  2. alter table `test_tb` add column `col3` varchar(20) not null DEFAULT 'abc'
  3.  
  4. # 修改原有默認(rèn)值 
  5. alter table `test_tb` alter column `col3` set default '3a'
  6. alter table `test_tb` change column `col3` `col3` varchar(20) not null DEFAULT '3b'
  7. alter table `test_tb` MODIFY column `col3` varchar(20) not null DEFAULT '3c'
  8.  
  9. # 刪除原有默認(rèn)值 
  10. alter table `test_tb` alter column `col3` drop default
  11.  
  12. # 增加默認(rèn)值(和修改類似) 
  13. alter table `test_tb` alter column `col3` set default '3aa'

2.幾點(diǎn)使用建議

其實(shí)不止非空字段可以設(shè)置默認(rèn)值,普通字段也可以設(shè)置默認(rèn)值,不過一般推薦字段設(shè)為非空。

 

  1. mysql> alter table `test_tb` add column `col4` varchar(20) DEFAULT '4a'
  2. Query OK, 0 rows affected (0.12 sec) 
  3. Records: 0  Duplicates: 0  Warnings: 0 
  4.  
  5. mysql>  desc test_tb; 
  6. +-------+-------------+------+-----+---------+----------------+ 
  7. | Field | Type        | Null | Key | Default | Extra          | 
  8. +-------+-------------+------+-----+---------+----------------+ 
  9. | id    | int(11)     | NO   | PRI | NULL    | auto_increment | 
  10. | col1  | varchar(50) | NO   |     | a       |                | 
  11. | col2  | int(11)     | NO   |     | 1       |                | 
  12. | col3  | varchar(20) | NO   |     | 3aa     |                | 
  13. | col4  | varchar(20) | YES  |     | 4a      |                | 
  14. +-------+-------------+------+-----+---------+----------------+ 
  15. rows in set (0.00 sec) 

在項(xiàng)目開發(fā)中,有些默認(rèn)值字段還是經(jīng)常使用的,比如默認(rèn)為當(dāng)前時(shí)間、默認(rèn)未刪除、某狀態(tài)值默認(rèn)為 1 等等。簡(jiǎn)單通過下表展示下常用的一些默認(rèn)值字段。

 

  1. CREATE TABLE `default_tb` ( 
  2.   `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主鍵'
  3.   ... 
  4.   `country` varchar(50) not null DEFAULT '中國'
  5.   `col_status` tinyint not null DEFAULT 1 COMMENT '1:代表啥 2:代表啥...'
  6.   `col_time` datetime NOT NULL DEFAULT '2020-10-01 00:00:00' COMMENT '什么時(shí)間'
  7.   `is_deleted` tinyint not null DEFAULT 0 COMMENT '0:未刪除 1:刪除'
  8.   `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時(shí)間'
  9.   `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時(shí)間'
  10.   PRIMARY KEY (`id`) 
  11. ) ENGINE=InnoDB  DEFAULT CHARSET=utf8; 

這里也要提醒下,默認(rèn)值一定要和字段類型匹配,比如說某個(gè)字段表示狀態(tài)值,可能取值 1、2、3... 那這個(gè)字段推薦使用 tinyint 類型,而不應(yīng)該使用 char 或 varchar 類型。

筆者結(jié)合個(gè)人經(jīng)驗(yàn),總結(jié)下關(guān)于默認(rèn)值使用的幾點(diǎn)建議:

  • 非空字段設(shè)置默認(rèn)值可以預(yù)防插入報(bào)錯(cuò)。
  • 默認(rèn)值同樣可設(shè)置在可為 null 字段。
  • 一些狀態(tài)值字段最好給出備注,標(biāo)明某個(gè)數(shù)值代表什么狀態(tài)。
  • 默認(rèn)值要和字段類型匹配。

總結(jié):

本篇文章主要講述 MySQL 字段默認(rèn)值相關(guān)知識(shí),比較簡(jiǎn)單易懂,希望各位有所收獲。

責(zé)任編輯:華軒 來源: MySQL技術(shù)
相關(guān)推薦

2010-09-28 10:23:36

SQL修改字段

2009-09-11 12:31:15

C# WinForm控設(shè)置默認(rèn)值

2010-11-23 16:49:42

MySQL設(shè)置當(dāng)前時(shí)間

2010-10-22 15:36:57

2010-09-28 10:35:58

SQL字段默認(rèn)值

2010-09-07 16:05:23

SQL語句刪除

2011-08-23 18:46:27

MySQLTIMESTAMP

2010-09-28 15:24:43

sql語句

2010-07-15 10:37:15

SQL Server默

2011-08-23 18:30:59

MySQLTIMESTAMP

2009-12-18 17:07:14

2022-06-21 08:13:34

MySQL查詢數(shù)據(jù)庫

2010-09-16 10:56:46

sqlserver建表

2012-08-01 09:50:11

交互設(shè)計(jì)UI設(shè)計(jì)

2009-12-24 16:03:16

ADO.NET部署

2010-10-22 16:56:35

sql server刪

2010-09-03 10:52:10

SQL刪除

2022-03-14 09:41:10

POJO類型系統(tǒng)

2021-10-11 09:32:40

包裝類型屬性

2010-10-08 14:59:00

MySql字段
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)