MariaDB [RUNOOB]>SHOW COLUMNS FROM testalter_tbl; +-------+---------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+---------+------+-----+---------+-------+ | c |char(1) | YES ||NULL|| | i |int(11) |NO||NULL|| +-------+---------+------+-----+---------+-------+ 2rowsinset (0.002 sec)
指定添加的位置
如果你需要指定新增字段的位置,可以使用MySQL提供的关键字 FIRST (设定位第一列), AFTER 字段名(设定位于某个字段之后)
CREATE INDEX indexName ON table_name (column_name)
实例:为alter_tbl表中的c添加索引
MariaDB [RUNOOB]>CREATE INDEX index_test ON alter_tbl (c); Query OK, 0rows affected (0.008 sec) Records: 0 Duplicates: 0 Warnings: 0
MariaDB [RUNOOB]>SHOW COLUMNS FROM alter_tbl; +-------+---------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+---------+------+-----+---------+-------+ | A |int(11) | YES ||NULL|| | B |int(11) | YES ||NULL|| | c |char(1) | YES | MUL |NULL|| | i |int(11) |NO||NULL|| +-------+---------+------+-----+---------+-------+ 4rowsinset (0.002 sec)
以下错误
MariaDB [RUNOOB]>CREATE INDEX index ON alter_tbl (c); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'index ON alter_tbl (c)'at line 1
mysql对于语法是不在意大小写的,所以添加名为index的索引,与关键字冲突,错误!
删除索引
通用语法:
DROP INDEX indexName ONtable;
实例:删除刚刚创建的索引
MariaDB [RUNOOB]>drop index index_test on alter_tbl; Query OK, 0rows affected (0.008 sec) Records: 0 Duplicates: 0 Warnings: 0
ALTER语句
ALTERtable tableName ADD INDEX indexName(columnName)
MariaDB [RUNOOB]>SHOW COLUMNS FROM alter_tbl; +-------+---------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+---------+------+-----+---------+-------+ | A |int(11) | YES ||NULL|| | B |int(11) | YES ||NULL|| | c |char(1) | YES | MUL |NULL|| | i |int(11) |NO||NULL|| +-------+---------+------+-----+---------+-------+ 4rowsinset (0.002 sec)
MariaDB [RUNOOB]>SHOW COLUMNS FROM mytable; +----------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +----------+-------------+------+-----+---------+-------+ | ID |int(11) |NO||NULL|| | username |varchar(16) |NO| MUL |NULL|| +----------+-------------+------+-----+---------+-------+ 2rowsinset (0.003 sec)
CREATEUNIQUE INDEX indexName ON table_name (column_name)
MariaDB [RUNOOB]>CREATEUNIQUE INDEX index_test ON alter_tbl (c); Query OK, 0rows affected (0.009 sec) Records: 0 Duplicates: 0 Warnings: 0
MariaDB [RUNOOB]>SHOW COLUMNS FROM alter_tbl; +-------+---------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+---------+------+-----+---------+-------+ | A |int(11) | YES ||NULL|| | B |int(11) | YES ||NULL|| | c |char(1) | YES | UNI |NULL|| | i |int(11) |NO||NULL|| +-------+---------+------+-----+---------+-------+ 4rowsinset (0.002 sec)
ALTER语句
MariaDB [RUNOOB]>SHOW COLUMNS FROM alter_tbl; +-------+---------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+---------+------+-----+---------+-------+ | A |int(11) | YES ||NULL|| | B |int(11) | YES ||NULL|| | c |char(1) | YES | UNI |NULL|| | i |int(11) |NO||NULL|| +-------+---------+------+-----+---------+-------+ 4rowsinset (0.002 sec)
MariaDB [RUNOOB]>SHOW COLUMNS FROM alter_tbl; +-------+---------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+---------+------+-----+---------+-------+ | A |int(11) | YES ||NULL|| | B |int(11) | YES ||NULL|| | c |char(1) | YES ||NULL|| | i |int(11) |NO| PRI |NULL|| +-------+---------+------+-----+---------+-------+ 4rowsinset (0.002 sec)
MariaDB [RUNOOB]>SHOW COLUMNS FROM mytable; +----------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +----------+-------------+------+-----+---------+-------+ | ID |int(11) |NO||NULL|| | username |varchar(16) |NO| PRI |NULL|| +----------+-------------+------+-----+---------+-------+ 2rowsinset (0.002 sec)
key的PRI莫非是主键的意思?看了下创建表时指定主键的表的列属性,确实如此
奇怪了,上面还是UNI呢,这里怎么直接转成PRI了,留个坑
哦,好像似乎NOT NULL的就是PRI,NULL的就是UNI,看来还挺符合主键的要求的
使用ALTER 命令添加和删除索引
ALTER TABLE tbl_name ADD PRIMARY KEY (column_list): 该语句添加一个主键,这意味着索引值必须是唯一的,且不能为NULL。
ALTER TABLE tbl_name ADD UNIQUE index_name (column_list): 这条语句创建索引的值必须是唯一的(除了NULL外,NULL可能会出现多次)。
ALTER TABLE tbl_name ADD INDEX index_name (column_list): 添加普通索引,索引值可出现多次。
ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list):该语句指定了索引为 FULLTEXT ,用于全文索引。