PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each...
-
Upload
kerrie-richardson -
Category
Documents
-
view
218 -
download
0
Transcript of PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each...
![Page 1: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/1.jpg)
PHP
![Page 2: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/2.jpg)
Basic SQL
![Page 3: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/3.jpg)
definition: Database
• A database which structures data in the form of tables. Each table contains information relevant to a particular feature, and is linked to other tables by a common value. For example, two attribute tables could be linked to a spatial data table via a Geocode, such as the postcode.
![Page 4: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/4.jpg)
A very short explanation
• In one database. There are many tables.• One table represents a set of entity. For example ‘People’ ,
‘Cars’ , ‘Movies’• Table has many rows. One row in table represents instance of
entities. For example Toni Meechai’ , ‘Kobota Sedan’ , ‘Twilight’• Table also has many column. One column represents property
of an instance. For example Nationality’ , ‘How fast’ , ‘How boring it is’
• When you want get or do something with Database Tables, you can query it like ‘Please show me all people’. Unfortunately, you have to follow SQL format.
![Page 5: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/5.jpg)
Play with PHP MyAdmin
• Pay attention to demonstration.• Try to understand ideas of ‘Database’ , ‘Table’,
‘Row’, ‘Basic usage of SQL’.• Learners can follow this article: http://www.php-editors.com/articles/sql_phpmyadmin.php
![Page 6: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/6.jpg)
SQL : Selecting data
![Page 7: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/7.jpg)
Selecting from 1 table
• SELECT * FROM COUNTRY;• SELECT * FROM COUNTRY
WHERE CONTINECT=‘ASIA’;• SELECT CODE,NAME,CONTINENT,POPULATION
FROM COUNTRYWHERE CONTINENT=‘ASIA’;
![Page 8: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/8.jpg)
• SELECT NAME,POPULATIONFROM COUNTRYWHERE CONTINENT=‘ASIA’ORDER BY POPULATION DESC;
• If we want to reverse it, change DESC to ASC instead
![Page 9: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/9.jpg)
• If it has many rows, we want just 10 rows• SELECT NAME,POPULATION
FROM COUNTRYWHERE CONTINENT=‘ASIA’ORDER BY POPULATION DESCLIMIT 10;
![Page 10: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/10.jpg)
• If we want to change column name• SELECT NAME AS “ประเทศ สุ่ม”
FROM COUNTRYWHERE CONTINENT=‘ASIA’ORDER BY RAND()LIMIT 10;
• NOTE, If you want to change name to be something like ประเทศ สุ่ม, it will throws an error
• Because that word contains whitespace.
![Page 11: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/11.jpg)
• List all countries started with ‘t’SELECT NAME FROM COUNTRYWHERE NAME LIKE ‘T%’;
• List all countries ended with ‘land’SELECT NAME FROM COUNTRYWHERE NAME LIKE ‘%land’;
• List all countries contains ‘ko’SELECT NAME FROM COUNTRYWHERE NAME LIKE ‘%ko%’;
![Page 12: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/12.jpg)
Tables joining
Why joining?• Refer to database course. It is better to store
different content into different table. Reducing data redundant.
• MySQL, a database server we use is a Relational engine. We can say one table can has relationship to other tables.
• To get data across tables, we have to do joining selection.
![Page 13: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/13.jpg)
Relationship in our 3 tables
• Consider our 3 tables: Country, City, CountryLanguage
• Table: Country has column ‘Code’ Table: City has column ‘CountryCode’ Table: CountryLanguage has column ‘CountryCode’
• Country.Code, City.CountryCode, CountryLanguage.CountryCode are sharing relationship.
![Page 14: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/14.jpg)
• List all city in ThailandSELECT * FROM CITY,COUNTRYWHERE CODE=‘THA’AND CODE=COUNTRYCODE;
• Remember. To join 2 tables we have to do 2 things.
1)Include 2 tables’name in From clause2)Match the related columns in each 2 tables by
using equality in where clause
![Page 15: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/15.jpg)
• If we have same columns name in 2 tables, you will get error cause there are ambiguous column names.
• To fix this problem, specific table name before column name by using .(dot)SELECT CITY.NAME,COUNTRY.NAMEFROM CITY,COUNTRYWHERE CODE=‘THA’AND COUNTRY.CODE=CITY.COUNTRYCODE;
![Page 16: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/16.jpg)
Count and Sum
• Find surface area of Thailand + Malaysia• SELECT SUM(SURFACEAREA)
FROM COUNTRYWHERE NAME=‘THAILAND’OR NAME=‘MALAYSIA’;
![Page 17: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/17.jpg)
• Find how many cities located in Asia• SELECT COUNT(CITY.NAME)
FROM CITY,COUNTRYWHERE CONTINENT=‘ASIA’ AND COUNTRY.CODE=CITY.COUNTRYCODE;
![Page 18: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/18.jpg)
Changing Data Using SQLINSERT / UPDATE / DELETE
![Page 19: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/19.jpg)
Prepare table first.
CREATE TABLE Student (id bigint(20) auto_increment PRIMARY KEY,email varchar(255),firstname varchar(255),lastname varchar(255),nick varchar(255));
![Page 20: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/20.jpg)
Inserting
• 1. Use SQL: “DESCRIBE tablename” to see the �target table’s structure.Remember column name, type and sequence
• 2. Use SQL: “INSERT INTO VALUES(xxx,yyy,zzz)” Replace xxx,yyy,zzz with actual values you want to insert ordering as the table column’s sequence.
![Page 21: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/21.jpg)
INSERT INTOstudentvalues(1,‘[email protected]’,‘Taw’,‘Poldee’,‘Tawny’);"
![Page 22: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/22.jpg)
Updating
• — Updating SQL syntax is UPDATE tablename SET columnanme = newvalue WHERE condition
![Page 23: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/23.jpg)
UPDATE studentSET nick=‘Tawjung’WHERE id=1;
![Page 24: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/24.jpg)
Deleting
• Deleting SQL syntax is DELETE FROM tablename WHERE condition.
![Page 25: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/25.jpg)
DELETE FROM studentWHERE firstname=‘Taw’;
![Page 26: PHP. Basic SQL definition: Database A database which structures data in the form of tables. Each table contains information relevant to a particular.](https://reader036.fdocuments.us/reader036/viewer/2022062407/56649e665503460f94b60675/html5/thumbnails/26.jpg)
PHP interface with MySQL
<?phpmysql_connect("localhost:portnum", "user","password") or
die(mysql_error());mysql_select_db("dbname") or die(mysql_error());$result = mysql_query("SELECT column1,column2 FROMtablename") or die(mysql_error());while($row = mysql_fetch_array($result)) {print $row['column1'];print $row['column2'];}?>