Você está na página 1de 2

ALTER command to add and drop INDEX:

There are four types of statements for adding indexes to a table:

ALTER TABLE tbl_name ADD PRIMARY KEY (column_l !t": This statement adds a PRIMARY KEY, hi!h means that indexed "alues must be uni#ue and !annot be $%&&' ALTER TABLE tbl_name ADD #NI$#E nde%_name (column_l !t": This statement !reates an index for hi!h "alues must be uni#ue ( ith the ex!eption of $%&& "alues, hi!h may appear multiple times)' ALTER TABLE tbl_name ADD INDEX nde%_name (column_l !t": This adds an ordinary index in hi!h any "alue may appear more than on!e' ALTER TABLE tbl_name ADD &#LLTEXT nde%_name (column_l !t": This !reates a spe!ial *%&&TE+T index that is used for text,sear!hing purposes'

-ere is the example to add index in an existing table'


mysql> ALTER TABLE testalter_tbl ADD INDEX (c);

You !an drop any I$.E+ by using .R/P !lause along ith A&TER !ommand' Try out the follo ing example to drop abo"e,!reated index'
mysql> ALTER TABLE testalter_tbl DROP INDEX (c);

You !an drop any I$.E+ by using .R/P !lause along ith A&TER !ommand' Try out the follo ing example to drop abo"e,!reated index'

ALTER 'ommand to add and drop PRIMARY KEY:


You !an add primary 0ey as ell in the same ay' 1ut ma0e sure Primary Key or0s on !olumns, hi!h are $/T $%&&' -ere is the example to add primary 0ey in an existing table' This ill ma0e a !olumn $/T $%&& first and then add it as a primary 0ey'
mysql> ALTER TABLE testalter_tbl MODIF ! INT NOT N"LL; mysql> ALTER TABLE testalter_tbl ADD PRIMAR #E (!);

You !an use A&TER !ommand to drop a primary 0ey as follo s:


mysql> ALTER TABLE testalter_tbl DROP PRIMAR #E ;

To drop an index that is not a PRIMARY KEY, you must spe!ify the index name'

Você também pode gostar