Are you

Introduction to Database


A database is a logically organized collection of structured data kept electronically in a computer system. A database management system is usually in charge of a database (DBMS). The data, the DBMS, and the applications that go with them are referred to as a database system, which is commonly abbreviated to just database.

To make processing and data querying efficient, data in the most common types of databases in use today is often described in rows and columns in a sequence of tables. Data may then be accessed, managed, updated, regulated, and organized with easily. For writing and querying data, most databases employ structured query language (SQL).

What is SQL (Structured Query Language)?

SQL is a computer programming language that nearly all relational databases use to query, manipulate, and define data as well as manage access. SQL was first developed in the 1970s at IBM with Oracle as a key contribution, leading to the creation of the SQL ANSI standard. Since then, SQL has developed a slew of extensions from businesses including IBM, Oracle, and Microsoft. Despite the fact that SQL is still commonly used today, other programming languages are emerging.

SQL Create Database Statement

The CREATE DATABASE statement is used to create a new SQL database. In SQL, the 'Create Database' statement is a first step for storing the structured data in the database.

This SQL statement is used by database developers and users to create new databases in database systems. It creates a database give it the name supplied in the Create Database statement.

Syntax of Create Database statement in SQL

CREATE DATABASE Database_Name;

Example

CREATE DATABASE myFirstDB;

Tip: Make sure you have admin privilege before creating any database. Once a database is created, you can check it in phpMyAdmin if you can access if or check in the list of databases with the following SQL command: SHOW DATABASES;

SQL does not allow developers to create the database with the existing database name. Suppose if you want to create another Student database in the same database system, then the Create Database statement will show the following error in the output:

Can't create database 'myFirstDB'; database exists

If you want to replace the existing myFirstDB database, type the SQL statement below:

CREATE OR REPLACE DATABASE myFirstDB ;

SQL DROP DATABASE Statement

The DROP DATABASE statement is used to drop an existing SQL database. The SQL Drop Database statement removes an existing database from the database system permanently. If stored in the database, this statement deletes all views and tables, thus use caution when executing this query in SQL.

The following are the most critical points to understand before deleting a database from a database system:
  • This statement clears all of the data in the database. You should keep a backup of the data from the database you wish to delete if you want to restore the deleted data in the future.
  • Another key element to remember is that you cannot delete a database that is already being used by another database user from the system. If you do so, the drop statement will display the following error:
Cannot drop database "name_of_the_database" because it is currently in use.

Syntax of Drop Database Statement in SQL

DROP DATABASE Database_Name;

Example

DROP DATABASE myFirstDB;

SQL RENAME Database

Database users and administrators may desire to change the database's name for technical reasons in some cases. As a result, the SQL Rename Database command is used to rename an existing database.

Syntax of Rename Database in SQL

ALTER DATABASE old_database_name MODIFY NAME = new_database_name;

Syntax of Rename Database in MySQL

RENAME DATABASE old_database_name TO new_database_name;

Example

ALTER DATABASE myFirstDB MODIFY NAME = mySecondDB ;
Next

Courses