您好,欢迎访问一九零五行业门户网

mysql修改表

欢迎进入linux社区论坛,与200万技术人员互动交流 >>进入 修改表结构: alter [ignore] table table specification table structure change 包括表属性改变,列属性改变,完整性约束改变。 1.表属性改变 1.1重命名表名 mysql alter table student rename to
欢迎进入linux社区论坛,与200万技术人员互动交流 >>进入
修改表结构:
alter [ignore] table
包括表属性改变,列属性改变,完整性约束改变。
1.表属性改变
1.1重命名表名
mysql> alter table student rename to s;
query ok, 0 rows affected (0.03 sec)
也可以直接使用rename命令改名:
mysql> rename table s to student;
query ok, 0 rows affected (0.13 sec)
1.2表排序改变
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 添加列
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删除列
mysql> alter table student
-> drop sex;
query ok, 4 rows affected (0.33 sec)
records: 4  duplicates: 0  warnings: 0
2.3修改列属性
初始列属性:
+-------------+-----------+
| column_name | data_type |
+-------------+-----------+
| stu_id      | int       |
| stu_name    | varchar   |
| stu_tel     | int       |
| stu_score   | int       |
+-------------+-----------+
将stu_tel列修改为tel char型,并放在stu_score后面。
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      |
+-------------+-----------+
[1] [2]
其它类似信息

推荐信息