How do you avoid column name conflicts?

萝らか妹 提交于 2019-11-30 21:41:28

My experience is that that the benefits of the extra keystrokes outweighs the short time it takes to type them by far in the long run, at latest when you need to look at a query that you have written a year ago or so.

May be you can try using aliases on the table names ?

select * from table1 as T1
join table2 as t2 on (t1.key=t2.foreignkey)

You need to use AS alias.

You can give a table or a column another name by using an alias. This can be a good thing to do if you have very long or complex table names or column names.

An alias name could be anything, but usually it is short.

Your approach is correct, but you can also provide an alias for your table:

SELECT a.* FROM TableA A

in here you can refer to TableA as simply A.

The golden rule of thumb is that if you are ever using more than one table at all, alias the tables, and explicitly alias all columns.

This consistency will get you deep into the force, young padawan.

Change your naming convention so that each data element has a unique name in the schema e.g. auction_id, bid_id, user_id, etc. Ideally the name of the data element will not change between tables but sometimes you will need to add a qualifier to create a synonym e.g. adding_user_id and bidding_user_id if user_id appeared twice in the same table. You should document data element names and their synonyms in a data dictionary.

You can also define the table column beside the table name:

SELECT P.*,T.name AS type FROM <TABLE-A> AS P LEFT JOIN <TABLE-B> AS T ON P.id=T.id
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!