SQL
SELECT DATABASE statement is used to switch to a specific database, allowing subsequent SQL commands to be executed within that selected database.
Now, let’s break down the syntax of the SELECT DATABASE
statement:
USE database_name;
To illustrate the concepts, let’s create a simple database named “school” with two tables: “students” and “courses.”
Sample Database:
students |
---|
student_id (PK) |
name |
age |
course_id (FK) |
courses |
---|
course_id (PK) |
course_name |
instructor |
credits |
USE school;
Output:
Database changed
In SQL the following syntax is used for renaming the database:
ALTER DATABASE old_database MODIFY NAME = new_database;
Replace old_database
with the current name of the database, and new_database
with the desired new name.
To rename a database in MySQL or MariaDB, you can use the RENAME DATABASE
statement. Note that this operation might require special privileges.
RENAME DATABASE old_database TO new_database;
In PostgreSQL, there is no direct SQL command to rename a database. However, you can achieve this by using the pg_dump
and pg_restore
utilities to create a backup of the original database and restore it with a new name.
pg_dump -U username -d old_database > dump.sql
CREATE DATABASE new_database;
psql -U username -d new_database < dump.sql
In SQL Server, you can use the ALTER DATABASE
statement to rename a database.
ALTER DATABASE old_database MODIFY NAME = new_database;
SQLite does not have a direct SQL statement to rename a database. To achieve this, you can attach the database with a new name and then detach the original one.
ATTACH DATABASE 'new_database.db' AS newdb;
-- Copy data from olddb to newdb
DETACH DATABASE olddb;
To rename a database in Oracle, you can use the NID
utility.
nid TARGET=SYS/oracle@old_database DBNAME=new_database