How to Change DB Schema to DBO in SQL?


Introduction

In SQL Server, a schema is a container for database objects such as tables, views, and stored procedures. The `dbo` schema is the default schema for the database owner (also known as the "database principal"). There may be times when you want to change the schema of a database object in SQL Server. For example, you might want to move a table from one schema to another to better organize your database, or you might want to change the schema of an object to match the schema of other objects in your database.

To change the schema of a database object in SQL Server, you can use the `ALTER SCHEMA` statement. This statement allows you to specify a new schema for the object, and it will move the object to the new schema.

Definition

In SQL Server, the `ALTER SCHEMA` statement is used to change the schema of a database object. A schema is a container for database objects such as tables, views, and stored procedures. The `dbo` schema is the default schema for the database owner (also known as the "database principal").

Syntax

The syntax for the ALTER SCHEMA statement is as follows −

ALTER SCHEMA new_schema_name
TRANSFER old_schema_name.object_name;

To change the schema of a database object, you specify the name of the new schema (`new_schema_name`) and the name of the object and its current schema (`old_schema_name.object_name`). The `TRANSFER` keyword is optional; you can simply use `ALTER SCHEMA` on its own if you don't need to specify a source schema.

Example 1

Here is an example of how to use the `ALTER SCHEMA` statement to change the schema of a table named customers from sales to `dbo` −

ALTER SCHEMA dbo
TRANSFER sales.customers;

This will move the `customers` table from the `sales` schema to the `dbo` schema.

You can also use the `ALTER SCHEMA` statement to move multiple objects at once. For example, to move all tables in the `sales` schema to the `dbo` schema, you can use the following statement −

ALTER SCHEMA dbo
TRANSFER sales.*;

The above example helps to clarify the definition of the `ALTER SCHEMA` statement and how it can be used to change the schema of a `database object` in SQL Server.

Example 2

Here is another example of how to use the `ALTER SCHEMA` statement to change the schema of a `database object` in SQL Server −

Suppose you have a database with the following structure −

sales.customers
sales.orders
sales.products

marketing.customers
marketing.campaigns
marketing.leads

You want to move all the objects in the `sales` schema to the `dbo` schema, and all the objects in the `marketing` schema to the `sales` schema. You can use the following statements to accomplish this −

ALTER SCHEMA dbo TRANSFER sales.*;
ALTER SCHEMA sales TRANSFER marketing.*;

After these statements are executed, the database will have the following structure −

dbo.customers
dbo.orders
dbo.products

sales.customers
sales.campaigns
sales.leads

The above example helps to clarify how to use the `ALTER SCHEMA` statement to change the schema of a `database object` in SQL Server.

Important Points while Changing DB Schema to DBO in SQL

Here are some important points to consider when changing the schema of a database object in SQL Server −

  • Make sure you have the necessary permissions to alter the schema of the object. In general, you need to have the `ALTER` permission on the object and the `CREATE SCHEMA` permission to move an object to a new schema.

  • If you are moving a table, be aware that any foreign key constraints that reference the table will be automatically updated to use the new schema. However, other dependencies on the table, such as views or stored procedures that reference the table, will not be automatically updated. You will need to update these dependencies manually.

  • If you are moving multiple objects at once, be aware that the order in which the objects are moved may be important. For example, if you are moving a table that has a foreign key constraint to another table, you will need to make sure the referenced table is moved first.

  • If you are moving an object to the `dbo` schema, be aware that the `dbo` schema is the default schema for the database owner (also known as the "database principal"). This means that if you create a new user or role and do not specify a default schema, the `dbo` schema will be used.

Conclusion

  • The `ALTER SCHEMA` statement in SQL Server allows you to change the schema of a database object.

  • You can also use the ALTER SCHEMA statement to move multiple objects at once.

Updated on: 25-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements