Presentation is loading. Please wait.

Presentation is loading. Please wait.

2010/11 : [1]Building Web Applications using MySQL and PHP (W1)MySQL Recap.

Similar presentations


Presentation on theme: "2010/11 : [1]Building Web Applications using MySQL and PHP (W1)MySQL Recap."— Presentation transcript:

1 2010/11 : [1]Building Web Applications using MySQL and PHP (W1)MySQL Recap

2 2010/11 : [2]Building Web Applications using MySQL and PHP (W1)MySQL Recap We need to remember.. Creating and populating databases: CREATE TABLE, INSERT, UPDATE, DELETE. Retrieving data from databases: SELECT, WHERE clauses, table JOIN s. Using the command line and MySQL query browser.

3 2010/11 : [3]Building Web Applications using MySQL and PHP (W1)MySQL Recap The manual will help! If in doubt, refer to the MySQL manual. www.mysql.com

4 2010/11 : [4]Building Web Applications using MySQL and PHP (W1)MySQL Recap Tables: what they are Tables are the basic units of a MySQL database. NameAgeGender John24Male Emily21Female James22Male Table students record field

5 2010/11 : [5]Building Web Applications using MySQL and PHP (W1)MySQL Recap Field Types INT an integer value FLOAT a floating point number DECIMAL(M,D) fixed decimal place num. CHAR(M) string of fixed length M. VARCHAR(M) string of variable length TEXT long variable length of text. ENUM(‘opt1’,‘opt2’,..) A string from a set of values DATE stores a date value. DATETIME stores a date and time value

6 2010/11 : [6]Building Web Applications using MySQL and PHP (W1)MySQL Recap Creating a Table The MySQL syntax: CREATE TABLE tbl_name (field definitions); e.g. Check table using DESCRIBE tbl_name NameAgeSex John24male Emily21female James22male Table students CREATE TABLE students ( name VARCHAR(100), age TINYINT, sex ENUM(‘male’,‘female’) );

7 2010/11 : [7]Building Web Applications using MySQL and PHP (W1)MySQL Recap Primary Keys When creating a table, you can specify in MySQL which field(s) is the primary key. Specifying a PRIMARY KEY forces every entry to be unique, and automatically defines the field(s) as NOT NULL. idnameagegender 1John24male 2Emily21female 3James22male Table students CREATE TABLE students ( id INT NOT NULL, name VARCHAR(50), age TINYINT, sex ENUM(‘male’,‘female’), PRIMARY KEY(id) );

8 2010/11 : [8]Building Web Applications using MySQL and PHP (W1)MySQL Recap Using AUTO_INCREMENT The AUTO_INCREMENT field attribute can be used to automatically generate a unique identity for each row. e.g. There can only be one AUTO_INCREMENT field and it must be defined as a PRIMARY KEY. idnameagegender 1John24male 2Emily21female 3James22male CREATE TABLE students ( id INT AUTO_INCREMENT, name VARCHAR(50), age TINYINT, sex ENUM(‘male’,‘female’), PRIMARY KEY(id) ); Table students

9 2010/11 : [9]Building Web Applications using MySQL and PHP (W1)MySQL Recap Adding Data to a Table New records are added to a table using the INSERT command. There is more than one way this can be used: INSERT INTO tbl_name (field_name) VALUES (expr) INSERT INTO tbl_name SET (field_name = expr) Easiest way to understand is through example…

10 2010/11 : [10]Building Web Applications using MySQL and PHP (W1)MySQL Recap Adding Data Example INSERT INTO students VALUES (‘John’, 24, ‘male’); …or… INSERT INTO students (name, age, sex) VALUES (‘John’, 24, ‘male’); …or… INSERT INTO students SET Name = ‘John’, Age = 24, Sex = ‘male’; NameAgeSex John24male Emily21female James22male Table students

11 2010/11 : [11]Building Web Applications using MySQL and PHP (W1)MySQL Recap The UPDATE command To update existing data, use the UPDATE command. UPDATE students SET age=25 WHERE name=‘John’; If there is no WHERE clause all rows are updated.

12 2010/11 : [12]Building Web Applications using MySQL and PHP (W1)MySQL Recap Deleting Records Records can be deleted using the DELETE statement. DELETE FROM students;..deletes all the records in table ‘students’. DELETE FROM students WHERE name=‘Emily’;..deletes the records in table ‘students’ where the ‘name’ field is equal to ‘Emily’.

13 2010/11 : [13]Building Web Applications using MySQL and PHP (W1)MySQL Recap Exercise Hands On Exercise Section A MySQL Recap: Database Construction

14 2010/11 : [14]Building Web Applications using MySQL and PHP (W1)MySQL Recap Simple SELECT The SELECT statement is the most common tool used to query a database. SELECT field FROM table This returns all the rows from a table, but only for the particular field specified. SELECT field1,field2 FROM table Can specify multiple fields by separating them with commas.

15 2010/11 : [15]Building Web Applications using MySQL and PHP (W1)MySQL Recap SELECT filters Displaying all the rows in a table can be handy, but if we have a million rows… Add a ‘filter’ to the SELECT statement, by adding the optional WHERE clause. SELECT field1,field2 FROM table WHERE rule;

16 2010/11 : [16]Building Web Applications using MySQL and PHP (W1)MySQL Recap SELECT Examples Select all student names over 21 yrs old: SELECT name FROM students WHERE age > 21; Select all female student names & ages: SELECT name,age FROM students WHERE sex = ‘female’; NameAgeSex John24male Emily21female James22male Table students

17 2010/11 : [17]Building Web Applications using MySQL and PHP (W1)MySQL Recap SELECT Compound Filters Can combine multiple logical rules into a single query using the logical operators AND, OR and NOT. –AND connects rules together such that ALL the rules must be true before the row is returned. –OR connects rules together such that ANY of the rules must be true before the row is returned. –NOT does the opposite of whatever comparison is being done.

18 2010/11 : [18]Building Web Applications using MySQL and PHP (W1)MySQL Recap SELECT Compound Filters Select all male student names over 21 yrs old: SELECT name FROM students WHERE age > 21 AND sex = ‘male’; NameAgeSex John24male Emily21female James22male Table students

19 2010/11 : [19]Building Web Applications using MySQL and PHP (W1)MySQL Recap Sorting Data Data can be sorted by use of ORDER BY clause added at the end of a query. e.g. SELECT fields FROM table ORDER BY field(s) The keywords ASC or DESC define the order ( ASC = ascending for numbers, alphabetical for text and DESC is the opposite).

20 2010/11 : [20]Building Web Applications using MySQL and PHP (W1)MySQL Recap SELECT sorting Select student names in order from youngest to oldest: SELECT name FROM students ORDER BY age ASC; NameAgeSex John24male Emily21female James22male Table students

21 2010/11 : [21]Building Web Applications using MySQL and PHP (W1)MySQL Recap Joining Tables To use multiple tables, you need to tell the database how to join the tables. An inner join connects only matching rows. A left or right join includes all the rows from one side of the join, and any matching rows from the other side of the join.

22 2010/11 : [22]Building Web Applications using MySQL and PHP (W1)MySQL Recap Example Tables To demo joins, we use 3 tables: CREATE TABLE vehicle ( reg VARCHAR(20) NOT NULL, make VARCHAR(50) NOT NULL, colour VARCHAR(30), price INT, owner VARCHAR(50), PRIMARY KEY (reg) ); CREATE TABLE driver ( name VARCHAR(50) NOT NULL, dob DATE NOT NULL, address_id TINYINT, PRIMARY KEY(name) ); CREATE TABLE address ( address_id TINYINT AUTO_INCREMENT, text VARCHAR(255) NOT NULL, postcode CHAR(8), PRIMARY KEY(address_id) );

23 2010/11 : [23]Building Web Applications using MySQL and PHP (W1)MySQL Recap Inner Join The join clause is included in the FROM clause. … FROM table1 JOIN table2 ON (rules) … So to join the tables vehicle and driver: SELECT * FROM vehicle JOIN driver ON (owner = name);

24 2010/11 : [24]Building Web Applications using MySQL and PHP (W1)MySQL Recap Alternative Inner Join SELECT * FROM vehicle, driver WHERE owner = name; Columns to include Tables to include Condition to relate the tables

25 2010/11 : [25]Building Web Applications using MySQL and PHP (W1)MySQL Recap Outer Joins An outer join extends the result of an inner join by including rows from one table that don’t have corresponding rows in the other table. In the modern join syntax this can be achieved by inserting the keyword RIGHT or LEFT in front of the keyword JOIN.

26 2010/11 : [26]Building Web Applications using MySQL and PHP (W1)MySQL Recap Left or Right? To decide whether to put in a LEFT or RIGHT keyword, you need to decide where the unmatched values that you want are. e.g. this example will return both the unmatched and matched values in the vehicle table: … FROM vehicle LEFT JOIN driver ON (owner = name); LEFT side of JOIN RIGHT side of JOIN

27 2010/11 : [27]Building Web Applications using MySQL and PHP (W1)MySQL Recap Exercise Hands On Exercise, Section B MySQL Recap: Data Retrieval

28 2010/11 : [28]Building Web Applications using MySQL and PHP (W1)MySQL Recap Review Recapped creating and populating databases: CREATE TABLE, INSERT, UPDATE, DELETE. Recapped retrieving data from databases: SELECT, WHERE clauses, table JOIN s. For more, look back at your notes from the DT module and consult the MySQL manual.


Download ppt "2010/11 : [1]Building Web Applications using MySQL and PHP (W1)MySQL Recap."

Similar presentations


Ads by Google