Spreadsheet
programs (such as Microsoft Excel) have row numbers to identify what row a
certain bit of data is in.
Databases have a similar identifier, called the Primary Key. You can draw out your
database with relative ease - or you can create a mock up of it in a
spreadsheet.
How do I remember what
the primary key is when I make a new row of data?
When you create the tables, there are going to be more
options that you need to include to make your database a little easier to
use...specifically being AUTO_INCREMENT and NULL / NOT NULL. First, the code
example...
Real World Code
Example:
<?php
// Make a MySQL Connection
mysql_connect("localhost", "user",
"password") or die(mysql_error());
mysql_select_db("test_db") or
die(mysql_error());
// Create a MySQL table in the selected database
mysql_query("CREATE TABLE Employees(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
employee_id INT,
first_name
VARCHAR(50),
last_name
VARCHAR(50)")
or
die(mysql_error());
echo "Table Created!";
?>
Let’s run through this one, step by step.
First, we make a connection to the MySQL database and select
the database we want to work on.
Then, we make a table called “Employees”, with a column
called “id” that will automatically increment by 1 each time a new record is
created, has a NOT NULL value (this simply means that the “id” value is real
and searchable), and that the Primary Key is set to be the “id” field. Now, we
create our three data fields (employee_id, first_name, last_name), and issue
the good ol’ “or die” command to tell us if something went wrong.
Check out one of the best PHP Editors