Creating a Database and a Sample Table
Problem
You want to create a database and to set up tables within it.
Solution
Use a CREATE DATABASE statement
to create a database, a
CREATE TABLE statement
for each table you want to use, and
INSERT to add records to the tables.
Discussion
The GRANT statement used in the previous section
defines privileges for the cookbook database, but
does not create it. You need to create the database explicitly before
you can use it. This section shows how to do that, and also how to
create a table and load it with some sample data that can be used for
examples in the following sections.
After the cbuser account has been set up, verify
that you can use it to connect to the MySQL server. Once
you’ve connected successfully, create the database.
From the host that was named in the GRANT
statement, run the following commands to do this (the host named
after -h should be the host where the MySQL server
is running):
%mysql -h localhost -p -u cbuserEnter password:cbpassmysql>CREATE DATABASE cookbook;Query OK, 1 row affected (0.08 sec)
Now you have a database, so you can create tables in it. Issue the
following statements to select cookbook as the
default database, create a simple table, and populate it with a few
records:[2]
mysql>USE cookbook;mysql>CREATE TABLE limbs (thing VARCHAR(20), legs INT, arms INT);mysql>INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2);mysql>INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0);mysql>INSERT INTO ...