Tables and Files – What’s the Connection?
What is the relationship between a table and the files that contain it? This isn’t as easy a question as you might think because there is not a direct relationship between a logical object like a table and a physical object like a file. For a small database like AdventureWorks (< 200 MB) all you need is a single data file to hold all your tables. As things start to get larger and more complex you may add files as a way of increasing performance. For example if you have a single table that is large and heavily queried you may want to put it on a faster (and probably more expensive) drive. You may also have non-clustered indexes that cover your query that would benefit from being separated. If terms like non-clustered index and covering query are unfamiliar you might benefit from this excellent article by Joe Webb.
In the picture above the Table01 is actually the clustered index and Table01/Index01 is a non-clustered index. Note that a file can be in exactly one filegroup, but the components of the table can be located and managed through the use of different filegroups.
–Create a test DB. Does the DB exist? Drop if it does.
–folder to use the script as written.
At this point you can take a look at the various structures using either the Object Explorer in SSMS or TSQL. I prefer TSQL because it is repeatable and far more flexible. The following script shows the relationship between tables, filegroups, and files.
I recommend you play with this script on the sample DB above but also on of your own DBs. Not only will it familiarize you with the various catalog views it will also point out how your data is arranged and may help you in managing it even better.