A database is a container that holds all your tables. A table is a container for a subset of your data. A table holds data organized in rows and columns. Consider a column to be a piece of data that is an attribute of an entity. A row is a set of columns that define attributes of an entity
The relationships are conceptually shown below:
In this post, we’ll learn how to use the CREATE statement. We can use the CREATE statement to create a database.
-- The post queries are reproduced below for convenient copy/paste into the terminal. -- Query 1 CREATE DATABASE MovieIndustry; -- Query 2 CREATE DATABASE IF NOT EXISTS MovieIndustry; -- Query 3 SHOW DATABASES; -- Query 4 DROP DATABASE MovieIndustry;
Let’s create the database first. We’ll name it MovieIndustry.
CREATE DATABASE MovieIndustry;
CREATE DATABASE IF NOT EXISTS
If we attempt to re-create an existing database, MySQL will report an error. We can circumvent this error by using the IF NOT EXISTS command as shown below:
CREATE DATABASE IF NOT EXISTS MovieIndustry;
The IF NOT EXISTS clause is useful when writing scripts that may be invoked repeatedly and will abort when creating a database that already exists.
When we create a database, MySQL creates a physical directory by the same name. Directories are case-sensitive in Linux and Mac, and correspondingly, MySQL will take the case into account. On a Linux or Mac, MovieIndustry isn’t the same as movieindustry as shown below:
Windows operating system is case-insensitive, and we can use MovieIndustry and movieindustry interchangeably.
Now you can inspect the database you created by using the following command:
The output lists all the databases in the system and one of them is MovieIndustry.
We can drop a database using the DROP statement. All the tables, indexes, and other structures created within the database are also deleted.
DROP DATABASE MovieIndustry;