Creating A Database

Goals

  • Create a database

  • Lets create our Suggestotron database add some example data

Steps

Step 1

Run the MySQL command line tool

Type this in the terminal:
mysql -u root -p

You will be prompted for a password. The password is root.

Using the root username for a real website is a bad idea. For more information on adding a new user, see this tutorial.

Step 2

Create your database

CREATE DATABASE suggestotron;
USE suggestotron;

Step 3

Next, create our table, it's going to look like this:

topics
id
title
description
CREATE TABLE topics ( 
    id INT unsigned NOT NULL AUTO_INCREMENT, 
    title VARCHAR(255) NOT NULL, 
    description TEXT NULL, 
    PRIMARY KEY(id) 
);

Step 4

Now we can insert our test data

INSERT INTO topics (
    title,
    description
) VALUES (
    'Make Rainbow ElePHPants',
    'Create an elePHPant with rainbow fur'
);

INSERT INTO topics (
    title,
    description
) VALUES (
    'Make Giant Kittens',
    'Like kittens, but larger'
);

INSERT INTO topics (
    title,
    description
) VALUES (
    'Complete PHPBridge',
    'Because I am awesome'
);

After each INSERT you will see something like:

Approximate expected result:
Query OK, 1 row affected (0.02 sec)
The result you get may differ and is not important.

Step 5

To view our data, we can SELECT it from the table:

SELECT * FROM topics;
Expected result:
+----+-------------------------+--------------------------------------+
| id | title                   | description                          |
+----+-------------------------+--------------------------------------+
|  1 | Make Rainbow ElePHPants | Create an elePHPant with rainbow fur |
|  2 | Make Giant Kittens      | Like kittens, but larger             |
|  3 | Complete PHPBridge      | Because I am awesome                 |
+----+-------------------------+--------------------------------------+
3 rows in set (0.00 sec)

Step 6

We are done with the database for now. To quit, type the following:

\q

Explanation

You have now create your first database, your first table, and your first rows of data!

We will be accessing this data via our PHP code in our application. Not only will our application be able to read it, but it will be able to create new data, edit, and delete existing data.

Next Step: