postgresql
  1. postgresql-alter-schema

ALTER Schema - (PostgreSQL Schema)

In PostgreSQL, a schema is a named container for tables, views, and other database objects. The ALTER SCHEMA statement is used to modify an existing schema in the database. In this tutorial, we'll explore the syntax, example, output, explanation, use, important points, and summary of the ALTER SCHEMA statement in PostgreSQL.

Syntax

The basic syntax of the ALTER SCHEMA statement is as follows:

ALTER SCHEMA schema_name
ACTION action_type [ cascade|restrict ]

Where schema_name is the name of the schema and action_type is the type of action to be performed. The cascade or restrict keyword can be specified to determine how to handle dependent objects of the schema.

Example

Let's consider an example of modifying a schema:

ALTER SCHEMA sales
RENAME TO new_sales;

This will rename the schema named "sales" to "new_sales".

Output

When executed successfully, the ALTER SCHEMA statement generates the following output:

ALTER SCHEMA

Explanation

In the example above, we used the ALTER SCHEMA statement to rename a schema called "sales" to "new_sales". This statement modifies an existing schema in the database. This is useful when you need to change the name of a schema, relocate tables from one schema to another, or make other changes to a schema.

Use

The ALTER SCHEMA statement is used to modify an existing schema in PostgreSQL. This statement can be used to rename a schema, relocate tables from one schema to another, or make other changes to a schema.

Important Points

  • The ALTER SCHEMA statement can only be executed by the database owner or a user with superuser privileges.
  • The cascade option is used to apply the action to all dependent objects of the schema being modified. For example, when renaming a schema, the cascade option will rename objects that depend on the schema being renamed. The restrict option will prevent the action if there are dependent objects.
  • When renaming a schema, you can only rename the schema itself, not the objects contained within the schema.
  • When relocating tables from one schema to another, you should be aware of the need to update queries and functions that reference those tables.

Summary

In this tutorial, we discussed the ALTER SCHEMA statement in PostgreSQL. We covered its syntax, example, output, explanation, use, important points, and summary. With this knowledge, you can now modify an existing schema in PostgreSQL by using the ALTER SCHEMA statement.

Published on: