MySQL: Using Aliases & Old Names

  • Thread starter EdmureTully
  • Start date
  • Tags
    Mysql
SELECT all rows fromIn summary, using the keyword AS in MYSQL to rename an attribute or relation does not store the alias outside of the query where it was created. This means that the alias cannot be used in other queries, including the old name. However, a workaround is to create a view and use that to select all rows from the table with the renamed attribute or relation.
  • #1
EdmureTully
20
0
If you rename an attribute or a relation with the keyword AS in MYSQL can you use the alias created outside the query where you renamed the attribute, also can you use the old name?
 
Technology news on Phys.org
  • #2
EdmureTully said:
If you rename an attribute or a relation with the keyword AS in MYSQL can you use the alias created outside the query where you renamed the attribute, also can you use the old name?

Not that I am aware of.
 
  • #3
The database does not store aliases you used in some query.

also can you use the old name?
Interesting, I never tried that. It is not possible.

SELECT * FROM `tablename` AS t WHERE t.fieldname=1 -> works
SELECT * FROM `tablename` WHERE tablename.fieldname=1 -> works
SELECT * FROM `tablename` AS t WHERE tablename.fieldname=1 -> does not work ("Unknown column 'tablename.fieldname' in 'where clause'")
 
  • #4
If you have some good reason for having external (global) aliases to columns or tables then you can create a view
 
Last edited:
  • #5


Yes, you can use the alias created outside the query where you renamed the attribute by using the keyword AS in MySQL. This allows for easier and more organized data manipulation within the query. Additionally, you can also use the old name of the attribute by referencing it in the query. This can be helpful in cases where the original name is more descriptive and easier to understand. However, it is important to note that the old name will still be associated with the original attribute, so any changes made to the old name will also affect the original attribute. It is recommended to use aliases for clarity and to avoid any confusion in the future.
 

Related to MySQL: Using Aliases & Old Names

1. What is an alias in MySQL?

An alias in MySQL is a temporary name given to a table or column in a query. It allows you to refer to the table or column by a different name, which can make the query easier to read and understand.

2. How do I use aliases in MySQL?

To use an alias in MySQL, you can use the AS keyword after the table or column name, followed by the desired alias. For example, "SELECT first_name AS name FROM users" will return the first name column with the alias "name". You can also use aliases in JOIN statements to refer to a specific table.

3. Are aliases case-sensitive in MySQL?

No, aliases in MySQL are not case-sensitive. This means that "name", "NAME", and "NaMe" would all be considered the same alias in a query.

4. Can I use aliases for multiple columns in MySQL?

Yes, you can use aliases for multiple columns in MySQL by separating them with commas. For example, "SELECT first_name AS name, last_name AS surname FROM users" would return the first name column with the alias "name" and the last name column with the alias "surname".

5. Can I use old names with aliases in MySQL?

Yes, you can use old names with aliases in MySQL. This can be helpful if you have a legacy database with different column names, but you want to use more descriptive aliases in your queries. You can use the old name in the query, followed by the AS keyword and the desired alias.

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
7
Views
465
  • Programming and Computer Science
Replies
12
Views
9K
  • Programming and Computer Science
Replies
7
Views
518
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
Replies
7
Views
270
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Computing and Technology
Replies
1
Views
2K
Back
Top