赞
踩
修改表的结构操作,有下面几种。1、修改列名
2、修改表的名字
3、修改列的类型
4、增加某列
5、删除某列相似的语法:alter table TableName xxx(操作)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> desc MyTable;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| stuname | varchar(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
1 row in set (0.01 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> alter table MyTable change stuname sname varchar(10);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> desc MyTable;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sname | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.01 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
修改表的名字的方法有两种:
1、ALTER TABLE TABLENAME RENAME NEWTABLENAME;
2、RENAME TABLE TABLENAME TO NEWTABLENAME;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| FirTable |
| FirTable_COPY |
| FirTable_COPY2 |
| FirstTable |
| MT |
+----------------+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> alter table FirTable_COPY rename FC;
Query OK, 0 rows affected (2.22 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| FC |
| FirTable |
| FirTable_COPY2 |
| FirstTable |
| MT |
+----------------+
5 rows in set (0.00 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> rename table FC to FCS;
Query OK, 0 rows affected (0.04 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| FCS |
| FirTable |
| FirTable_COPY2 |
| FirstTable |
| MT |
+----------------+
5 rows in set (0.00 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ALTER TABLE TABLENAME MODIFY 列名 想改的类型
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> desc FCS;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sno | int(11) | YES | | NULL | |
| sname | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> ALTER TABLE FCS MODIFY sno varchar(13);
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> desc FCS;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sno | varchar(13) | YES | | NULL | |
| sname | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.03 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> desc MyTable;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| stuname | varchar(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
1 row in set (0.01 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> alter table MyTable add stuNum long;
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> desc MyTable;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| stuname | varchar(10) | YES | | NULL | |
| stuNum | mediumtext | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> alter table MyTable add stuNum long;
ERROR 1060 (42S21): Duplicate column name 'stuNum'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> alter table MyTable add stuName varchar(10), stuCount varchar(16);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'stuCount varchar(16)' at line 1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> alter table INNODB_FT_CONFIG add count int;
ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'information_schema'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> desc MyTable;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| stuname | varchar(10) | YES | | NULL | |
| stuNum | mediumtext | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> ALTER TABLE MyTable DROP stuNum;
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mysql> desc MyTable;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| stuname | varchar(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
1 row in set (0.01 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。