DavidSzczesniak
1/26/2018 - 12:06 PM

Add keys/indexes to a table

\!h Add an index to an existing table:
ALTER TABLE artist ADD INDEX by_name (artist_name); -- adds key called 'by_name' to artist_name column

\!h Add a primary key:
ALTER TABLE artist ADD PRIMARY KEY (artist_id) -- adds primary key to artist_id column

\!h Remove non-primary key index:
ALTER TABLE artist DROP INDEX by_name;

\!h Remove primary key:
ALTER TABLE artist DROP PRIMARY KEY;

\!h Modify an index:
-- can't really be done, you have to drop and re-create with new specifications
-- Example: have the by_name index include only the first 10 characters of the artist_name:
ALTER TABLE artist DROP INDEX by_name;

ALTER TABLE artist ADD INDEX by_name (artist_name(10));