SQL中的Table,View。。。

Table,表是一種數據庫中的物理存在。

View,視圖只是一個虛擬的存在,是一個預編譯的sql select語句。視圖可以組合多個表組成一個sql語句,可以顯示一些字段,可以不顯示一些字段,這樣視圖就做到了安全性

 

參考:http://myitforum.com/myitforumwp/2011/10/21/sql-101-whats-the-difference-between-tables-and-views-or-views-and-tables/

A common theme I seem to be getting from people in the SMS/SCCM forums is possibly a misunderstanding of the difference between tables and views. I don’t wish to project in any way like I’m talking down to anyone or that I think I’m better than anyone with this post. There are people who I consider to be twice the engineer that I am who are awesome SMS guys, but who hold some misconceptions about the SQL side of things. There are others who are just starting out and are looking for anyone who will stop and help them out with the basics without talking way over their head. So, if you all don’t mind I’d like to clarify some things for people in any of those camps who might need clarification about tables and views.

 

I think pretty much everyone understands the basics with Excel workbooks/spreadsheets (or the Open Office equivalent to Excel) , so I’d like to use that analogy to help explain the difference between tables and views. It’s not a perfect analogy because SQL is SOOO much more than Excel, but I think it’s close enough to illustrate the point and enough people use it to make it the best example I can think of.

So first, think of an Excel workbook
Inside that workbook may be multiple worksheets with their own name
Inside each worksheet are columns across the top and rows down the left
Where the rows and columns come together are called cells
The cells physically hold data that’s used for all kinds of purposes.

 

Now, think of a SQL database as an Excel workbook.
Inside that database is a bunch of tables, each table is like a different worksheet in the workbook
Inside each table are a bunch of fields/columns, each field/column is like a column in the worksheet
When you insert data into the fields, they are physically inserted into new rows like you enter stuff into cells in the spreadsheet.

As for views…think about that workbook again. If you create a new worksheet and then start putting formulas in the cells that reference cells in other worksheets, it’s now like a SQL view. The data isn’t physically stored in this new worksheet, it’s stored in the cells of the other worksheets, but you are able to see and combine the data from a bunch of different cells in one spot and manipulate it however you want. This is what a view is like. A view doesn’t store the data, it’s just a way to get and/or combine the data that’s already stored somewhere in the DB and you can use different column names.

More than one person has asked how to dynamically create views from SMS web reports or from within their query and then delete them when they’re done. If you now think of a view as a worksheet that doesn’t store the data, but just links to other data you can imagine it wouldn’t make much sense to create a new worksheet on the fly, fill the cells with formulas to get the data from the other worksheets, do something with that data, and then remove the sheet. It would make more sense to create this new worksheet, fill the cells with formulas to get data from the other worksheets and then save it so it can be used again and again. It’s the same thing with a view, you create it, reference tables or even other views, and then save it.

So in summary, tables hold the physical data and views just link to that data. Of course, there’s so much more to tables and views than I’m explaining here, but I don’t want to overwhelm anyone with information if you’re not really going to use yet so I’m going to leave it at that.

Thanks for listening! As always, let me know if there’s something you’d like to have explained, or explained better.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章