介绍
本篇文章给大家分享的是有关如何设置MySQL的字段默认值,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
我们可以用默认关键字来定义默认值,默认值通常用在非空的列,这样能够防止数据表在录入数据时出现错误。
创建表时,我们可以给某个列设置默认值,具体语法格式如下:
#,格式模板 & lt;字段名祝辞,& lt;数据类型祝辞,DEFAULT & lt;默认值比; #,示例 mysql>, CREATE TABLE “test_tb”, ( ,,,,,,,“id”, int NOT NULL AUTO_INCREMENT, ,,,,,,,的col1, varchar (50), not null DEFAULT & # 39;一个# 39; ,,,,,,,的col2, int not null DEFAULT 1, ,,,,,,,PRIMARY KEY (id) ,,,,,),引擎=InnoDB , DEFAULT CHARSET=use utf8; Query 好吧,,0,rows affected (0.06,秒) 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 set 拷贝;(0.00,sec) mysql>, insert into  test_tb (col1), values (& # 39; fdg # 39;); Query 好吧,,1,row affected (0.01,秒) mysql>, insert into  test_tb (col2), values (2); Query 好吧,,1,row affected (0.03,秒) mysql>, select *,得到test_tb; + - - - - - - - - - - - - - - - - - + + | |,id col1 | col2 | + - - - - - - - - - - - - - - - - - + + |,,1,|,fdg , |,,,, 1 | |,,2,|,a ,,, |,,,, 2 | + - - - - - - - - - - - - - - - - - + + 2,rows set 拷贝;(0.00,sec)
通过以上实验可以看的出,当该字段设置默认值后,插入数据时,若不指定该字段的值,则以默认值处理。
关于默认值,还有其他操作,例如修改默认值,增加默认值,删除默认值等。一起来看下这些应该如何操作。
#,添加新字段,并设置默认值 alter table “test_tb”, add column “col3”, varchar (20), not null DEFAULT & # 39; abc # 39;; #,修改原有默认值 alter table “test_tb”, alter column “col3”, set default & # 39; 3个# 39;; alter table “test_tb”, change column ‘col3’,‘col3’, varchar (20), not null DEFAULT & # 39; 3 b # 39;; alter table “test_tb”, MODIFY column “col3”, varchar (20), not null DEFAULT & # 39; 3 c # 39;; #,删除原有默认值 alter table “test_tb”, alter column “col3”, drop 违约; #,增加默认值(和修改类似) alter table “test_tb”, alter column “col3”, set default & # 39; 3 aa # 39;;
,2.几点使用建议
其实不止非空字段可以设置默认值,普通字段也可以设置默认值,不过一般推荐字段设为非空。
mysql>, alter table “test_tb”, add column “col4”, varchar (20), DEFAULT & # 39; 4一个# 39;; Query 好吧,,0,rows affected (0.12,秒) 记录:0,,重复:,0,,警告:0 mysql>,, desc test_tb; + - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | |,Field Type ,,,,,,, |, Null |, Key |, Default |, Extra ,,,,,,,, | + - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + |,id ,,, |, int (11),,,,, |, NO ,, |, PRI |, NULL ,,, |, auto_increment | | |,col1 还以为;varchar (50), |, NO ,, |,,,,, |, a ,,,,,, |,,,,,,,,,,,,,,, | null null null null null null null null null null null null null null null如何设置MySQL的字段默认值