Drop Schema in SQL Server


A schema is a logical structure that stores database objects in SQL Server. It offers a method for classifying and organizing database items including tables, views, and processes. There could be instances where you need to delete a schema from your database. The SQL Server DROP SCHEMA command is useful in this situation. We can delete a schema from the database using the DROP SCHEMA statement. For database administrators and developers who wish to effectively control the structure of their databases, the Drop statement is a crucial tool.

Syntax

DROP SCHEMA [ IF EXISTS ] schema_name;

Here,

If exist is as optional, if the schema does not exist, then the statement will not throw an error

schema_name is the name of the schema that is to be dropped

Example

This example contains two conditions that are used to show the ways to drop a schema.

CREATE SCHEMA school #schema is created
CREATE TABLE school. student #table is created inside schema
(id INT PRIMARY KEY, name VARCHAR(50), 
address VARCHAR(50), roll_no INT); #values are provided in table

Condition 1:Drop the schema only

Example

DROP SCHEMA school;

Output

Cannot drop schema 'school' because it is being referenced by object 'student'

Condition 2:Drop the table first and then drop the schema.

Example

DROP TABLE student;
DROP SCHEMA school;

Output

school schema is deleted

Points to Remember While Using Drop Schema

Dropping an Empty Schema: You must make sure that the schema is empty of all objects before you can drop it. The DROP SCHEMA command will fail if the schema contains any objects, such as tables, views, or procedures. Therefore, before executing the DROP SCHEMA statement, all objects must be deleted or moved outside of the schema.

Dependencies between schemas and objects: The DROP SCHEMA command will fail if an object in one schema refers to an item in another schema that you want to delete. You must eliminate these dependencies by deleting or changing the dependent items to correctly delete a schema.

Schema ownership: Each schema in SQL Server has a corresponding owner. The person who creates a schema is automatically considered to be its owner. However, while creating the schema, you have the option to designate a different owner. A schema's owner is granted specific rights and privileges about the objects included within the schema. When dropping a schema, the owner continues to own all of the objects contained in the discarded schema.

Dropping Conditions: You may only conditionally drop a schema using the IF EXISTS clause if it already exists. When you want to make sure the schema is there before attempting to discard it, this is helpful. The DROP SCHEMA statement with IF EXISTS will not throw an error if the schema is not there.

Conclusion

This article consists of drop schema in SQL server. A drop schema statement is used to delete a schema from the database. Examples are provided in which the drop statement is used, Conditions are taken in which Schema is deleted without removing the object and after removing the objects or table. Points to remember while using drop schema statements are emptying the schema before dropping, dependencies between schema and objects, schema ownership, and dropping conditions.

Updated on: 13-Jul-2023

605 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements