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

后端 未结 14 2167
执念已碎
执念已碎 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:48

    These other answers all have good points, but on SQL server at least they also have some wrong points. Try this:

    create table temp (i int, j int)
    go
    create view vtemp as select * from temp
    go
    insert temp select 1, 1
    go
    alter table temp add k int
    go
    insert temp select 1, 1, 1
    go
    select * from vtemp
    

    SQL Server doesn't learn about the "new" column when it is added. Depending on what you want this could be a good thing or a bad thing, but either way it's probably not good to depend on it. So avoiding it just seems like a good idea.

    To me this weird behavior is the most compelling reason to avoid select * in views.

    The comments have taught me that MySQL has similar behavior and Oracle does not (it will learn about changes to the table). This inconsistency to me is all the more reason not to use select * in views.

提交回复
热议问题