Reputation: 287430
At least on my local instance, when I create tables, they are all prefixed with "dbo.". Why is that?
Upvotes: 402
Views: 425668
Reputation: 16291
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 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.
Upvotes: 42
Reputation: 1
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.
Upvotes: 0
Reputation: 4617
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.
Upvotes: 2
Reputation: 250922
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.
Upvotes: 109
Reputation: 4638
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
Upvotes: 25
Reputation: 4967
dbo is the default schema in SQL Server. You can create your own schemas to allow you to better manage your object namespace.
Upvotes: 309