PHP & Mysql Lab 2: Amazon Barnes & Noble
PHP & Mysql Lab 2: Amazon Barnes & Noble
PHP & Mysql Lab 2: Amazon Barnes & Noble
This handout is based on the book “How to do Everything with PHP & MySQL” by
Vikram Wasvani, from McGraw Hill / Osbourne publications. You can buy the book
from Amazon. The book is also available at Barnes & Noble.
We will use the MySQL Command Line Client to run our SQL commands. The latest
MySQL server is available for download at:
http://dev.mysql.com/downloads/mysql/5.0.html.
We will use MySQL Administrator graphical administration 1.1 client to access the
MySQL server. You can download it from:
http://dev.mysql.com/downloads/administrator/1.1.html.
After installing the MySQL server and the MySQL Adminnistrator, open the MySQL
Administrator and enter the password you have created during the installation.
You first need to create a database in which you will store your tables. This can be
considered analogous to opening a blank database in Microsoft Access. To create a
database we use the CREATE DATABASE command:
Before starting to work (e.g. create tables, write queries etc.) with your database, you
must indicate the database you are going to use. To do this, we use the USE command:
Now, all the operations will be implemented on the database with the name moviedb.
There are no tables in this database yet. To create tables, we use the CREATE TABLE
command:
With the command above, we created a table with the name “movies” which has 3 fields:
mid, mtitle and myear. In MySQL, we must specify a data type for each field. The most
commonly used data types in MySQL are given in the table below:
In addition to the data type of the fields, we can also specify field modifiers and keys
when creating a table:
Is the field allowed to be empty? We can specify this using the modifiers NULL
and NOT NULL.
Using the DEFAULT modifier we can specify a default value for the field.
AUTO_INCREMENT modifier enables us to create values for a field
automatically.
If we want the values for a field to be unique, we can use the UNIQUE modifier.
Altering Tables
We use the command ALTER TABLE to modify the design of the tables after we create
them. There are various types of altering operations in MySQL:
Renaming a table:
or
Renaming a field:
Deleting a field:
Deleting a database:
mysql> DROP DATABASE moviesdb;
mysql> UPDATE movies SET mtitle = ‘Maltese Falcon, The’ WHERE mtitle =
‘The Maltese Falcon’;
Performing Queries
Before querying the database, let’s enter more records in our tables:
We can use the SELECT command to extract information from the existing tables in our
database. Following are some examples of SQL SELECT queries that is supported by
MySQL:
Filtering records:
Using operators (To see a full list of operators visit the link at the end of this
handout):
Eliminating duplicates:
Using built in functions (To see other built in functions go to the relevant link at
the end of this handout):
Grouping records:
Joining tables:
Using subqueries (To see other examples go to the link at the of this handout):
Using aliases:
Quiz:
Install MySQL and create the ‘moviedb’ database in MySQL, following the instruction of
the lab notes. Return the screenshot of all the three tables created.