I am trying to learn mysql properly. When i have a query using 2 tables, Do i need to write table names before the field name all the time?
This is the one i have got
SELECT
owner.title, owner.forename, owner.surname,
pet.name, pet.breed
FROM
owner,pet
WHERE
owner.owner_id = pet.owner_id
ORDER BY
owner.surname ASC, pet.name ASC
if i do this way is it ok aswell?
SELECT
title, forename, surname,
name, breed
FROM
owner,pet
WHERE
owner.owner_id = pet.owner_id
ORDER BY
surname ASC, name ASC
Since 1992, this:
is (by SQL-92 standards) written as:
To answer your question, I would prefer the first choice (modified as above) over the second, mainly for readibility. Someone else may have to read your code (or you, after a few days or months) and knowing which table every column belongs can be useful:
Another reason is to catch the case you (or someone) later adds (or renames) columns on the tables and you have a “name clash” which will make your query either give error and stop working or give different than expected results.
You could also use aliases, like this: