[mysql新增空格字段]MySQL中给表添加一个字段(字段名、是否为空、默认值)

责任编辑如是说MySQL资料库采用SQL句子来在原有的附注,加进两个捷伊表头,主要包括增设表头的与否为空、缺省等。

先看呵呵最简单的范例,在test中,加进两个表头,表头名为birth,类别为date类别。

mysql> alter table test add column birth date;

Query OK, 0 rows affected (0.36 sec)

Records: 0? Duplicates: 0? Warnings: 0

查阅呵呵统计数据,看一看结论:

mysql> select * from test;

+------+--------+----------------------------------+------------+-------+

| t_id | t_name | t_password | t_birth? | birth |

+------+--------+----------------------------------+------------+-------+

|? 1 | name1? | 12345678901234567890123456789012 | NULL | NULL? |

|? 2 | name2? | 12345678901234567890123456789012 | 2013-01-01 | NULL? |

+------+--------+----------------------------------+------------+-------+

2 rows in set (0.00 sec)

从上面结论可以看出,插入的birth表头,缺省为空。我们再来试呵呵,加进两个birth1表头,增设它不允许为空。

mysql> alter table test add column birth1 date not null;

Query OK, 0 rows affected (0.16 sec)

Records: 0? Duplicates: 0? Warnings: 0

居然执行成功了!?意外了!我原来以为,这个句子不会成功的,因为我没有给他指定两个缺省。我们来看一看统计数据:

mysql> select * from test;

+------+--------+----------------------------------+------------+-------+------------+

| t_id | t_name | t_password | t_birth? | birth | birth1 |

+------+--------+----------------------------------+------------+-------+------------+

|? 1 | name1? | 12345678901234567890123456789012 | NULL | NULL? | 0000-00-00 |

|? 2 | name2? | 12345678901234567890123456789012 | 2013-01-01 | NULL? | 0000-00-00 |

+------+--------+----------------------------------+------------+-------+------------+

2 rows in set (0.00 sec)

哦,明白了,系统自动将date类别的值,增设了两个缺省:0000-00-00。下面我来直接指定两个缺省看一看:

mysql> alter table test add column birth2 date default 2013-1-1;

Query OK, 0 rows affected (0.28 sec)

Records: 0? Duplicates: 0? Warnings: 0

mysql> select * from test;

+------+--------+----------------------------------+------------+-------+------------+------------+

| t_id | t_name | t_password | t_birth? | birth | birth1 | birth2 |

+------+--------+----------------------------------+------------+-------+------------+------------+

|? 1 | name1? | 12345678901234567890123456789012 | NULL | NULL? | 0000-00-00 | 2013-01-01 |

|? 2 | name2? | 12345678901234567890123456789012 | 2013-01-01 | NULL? | 0000-00-00 | 2013-01-01 |

+------+--------+----------------------------------+------------+-------+------------+------------+

2 rows in set (0.00 sec)

看到没,将增加的birth2表头,就有两个缺省了,而且这个缺省是我们手工指定的。

发布于 2022-09-10 22:09:58
收藏
分享
海报
119
上一篇:[mysql新增空格字段]MySQL-2-函数和约束(结合案例学习) 下一篇:[mysql新增空格字段]mysql去掉字段空格(亲测可用)
目录