Why is using '*' to build a view bad?

后端 未结 14 2225
执念已碎
执念已碎 2020-11-27 07:02

Why is using \'*\' to build a view bad ?

Suppose that you have a complex join and all fields may be used somewhere.

Then you just have to chose fields needed

14条回答
  •  感动是毒
    2020-11-27 07:53

    I don't think there's much in software that is "just bad", but there's plenty of stuff that is misused in bad ways :-)

    The example you give is a reason why * might not give you what you expect, and I think there are others. For example, if the underlying tables change, maybe columns are added or removed, a view that uses * will continue to be valid, but might break any applications that use it. If your view had named the columns explicitly then there was more chance that someone would spot the problem when making the schema change.

    On the other hand, you might actually want your view to blithely accept all changes to the underlying tables, in which case a * would be just what you want.

    Update: I don't know if the OP had a specific database vendor in mind, but it is now clear that my last remark does not hold true for all types. I am indebted to user12861 and Jonny Leeds for pointing this out, and sorry it's taken over 6 years for me to edit my answer.

提交回复
热议问题