Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA
The MySQL Database System Database: collection of structured records MySQL = My Structured Query Language Database developed in the mid-90s but based on Relational Database Management System (RDBMS) and IBM’s SQL dating from the 70s
MySQL Usage Open Source and very popular as a database support to dynamic web applications – 6 million registered installations Popularly bundled with other Open Source solutions for web server, etc (e.g. LAMP) Accessible through a command line terminal or web client
MySQL in the DCU Context Accessible by command line SSH to student.dcu.ie Connect to mysql database Set up tables using commands This is not usual and in most cases a client like PHPMyAdmin is used!
Database Activities Use a database Create a table Alter/Delete tables Add or update records Delete records Read records
Selecting a DB and TABLE (If you have sufficient permissions on the system) you can create DATABASEs – By default on the DCU server you have one DATABASE named with your username CREATE DATABASE cm378; USE cm378; It’s unlikely you’ll need to create and use any other DATABASE Within each database, you can have unlimited TABLEs
What is a TABLE Think of it like an excel worksheet Columns are the categories (fields) Rows are the entries (records)
Working with a TABLE To create a table, its fields have to be defined (name and type) – CREATE TABLE addressbook (id INT, firstname TEXT, surname TEXT, TEXT); To delete, use DROP – DROP TABLE addressbook; To check the format, use DESCRIBE – DESCRIBE addressbook;
Adding a Record To add a new record, use the INSERT command: – Format is always: INSERT INTO table_name (field_name) VALUES (“value”); – Not all fields need to be filled! – INSERT INTO addressbook (id, firstname, surname) VALUES (5, “Bob”, “Loblaw”);
Updating an existing record To change an existing record, use UPDATE This command specifies the table to update and uses SET to set a field in a record to a new value. The WHERE command can then be used to locate which record to change: UPDATE addressbook SET WHERE surname = “Loblaw”;
Deleting a record A record can be deleted using the DELETE command to specify which TABLE to delete from and a WHERE command to specify the record(s) to delete – DELETE FROM addressbook WHERE id=3;
Selecting a record: Search The SELECT command can be used to search for a particular record by matching the value in a field – SELECT * FROM addressbook WHERE id=2; Particular fields of a record can be recalled: – SELECT firstname, surname FROM addressbook WHERE id=4;
Selecting a record: Search Instead of matching an exact field entry, MySQL can search for a partial match using the % wildcard: – SELECT * FROM addressbook WHERE LIKE “%dcu.ie”;
Selecting a record: Search Where multiple records are returned, their order can be set using a field: – SELECT * FROM addressbook WHERE LIKE ORDER BY firstname; The order can be reversed by using a command to order descending (ORDER BY field DESC;)