As defined before, schemas or databases— in MySQL, they are synonyms—are collections of tables with a common context, usually belonging to the same application. Actually, there are no restrictions around this, and you could have several schemas belonging to the same application if needed. However, for small web applications, as it is our case, we will have just one schema.
Your server probably already has some schemas.
They usually contain the metadata needed for MySQL in order to
operate, and we highly recommend that you do not modify them.
Instead, let's just create our own schema. Schemas are quite simple
elements, and they only have a mandatory name and an optional
charset. The name identifies the schema, and the charset defines
which type of codification or "alphabet" the strings should follow.
As the default charset is latin1
, if you
do not need to change it, you do not need to specify it.
Use CREATE SCHEMA
followed by the name of the schema in order to create the schema
that we will use for our bookstore. The name has to be
representative, so let's name it bookstore
. Remember to end your line with a
semicolon. Take a look at the following:
mysql> CREATE SCHEMA bookstore; Query OK, 1 row affected (0.00 sec)
If you need to remember how a schema was
created, you can use SHOW CREATE SCHEMA
to see its description, as follows:
mysql> SHOW CREATE SCHEMA bookstore \G *************************** 1. row *************************** Database: bookstore Create Database: CREATE DATABASE `bookstore` /*!40100 DEFAULT CHARACTER SET latin1 */ 1 row in set (0.00 sec)
As you can see, we ended the query with
\G
instead of a semicolon. This tells
the client to format the response in a different way than the
semicolon does. When using a command of the SHOW CREATE
family, we recommend that you end it
with \G
to get a better
understanding.
Tip
Should you use uppercase or lowercase?
When writing queries, you might note that we used uppercase for keywords and lowercase for identifiers, such as names of schemas. This is just a convention widely used in order to make it clear what is part of SQL and what is your data. However, MySQL keywords are case-insensitive, so you could use any case indistinctively.
All data must belong to a schema. There cannot be data floating
around outside all schemas. This way, you cannot do anything unless
you specify the schema you want to use. In order to do this, just
after starting your client, use the USE
keyword followed by the name of the schema. Optionally, you could
tell the client which schema to use when connecting to it, as
follows:
mysql> USE bookstore; Database changed
If you do not remember what the name of your
schema is or want to check which other schemas are in your server,
you can run the SHOW SCHEMAS;
command to
get a list of them, as follows:
mysql> SHOW SCHEMAS; +--------------------+ | Database | +--------------------+ | information_schema | | bookstore | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec)