MySQL敕令行中給表添加一個字段(字段名、能否為空、默許值)。本站提示廣大學習愛好者:(MySQL敕令行中給表添加一個字段(字段名、能否為空、默許值))文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL敕令行中給表添加一個字段(字段名、能否為空、默許值)正文
先看一下最簡略的例子,在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字段,就有一個默許值了,並且這個默許值是我們手工指定的。
關於MySQL中給表添加一個字段,本文就引見這麼多,願望對年夜家有所贊助,感謝!