Create a table

The command below can be pasted into the form on /data/index.php;

CREATE TABLE Patients(
  Patient     INT AUTO_INCREMENT,
  FirstName   VARCHAR(15),
  LastName    VARCHAR(15),
  Birthdate   DATE,
  Gender      VARCHAR(1),
  PRIMARY KEY(Patient)
)

This creates a table called Patients with four fields: The key, Patient, is an integer that is increment automatically. The first record you add will be 1, then 2, etc. The FirstName and LastName are strings that can have a varying number of letters with a maximum of 15 letters. The birthdate is a date and will be stored in the database in the format year-month-day, for example 1960-01-29 for january 29th, 1960. Gender will be "M" for male or "F" for female. We have allowed the gender to be blank if we don't know the gender. (For example we may have a patient named Chris, or Robin and don't know the gender when we create the record in the database.)

You need to get this working before you continue. If you get an error message you need to check the password, database name etc. in the connection file. This information is case sensitive!

Next, copy the file createPatients to your data folder, changing the name from .txt to .php, then open that page in the browser to execute the code. This file has an array of female names, an array of male names, an array of last names, and an array of diseases. When you execute the code it creates 100 random patients, creates a table called Diseases and assigns 50 diseases to randomly selected patients from the first 100. Some of the patients may have no diseases, other may have several. These two tables will let use learn about some of the MySQL commands without having to tediously type in a bunch of data. Obviously, this is not the way that we would do this in a real life situation.

Once you have learned to query the database, add new records, and change the records, we will discuss doing this using forms and PHP to execute the queries.


INDEX, Introduction, Queries, The SELECT query, Functions, Select Fields
Next lesson: MySQL (continued)MAIN INDEX
EXAMPLES INDEX

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Queries