Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2014-02-05
Medium Priority
Last Modified: 2014-02-10
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:



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
Question by:mjs082969
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 23

Expert Comment

by:Steve Wales
ID: 39836499
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

Author Comment

ID: 39836650
So why does Microsoft label that column 'Default Schema'?

(See attached)
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1000 total points
ID: 39836813
>> 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?

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.<<

SQL first uses the default schema for that user (not login, since a login doesn't have a default schema).  If the object is not found under that default schema, SQL then defaults to a schema of "dbo".

A user can only have a single default schema, not a list of default schemas to check in order.

If a user has a default schema of "db_datareader" -- which, except for user "db_datareader" should never be the case -- then the statement:


would look for "db_datareader.IPLookup" and if that is not found, then "dbo.IPLookup".
LVL 23

Accepted Solution

Steve Wales earned 1000 total points
ID: 39836814
In SQL Server 2000, a schema and a user were the same thing.  In SQL Server 2005, this was changed.  See: http://technet.microsoft.com/en-us/library/ms190387%28v=sql.105%29.aspx

The behavior of schemas changed in SQL Server 2005. Schemas are no longer equivalent to database users; each schema is now a distinct namespace that exists independently of the database user who created it. In other words, a schema is simply a container of objects. A schema can be owned by any user, and its ownership is transferable.

I have been trying to find anywhere in the SQL Server documentation why the fixed roles have similarly named schemas.

While I can't find anything in the documentation to support it, it appears that as a backward compatibility feature in SQL Server 2005, fixed database roles were also created as schemas of the same name.

The appear to be empty and it is even suggested in on of the posts below that they can be dropped.

I did find a couple of blogs / forums talking about it but that's all I have found to date:


If you ran this query:

select * 
from sys.tables 
where schema_id in 
(select schema_id from sys.schemas where name = 'db_datareader');

Open in new window

You will probably find that there are no objects in that schema.

A user can own multiple schemas - but each user can have only one default schema.

Author Comment

ID: 39848385
Thank you both, I found both of your responses helpful and will split the points.

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question