|

Mglbayar.
Bucharest.
2002 ��� 8-� ����� 14.
�����������
����
��� PHP, MySql ��� �� ������ ������. ��� �� �������� ����� �-��������
����� ������ , �������� ��� ��������� ��������� ������ ����
�v� �v���� ������ ����� WEB ����-��� ���� ��������� ���������
�����. ������ �� �v���� ������� WEB �v������ ��������� PHP (server
side script language), ��� MySql ��������� ����-��� ���������
������������ SQL (Structured Query Language) � � ������
�����������.
MySql
��� �� ��?
���� ������������
, ��� �� (server side script language) ������, ��� ������
�������������� ������ ��������� WEB ������ ����� ����������
����� �� browser ���� ������ ����� ��������.
WEB ������ � Apache , IIS ��� ���.
��������-�
(database) ���� ����� �������� ��������� �����
������ ������ �� ��������-�� ���������� ���� ��������. Yv���
���� ������ MySql ����� ����� �����. �� ���� ����� �����������
, ��� ��� ������������ ������� ����� ����� �����. ����������
��� ��� �� ����� ���� ������� ������ �� ��������� ����������
������ ���������, ���� ������ , ��� ����������� ������� ������
�����. MySql �� ��� ��� �������������� ����������� ������ �������
, ������� ������� ��������� ������ ��� ������. MySql �� ���
GNU (General Public License) ���� ���� ������ �����������
����� ��������. ����� ����� ������ Unix ������� �������� (Linux
��� ���) ����. ��� � �������� ��� OSX ����� Windows9x/ME/NT/2000
���� ������� ��������� �����.
��������
������ (������ ����� MySql) ����� �� ��������� ����������
��� ��������, ������ ������������� ������� ������ ����� �������
�������� ��. ��� ��� ������ ������ �������������� ������ �����������
������ ������� ������� ��������� �����.
���:
�� ���-�, ��������-� ������ ������ (Jokes) ����� �����������
����� ����� ��� �������� ������ ������! ����� ������� �����
��� �����.
������
��������� ������ (jokes)�� ����� ��������-� ���� �������������
���� �����.
��, �v�������
���� ������ ��������� ��� ��������� (jokes) ��������-���
����������� �����.
��������
�� ��� ����� ��� ����� �v����� (table) �c �v���� �㺺�
, �v����� (table) � �� ������ things (�� - ���� ��������)
��� ���������� ��������.
(���:
8 ������ ��������:
->
��������: 8 ����
-> Table: �������, �������
-> ������� table-� Things: �����, ������, ������, ������,
������ ��� ���..
-> ������� table-� Things: �����, �����, ������, ������
��� ���)
������
����� ������ ����� ������ (jokes) ���������, ����������
(Jokes) ����� �v����� (table) ���� ��������. �v�����
(table) ������ ������ ����� ������ (fields)���� �����.
̺� ������� ��� ��� ������ (JokeText) ����� ������ ���������
�� ��� ��� (JokeDate) ��������.
|
ID
|
JokeText |
JokeDate |
| 1 |
Why
did the chicken cross the road ? |
2001-04-01 |
| 2 |
Knock
� knock ! Who is there ? |
2002-08-14 |
������
����������� �������� vv����� ��� �ID� ����� ������� ���� ��������.
��� �� ������ ��������� ������ ������ ����� ������ �����.
������
�v����� (table) �� 3 ������ (column), 2 �� (row)
�� �v���� �����. ̺� ������ �������� ID (������), ��������
������ (JokeText), ������ �������� ���� (JokeDate)
���� �����.
���������
���������� ��� ���� MySql -��� ����� ��������� ����� ������.
������
�� ����
MySql ��� ����������
MySql �����������
������� �������� ��������� �� MySql ��������� software ��� ����������
������� �������� ������. Yv��� ���� ������ MySql ������ ��������
����� ����� ������ ���� ���������� MySql ������ software
����������� ��� ������ ������ �������� �� ���������-��� ����
����� directory-� ������. ��� ������ ��������-� ��� �� MySql.���
��.
���: (�����
�� ����� Windows v������� ���������, ������v� ����� ��� Y� ����
����� ��. ������ ��� ������ �������� ����� ����� v�� ��� Y�-��
����� ����� ������v� �����..)
�:\mysql\bin
������
�� ��� , Apache MySql-� �����
����� ��������� PHPTriad-�� ��������� ���
C:\apache\mysql\bin
C:\Program
Files\PHPTriad\Apache Group\Apache\Mysql\bin
����� �����
������.
������
�� ��������� �������� ���� WEB server ����������, WEB hosting-�
WEB server ������� ������ ��� MySql server-��� ��������� 2 ����
��� �����.
1. ��lnet
����� SSH (Secure Shell) ������� ������� WEB host ������-������
��������� ��������� mysql ������ ���������� ����������.
2. http://www.mysql.com/
��� ���������� mysql ������� ���������� ����� ����� ���������
�������� ���� �������. ��� �� v�� ��������� ������ Windows �����
Linux v������� ������-��� ������� ���������.
����� �����
��� ������ ��vv �������� �������� ��� ����� �MySql
����� ��� ��� ������� ��?� ����� ��vv������ ������ ��?
������
������ MySql ������ ���������� ����������� ����� ��������� ���
���� ����.
mysql
�h hostname �u username �p
-
hostname
�� ����� �� MySql- server ��������� ������ ����������� hostname
����� IP �������� ����� ��������. ������ ������ �������� �����
������������ ���� ��� �������� ������� ������� ������ �������:
- h
localhost ����� -h
(IP �������) ��� ����� �����.
-
username
��� MySql user name. ������ �� MySql server-��� ������ ��������
������ ��������� ���
-
h localhost �u root �p
���
�����.
-
� ��
������ password-��
���� ����� �����.
������
�v� ������� ��� ������ ��� ������ �������� (mysql.exe)
������������� ������� ����� ��� ����� ����� ���� ������� ���������
���������.
mysql>
������
��� �������������� ��������� ������� ��� ����� ��������.
������
������ ���� ����� ������ ���������� �������� !
�������
��������� ������ Enter ��������! ������ ��� �������� (;)
������� ������ ��! ��� �� ��������� ����� ���������� ����
������ ��.
mysql>show
databases;
MySql-server
������ ���������� ����� �������� �����. ������ ������ ���������
������ ����� ������� ��������� ���������.
2
rows in set (0.11 sec)
������
����� ����� �mysql� ���������� ��������� ������ vv���� �������������
����� ���, ������ ���� �� ����� �� �������� �� ������� v�����.
�test�
����� �������� �� ������ � ��� ������ �������� ��. Yv���� ������,
���� ������� ���� ��뺺�� ����� �� �������� MySql-� �dropping�
��� ��������.
mysql>drop
database test;
������
��������� ������ enter �������� MySql �test� ����� ��������-�
�������, �Query
OK� ����� ����� ����. (Y������� ���������).
��� ��������
���� ��� ��������� ������ �enter� ������ � ��� ������ ��� ���
v������� ������� �� �v��� ����� ��?� ��� ���������� ������v�����
���� �v������� �����. ��� ����� ������ ��� ������ ����� ��������
����� ����� ��� ��� �drop� ����� �������-��� ����������� �������
��������.
���� ������������
������� ���� , MySql �� ��������� ��� �� ������ semicolon
- ��� ������ (;) �� ���� ����� ������ ����� ��������!
semicolon (;) ��� �������� ���������� MySql �� ����� ���������
����� ��������� ����� ���� ��������� 2-���� ������ �������.
mysql>show
databases ����
enter �������
->
2���� ��� ���� ����. ���� �� �� semicolon (;) ������� �������
�����. �� ���� � ����� ��������v� ������ semicolon (;) �������
->;
�v� �v��
������ �����.
Mysql>show
->databases;
������
��� ��������� ��� ����� ��������� ������ ������ ����� �����
������ ����� ������������ �������� � semicolon (;) ������� ����������.
�� ���������
����� ������� ����� ��������� ������
Mysql>droop
database \ c
��� ��
drop ������ droop
��� ������ �����. \
� ��� ���������
Mysql>
����� ����
������ ����� ����� ��������� ��� ������ ����� ��!
�� Mysql
������ ����������� ������� ������
Mysql>quit
Bye
��� ������
� �����.
������
�� ����
SQL
��� �� ��?
Mysql ������
���������� �� ������� �� ���� ����� ������������ ������ SQL
(Structured Query Language) ��� �������.
- SQL
� �sequell� - ����-�v�-�� ��� ������.
- SQL
���� ������������ �queries� ����.
- SQL
�� ���� ������� ��������-��� ������� ��������� �������� ���
��. (MySql Database, Microsoft database ��� ������ ��������-�
������ ���������� �� ��������� ������).
�� �� SQL,
MySql ��� �� �������� �������� �� �� ?
- MySql
�� �������� ������ ����� ��������.
- SQL
�� ������ ��������-������ ������� ������ ��� ��.
������
�� ����
��������
��������� (�v����, ������)
���������
MySql ������-�� ������ �������� ������� ���������� ���� ��������
vv���� ! �������� vv���� �� ����������� ���� ��� ������� �����.
Mysql>create
database jokes;
��� ��������-��
������ (jokes) ��� ��������. �� ��������-�� ������ �v����
������ �������� ������ �����.
����� WEB
Host-� MySql ������ ���� ������� ������ ������� �������� ��
��������� vv�������, ������ ����� ������ ���� ��� ����������
�������� ������ �������v� �����.
��, ����
������ ������ (jokes) ����� �������� �����. ����� ����
��� ������ (jokes) ����� �������� ������ ������� ���
MySql-��� �����.
Mysql>use jokes;
���������
��� ������ (jokes)-������ ������� �v��� ������ �����
��.
���� ��������
������ �v�� ��������� ������ �����v� ������. ���� ������������
����� ������� ���� �v����� (table) �������� ���� �����.
������
�� ����
�v�����
(table) ����
�v�����
(table) ����� ����� ��� ������ ����� ���������� �����
������ �v�� ���vv ���� �����. �������� �� ��� ������, �v�� ���vv
����� ��, drop create
��� ������� ��� �� � ����� v�.
�v�����
(table) vv���� ��������� ������� ������ ��:
Mysql>create table table_name (
->column_1_name
column_1_type column_1_details,
->
column_2_name column_2_type column_2_details,
->�������.
->);
������
����������� �Jokes� �v����� (table) ��� ��������. ������
3 ������ ��������:
- ID -
������
- JokeText
- �������� ������
- JokeDate
- �������� ��������� ����
Mysql>create table Jokes (
-
>ID int not null auto_increment primary key,
-
>JokeText text,
-
> JokeDate date not null
-
> );
�����?
�v� ������ �������� ����� ��? �� � ����� ��� ������ ��� ������
��. ����� ��� ��� �������� v������ ����� ����v� �� ����� ���.
��, ��������� ������ ����������.
- �����
��� ����� ����� � Jokes
����� table ��� ������� ���� �����.
- 2 ����
������ ID � ������
����� �v��� ���� ������� (integer ���� int) ��� ������
�v���� ��� �����. ����� �� ������ vv������ ���������� ������
���������������.
- not
null
= NULL ���� �����v� ����
- auto_increment
= ��������� ������ ��������� ������ ��������v� ������� ����������
�� ����������.
- primary
key
= �v����������� ��� �v��vv� ������ �� �����, ��������� ����
����������v� �㺺�, ���������� ������������.
�� ������
������ ������������ ��� ������ ��� MySql �Query
OK� ��� ����� ����������� ��� ������ (Jokes) �����
table-�� ���������. �� ���� ����� ��� ������� ����� ��������
��� , MySql ���� ������ ������� �������� ����� ����� ����� �����
��������. �������� ����� ��������� ����� ��������.
���� ���������
��� ������ ������ (Jokes) ����� table-�� v��� �����.
Mysql>show tables;
����� �������
������
1
row in set (0.11 sec)
��� ������
��������-� ������ jokes ������ �v� table-� �������� ��������
�����. �������� ������ ������ ���� ������ (Jokes) �����
������ �v����� (table) �����.
���� ���
������ (Jokes) �v����� (table)-��� �� �������
�����.
Mysql>describe Jokes;
|
Field
|
Type
|
Null |
Key |
Default |
Extra |
| ID |
Int
(11) |
Yes |
Primary |
0 |
Auto |
| JokeText |
Text |
Yes |
|
Null |
Increment |
| JokeDate |
Date |
Yes |
|
0000-00-00 |
|
����� �v�����
(table)-� �������, ���� 3 ������ (columns) ����
�� ��� �� 3 ��� ��������� �������� �����.
���� ������
(Jokes)-��� �v�������� ��� ������� ������ ����.
���� ����
�v����� (table) ��� ��������� (��������) �����
�������.
Mysql>drop
table table_name;
������
�� ����
�v������� �������� (������) �������
��� ������
(jokes) ����� ��������, ������ (Jokes) ����� �v�����
(table) ��������� �����. ���� ������ ���� ���� ����������
����� ������� ����� � v�����.
����� ���� ��� INSERT ����� ������� ������� ������ �������
������ �� ������� ��������� �����.
Mysql>insert into table_name set
->
columnName1 = value1,
->
columnName2 = value2,
->
������.
->;
��� ���
������ ��
Mysql>insert into table_name
->
(columnName1, column2, ��)
->
values (value1, value2, ��..);
���� �����������
������ ���� ��������� ����������� ����������.
����� ���
��������� ������ ��������� ��� ��� v�vv���:
Mysql>insert into Jokes set
->
JokeText = � Why did the chicken cross the road ? To
get the other side �,
->
JokeDate = �2002-08-15�
->
;
����� ������:
Mysql>insert into Jokes
->
(JokeText, JokeDate)
->
values (� Why did the chicken cross the road? To get the other
side �, �2002-08-15�) ;
��� ����
������� column ��� �� �������� values (�������)-����� �����������
�������� ������ ���� ������ �������� �� !
������
��� ��������� (����������) ����������� ������ ������
���������. ����� ���� �������� ������ (����������, data)
���� ��� ���� ����� ������ v�������.
������
�� ����
���������
�������� (�������)
v���
��������-�
��������� ��������� v��� ������� �� SELECT ������ ���
�� SQL ������ ������� �������� ������� �����. ���� �� ��� ���������
��� ���� ������� v��� ����� �����. ������ ��� ������ ������
������ ���� ��� ������ ��������� ����� �������� SELECT �������
�� ������� ���� �����.
����� ���������
��������� Jokes �v����� (table) ������ �vx ������� v�vv���.
Mysql>select
* from Jokes;
���� ����
��� �����v� ���������� ������ �� ������ �����. ���� �� ������
������� ������ ������ (JokeText) ����� ��� ������� ���
��� ��������� ������ ��������� ��������� �����. ������ ��� �������
����������� ���� ��;
Mysql>select
ID, left (JokeText,20), JokeDate from Jokes;
��� ���������
�������� ����� 20 v����� �������� ������ �������� ����� � ���
������� ���������.
������
�� ������������ �� ��� ������ �� ��������� ����� �����;
Mysql>select ID, JokeDate from Jokes;
Y� �v� ��:
2 rows in set (0.3 sec)
�� ������
� ��������� � ����� ��� ���. ���� ��� ��������� (������)
������, �� ��� ������ �� �������� ����� ��� �������� ����� 20
v����� ���� ������ ������ ��� ���.
������
COUNT ����� function ������� ����� ������ ����� ���������
�������������� ���������.
Mysql>select count (*) from Jokes;
������
���� ���������� ����� � ������� ������ ������ � ������ �����.
WHERE
function-� ������� ����������� ���������� ���� ������, ������
������ ���� �����.
����� ��:
2000-04-01 �� ���� ��������� ������������ ������� ���� �������
��������� ������� � ����������.
Mysql>select
count (*) from Jokes
->
where JokeDate > = �2000-04-01� ;
��� vv���
�������:
Mysql>select count (*) from Jokes
-> where JokeDate < = �2000-04-01� ;
2000-04-01
�� ���� ��������� ������������ ���� ��������.
���� �����
�������� v� �������� ������� ������ LIKE �v��vv� v����
���������.
Mysql>select JokeText from Jokes
-> where JokeText like �% chicken %� ;
��� �����
����� ���� ������ ���������� (2000-04-01 �� 2002-08-16 �������
���������, chicken ����� �� ��������) ������� ������� �����.
Yv��� ����:
Mysql>select
JokeText from Jokes
->
where JokeText like �% chicken %� and
->
JokeDate >= �2000-04-01� and
->
JokeDate <= �2002-08-15� ;
���� ��
����� ��� ���� ������ ����� ������ ����������� �������� ���
SELECT, LIKE, WHERE, COUNT, INSERT ����� ����� �����
�v��vv� v�������� ������ ������ �������� ��������� !
������
�� ����
���������
��������� (������) ����� �������
���������
(������) ��������-� �v������� �������� ����� �� ��� ���������
�������� �����, ���� ����������� ��������� ����� ����� ����
�����. ��� ���������� �� UPDATE ��������� ������� �����.
UPDATE ������� �� INSERT ��������� ����� ������ (column values),
SELECT ��������� ����� ���������� ��������. ������� ������ ��;
Mysql> update table_name set
->col_name
= new_value, ����.
->where
conditions;
����� ������
�v�vv��� �������� ���������� ����� ����� �������� ����:
Mysql> update Jokes set
->JokeDate
= � 1990-04-01�
->where
JokeText like �% chicken %� ;
���������
��� chicken ����� �� �������� ��������� (������) ��������
����� ��������� �� ��� ������ �� 1990-04-01 ������ ������� �����.
������
�� ����
���������
��������� (������) ������
��������
������, ������� �� ������� ������� ��� ��� ���������� �����������
��������� ������� �����!
Mysql> delete from table_name WHERE conditions ;
Chicken
����� v���� ������������ �v����� �� �v����� (table) ��
������� ����;
Mysql>delete from Jokes
->where
JokeText like �% chicken %� ;
���� �
�����. �����:
Mysql>delete
from Jokes ;
�������
���� �v��� ������ �������� Jokes �v����� (table) ����
v�v���� �������, ���� � �������� v�v� ������ ����� ����� �������.
���� DELETE ���� ������ ����������� ��������� ����� ��������
!
������
�� ����
������� v�
��� Mysql
�������� ������ ����� SQL ������ ������ v����� �������
��������� ���� �����. ������ ������ ���������� ����� �������
������, ����� ������������ ���������� ��.
��� �����
��� ������ ��� �v����� (table) ������, �vv������� ���������
������. ���� ����, ������������ �������� ������� ���� ���� �v�����
(table) ���� ����� ������� , ���������� ������� �� ���
������ �������� ����� ��������.
������
��� ��������� ����, ������ ����� Mysql �������� ������ �����
SQL ������ ������ ������ ������ ���������� ���� �v�����������
�������� ������ ����������� ���������� ���� ������ ��������,
������� WEB site ��� ���� �������� ��� ��� !!!
Note:
����� �����, ��������� ����� ����� ������ ����� ������ ���������
�-���� ����� ���� ������
�������� Developer's World ������ ������� ��!
���������
�-mail:
[email protected]
���������
����vv�
������
�� ����
|