PHP and Mysql: John Ryan B. Lorca Instructor I
PHP and Mysql: John Ryan B. Lorca Instructor I
Introduction to MySQL
SQL is a standard computer language for accessing and manipulating databases. SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against a database SQL can retrieve data from a database SQL can insert new records in a database SQL can delete records from a database SQL can update records in a database SQL is easy to learn
2
Introduction to MySQL
SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems. SQL statements are used to retrieve and update data in a database. SQL works with database programs like MS Access, DB2, Informix, MS SQL Server, Oracle, Sybase, etc. Unfortunately, there are many different versions of the SQL language, but to be in compliance with the ANSI standard, they must support the same major keywords in a similar manner (such as SELECT, UPDATE, DELETE, INSERT, WHERE, and others). Note: Most of the SQL database programs also have their own proprietary extensions in addition to the SQL standard!
3
SQL links
Tutorials
http://www.w3schools.com/sql/ http://www.sqlzoo.net http://sqlcourse.com (part 2) http://sqlcourse2/com (part 1)
Introduction to MySQL
SQL Database Tables A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data. Below is an example of a table called "Persons":
The table above contains three records (one for each person) and four columns (LastName, FirstName, Address, and City).
5
Introduction to MySQL
With SQL, we can query a database and have a result set returned. A query like this: Gives a result set like this:
WARNING
Always assume that everything is case sensitive, especially table names. This is not the case in Windows XP but it is the case in Linux
7
10
mysql>
11
isbn title
books table
author
pub
year
14
DATETIME
format YYYY-MM-DD HH:MM:SS
TIMESTAMP
format YYYYMMDDHHMMSS
TIME
format HH:MM:SS
YEAR
default length is 4
16
VARCHAR
variable length string, e.g., VARCHAR(20)
ENUM
list of items from which value is selected
17
USE
Specify which database to use Example use bookstore;
18
CREATE TABLE table_name ( column_name1 column_type1, column_name2 column_type2, ... column_nameN column_typeN );
Note: To create a database use the statement CREATE db_name;
19
CREATE TABLE table_name ( column_name1 column_type1 NOT NULL DEFAULT '0', column_name2 column_type2, ... column_nameN column_typeN, PRIMARY KEY (column_name1) );
20
CREATE TABLE table_name ( column_name1 column_type1 PRIMARY KEY NOT NULL DEFAULT '0' AUTO_INCREMENT, column_name2 column_type2, ... column_nameN column_typeN, );
21
22
24
The result
25
Result
26
27
"Orders" table
Results
28
Result
29
30
"Persons" table
Result
31
32
A "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.
33
The following SQL statement will return persons with first names that end with an 'a':
The following SQL statement will return persons with first names that contain the pattern 'la':
34
You can also specify the columns for which you want to insert data:
35
Person
37
Result
38
Result
39
Person
40
Result
41
42
Result:
43
Result:
44
45
Result:
Notice that there are two equal company names (W3Schools) in the result above. The only time you will see the second column in ASC order would be when there are duplicated values in the first sort column, or a handful of nulls.
46
Result:
48
Result:
49
Result:
50
51
Result:
52
53
Result:
54
55
Result:
56