The Wayback Machine - https://web.archive.org/web/20240930175401/https://www.geeksforgeeks.org/sql-query-to-rename-database/
Open In App

SQL Query to Rename Database

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

To rename a database in SQL use the ALTER DATABASE Statement with MODIFY NAME clause.

The ALTER command in SQL is used to make changes to a table or database. In this article, we will learn how to use the ALTER DATABASE statement with the MODIFY NAME clause to rename a database.

How to Rename Database in SQL?

To change the name of a database in SQL, use the syntax:

ALTER DATABASE [current_database_name]
MODIFY NAME = [new_database_name];

To rename a database in MySQL use the query:

RENAME DATABASE [current_database_name] TO [new_database_name];

Note: SQL RENAME DATABASE statement is used in MySQL, and for other SQL distributions, we need to use ALTER DATABASE statement 
with MODIDY NAME clause.

SQL Rename Database Example

Let’s look at an example of how to rename a database in SQL.

First, we will create a database which will be renamed in the example:

Query:

CREATE DATABASE Test

Output:

database created

Rename database in SQL Example

In this example, we will use the ALTER command with MODIFY NAME clause to rename the database.

Query:

ALTER DATABASE Test MODIFY NAME = Example

Output:

database renamed

The database name is changed from Test to Example.


Previous Article
Next Article

Similar Reads

SQL Query to Rename Stored Procedure
Stored Procedure is a saved SQL code. It is created to save time as we can use it again and again without writing the whole query. In this article, we will see how to Rename a Stored Procedure. To create a Procedure:Syntax: CREATE PROCEDURE procedure_name AS SELECT * FROM table_name;SQL query whose procedure needs to be created is always written af
2 min read
How to Use SQL Query to Rename a Constraint?
In SQL, we sometimes need to rename the constraints of a table. The whole process for doing the same is demonstrated below. For this article, we will be using the Microsoft SQL Server as our database. Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks. Query: CREATE DATABASE GeeksForGeeksOutput: Step
2 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
SQL Query to Add Email Validation Using Only One Query
In this article let us see how can we check for the validation of mails in the student database using MSSQL as the database server. For example, if the email is like abcdedfABCDEF...0123456@gmail.com this is the valid form of an email if it is other than this then that is said to Invalid. So, now we will discuss this concept in detail step-by-step:
2 min read
SQL Query to Set First Day of the Week in a Database
In this article, we are going to write a SQL script to Set the first day of the week in a Database. Below is the implementation for the same. You should have MSSQL in your server. Query in SQL - A query is a request for data or information from a database table or combination of tables. This data can be used for various purposes like Training a mod
2 min read
SQL Query to Add Ranking Positions of Rows in a Database With RANK()
In this article, we will discuss the overview of SQL function RANK and then our main focus will be on Add Ranking Positions of Rows in a Database With RANK() in SQL. Let's discuss it one by one. Overview :Generally, when max/min commands are executed, a single row is displayed as output. RANK() is an SQL function introduced in order to rank the row
2 min read
SQL Query to Add an Agent Parameter in a Database
In this article, we will learn how we add an extra or a new parameter after a table is created using ALTER command in Microsoft SQL Server in detail step-by-step: Step 1: Creating database sales by using the following SQL query: CREATE DATABASE sales; Step 2: Using the database sales by using the following SQL query: USE sales; Step 3: Creating a t
2 min read
SQL Query to Get First and Last Day of a Week in a Database
An SQL query is a single line statement of a Program written in a particular language to perform a specific task. A query consists of some pre-defined functions in SQL like SELECT, CREATE, etc. So, in this article, we will learn about SQL query to get the first and last day of a week in a database. So let's create a database first to do our task, h
2 min read
SQL Query to get first and last day of a month in a Database
To get the first and last day of a month in a database, use the DATE_SUB function. It subtracts the day of the month (using the DAYOFMONTH function) minus 1 from the daydate column. This gives us the first day of the month. The LAST_DAY function is used to get the last day of the month for the daydate column. SyntaxThe syntax to get the first and l
2 min read
SQL Server Query to Find All Permissions/Access for All Users in a Database
In SQL databases, managing and auditing user permissions is crucial for ensuring security and proper access control. To find out what permissions or access levels are granted to users within a database, we can use built-in functions such as sys.fn_my_permissions and sys.fn_builtin_permissions. These functions help administrators and database manage
3 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
SQL - Rename View
In relational databases, views play the main role in simplifying complex queries, enhancing data security, and improving overall database management. So, the requirements of the database evolve to need to rename existing views to reflect their purpose better or to maintain the consistency in the database schema. Renaming the views in SQL is a funda
4 min read
How to Rename a View in SQL Server?
The view is a virtual table based on the result set of an SQL statement. It is like the subset of the table and created to optimize the database experience. Like a real table, this also contains rows and columns. The data in a view are extracted from one or more real tables in the database. Renaming a View in MySQLWe can perform all the functions t
2 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 Check if Date is Greater Than Today in SQL
In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm: ss. mmm’ pattern. Features: This function is used to find the present date a
2 min read
SQL Query to Add a New Column After an Existing Column in SQL
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve data from relational databases like MySQL, Oracle, SQL Server, Postgres, etc. In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modif
3 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
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
MySQL | RENAME USER
In MySQL, the RENAME USER statement provides a straightforward way to change the username of an existing user account without altering any associated privileges or settings. In this article, we will learn RENAME USER in MySQL in a step-by-step approach. MySQL RENAME USERIn MySQL, the RENAME USER statement is used to change the username of an existi
2 min read
SQL Query to Find Number of Employees According to Gender Whose DOB is Between a Given Range
Query in SQL is like a statement that performs a task. Here, we need to write a query that will find the number of employees according to gender whose DOB is in the given range. We will first create a database named “geeks” then we will create a table “department” in that database. Creating a Database : Use the below SQL statement to create a datab
2 min read
SQL Query to Get Yesterday and Tomorrow
Queries help the users to interact with a database for creating, insertion, deleting, updating data in a database with different queries. In this article let us see how to get yesterday and tomorrow with respect to the given dates in the database. Example - Given date: 2021-03-23Yesterday: 2021-03-22 MondayTomorrow: 2021-03-24 Wednesday Creating a
2 min read
SQL Query for Finding Maximum Values in Rows
SQL stands for Structured Query Language and is used to create, maintain and retrieve the data from relational databases. Relational Database Management Systems (RDBMS) like MySQL, MS Access, Oracle, and SQL Server use SQL as their standard database language. Here we are going to see the SQL query for Finding Maximum Values in Rows. Let us understa
3 min read
SQL Query to Delete Duplicate Columns
Through this article, we will learn how to delete duplicate columns from a database table. As we know that duplicity in our database tends to be a waste of memory space. It records inaccurate data and is also unable to fetch the correct data from the database. To remove the duplicate columns we use the DISTINCT operator in the SELECT statement as f
2 min read
What are the Best Ways to Write a SQL Query?
An SQL Query is used to retrieve the required data from the database. However, there may be multiple SQL queries that yield the same results but with different levels of efficiency. An inefficient query can drain the database resources, reduce the database speed or result in a loss of service for other users. So it is very important to optimize the
9 min read
SQL Query to Exclude Multiple Values
To exclude multiple values to be fetched from a table we can use multiple OR statements but when we want to exclude a lot of values it becomes lengthy to write multiple AND statements, To avoid this we can use the NOT IN clause with the array of values that need to be excluded with the WHERE statement. In this article let us see the SQL query to ex
2 min read
Query Execution Engine in SQL
In this article, we will be discussing Query-Execution Engine in SQL and all aspects when we run any query. When we run any query to fetch the data from the database then at the same time many things happen simultaneously to get the desired results. Let's discuss what happens when we submit a query in brief and then we will discuss all phases of qu
3 min read
Features of Structured Query Language (SQL)
Here are some key features of Structured Query Language (SQL): Data Definition Language (DDL): SQL provides a set of commands to define and modify the structure of a database, including creating tables, modifying table structure, and dropping tables.Data Manipulation Language (DML): SQL provides a set of commands to manipulate data within a databas
4 min read
SQL Query to Display Last 5 Records from Employee Table
Summary : Here we will be learning how to retrieve last 5 rows from a database table with the help of SQL queries. The Different Approaches we are going to explore are : With the help of LIMIT clause in descending order.With the help of Relational operator and COUNT function.With the help of Prepared Statement and LIMIT clause. Creating Database :
4 min read
SQL Query to Find the Average Value in a Column
In this article, we are going to see how to find the average value in a column in SQL. A column in the SQL table is the vertical catalog structure. In this article, we will be using the Microsoft SQL Server as our database. For the purpose of example, we will be creating a sample table and performing the same operations on it. Table Definition: We
3 min read
SQL | Query to select NAME from table using different options
Let us consider the following table named "Geeks" : G_ID FIRSTNAME LASTNAME DEPARTMENT 1 Mohan Arora DBA 2 Nisha Verma Admin 3 Vishal Gupta DBA 4 Amita Singh Writer 5 Vishal Diwan Admin 6 Vinod Diwan Review 7 Sheetal Kumar Review 8 Geeta Chauhan Admin 9 Mona Mishra Writer SQL query to write “FIRSTNAME” from Geeks table using the alias name as NAME.
2 min read
Article Tags :