萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> mysql修改表

mysql修改表

   修改表結構:
?

1 alter [ignore] table <table specification> <table structure change>

   包括表屬性改變,列屬性改變,完整性約束改變。

 

  1.表屬性改變

  1.1重命名表名

? 1 2 mysql> alter table student rename to s; Query OK, 0 rows affected (0.03 sec)

  也可以直接使用rename命令改名:

?

1 2 mysql> rename table s to student; Query OK, 0 rows affected (0.13 sec) 

  1.2表排序改變

?

1 2 3 4 5 6 7 8 9 10 mysql> alter table student order by stu_id desc; mysql> select * from student; +--------+----------+---------+-----------+ | stu_id | stu_name | stu_tel | stu_score | +--------+----------+---------+-----------+ |      4 | d        |     154 |        63 | |      3 | c        |     153 |        62 | |      2 | b        |     152 |        61 | |      1 | a        |     151 |        60 | +--------+----------+---------+-----------+

  2列屬性改變

  2.1 添加列

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 mysql> alter table student     -> add sex char(1) after stu_name; Query OK, 4 rows affected (0.34 sec) Records: 4  Duplicates: 0  Warnings: 0 mysql> select * from student; +--------+----------+------+---------+-----------+ | stu_id | stu_name | sex  | stu_tel | stu_score | +--------+----------+------+---------+-----------+ |      1 | a        | NULL |     151 |        60 | |      2 | b        | NULL |     152 |        61 | |      3 | c        | NULL |     153 |        62 | |      4 | d        | NULL |     154 |        63 | +--------+----------+------+---------+-----------+ 4 rows in set (0.02 sec)

  新添加的列默認放在最後一列,且默認填充空值。這裡使用after指定了新增列sex放在stu_name後面.如果新增列設置不能為空,那麼mysql將根據列的數據類型填入實際的值:對於數值填入0,對於字符串填入空字符串,對於日期填入0000-00-00,對於時間填入00:00:00.

  2.2刪除列

?

1 2 3 4 mysql> alter table student     -> drop sex; Query OK, 4 rows affected (0.33 sec) Records: 4  Duplicates: 0  Warnings: 0

  2.3修改列屬性

  初始列屬性:

?

1 2 3 4 5 6 7 8 +-------------+-----------+ | column_name | data_type | +-------------+-----------+ | stu_id      | int       | | stu_name    | varchar   | | stu_tel     | int       | | stu_score   | int       | +-------------+-----------+

  將stu_tel列修改為tel char型,並放在stu_score後面。

?

1 2 3 4 5 6 7 8 9 10 11 mysql> alter table student     -> change stu_tel tel char(3) after stu_score;; Query OK, 4 rows affected (0.23 sec) +-------------+-----------+ | column_name | data_type | +-------------+-----------+ | stu_id      | int       | | stu_name    | varchar   | | stu_score   | int       | | tel         | char      | +-------------+-----------+

  如果僅僅是更改列其他屬性而不改列名時,可以使用modify參數。

  將tel列改回int型

?

1 2 3 4 5 6 7 8 9 10 11 12 mysql> alter table student     -> modify tel int(3); Query OK, 4 rows affected (0.25 sec) Records: 4  Duplicates: 0  Warnings: 0 +-------------+-----------+ | column_name | data_type | +-------------+-----------+ | stu_id      | int       | | stu_name    | varchar   | | stu_score   | int       | | tel         | int       | +-------------+-----------+

  3.完整性約束改變

?

1 2 3 4 5 6 7 8 <integrity constraint change>:= add primary key<index
copyright © 萬盛學電腦網 all rights reserved