-
Notifications
You must be signed in to change notification settings - Fork 14
Home
Welcome to the SQL-tutorial wiki!
In SQL, you can perform various operations related to databases, such as creating, dropping, selecting, renaming, showing, and backing up databases. Here are examples of SQL commands for each of these operations:
-
Create Database: To create a new database, you can use the
CREATE DATABASE
statement.CREATE DATABASE mydatabase;
-
Drop Database: To delete an existing database, you can use the
DROP DATABASE
statement. Be cautious when using this command, as it permanently deletes the database and its data.DROP DATABASE mydatabase;
-
Select Database: To select a specific database for subsequent SQL operations, use the
USE
statement.USE mydatabase;
-
Rename Database: SQL does not provide a direct command to rename a database. However, you can achieve this by creating a new database with the desired name and then copying or migrating the data from the old database to the new one. The exact process can vary depending on your database management system (e.g., MySQL, PostgreSQL, SQL Server).
-
Show Databases: The command to list all available databases can vary depending on your database management system. Here are a few examples:
-
MySQL and MariaDB:
SHOW DATABASES;
-
PostgreSQL:
SELECT datname FROM pg_database;
-
SQL Server:
SELECT name FROM sys.databases;
-
-
Backup Database: To create a backup of a database, you typically use database-specific tools or commands. The method varies depending on the database system you're using. Here's a common example using the
mysqldump
utility for MySQL:mysqldump -u username -p mydatabase > backup.sql
This command exports the database
mydatabase
to a SQL file namedbackup.sql
. You'll be prompted to enter the password for the database user.
Remember to replace mydatabase
with the actual name of your database and username
with a valid database user in the examples above. Additionally, database management systems may have their own specific syntax and tools for these operations, so consult the documentation for your specific database system for more details.