The Wayback Machine - https://web.archive.org/web/20250126171917/https://www.geeksforgeeks.org/sql-create-database/
Open In App

SQL CREATE DATABASE

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

The CREATE DATABASE statement is a foundational SQL command used to create new databases in SQL-based Database Management Systems (DBMS), including MySQL, PostgreSQL, SQL Server, and others. Understanding how to use this command effectively is crucial for developers, database administrators, and anyone working with relational databases.

In this article, we’ll provide a comprehensive overview of how to use the CREATE DATABASE command effectively. We will begin with the basics, explaining the syntax and structure of the command.

CREATE DATABASE in SQL

The CREATE DATABASE command establishes a new database within your SQL ecosystem. A database is a repository that organizes data in structured formats through tables, views, stored procedures, and other components.

Syntax:

The syntax to use the CREATE DATABASE command in SQL is:

CREATE DATABASE database_name;

SQL CREATE DATABASE Example

Let's look at examples of how to create a database in SQL and how to verify database creation by listing databases in the server.

Creating Your First SQL Database

To create a new database in SQL we use the CREATE DATABASE command and then we mention the name of the database. Note that blank spaces are not allowed in the name of the database, we can only use underscore (_). 

As an example, we will create a new database with the name "GeeksForGeeks".

SQL Query:

CREATE DATABASE GeeksForGeeks;

Output

creating new database
Creating a new database

Database Successfully Created!!

List Databases in SQL

Now we will verify whether the new database that we have just created has been successfully added to our system or not. 

We use the SHOW DATABASES command and it will return a list of databases that exist in our system. Now we will look for the name of the database (GeeksForGeeks) that we have just created.

SQL Query:

SHOW DATABASES;

Output

list of databases created
Database successfully created

As we can see the SHOW DATABASE command returned the list of all databases that exist in our system and in that list, the name of the database (GeeksForGeeks) that we have just created has also been added which verifies that the database has been successfully created.

USE Database in SQL

Once your database is created, we can switch to that database to begin adding tables, inserting data, and performing queries. To do this, use the USE command.

Syntax:

The syntax to use a database in SQL is:

USE database_name

After running this command, any subsequent queries will apply to the GeeksForGeeks database. This allows you to work within the scope of the newly created database.

Delete a Database in SQL

If you ever need to remove a database, the DROP DATABASE command can be used to delete the database and all its contents:

DROP DATABASE GeeksForGeeks;

Be cautious when using this command, as it permanently deletes the database and all of its data.

Important Points About SQL CREATE DATABASE

  • Database Naming Conventions: When establishing a database, it is essential to ensure that the name is unique within your SQL environment. While most SQL implementations treat database names as case-insensitive, maintaining consistency in the naming format is advisable. Avoid using spaces; instead, opt for underscores (_). Example: my_database is preferable to my database.
  • Maximum Length of Database Names: The typical maximum length for a database name in SQL is 128 characters. Ensure that your database name is both clear and concise while complying with this limit.
  • Permissions: It is imperative to possess the requisite privileges to create databases within your SQL environment. In many systems, the CREATE DATABASE command can only be executed by users with administrative privileges.
  • Case Insensitivity: Most SQL DBMSs consider database names to be case-insensitive; however, it is recommended to consistently use lowercase or follow a uniform formatting style throughout your environment.
  • Compatibility Across SQL Databases: While the CREATE DATABASE statement is generally similar across various SQL databases such as MySQL, PostgreSQL, and SQL Server, be mindful of minor syntactical differences that may exist in specific systems.

Conclusion

Creating a database in SQL is one of the first steps in database management and development. The CREATE DATABASE statement allows you to structure your data effectively by defining new databases within your SQL environment. By following the syntax and best practices outlined above, you can create well-organized databases that will serve as the foundation for your applications.

SQL databases, like MySQL, PostgreSQL, and SQL Server, offer slightly different implementations of the CREATE DATABASE command, so it’s important to familiarize yourself with the syntax and features of the specific DBMS you're using.


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg