Basics of PHP & MySQL
Basics of PHP & MySQL
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:
With the command above, we created a table with the name “nacp” 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:
• 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.
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
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:
mysql> INSERT INTO movies (mtitle, myear) VALUES ('Rear Window',
1954),
('To Catch A Thief', 1955), ('The Maltese Falcon', 1941),
('The Birds', 1963), ('North By Northwest', 1959),
('Casablanca', 1942), ('Anatomy Of A Murderer', 1959);
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:
• Using built in functions (To see other built in functions go to the relevant link at
the end of this handout):
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.