Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Views vs Tables

Views vs Tables

Scheduled Pinned Locked Moved Database
databasesql-servervisual-studioquestion
25 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eric Whitmore
    wrote on last edited by
    #1

    My DBA is telling me that i should always insert into Views and never into tables. He indicates that if I do "inserts" into "tables" I am doing it "wrong" but didnt explain why. Can someone explain why he would choose inserting into Views or table inserts? Normally i would just insert into tables and use the Views as a pre-defined query. Using MSSQL 2008R2. Thanks

    Eric

    P C J M 4 Replies Last reply
    0
    • E Eric Whitmore

      My DBA is telling me that i should always insert into Views and never into tables. He indicates that if I do "inserts" into "tables" I am doing it "wrong" but didnt explain why. Can someone explain why he would choose inserting into Views or table inserts? Normally i would just insert into tables and use the Views as a pre-defined query. Using MSSQL 2008R2. Thanks

      Eric

      P Online
      P Online
      PIEBALDconsult
      wrote on last edited by
      #2

      You may have misunderstodd, or he's wrong. I've never heard of inserting into views, a view is essentially readonly, you have no idea how the data feeding it is structured -- one of the benefits of views. Then again databases have advanced quite a bit in the last twenty years and I may be behind the times. It may be that some versions of some databases support it in some cases, but I would never do it.

      E G 2 Replies Last reply
      0
      • P PIEBALDconsult

        You may have misunderstodd, or he's wrong. I've never heard of inserting into views, a view is essentially readonly, you have no idea how the data feeding it is structured -- one of the benefits of views. Then again databases have advanced quite a bit in the last twenty years and I may be behind the times. It may be that some versions of some databases support it in some cases, but I would never do it.

        E Offline
        E Offline
        Eric Whitmore
        wrote on last edited by
        #3

        I had the same thoughts. You "can" do it in MS SQL 2008R2 but i never have... It seems to me to be a really bad practice as the view can (and does in my case) span multiple containers & servers.

        Eric

        P 1 Reply Last reply
        0
        • E Eric Whitmore

          I had the same thoughts. You "can" do it in MS SQL 2008R2 but i never have... It seems to me to be a really bad practice as the view can (and does in my case) span multiple containers & servers.

          Eric

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #4

          Eric Whitmore wrote:

          a really bad practice

          Yes, it is. It's got to be less efficient as the engine has to figure out how to do it and in a great many cases it simply can't. Plus it's not database agnostic, so why go to the trouble when inserting to tables works everywhere? "Simplify. Simplify." -- Thoreau

          1 Reply Last reply
          0
          • E Eric Whitmore

            My DBA is telling me that i should always insert into Views and never into tables. He indicates that if I do "inserts" into "tables" I am doing it "wrong" but didnt explain why. Can someone explain why he would choose inserting into Views or table inserts? Normally i would just insert into tables and use the Views as a pre-defined query. Using MSSQL 2008R2. Thanks

            Eric

            C Offline
            C Offline
            Corporal Agarn
            wrote on last edited by
            #5

            Since a view can become out of sync with the underlying table, it is not a good idea to insert through a view, [sarcasm]unless you are using MS Access[/sarcasm]. Another problem is, if there are multiple tables in the view things go wrong quickly. Now if he would have said you should use a stored procedure to do the insert then I would say he has a point. However it is not wrong to insert directly into a table.

            1 Reply Last reply
            0
            • E Eric Whitmore

              My DBA is telling me that i should always insert into Views and never into tables. He indicates that if I do "inserts" into "tables" I am doing it "wrong" but didnt explain why. Can someone explain why he would choose inserting into Views or table inserts? Normally i would just insert into tables and use the Views as a pre-defined query. Using MSSQL 2008R2. Thanks

              Eric

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Eric Whitmore wrote:

              if I do "inserts" into "tables" I am doing it "wrong

              You should ask why. There could be an architectural reason such as an intent to move to another schema or legacy reasons. Could just be an odd way to provide a database layer though. For that though I use stored procs.

              P 1 Reply Last reply
              0
              • E Eric Whitmore

                My DBA is telling me that i should always insert into Views and never into tables. He indicates that if I do "inserts" into "tables" I am doing it "wrong" but didnt explain why. Can someone explain why he would choose inserting into Views or table inserts? Normally i would just insert into tables and use the Views as a pre-defined query. Using MSSQL 2008R2. Thanks

                Eric

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                I ran across this attitude in the 90s (and not since) I believe it was based on security, there was a difference between table and view security back then. I would be very interested in your DBAs reasoning behind the statement. PieBald You can insert into views if there is 1 table in the view (how dumb is that) or the relationships are not too complex. I have never explored this as I also think it is a horror waiting to happen.

                Never underestimate the power of human stupidity RAH

                P J 2 Replies Last reply
                0
                • J jschell

                  Eric Whitmore wrote:

                  if I do "inserts" into "tables" I am doing it "wrong

                  You should ask why. There could be an architectural reason such as an intent to move to another schema or legacy reasons. Could just be an odd way to provide a database layer though. For that though I use stored procs.

                  P Online
                  P Online
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  jschell wrote:

                  I use stored procs

                  Right, I don't (willingly), but I know many do and, of the two techniques, procedures make more sense.

                  1 Reply Last reply
                  0
                  • M Mycroft Holmes

                    I ran across this attitude in the 90s (and not since) I believe it was based on security, there was a difference between table and view security back then. I would be very interested in your DBAs reasoning behind the statement. PieBald You can insert into views if there is 1 table in the view (how dumb is that) or the relationships are not too complex. I have never explored this as I also think it is a horror waiting to happen.

                    Never underestimate the power of human stupidity RAH

                    P Online
                    P Online
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Mycroft Holmes wrote:

                    in the 90s

                    At that time I was using Oracle (6) and I remember asking the DBAs why the views (there weren't many) had an _v suffix and the response was "to remind the developers that they are views and therefore readonly".

                    M 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Mycroft Holmes wrote:

                      in the 90s

                      At that time I was using Oracle (6) and I remember asking the DBAs why the views (there weren't many) had an _v suffix and the response was "to remind the developers that they are views and therefore readonly".

                      M Offline
                      M Offline
                      Mycroft Holmes
                      wrote on last edited by
                      #10

                      I think I was working with MSSQL 6.5! Try creating a view of only 1 table, no joins, and inserting/editing a record. Now add an inner join and repeat. Even though I have SSMS open I'll not bother as it is a complete WOTAM.

                      Never underestimate the power of human stupidity RAH

                      1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        You may have misunderstodd, or he's wrong. I've never heard of inserting into views, a view is essentially readonly, you have no idea how the data feeding it is structured -- one of the benefits of views. Then again databases have advanced quite a bit in the last twenty years and I may be behind the times. It may be that some versions of some databases support it in some cases, but I would never do it.

                        G Offline
                        G Offline
                        GuyThiebaut
                        wrote on last edited by
                        #11

                        I recently tested updates on a mock view, in SQL Server 2008, which contained a join between two tables. I did this because where I work the majority of what our users see is through views and they occasionally need to update what they see. I read up on it beforehand and all the documentation pointed to this being possible as long as it was unambiguous as to which row in which table was being updated. The update did work - I then tried to break it and succeeded by using an aggregate function in the view so that there was ambiguity in what would be updated. It can be useful to insert or update a view, since if one is passing a view into a control then it is easier to perform an update, on the source of that control, than to have to attach lots of meta data documenting which tables to update.

                        “That which can be asserted without evidence, can be dismissed without evidence.”

                        ― Christopher Hitchens

                        P J 2 Replies Last reply
                        0
                        • G GuyThiebaut

                          I recently tested updates on a mock view, in SQL Server 2008, which contained a join between two tables. I did this because where I work the majority of what our users see is through views and they occasionally need to update what they see. I read up on it beforehand and all the documentation pointed to this being possible as long as it was unambiguous as to which row in which table was being updated. The update did work - I then tried to break it and succeeded by using an aggregate function in the view so that there was ambiguity in what would be updated. It can be useful to insert or update a view, since if one is passing a view into a control then it is easier to perform an update, on the source of that control, than to have to attach lots of meta data documenting which tables to update.

                          “That which can be asserted without evidence, can be dismissed without evidence.”

                          ― Christopher Hitchens

                          P Online
                          P Online
                          PIEBALDconsult
                          wrote on last edited by
                          #12

                          GuyThiebaut wrote:

                          passing a view into a control

                          But that's not a good idea. It doesn't lead to a properly layered application.

                          GuyThiebaut wrote:

                          aggregate function

                          Or a UNION or a CASE , probably if there's CAST or CONVERT ... I suppose it requires that there be a primary key as well.

                          G 1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            GuyThiebaut wrote:

                            passing a view into a control

                            But that's not a good idea. It doesn't lead to a properly layered application.

                            GuyThiebaut wrote:

                            aggregate function

                            Or a UNION or a CASE , probably if there's CAST or CONVERT ... I suppose it requires that there be a primary key as well.

                            G Offline
                            G Offline
                            GuyThiebaut
                            wrote on last edited by
                            #13

                            "But that's not a good idea. It doesn't lead to a properly layered application." - I don't understand why using a view in a control would do this, could you let me know why?

                            “That which can be asserted without evidence, can be dismissed without evidence.”

                            ― Christopher Hitchens

                            J 1 Reply Last reply
                            0
                            • M Mycroft Holmes

                              I ran across this attitude in the 90s (and not since) I believe it was based on security, there was a difference between table and view security back then. I would be very interested in your DBAs reasoning behind the statement. PieBald You can insert into views if there is 1 table in the view (how dumb is that) or the relationships are not too complex. I have never explored this as I also think it is a horror waiting to happen.

                              Never underestimate the power of human stupidity RAH

                              J Offline
                              J Offline
                              Jorgen Andersson
                              wrote on last edited by
                              #14

                              Mycroft Holmes wrote:

                              You can insert into views if there is 1 table in the view (how dumb is that)

                              It's quite useful if you want to filter the contents of a table for a certain group of users.

                              "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

                              J M 2 Replies Last reply
                              0
                              • G GuyThiebaut

                                I recently tested updates on a mock view, in SQL Server 2008, which contained a join between two tables. I did this because where I work the majority of what our users see is through views and they occasionally need to update what they see. I read up on it beforehand and all the documentation pointed to this being possible as long as it was unambiguous as to which row in which table was being updated. The update did work - I then tried to break it and succeeded by using an aggregate function in the view so that there was ambiguity in what would be updated. It can be useful to insert or update a view, since if one is passing a view into a control then it is easier to perform an update, on the source of that control, than to have to attach lots of meta data documenting which tables to update.

                                “That which can be asserted without evidence, can be dismissed without evidence.”

                                ― Christopher Hitchens

                                J Offline
                                J Offline
                                jschell
                                wrote on last edited by
                                #15

                                GuyThiebaut wrote:

                                since if one is passing a view into a control then it is easier to perform an updat

                                Control? As in a GUI control? For small systems with stand along apps that might be appropriate. For anything larger one should have a database layer. And of course it won't work at all for a web app.

                                1 Reply Last reply
                                0
                                • G GuyThiebaut

                                  "But that's not a good idea. It doesn't lead to a properly layered application." - I don't understand why using a view in a control would do this, could you let me know why?

                                  “That which can be asserted without evidence, can be dismissed without evidence.”

                                  ― Christopher Hitchens

                                  J Offline
                                  J Offline
                                  jschell
                                  wrote on last edited by
                                  #16

                                  GuyThiebaut wrote:

                                  I don't understand why using a view in a control would do this, could you let me know why?

                                  Because if you use the view in the GUI then there is no database layer. Nor presumably a business layer for that matter.

                                  G 1 Reply Last reply
                                  0
                                  • J Jorgen Andersson

                                    Mycroft Holmes wrote:

                                    You can insert into views if there is 1 table in the view (how dumb is that)

                                    It's quite useful if you want to filter the contents of a table for a certain group of users.

                                    "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

                                    J Offline
                                    J Offline
                                    jschell
                                    wrote on last edited by
                                    #17

                                    Jörgen Andersson wrote:

                                    It's quite useful if you want to filter the contents of a table for a certain group of users.

                                    Why would you do that in the database versus the application layer? And of course in general when you say 'user' if you mean an actual database user then there can be negative impacts such as licensing.

                                    J 1 Reply Last reply
                                    0
                                    • J Jorgen Andersson

                                      Mycroft Holmes wrote:

                                      You can insert into views if there is 1 table in the view (how dumb is that)

                                      It's quite useful if you want to filter the contents of a table for a certain group of users.

                                      "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

                                      M Offline
                                      M Offline
                                      Mycroft Holmes
                                      wrote on last edited by
                                      #18

                                      Jörgen Andersson wrote:

                                      f you want to filter the contents of a table for a certain group of users

                                      Sure but that is one of the uses of a view - a very old fashioned use, but still valid. Who puts authorisation logic at that level these days. Apps no longer have to rely on the database for authorisation, very 80s :laugh:

                                      Never underestimate the power of human stupidity RAH

                                      J 1 Reply Last reply
                                      0
                                      • J jschell

                                        GuyThiebaut wrote:

                                        I don't understand why using a view in a control would do this, could you let me know why?

                                        Because if you use the view in the GUI then there is no database layer. Nor presumably a business layer for that matter.

                                        G Offline
                                        G Offline
                                        GuyThiebaut
                                        wrote on last edited by
                                        #19

                                        There is a database layer - the control is populated via a query from this layer. Any updates to this view are then passed through this layer via update commands. So we can lock down particular columns and perform verification on the values before update so that we both have a business and database layer. So I am still not getting what is wrong with using the results of a view in a GUI.

                                        “That which can be asserted without evidence, can be dismissed without evidence.”

                                        ― Christopher Hitchens

                                        J 1 Reply Last reply
                                        0
                                        • J jschell

                                          Jörgen Andersson wrote:

                                          It's quite useful if you want to filter the contents of a table for a certain group of users.

                                          Why would you do that in the database versus the application layer? And of course in general when you say 'user' if you mean an actual database user then there can be negative impacts such as licensing.

                                          J Offline
                                          J Offline
                                          Jorgen Andersson
                                          wrote on last edited by
                                          #20

                                          Because some companies are quite divided. The right place to do it is in the application, but I've been working for companies where the database department were responsible for that one department couldn't see the same columns as another department. It's a lot easier to keep track on this with a view than with column permissions. When it comes to licensing it doesn't matter. It's the number of actual users that counts, not the number of users you setup in the database. It's a common misconception.

                                          "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

                                          J 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups