Is there a way to setup an alias for indexes/primary keys? In other words, MySql has my primary key named "PRIMARY", and I want to name it something else, also, to make it compatible with my software. I know primary keys have to be named "PRIMARY" ( see here and here for the discussion ). So I figured, if there was a way to make an alias to the primary key, that would solve my problem.
NOTE: I am not talking about the column name, but the actual name of the index, as would appear in the "Key_name" column when you run the command "show indexes from " command.
2/29/2015 Edit in response to RandomSeed comment: The PRIMARY KEY column is, say, ithkey. In SQLServer, the index name is "ITHKEY", so our code was using "ITHKEY" as the index hint. But, of course, that won't work with my current MySql setup because the index/primary key for ithkey is named "PRIMARY", not "ITHKEY". I could, I suppose, change my code to reference "PRIMARY" in the index hints, was just trying to avoid that.
The name of a PRIMARY KEY is always PRIMARY , which thus cannot be used as the name for any other kind of index. If you do not have a PRIMARY KEY and an application asks for the PRIMARY KEY in your tables, MySQL returns the first UNIQUE index that has no NULL columns as the PRIMARY KEY .
A primary key index is created by default when a table is created with a primary key specified. It will match the primary key in nature, in that it will be a single-column index if the primary key is on a single column and a multi-column composite index if the primary key is a composite primary key.
But in the database world, it's actually not necessary to create an index on the primary key column — the primary index can be created on any non primary key column as well.
Advantages of MySQL AliasesIt makes the column or table name more readable. It is useful when you use the function in the query. It can also allow us to combines two or more columns. It is also useful when the column names are big or not readable.
From a functional perspective (as far as MySQL is concerned), the PRIMARY
key is just a UNIQUE NOT NULL
constraint*.
Therefore you could replace the PRIMAY KEY
with a UNIQUE NOT NULL
index that you can name as you see fit.
If the PRIMARY KEY
is also required by your application (or if the idea of a table without a primary key makes you sick), you can keep both keys on the table, which doubles the corresponding overhead, both in terms of CPU time and storage space (so this is a bad idea).
There is no such thing as an index alias with MySQL.
*If you do not define a PRIMARY KEY
for your table, MySQL locates the first UNIQUE
index where all the key columns are NOT NULL
and InnoDB uses it as the clustered index (reference).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With