Creating & Opening a Database
A fresh MySQL server holds no data of yours. Before there can be a table of students there has to be a database to keep it in — a container with a name, holding all the tables that belong to one job. Three commands do the whole of it.
1A database is a container
The school's data is not one table. There will be students, teachers, subjects, maybe library books. They belong together, so they go in one database, named after the school:
2Making one
Query OK means it worked. Ignore “1 row affected” — no row of yours was touched; MySQL is counting an entry it made in its own records.
lambdalab or school are good; My Database is not, because the space would have to be quoted every single time you referred to it.3Seeing what exists
There is lambdalab, so it really was created. The four names you did not choose — information_schema, mysql, performance_schema and sys — belong to MySQL itself and should be left alone.
4Opening one
Creating a database does not put you inside it. A server can hold many, so every query needs to know which one you mean. You say so once, with use:
From now until you change it again, every table you create and every query you run applies to lambdalab. Notice that the reply is not a resultset — there is nothing to show, so MySQL just says what happened.
ERROR 1046 (3D000): No database selected. The fix is not to rewrite the query — it is to run use lambdalab; first.5What is inside it
Empty set is the right answer, not a failure. The database was created a moment ago and nothing has been put in it yet. It is a labelled, empty container.
Once a table exists, the same command lists it:
The column heading is built from the database's name, which is a quiet way of confirming which database you are actually in.
6The order, every time
Make the container. Once, ever.
Open it. Every session, before anything else.
Build a table inside it. Also once, ever — not every session.
Check what is in there.
create database is run once in the life of the database. use is run every time you open the MySQL client, because closing the client forgets which database you had chosen.7Recap
Makes a new, empty database.
Lists every database on the server.
Opens one. Replies 'Database changed'.
Lists the tables in the open database.
You have just run create database school; What must you run before creating a table in it?
show tables; replies 'Empty set'. What does that mean?
Which reply tells you use worked?