Beginners Guide to SQL – SQL CREATE DATABASE Statement

Hits: 13

(SQL Tutorials for Citizen Data Scientist)

SQL CREATE DATABASE Statement

In this tutorial you will learn how to create database in a relational database management system like, MySQL, SQL Server, etc. using SQL.

Creating a Database

Before doing anything with the data we must need to create a database first. We’re assuming that you already have a MySQL, or SQL Server available for your use, as well as you’ve all the necessary privileges, if not please check out the getting started guide.

The SQL CREATE DATABASE statement is used to create a database.

Syntax

The basic syntax for creating a database can be given with:

CREATE DATABASE database_name;

The following SQL statement creates a database named demo:

CREATE DATABASE demo;

Creating a database does not select it for use. So, before moving further we must need to select the target database with the USE statement. For example, the USE demo; command sets the demo database as target database for all future commands.

Note: In Unix, the database and table names are case-sensitive, so you must always refer to your database as demo, not as DemoDEMO, or something else. But, SQL keywords are case-insensitive, like CREATE DATABASE is same as create database.


Creating Database in MySQL

Let’s create a database in MySQL using the command-line tool.

 

Step 1: Invoke the MySQL command-line tool

To invoke the MySQL command line, we’ve to log in to the MySQL server first. To log in as root user, type the following command in terminal and press enter. You will be asked for your password. Enter your password and press enter, if it is correct the mysql> prompt will appear, via which you will be able to issue SQL statements and view the results.

shell> mysql -u root -p

Step 2: Creating a MySQL Database

Now, execute the following command to create the database named demo.

mysql> CREATE DATABASE demo;

If the database created successfully you’ll see the output something like this:

Query OK, 1 row affected (0.03 sec)

If you try to create a database that is already exists you’ll get an error message. To avoid this in MySQL you can use an optional clause IF NOT EXISTS as follow:

mysql> CREATE DATABASE IF NOT EXISTS demo;

Step 3: Selecting the Database

Type the following command and press enter. You will see the output “Database changed”. Now our demo database is selected as default database for all future operations.

mysql> USE demo;

Beginners Guide to SQL – SQL CREATE DATABASE Statement

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!