MySQL命令列中給表添加一個欄位(欄位名、是否為空白、預設值)_Mysql

來源:互聯網
上載者:User

先看一下最簡單的例子,在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中給表添加一個欄位,本文就介紹這麼多,希望對大家有所協助,謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.