Вы находитесь на странице: 1из 43

MySQL is a database.

The data in MySQL is stored in database


objects called tables.
A table is a collection of related data entries
and it consists of columns and rows.

MySQL is Free.
MySQL is Open Source.
MySQL is easy to use.

A query is a question or a request.


With MySQL, we can query a database for
specific information and have a recordset
returned.

You must download and install MySQL Server


You can use wamp or xampp
Download at:
http://www.wampserver.com/en/ for wamp server
http://sourceforge.net/projects/xampp/files/ for
xampp server

Iam can running on Linux,


Windows, Mac Os and
Solaris.
But just for 32 bit
Architecture

Iam can running only on


Windows for 32 bit (x86)
and 64 bit (x64)
Architecture

U can access easily MySQL from phpMyAdmin.

U can access MySQL from windows command promt


(cmd) or terminal / shell on linux.

In PHP, this is done with the mysql_connect()


function.
mysql_connect(servername,username,password);
Parameter
servername
username
password

Description
Optional. Specifies the server to connect to. Default value is
"localhost:3306"
Optional. Specifies the username to log in with. Default value
is the name of the user that owns the server process
Optional. Specifies the password to log in with. Default is ""

mysql_pconnect("localhost","peter","abc123"
);
Pconnect stand for persisten connect
Pconnect can not be close with mysql_close();

use the mysql_close() function

To get PHP to execute the statement above we


must use the mysql_query() function.
This function is used to send a query or
command to a MySQL connection.

Syntax
CREATE DATABASE database_name

Syntax

Data Type

Storage Required

FLOAT(p)

4 bytes if 0 <=p<= 24,


8 bytes if 25 <=p<= 53

FLOAT

4 bytes

DOUBLE [PRECISION], REAL

8 bytes

DECIMAL(M,D), NUMERIC(M,D)

0 byte = 0
1 byte = 1-2
2 bytes = 34
3 bytes = 56
4 bytes = 79

BIT(M)

approximately (M+7)/8 bytes

Data Type

Format

Range

DATE

YYYY-MM-DD

'1000-01-01' to '9999-12-31'

DATETIME[(fsp)]

YYYY-MM-DD
HH:MM:SS[.fraction]

'1000-01-01 00:00:00.000000' to
'9999-12-31 23:59:59.999999'

TIMESTAMP[(fsp)] YYYY-MM-DD
HH:MM:SS[.fraction]

'1970-01-01 0:00:01.000000' UTC


to '2038-01-19 03:14:07.999999'

TIME[(fsp)]

'-838:59:59.000000' to
'838:59:59.000000'

YEAR[(2|4)]

YYYY 4 digits
YY 2 digits

1901 to 2155, and 0000.


70 to 69 from 1970 to 2069

Data Type

MySQL before 5.6.4

MySQL 5.6.4 and up

YEAR

1 byte

1 byte

DATE

3 bytes

3 bytes

TIME

3 bytes

3 bytes + fractional
seconds storage

DATETIME

8 bytes

5 bytes + fractional
seconds storage

TIMESTAMP

4 bytes

4 bytes + fractional
seconds storage

Fractional Seconds Precision Storage Required


0

0 bytes

1,2

1 byte

3,4

2 bytes

5,6

3 bytes

In the following table, M represents the declared column length in characters for
nonbinary string types and bytes for binary string types. L represents the actual
length in bytes of a given string value.

Data Type

Collations

CHAR

Length
Character Set
Specifications
Yes
Yes

VARCHAR
TEXT

Yes
Yes

Yes
Yes

Yes
Yes

ENUM

No

Yes

Yes

SET

No

Yes

Yes

Yes

The CHARACTER SET attribute specifies the


character set
The COLLATE attribute specifies a collation for
the character set.

Value

CHAR(4)

Storage
Required

VARCHAR(4)

Storage
Required

4 bytes

1 byte

ab

ab

4 bytes

ab

3 bytes

abcd

abcd

4 bytes

abcd

5 bytes

abcdefghij abcd

4 bytes

abcd

5 bytes

VARCHAR uses a single-byte character set


such as latin1.

If a given value is stored into the CHAR(4) and VARCHAR(4) columns,


the values retrieved from the columns are not always the same
because trailing spaces are removed from CHAR columns upon
retrieval. The following example illustrates this difference

If a given value is stored into the CHAR(4) and VARCHAR(4) columns,


the values retrieved from the columns are not always the same
because trailing spaces are removed from CHAR columns upon
retrieval. The following example illustrates this difference

Important: A database must be selected


before a table can be created. The database is
selected with the mysql_select_db() function.

DROP TABLE [IF EXISTS] table_name ;


DROP DATABASE [IF EXISTS] db_name;

Empty Table
TRUNCATE table_name ;

The INSERT INTO statement is used to add


new records to a database table.
Syntax

OR

Syntax
SELECT column_name(s) FROM table_name
Example:

Firstname

Lastname

Glenn

Quagmire

Peter

Griffin

Please find the term of :


mysql_fetch_array()
mysql_num_rows()
mysql_affected_rows()
Note: will be frequently used and encountered
when doing select table

The WHERE clause is used to extract only


those records that fulfill a specified criterion.

The ORDER BY keyword is used to sort the


data in a recordset.
The ORDER BY keyword sort the records in
ascending order by default.
If you want to sort the records in a descending
order, you can use the DESC keyword.

The UPDATE statement is used to update


existing records in a table.

The DELETE FROM statement is used to delete


records from a database table.

Вам также может понравиться