The Wayback Machine - https://web.archive.org/web/20240921145645/https://www.geeksforgeeks.org/alter-schema-in-sql-server/
Open In App

ALTER SCHEMA in SQL Server

Last Updated : 02 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The SQL Server ALTER SCHEMA statement is used to transfer an object (table, view, or function) from one schema to another schema in the same database.

The ALTER SCHEMA statement is used to transfer an object from a schema to another schema in the same database.

Syntax

ALTER SCHEMA target_schema_name    
TRANSFER [ object_type :: ] object_name;

Parameters:

  • target_schema_name is the schema in the database, into which the object will be moved.
  • object_type represents the type of the object for which the owner schema will be changed.
  • object_name is the name of the object that will be moved to the target_schema_name.

Note: SYS or INFORMATION_SCHEMA cannot be altered.

SQL Server ALTER SCHEMA Example

Let’s look at an example of ALTER SCHEMA in SQL Server.

First, we will create a table named “geektab” in the dbo schema.

Query:

CREATE TABLE dbo.geektab (
id INT PRIMARY KEY IDENTITY,
name NVARCHAR(40) NOT NULL,
address NVARCHAR(255) NOT NULL
);

INSERT INTO dbo.geektab
(id, name, address)
VALUES
(1, 'Neha', 'B-Wing, Delhi'),
(2, 'Vineet', 'D-Wing, Noida');

Let us create a stored procedure that finds id:

CREATE PROCEDURE sp_get_id (@id INT)  AS
BEGIN
SELECT *
FROM dbo.geektab
WHERE id = @id;
END;

Let us move this dbo.geektab table to the geek schema:

ALTER SCHEMA geek TRANSFER OBJECT::dbo.geektabs;

Run the sp_get_id stored procedure :

EXEC sp_get_id;

SQL Server will throw an error similar to mentioned below:

strong>Msg 208, Level 16, State 1, Procedure sp_get_id, Line 3
Invalid object name 'dbo.geektab'

Now, let us manually alter the stored procedure to reflect the geek schema:

ALTER PROCEDURE sp_get_id ( @id INT) AS
BEGIN
SELECT *
FROM geek.geektab
WHERE id = @id;
END;

Run the sp_get_id stored procedure:

EXEC sp_get_id 1; 

Output:

id name address
1 Neha B-Wing, Delhi

Important Points about ALTER SCHEMA in SQL Server

  • The ALTER SCHEMA statement is used to transfer objects (e.g., tables, views, functions) from one schema to another within the same database, allowing for better organization and management of database objects.
  • The SYS and INFORMATION_SCHEMA schemas cannot be altered, as they are reserved for system objects and metadata.
  • You need ALTER permission on the object being transferred and CONTROL permission on the target schema to use the ALTER SCHEMA statement.
  • The ALTER SCHEMA operation is transactional, meaning it will roll back if an error occurs during the operation, ensuring database integrity.


Similar Reads

Create, Alter and Drop schema in MS SQL Server
Schema management in MS SQL Server involves creating, altering, and dropping database schema elements such as tables, views, stored procedures, and indexes. It ensures that the database structure is optimized for data storage and retrieval. In this article, we will be discussing schema and how to create, alter, and drop the schema. What is Schema?A
4 min read
Alter login in SQL Server
Alter login statements can be used to changes the properties of a SQL Server login account. Syntax: ALTER LOGIN loginname; GO Enable a disabled login - Syntax : ALTER LOGIN loginname ENABLE; Example - ALTER LOGIN geeks ENABLE; Change password of a login Syntax : ALTER LOGIN geeks WITH PASSWORD = 'newpassword'; Example - ALTER LOGIN geeks WITH PASSW
1 min read
How to Alter Multiple Columns at Once in SQL Server?
In SQL, sometimes we need to write a single query to update the values of all columns in a table. We will use the UPDATE keyword to achieve this. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database and Select keyword. Step 1: Create a Database. For t
3 min read
How to Alter a Column from Null to Not Null in SQL Server
In SQL Server, columns are defined with specific constraints, one of which is the nullability of the column whether or not it can hold NULL values. As a database evolves, this setting may need to be changed particularly to ensure that certain data fields are always populated. Altering a column from NULL to NOT NULL is a common task when you want to
4 min read
Difference between Star Schema and Snowflake Schema
Star Schema: Star schema is the type of multidimensional model which is used for data warehouse. In star schema, The fact tables and the dimension tables are contained. In this schema fewer foreign-key join is used. This schema forms a star with fact table and dimension tables. Snowflake Schema: Snowflake Schema is also the type of multidimensional
2 min read
Difference between Snowflake Schema and Fact Constellation Schema
Snowflake Schema: Snowflake Schema is a type of multidimensional model. It is used for data warehouse. In snowflake schema contains the fact table, dimension tables and one or more than tables for each dimension table. Snowflake schema is a normalized form of star schema which reduce the redundancy and saves the significant storage. It is easy to o
4 min read
Difference between Star Schema and Fact Constellation Schema
Star Schema: Star schema is a multidimensional model type that uses simple queries in order to access data from the database. A star schema depicts each dimension with only one-dimension table. Star schema is easy to operate as compared to fact constellation schema as it has less number of joins between the tables. It uses less space as compared to
2 min read
DROP SCHEMA in SQL Server
The DROP SCHEMA statement could be used to delete a schema from a database. SQL Server have some built-in schema, for example : dbo, guest, sys, and INFORMATION_SCHEMA which cannot be deleted. Syntax : DROP SCHEMA [IF EXISTS] schema_name; Note : Delete all objects from the schema before dropping the schema. If the schema have any object in it, the
1 min read
How to Rename SQL Server Schema?
In SQL, we cannot RENAME a SCHEMA. To achieve this, we need to create a new SCHEMA, transfer all the contents(objects) from the old schema to new schema and then finally delete the old schema using the DROP command. The same is depicted in the below article. For this article, we will be using the Microsoft SQL Server as our database. Step 1: Displa
3 min read
CREATE SCHEMA in SQL Server
A schema is a collection of database objects like tables, triggers, stored procedures, etc. A schema is connected with a user which is known as the schema owner. The database may have one or more schema. To create a schema in SQL Server use the 'CREATE SCHEMA' Statement. SQL CREATE SCHEMACREATE SCHEMA statement used to create a new schema in the cu
2 min read
Difference between ALTER and UPDATE Command in SQL
1. ALTER Command: ALTER SQL command is a DDL (Data Definition Language) statement. ALTER is used to update the structure of the table in the database (like add, delete, modify the attributes of the tables in the database). Syntax: // add a column to the existing table ALTER TABLE tableName ADD columnName columnDefinition; // drop a column from the
3 min read
SQL Query to Drop Unique Key Constraints Using ALTER Command
Here, we see how to drop unique constraints using alter command. ALTER is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table. Syntax : ALTER TABLE table_name DROP CONSTRAINT unique_constraint; For instance, consider the below table 'Employee'. Create a Table: C
2 min read
SQL Query to Add Unique key Constraints Using ALTER Command
Here we will see how to add unique key constraint to a column(s) of a MS SQL Server's database with the help of a SQL query using ALTER clause. For the demonstration purpose, we will be creating a demo table in a database called "geeks". Creating the Database : Use the below SQL statement to create a database called geeks: CREATE DATABASE geeks; Us
2 min read
SQL ALTER COLUMN
In today's data-driven world, successful organizations require the ability to process and manipulate data effectively as part of their business foundation. Structured Query Language (SQL) is a powerful tool that enables users to manipulate relational databases. Within SQL, there is an entity where data can be stored, retrieved, and manipulated. Thi
4 min read
SQL ALTER TABLE
SQL ALTER TABLE command can add, delete, or modify columns of an existing table. This article discusses the SQL ALTER TABLE statement with examples and syntax. ALTER TABLE STATEMENTThe ALTER TABLE statement in SQL is used to add, remove, or modify columns in an existing table. The ALTER TABLE statement is also used to add and remove various constra
2 min read
SQL Query to Alter Column Size in MySQL
To Alter the Column size in MySQL use the ALTER TABLE statement with MODIFY Clause. The ALTER TABLE statement allows users to add, delete, or modify columns in an existing table in MySQL. SyntaxSyntax to Alter column size in MySQL is: ALTER TABLE table_nameMODIFY column_namevarchar(new_length); MySQL Alter Column Size in ExamplesLet us look at some
2 min read
SQL Query to Drop Foreign Key Constraint Using ALTER Command
A foreign key is included in the structure of a table, to connect multiple tables by referring to the primary key of another table. Sometimes, you might want to remove the primary key constraint from a table for performance issues or other requirements. A foreign key constraint can be removed from a table using the ALTER TABLE command and DROP CONS
3 min read
ALTER (RENAME) in SQL
Sometimes we may want to rename our table to give it a more relevant name. For this purpose, we can use ALTER TABLE to rename the name of the table. SQL ALTER TABLE is a command used to modify the structure of an existing table in a database. Here we will discuss Alter Command in SQL in detail. Alter in SQLHere we are discussing the syntax of Alter
2 min read
SQL Query to Add Foreign Key Constraints Using ALTER Command
In relational databases, a Foreign Key Constraint is a fundamental concept that defines a relationship between two tables. It ensures that the value of one or more columns in a child table corresponds to the value in a primary key or unique key in a parent table. In this article, we will look into how we can add a foreign key constraint using the A
3 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
How to Change DB Schema to DBO in SQL?
In this article, we will look at two methods for changing the database schema in SQL Server to DBO. Schema: SQL Schema is defined as a logical grouping of database objects. Tables, views, stored procedures, functions, indexes, and triggers are all part of database object. It is a handy tool for segregating database items for various applications, r
2 min read
How to Export Database Schema Without Data in SQL?
Database Schema specifies the structure of a database with its components like tables, columns, and indexes. Exporting data in SQL is an essential task in database management used to perform functions like data backup, recovery, migration, data analysis, performance optimization, compliance, auditing, etc. There may be situations when there is a ne
4 min read
How to add a foreign key using ALTER in MySQL
In this article, we will discuss the overview of foreign keys and will discuss how to add a foreign key using ALTER in MySQL step by step. Let's discuss it one by one. Foreign key : If an attribute is a primary key in one table but was not used as a primary key in another table then the attribute which is not a primary key in the other table is cal
3 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
SQL Query to Convert Rows to Columns in SQL Server
In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we have a table given below: NAMECOLLEGEROLL NUMBERSUB
2 min read
How to SQL Select from Stored Procedure using SQL Server?
There may be situations in SQL Server where you need to use a stored procedure to get data from a SQL query. For direct data selection from a stored procedure within a query, SQL Server offers options like OPENQUERY and OPENROWSET. The usual way is running the stored procedure independently and then querying the outcomes. The idea of utilizing SQL
3 min read
BULK INSERT in SQL Server(T-SQL command)
BULK INSERT in SQL Server(T-SQL command): In this article, we will cover bulk insert data from csv file using the T-SQL command in the SQL server and the way it is more useful and more convenient to perform such kind of operations. Let's discuss it one by one.  ConditionSometimes there is a scenario when we have to perform bulk insert data from .cs
3 min read
Article Tags :