国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

MySQL命令行中給表添加一個字段(字段名、是否為空、默認(rèn)值)

2020-01-18 22:31:17
字體:
供稿:網(wǎng)友

先看一下最簡單的例子,在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

查詢一下數(shù)據(jù),看看結(jié)果:

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)

從上面結(jié)果可以看出,插入的birth字段,默認(rèn)值為空。我們再來試一下,添加一個birth1字段,設(shè)置它不允許為空。

mysql> alter table test add column birth1 date not null;
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

居然執(zhí)行成功了!?意外了!我原來以為,這個語句不會成功的,因為我沒有給他指定一個默認(rèn)值。我們來看看數(shù)據(jù):

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)

哦,明白了,系統(tǒng)自動將date類型的值,設(shè)置了一個默認(rèn)值:0000-00-00。下面我來直接指定一個默認(rèn)值看看:

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)

看到?jīng)],將增加的birth2字段,就有一個默認(rèn)值了,而且這個默認(rèn)值是我們手工指定的。

關(guān)于MySQL中給表添加一個字段,本文就介紹這么多,希望對大家有所幫助,謝謝!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 嘉峪关市| 穆棱市| 玛沁县| 台北市| 齐齐哈尔市| 大姚县| 青冈县| 德兴市| 天全县| 和平区| 山西省| 光泽县| 泽普县| 定兴县| 旅游| 陆川县| 临清市| 黑山县| 沾益县| 怀化市| 克什克腾旗| 沧源| 抚州市| 晴隆县| 乌恰县| 甘泉县| 台北市| 外汇| 石屏县| 铜川市| 鄢陵县| 隆尧县| 浑源县| 邵阳县| 莱西市| 田东县| 镇雄县| 青铜峡市| 临沂市| 来宾市| 穆棱市|