At least on my local instance, when I create tables, they are all prefixed with "dbo.". Why is that?
6 Answers
dbo is the default schema in SQL Server. You can create your own schemas to allow you to better manage your object namespace.
-
33As a best practice, I always add the "dbo." prefix even though it is not necessary. Most of the time in SQL it's good to be explicit. Commented Jun 30, 2009 at 13:56
-
5@SurroundedByFish: Probably not a best practice, but I could be wrong as I'm not a SQL expert. stackoverflow.com/a/769639/602245– BrettCommented Jan 20, 2012 at 19:19
-
16This article from a different answer claims that it is in fact a best practice: "The code would not have to use the fully qualified name, though there is a slight performance gain in doing so and is considered a best practice. "– Carl GCommented Oct 9, 2012 at 16:33
-
7Also, the answer you linked also recommends including the "dbo." so that the optimizer doesn't have to look up the schema.– Carl GCommented Oct 9, 2012 at 16:42
-
9dbo is not a good practice. Create your own schema and always use it. dbo exists only a a migration trick so pre-SQL Server 2005 continues to work. Yes, always use a schema for perf. No, not the migration one (e.g. dbo). Commented Nov 5, 2015 at 22:02
If you are using Sql Server Management Studio, you can create your own schema by browsing to Databases - Your Database - Security - Schemas.
To create one using a script is as easy as (for example):
CREATE SCHEMA [EnterSchemaNameHere] AUTHORIZATION [dbo]
You can use them to logically group your tables, for example by creating a schema for "Financial" information and another for "Personal" data. Your tables would then display as:
Financial.BankAccounts Financial.Transactions Personal.Address
Rather than using the default schema of dbo.
-
Do you know if that causes any problem when we use Entity framework?– EmilCommented May 24, 2013 at 2:36
-
7You can use schemas with Entity Framework - even with code first if you like:
[Table("Customer", Schema = "MySchema")]
– FentonCommented May 24, 2013 at 8:36 -
1Does the 'AUTHORIZATION [dbo]' grant the same permissions as dbo on the schema, or do I still need to grant permissions to the users? Commented Apr 3, 2014 at 3:14
Microsoft introduced schema in version 2005. For those who didn’t know about schema, and those who didn’t care, objects were put into a default schema dbo
.
dbo
stands for DataBase Owner, but that’s not really important.
Think of a schema as you would a folder for files:
- You don’t need to refer to the schema if the object is in the same or default schema
- You can reference an object in a different schema by using the schema as a prefix, the way you can reference a file in a different folder.
- You can’t have two objects with the same name in a single schema, but you can in different schema
- Using schema can help you to organise a larger number of objects
- Schema can also be assigned to particular users and roles, so you can control access to who can do what.
You can generally access any object from any schema. However, it is possible to control which users have which access to particular schema, so you can use schema in your security model.
Because dbo
is the default, you normally don’t need to specify it within a single database:
SELECT * FROM customers;
SELECT * FROM dbo.customers;
mean the same thing.
I am inclined to disagree with the notion of always using the dbo.
prefix, since the more you clutter your code with unnecessary detail, the harder it is to read and manage.
For the most part, you can ignore the schema. However, the schema will make itself apparent in the following situations:
If you view the tables in either the object navigator or in an external application, such as Microsoft Excel or Access, you will see the
dbo.
prefix. You can still ignore it.If you reference a table in another database, you will need its full name in the form
database.schema.table
:SELECT * FROM bookshop.dbo.customers;
For historical reasons, if you write a user defined scalar function, you will need to call it with the schema prefix:
CREATE FUNCTION tax(@amount DECIMAL(6,2) RETURNS DECIMAL(6,2) AS BEGIN RETURN @amount * 0.1; END; GO SELECT total, dbo.tax(total) FROM pricelist;
This does not apply to other objects, such as table functions, procedures and views.
You can use schema to overcome naming conflicts. For example, if every user has a personal schema, they can create additional objects without having to fight with other users over the name.
-
adding schema to the sql is not clutter, if anything it clarifies. Commented Aug 10, 2023 at 17:47
-
@TMcKeown It clarifies nothing if it’s the only schema in use.– ManngoCommented Aug 11, 2023 at 8:31
-
we use, its standard. always have schema in the table or sproc we are executing/accessing. never a question about if we should or not, we always do it. Commented Aug 11, 2023 at 15:50
-
It's new to SQL 2005 and offers a simplified way to group objects, especially for the purpose of securing the objects in that "group".
The following link offers a more in depth explanation as to what it is, why we would use it:
Understanding the Difference between Owners and Schemas in SQL Server
Something from Microsoft (Documentation)
The dbo
user is a special user principal in each database. All SQL Server administrators, members of the sysadmin
fixed server role, sa
login, and owners of the database, enter databases as the dbo
user. The dbo
user has all permissions in the database and cannot be limited or dropped. dbo
stands for database owner, but the dbo
user account is not the same as the db_owner
fixed database role, and the db_owner
fixed database role is not the same as the user account that is recorded as the owner of the database.
The dbo
user owns the dbo
schema. The dbo
schema is the default schema for all users, unless some other schema is specified. The dbo
schema cannot be dropped.
The dbo user owns the dbo schema. The dbo schema is the default schema for all users, unless some other schema is specified. The dbo schema cannot be dropped.
DBO is the default schema in SQL Server. You can create your own schemas to allow you to better manage your object namespace. As a best practice, I always add the "DBO." prefix even though it is not necessary. Most of the time in SQL it's good to be explicit.