MySQL表結構修改詳解

來源:互聯網
上載者:User

MySQL表結構修改詳解

MySQL修改表的文法
=========================
增加列[add 列名]
=========================
①alter table 表名 add 列名 列類型 列參數【加的列在表的最後面】
    例:alter table test add username char(20) not null default '';
        alter table test add birth date not null default '0000-00-00';

②alter table 表名 add 列名 列類型 列參數 after 某列【把新列加在某列後面】
    例:alter table test add gender char(1) not null default '' after username;

③alter table 表名 add 列名 列類型 列參數 first【把新列加在最前面】
    例:alter table test add pid int not null default 0 first;

=========================
刪除列[drop 列名]
=========================
①alter table 表名 drop 列名
    例:alter table test drop pid;

=========================
修改列[modife 列名]
=========================
①alter table 表名 modify 列名 新類型 新參數【修改列類型】
    例:alter table test modify gender char(4) not null default '';
②alter table 表名 change 舊列名 新列名 新類型 新參數【修改列名和列類型】
    例:alter table test change pid uid int unsigned not null default 0;

=========================
查詢列
=========================
①desc 表名【查詢所有列】
    例: desc test;
mysql> desc department;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| dId  | int(11)    | NO  | PRI |        |      |
| dName | varchar(32) | YES  |    | NULL    |      |
+-------+-------------+------+-----+---------+-------+

②show columns from 表名【效果和desc一樣】
mysql> show columns from department;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| dId  | int(11)    | NO  | PRI |        |      |
| dName | varchar(32) | YES  |    | NULL    |      |
+-------+-------------+------+-----+---------+-------+

③show create table 表名【查看錶的建立代碼】
mysql> show create table department;
CREATE TABLE `department` (
  `dId` int(11) NOT NULL,
  `dName` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`dId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

如何避免 MySQL 修改表結構時導致表無法使用的問題 

--------------------------------------分割線 --------------------------------------

Ubuntu 14.04下安裝MySQL

《MySQL權威指南(原書第2版)》清晰中文掃描版 PDF

Ubuntu 14.04 LTS 安裝 LNMP Nginx\PHP5 (PHP-FPM)\MySQL

Ubuntu 14.04下搭建MySQL主從伺服器

Ubuntu 12.04 LTS 構建高可用分布式 MySQL 叢集

Ubuntu 12.04下原始碼安裝MySQL5.6以及Python-MySQLdb

MySQL-5.5.38通用二進位安裝

--------------------------------------分割線 --------------------------------------

本文永久更新連結地址:

相關文章

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.