Monday, May 16, 2011

Sql server object names, why can't they be same

Recently one of my colleagues was creating a view on sql server with the same name as a table, got errors creating it and inquired why can't the name of the view and that of another table be same as both are different types of objects.

Following were my thoughts and response why it was not possible or allowed:

  • You can create same named object within a different schema (you can think of them as namespaces)
  • Views and tables can't have same names within a schema just because they are used the same way in queries. (unless you know that an object is a view or the name somehow tells you, you use the view just like a table in your queries)
  • In certain circumstances how would the engine know what type of object you are trying to call/invoke.
  • When an error is reported by the database, it provides the object name and probably not the object type. It would be painful for the user/developer to understand which object is being referred to, to fix the error.
  • Most of the databases have similar restrictions, so they must have many more reasons than I have stated here. So that's what you have, so work under that restriction.
If you have any more, then you can add to the list.

No comments:

Post a Comment