Does MySQL have any aliasing capabilities for column/table names without specifying them in the query?
For example, Let’s pretend I have a very complex application talking to an equally complex schema that’s been touched by many hands over the years.
There are inconsistent names, such as: ExpiryDate and DateExpiry, and we wish to standardize the naming in the database, without changing the entire codebase at the same time because the complexity in ensuring the whole thing will work with the new name.
In other words, could we define 2 potential names for the same column (or table)?
That way any "old" queries yet to be updated can still work while all new queries can reference the new (actual) column name?
Like a symbolic link so to speak.
I can’t do this at the application layer as there are some raw SQL queries, and not everything goes through an ORM type thing.
Fake example, FAKE SYNTAX:
CREATE TABLE Tickets (
TicketID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
DateExpiry DATETIME, /*Actual Column*/
COLUMN ALIAS (DateExpiry, ExpiryDate) /*Fake Syntax for creating an alias*/
)
And then these 2 will do the same thing:
INSERT INTO Tickets (DateExpiry) VALUES ("2023-05-15 23:59:59");
INSERT INTO Tickets (ExpiryDate) VALUES ("2023-05-15 23:59:59");
I’ve looked at the documentation, but can’t find anything. It’s difficult because searched for Aliases end up with the traditional definition of the term in the context of SQL.
2
Answers
You cannot define column aliases in table definition in MySQL. But you are able to use views and define ‘aliases’ there.
E.g.:
In this view there are two columns
ExpiryDate
andDateExpiry
which both point to the same column inTickets
table.You then need to change your application code to use
Tickets_View
instead ofTickets
table.But this might be probably easier to change than all column references.
Another approach is to change your application code to use only one name for particular column. This might be harder, but it might be a good investment regarding the quality of your application code.
Just have both columns in the table and use triggers to sync them.