标签:字符集 index log init 实例 extra first 梳理 change
CREATE TABLE t1 (a INTEGER,b CHAR(10));
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 | +-------+-----------+------+-----+-------------------+-----------------------------+
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 | +-------+-----------+------+-----+-------------------+-----------------------------+
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
CREATE TABLE s1(id INT,NAME VARCHAR(10),address CHAR(20)); CREATE TABLE s2(id INT,s1_id INT);
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 | | +---------+-------------+------+-----+---------+-------+
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
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
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
ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
ALTER TABLE t1 ALTER COLUMN a SET DEFAULT ‘1‘;
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
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;
MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
ALTER TABLE t4 MODIFY a INT NOT NULL,CHANGE b c VARCHAR(20);
DROP [COLUMN] col_name
ALTER TABLE t1 DROP COLUMN a;
DROP PRIMARY KEY
ALTER TABLE t1 DROP PRIMARY KEY;
DROP {INDEX|KEY} index_name
ALTER TABLE t1 DROP INDEX idx_d;
DROP FOREIGN KEY fk_symbol
ALTER TABLE s2 DROP FOREIGN KEY s2_ibfk_1
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
RENAME [TO|AS] new_tbl_name
ALTER TABLE t1 rename to t2;
[DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name]
ALTER TABLE t1 CHARACTER SET = utf8;
【SQL篇章】【SQL语句梳理 :--基于MySQL5.6】【已梳理:ALTER TABLE解析】
标签:字符集 index log init 实例 extra first 梳理 change
原文地址:http://www.cnblogs.com/cuisi/p/6142203.html