Link to home
Start Free TrialLog in
Avatar of mjs082969
mjs082969

asked on

SQL Server: Tables Named the Same With Different Schemas in Same Database

If two tables are named the same in a database with different owners, and a schema is not specified when referencing that table, is there an order of precedence that is used in referencing the tables?

For example:

master.dbo.IPLookup
guest.dbo.IPLookup

SELECT * FROM IPLookup

I have done some preliminary testing and it appears to go to the table owned by the default schema used by the current login, for a login with a single schema.

And I thought I recalled the ability for specifying multiple default schemas in SSMS, so I looked in SSMS and attempted to assign multiple default schemas to myself.  I went to the Logon Properties window for my account, then the User Mapping tab.  I selected the appropriate database, and then clicked the lookup button under the Default Schema option.  The Select Schema window appeared, and I clicked the Choose Object button.  I DID then choose multiple schemas (db_datareader and db_datawriter).  Clicked OK, then OK.  Back on the Logon Properties, only the first selected schema is listed under the Default Schema.  So it appears to allow you to select more than one, but only actually accepts one?

In the example where there were tables owned by the owner and guest schemas above, what table would it go to if the logon did not specify a schema when refernecing the object and had a default schema of db_datareader?

Thanks In Advance
Avatar of Steve Wales
Steve Wales
Flag of United States of America image

I want to address a couple of things here.

First:  If there is ever any possibility of duplicate object names in different schemas, allowing defaults to take over is dangerous.  If I have mydb.dbo.table1 and mydb.schema1.table1 and when we do 'select * from table1', it will go to the current user's default schema to get the table.

If the user's default schema changes from dbo to schema1, you're now selecting data from a completely different table.  If you ever find yourself in this sort of setup, you will always want to fully qualify your database object name in order to ensure you always address the correct object.   (Some may say you always want to fully qualify your database objects all the time, possibly, that's an argument for another time).

Note (see docs below) that a user in the sysadmin server role can only have dbo as default.

However, you mention db_datareader and db_datawriter as schemas.  They are not schemas.  A Schema is a container that contains database objects.

db_datareader and db_datawriter are fixed database roles, allowing a user to have read access or write access to tables within the database.

A role is a permission set granted to a user.
A schema is a container that contains database objects.

Some references from the documentation:

ALTER USER statement: http://msdn.microsoft.com/en-us/library/ms176060.aspx
USER / Schema separation: http://technet.microsoft.com/en-us/library/ms190387%28v=sql.105%29.aspx
db_datareader: http://technet.microsoft.com/en-us/library/ms188629%28v=sql.90%29.aspx
Database level roles: http://technet.microsoft.com/en-us/library/ms189121%28v=sql.90%29.aspx
Ownership and User/Schema Separation in SQL Server: http://msdn.microsoft.com/en-us/library/bb669061%28v=vs.110%29.aspx

This one has the key piece:  When database objects are referenced by using a one-part name, SQL Server first looks in the user's default schema. If the object is not found there, SQL Server looks next in the dbo schema. If the object is not in the dbo schema, an error is returned.

Also I believe (I haven't found anything to the contrary) that unless you have active synonyms in place, you only refer to local objects (current database) when using just the table name in a select statement.

The example you give refers to tables in two different databases.

See CREATE SYNONYM: http://technet.microsoft.com/en-us/library/ms177544.aspx
Avatar of mjs082969
mjs082969

ASKER

So why does Microsoft label that column 'Default Schema'?

(See attached)
Login-Properties.jpg
SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you both, I found both of your responses helpful and will split the points.