MySql表結構修改詳解

來源:互聯網
上載者:User

標籤:test   add   int   結構   nod   end   esc   ==   pid   


修改表的文法
=========================
增加列[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表結構修改詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.