ALTER TABLE 解析实例:
SQL:
1.增加列
2.增加列,调整列顺序
3.增加索引
4.增加约束
5.增加全文索引FULL-TEXT
6.改变列的默认值
7.改变列名字(类型,顺序)
8.不改变列名字
9.删除列
10.删除主键
11.删除索引
12.删除约束
13.改表名
14.改变字符集
创建一张表
CREATE TABLE t1 (a INTEGER,b CHAR(10));
1. 增加列
格式:
ADD [COLUMN] (col_name column_definition,...)
例子:
ALTER TABLE t1 ADD COLUMN c TIMESTAMP;
mysql> desc t1; +-------+-----------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------+------+-----+-------------------+-----------------------------+ | a | int(11) | YES | | NULL | | | b | char(10) | YES | | NULL | | | c | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +-------+-----------+------+-----+-------------------+-----------------------------+
2.增加列,调整列顺序{只有FIRST和AFTER}
格式:
ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name ]
例子:
ALTER TABLE t1 ADD COLUMN d int after a;
mysql> desc t1; +-------+-----------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------+------+-----+-------------------+-----------------------------+ | a | int(11) | YES | | NULL | | | d | int(11) | YES | | NULL | | | b | char(10) | YES | | NULL | | | c | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +-------+-----------+------+-----+-------------------+-----------------------------+
3.增加索引
格式:
ADD {INDEX|KEY} [index_name] [index_type] (index_col_name,...) [index_option] ...
例子:
ALTER TABLE t1 ADD INDEX idx_d(d); (d列增加索引)
表结构:
CREATE TABLE `t1` ( `a` int(11) DEFAULT NULL, `d` int(11) DEFAULT NULL, `b` char(10) DEFAULT NULL, `c` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, KEY `idx_d` (`d`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1
4.增加约束
- Primary key
- Unique key
- Foreign key
创建新表:s1,s2,
CREATE TABLE s1(id INT,NAME VARCHAR(10),address CHAR(20)); CREATE TABLE s2(id INT,s1_id INT);
例子1:
ALTER TABLE s1 ADD PRIMARY KEY(id); (列被置成not null)
mysql> desc s1; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | 0 | | | name | varchar(10) | YES | | NULL | | | address | char(20) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+
例子2:
ALTER TABLE s1 ADD UNIQUE KEY (NAME);
表结构:
mysql> desc s1; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | 0 | | | name | varchar(10) | YES | UNI | NULL | | | address | char(20) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+
格式:
ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...) reference_definition
例子3:
ALTER TABLE s2 ADD FOREIGN KEY (s1_id) REFERENCES s1 (id); 指向表s1的id列
表结构:
CREATE TABLE `s2` ( `id` int(11) DEFAULT NULL, `s1_id` int(11) DEFAULT NULL, KEY `s1_id` (`s1_id`), CONSTRAINT `s2_ibfk_1` FOREIGN KEY (`s1_id`) REFERENCES `s1` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1
5.增加全文索引FULL-TEXT
ALTER TABLE t1 ADD FULLTEXT full_f (f);
表结构:
CREATE TABLE `t1` ( `a` int(11) NOT NULL DEFAULT '0', `d` int(11) DEFAULT NULL, `b` char(10) DEFAULT NULL, `c` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `f` text, PRIMARY KEY (`a`), KEY `idx_d` (`d`), FULLTEXT KEY `full_f` (`f`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1
6.改变列的默认值
格式:
ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
例子1:
ALTER TABLE t1 ALTER COLUMN a SET DEFAULT '1';
例子2:
ALTER TABLE t1 ALTER COLUMN d DROP DEFAULT ;
CREATE TABLE `t1` ( `a` int(11) NOT NULL DEFAULT '1', `d` int(11), `b` char(10) DEFAULT NULL, `c` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `f` text, PRIMARY KEY (`a`), KEY `idx_d` (`d`), FULLTEXT KEY `full_f` (`f`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1
7.改变列名字(类型,顺序)
CREATE TABLE t1 (a INTEGER,b CHAR(10));
格式:
CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name]
例子:
ALTER TABLE t3 CHANGE a a_1 INT NOT NULL AFTER b;
8.不改变列名字
格式:
MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
例子:
ALTER TABLE t4 MODIFY a INT NOT NULL,CHANGE b c VARCHAR(20);
9.删除列
格式:
DROP [COLUMN] col_name
例子:
ALTER TABLE t1 DROP COLUMN a;
10.删除主键
格式:
DROP PRIMARY KEY
例子:
ALTER TABLE t1 DROP PRIMARY KEY;
11.删除索引
格式:
DROP {INDEX|KEY} index_name
例子:
ALTER TABLE t1 DROP INDEX idx_d;
12.删除约束
格式:
DROP FOREIGN KEY fk_symbol
例子:
ALTER TABLE s2 DROP FOREIGN KEY s2_ibfk_1
s2表结构:
CREATE TABLE `s2` (
`id` int(11) DEFAULT NULL,
`s1_id` int(11) DEFAULT NULL,
KEY `s1_id` (`s1_id`),
CONSTRAINT `s2_ibfk_1` FOREIGN KEY (`s1_id`) REFERENCES `s1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
13.改表名
格式:
RENAME [TO|AS] new_tbl_name
例子:
ALTER TABLE t1 rename to t2;
14.改变字符集
格式:
[DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name]
例子:
ALTER TABLE t1 CHARACTER SET = utf8;