Now that you know how to enter a command, it's time to access a database.
Suppose you have a lot of pets in your home (your "zoo"), and you want to track all kinds of information about them. You can save your data by creating a table and load them according to the information you need, and then you can answer the different kinds of questions about your animal by retrieving the data from the table. This section shows how to do all of these things:
How to create a database
How to create a database table
How to load data into a database table
How to retrieve data from a table in various ways
How to use multiple tables
The zoo database will be simple (intentional), but it is not difficult to think of it as a real-world situation where similar types of databases can be used. For example, such a database could be used by a farmer to track livestock, or a veterinary surgeon to track the records of a diseased animal.
Use the show statement to find out what databases are currently on the server:
mysql> SHOW DATABASES;
+----------+
| Database |
+----------+
| mysql |
| test |
| tmp |
+----------+
The list of databases may be different on your machine, but MySQL and test databases are likely to be in between. MySQL is required because it describes user access rights, and the test database is often provided to users as a workspace to try their skill.
If the test database exists, try to access it:
mysql> Use test
Database changed
Note that use, like quit, does not require a semicolon. (If you like, you can use a semicolon to terminate such a statement; This is not a hindrance) the USE statement also has another special place on it: it must be given on a single line.
You can use the test database (if you can access it) in a later example, but anything you create in the database can be deleted by someone else who accesses it, and for that reason, you might want to ask your MySQL administrator for permission to use a database of your own. Assuming you want to invoke your menagerie, the administrator needs to execute one of these commands:
Mysql> GRANT all on menagerie.* to Your_mysql_name;
Here your_mysql_name is the MySQL username assigned to you.
8.4.1 Create and select a database
If the administrator creates a database for you when you set your permissions, you can start using it. Otherwise, you need to create it yourself:
mysql> CREATE DATABASE Menagerie;
Under UNIX, database names are case-sensitive (unlike SQL keywords), so you must always refer to your database as menagerie, not Menagerie, menagerie, or some other variant. The same is true for table names. (This restriction does not apply under Windows, although you must use the same case to refer to the database and tables in a given query.) )
Creating a database is not selected to use it, you have to do it explicitly. To make menagerie known as the current database, use this command:
Mysql> Use Menagerie
Database changed