Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA

Similar presentations


Presentation on theme: "Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA"— Presentation transcript:

1 Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA donal.mulligan@dcu.ie

2 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

3 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

4 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!

5 Database Activities Use a database Create a table Alter/Delete tables Add or update records Delete records Read records

6 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

7 What is a TABLE Think of it like an excel worksheet Columns are the categories (fields) Rows are the entries (records)

8 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, email TEXT); To delete, use DROP – DROP TABLE addressbook; To check the format, use DESCRIBE – DESCRIBE addressbook;

9 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”);

10 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 email=“bob.loblaw@blah.com” WHERE surname = “Loblaw”;

11 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;

12 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;

13 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 email LIKE “%dcu.ie”;

14 Selecting a record: Search Where multiple records are returned, their order can be set using a field: – SELECT * FROM addressbook WHERE email LIKE “%@%” ORDER BY firstname; The order can be reversed by using a command to order descending (ORDER BY field DESC;)


Download ppt "Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA"

Similar presentations


Ads by Google