SQL
3. Database Queries

Database Queries

Here are some examples of how to query the database using the sql function.

Basic Query on DB

database-query.sql
CREATE DATABASE school;

Syntax: CREATE DATABASE database_name;

💡
This creates a DATABASE by name school!

Creating DB with IF NOT EXISTS

database-query.sql
CREATE DATABASE IF NOT EXISTS school;

Syntax: CREATE DATABASE IF NOT EXISTS database_name;

💡
This creates a DATABASE by name school if it does not exist!

MySQL Workbench Example:

MySQL

This give us warning not error! If the database already exists, it will not create a new one. Instead, it will return a message saying that the database already exists.

USE DB

Inorder to perform operations on a database, we need to use it.

database-query.sql
USE school;

Syntax: USE database_name;

Show Databases

database-query.sql
SHOW DATABASES;

MySQL Workbench Example:

MySQL

💡
This will show all the databases in the server!