  MySQL.   PHP



,     :   MySQL?

MySQL           (   )  .         ,       ,  ,    .

MySQL    , , .   ,  ,    .   MySQL     PHP.

    . MySQL      GNU (GPL, GNU Public License).

        :      ,       .          Web-:    ,    ,        ..

 ,       :       ,   , ,     ,         -,          Unix - ,            .      ,       .

      .                    .        ,     .  ,       -        ,   .



  ,       (  MySql)    ,   .   ,       C++,    PHP ,          -    ,    PHP    ,    C++,    - ,  ++ - .

 ,       ,               .


2   



            .       60-     ,       ().         ,      .   80-         ().        -,   ,     ,      (SQL),         .



           (),           .                 Web-.       ,      " ",    .

    :

;
;
-;
.
        .           .

       ,         ,     .       ,       .    ,  ,     ,     "".

 -       .  -    ,  - . ,      -     ,       .

        -  .

 Web-,  ,    .      ,     ,     ,     .         (authors),    (forums),    (themes) , ,   (posts).  ,       :

 1    Forum.

authors

forums

posts

themes


        ,     ,     .      .2:

 2    .

  


 id_forum
 name
 Description

  1
 
   

  2
 MySQL
  ,   MySQL

  3
 PHP
  ,   PHP

  4
 
  


 .2    forums    ,     ,     Web-.         .       .

   forums        .  id_forum    ,  name      description    ,   .

        :

   ,     ;
          ;
     ,    ,         . ,   id_forum     ,    name - ;
    ,     ,    ,     .        ,        ;
        ,       .

3 





 -     ,       .

,  ,   ,   .

     ,     .  ,    ,          " ",         .       ,     " "    .      Forum,  ,          ,          .

4  



  (primary key)              .            .       PK (primary key).

   ,             .          .

 ,    Forum  themes  posts     :


   themes  id_theme,   posts - id_post.  ,   id_theme     posts.       posts    (          themes).      FK (foreign key).   .1,        themes,        themes  posts.  ,      ()  PK=1   posts      1,   ,        PK=1  themes.

5    



       ()    ().

          ,     .             .

         ,   .     -    ,      .

:

        ,    ,          .       ,            .

     auto_increment,     ,     .           , MySQL      ,            .

6   



:

         .

:

     ,          .

     . ,         ,         ..

 ,      ,   .   ,        :

 3.      


 

""
  ..

""
  ..

""
  ..

""
  ..

""
  ..


   -        . ..      .       ,             ,   ,   .         -   (. . 4)     (. . 5).

 4.  


  

""
 1

""
 2


 5.  

 ( )
 

1
  ..

1
  ..

2
  ..

2
  ..

2
  ..


      ,          :   .   "" ,      ,  .



      ,        .            SQL-,      -    .           - .

6    MySQL



      ,      mysql,       MySQL-,  SQL-     .      ,        mysql      MySQL.

    MySQL    mysql    , ,  , .        ,     ,     MySQL:

  shell> mysql -h host -u user -p

      Enter password:,       .    ,           mysql>:

  Welcome to the MySQL monitor. Commands end with ; or \g.
  Your MySQL connection id is 459 to server version:
  Type 'help' for help.
  mysql>

  mysql> ,   mysql   .

      ,   QUIT:

  mysql> QUIT
  Bye

 ,      ,    +.



 ,  ,   MySQL          ,   mysql   .

 ,     ,  ,     ,     .        ,      .

   ,          :

  mysql> SELECT VERSION(), CURRENT_DATE;

 MySQL      :

  +--------------+--------------------+
  | version() | current_date |
  +--------------+--------------------+
  1 row in set (0.02 sec)

            MySQL:

,  ,    SQL-,      .      ,        ,  ,    QUIT;
MySQL      ;
     , mysql        .  ,      ,     ;
       , mysql    mysql>,       .
,   MySQL    ,      :

  mysql> select version(), current_date;
  mysql> SELECT VERSION(), CURRENT_DATE;
  mysql> Select Version(), urrent_DATE;

MySQL       ,         .  :

  mysql> SELECT VERSION(); SELECT NOW();

      :

  +---------------+
  |  version() |
  +---------------+
  | 4.0.13-nt |
  +---------------+
  1 row in set (0.00 sec)
  +----------------------------------+
  | NOW()                        |
  +----------------------------------+
  | 2004-01-25 16:57:00 |
  +----------------------------------+
  1 row in set (0.03 sec)

        :

  mysql> SELECT USER(),
  -> CURRENT_DATE;

  :

  +----------------------------+---------------------+
  | user()                    | current_date |
  +----------------------------+---------------------+
  | ODBC@localhost | 2004-01-25   |
  +----------------------------+---------------------+
  1 row in set (0.00 sec)

,   ,      ,      mysql>  ->.   mysql ,          .    ,      .  ,          , mysql    ,   ->:

  mysql> select user()
  ->

   , ,    MySQL    , ,  ,  :

  mysql> select cos(pi()/10), (2*5)-5;

7  SQL



   SQL       :  , ,      ,      ..       .



   ,    SQL   1992 ,        ,     .  ,        -.     SQL    MySQL          .

8  SQL
create database
use
create table
describe
 alter table
drop table
drop database
insert intovalues
 delete
select
update
show



        SQL.       MySQL    mysql,       SQL.



 SQL    ,      .

   
        13.6 - 13.8.        ,   ,       max.  ,  INT(2) ,        100.

          .     ,            ,    P.

 13.6.  


 

TINYINT[(max)]
      -127128.

SMALLINT[(max)]
     -3276832767.

MEDIUMINT[(max)]
   .

INT[(max)]
   .

FLOAT[(max,P)]
      .

DOUBLE[(max,P)]
      .

DECIMAL[(max,P)]
    ,    char.


       13.7.

 13.7.    


 

DATE
    --.

TIME
    --.

DATETIME
      -- --.

YEAR
      .

TIMESTAMP
         -- --.


      13.8.

 13.8.  


 

CHAR (len)[BINARY]
    len,    255 .   BINARY   ,       .

CHAR
  CHAR(1).

VARCHAR (len)[BINARY]
  CHAR(len)  ,      .

TEXT
       65535.      .

BLOB
        65535.  BLOB (binary large object -   )     ,       .




CREATE DATABASE
     :

CREATE DATABASE db_name;

 db_name     .        forum,   -  MySQL mysql>       :

mysql> CREATE DATABASE forum;



  MySQL    .

    MySQL  ,   ,        ,    :

Query OK, 1 row affected (0.02 sec)

    MySQL           .

 ,  ,    forum  ,    SHOW DATABASES,  ,       :

mysql> SHOW DATABASES;

 ,               forum:




 SHOW DATABASES    MySQL,    SQL     .



,  SQL     : test  mysql.     ,     MySQL.




USE
      ,   MySQL       .      USE:

USE db_name;

 db_name -    .    forum:

mysql> CREATE DATABASE forum;
Database changed;




CREATE TABLE
 CREATE TABLE               :

CREATE TABLE table_name [(create_definition, ...)]

 table_name -   .

     forum,   authors        :  (name),  (passw), e-mail (email), Web-   (url),  ICQ (iq),    (about),        (photo),    (time),     (last_time),   -    ,     (statususer).        id_author,    . SQL-,      :

mysql> CREATE TABLE authors (
  id_author int(6) NOT NULL auto_increment,
  name text,
  passw text,
  email text,
  url text,
  iq text,
  about text,
  photo text,
  time datetime default NULL,
  last_time datetime default NULL,
  themes int(10) default NULL,
  statususer int(2) default NULL,
  PRIMARY KEY (id_author)
) TYPE=MyISAM;

 SQL- SHOW TABLES,  ,   authors  


      .

     forums,       .



         .  ,       ,    ,    ,     : ++, PHP, Java  . .

  forums   :   (id_forum),   (name),   (rule),    (logo),   (pos), ,   1,     0,   (hide).

 SQL-,   forums

mysql> CREATE TABLE forums (
  id_forum int(6) NOT NULL auto_increment,
  name text,
  rule text,
  logo text,
  pos int(6) default NULL,
  hide int(1) default NULL,
  PRIMARY KEY  (id_forum)
) TYPE=MyISAM;

    :   ,         .          ,     .    themes,   :

mysql> CREATE TABLE themes (
  id_theme int(11) NOT NULL auto_increment,
  name text,
  author text,
  id_author int(6) default NULL,
  hide int(1) default NULL,
  time datetime default NULL,
  id_forum int(2) default NULL,
  PRIMARY KEY (id_theme)
) TYPE=MyISAM;

  themes   :   (id_theme),   (name),   (author),      (id_author), ,   1,      0,   (hide) -     ,    (time),      (id_forum),           .

  themes   ,     : id_author  id_forum      ,         author,      authors   name.        ,              ,      .

   posts,    :

mysql> CREATE TABLE posts (
  id_post int(11) NOT NULL auto_increment,
  name text,
  url text,
  file text,
  author text,
  id_author int(6) default NULL,
  hide int(1) default NULL,
  time datetime default NULL,
  parent_post int(11) default NULL,
  id_theme int(11) default NULL,
  PRIMARY KEY  (id_post)
) TYPE=MyISAM;

  posts   :   (id_post),   (name),    ,         (url),       (file),   (author),      (id_author), ,   1,       0,    (hide) -     ,    (time),        (parent_post),       -    0,     (id_theme),         .

,     ,   SHOW TABLES.




DESCRIBE
 DESCRIBE        :

DESCRIBE tble_name

 tble_name -     .



 DESCRIBE     SQL      MySQL.

 ,  ,   forums,   SQL-:

mysql> DESCRIBE forums;

   ,  mysql   




ALTER TABLE
 ALTER TABLE    .       ,    ,     .    :

ALTER TABLE table_name alter_spec

 alter_spec  ,   


  

ADD create_definition [FIRST|AFTER column_name]
    create_definition. create_definition        .  FIRST      column_name.  AFTER      column_name.     ,       .

ADD INDEX [index_name] (index_col_name,...)
   index_name   index_col_name.    index_name  ,        index_col_name.

ADD PRIMARY KEY (index_col_name,...)
   index_col_name      .

CHANGE old_col_name new_col_name type
     old_col_name     new_col_name   type.

DROP col_name
     col_name.

DROP PRIMARY KEY
    .

DROP INDEX index_name
   index_name.


   forums   test,     name.

mysql> ALTER TABLE forums ADD test int(10) AFTER name;

  DESCRIBE forums,  ,   test     name


    test    new_test

mysql> ALTER TABLE forums CHANGE test new_test text;

   ,   :


    ,    ,   - ,        .

mysql> ALTER TABLE forums CHANGE new_test new_test int(5) not null;

      :


   new_test:

mysql> ALTER TABLE forums DROP new_test;

   ,      forums   :




DROP TABLE
 DROP TABLE       :

DROP TABLE table_name [ ,table_name,...]

 ,    forums    SQL-:

mysql> DROP TABLE forums;




DROP DATABASE
 DROP DATABASE          :

DROP DATABASE database_name

, ,   forum:

mysql> DROP DATABASE forum;




INSERT INTOVALUES
 INSERTVALUES      .  :

INSERT INTO table_name VALUES (values,)

  VALUES              .

     authors  ,        :

mysql> INSERT INTO authors VALUES (1, 'Maks', '123', ' maks@mail.ru ',
' www.softtime.ru ', '', '', '', '', '', 0, 0);
mysql> INSERT INTO authors VALUES (2, 'Igor', '123', 'igor@mail.ru',
'http://www.softtime.ru', '', '', '', '', '', 407, 0);
mysql> INSERT INTO authors VALUES (3, 'Sergey', '212', 'sergey@mail.ru',
'http://www.softtime.ru', '', '', '', '', '', 408, 0);




DELETE
DELETE FROM table_name [WHERE definition]

 DELETE    table_name ,    definition ,     .

        authors:

mysql> DELETE FROM authors;

   DELETE, UPDATE  SELECT   WHERE,       ,      .      ,      1:

mysql> DELETE FROM authors WHERE id_author = 1;

     ,    13.16      '123'      10:

mysql> DELETE FROM authors WHERE passw = '123' AND id_author > 10;

 AND   "".         "".




SELECT
 SELECT                 :

SELECT column,...
[FROM table WHERE definition]
[ORDER BY col_name [ASC | DESC], ...]
[LIMIT [offset], rows]

 column -   .      .           *.   FROM   table    .   WHERE ,       DELETE    .   ORDER BY      col_name   (ASC)    (DESC).   LIMIT  MySQL    rows     offset.

    forums  ,          SELECT.

mysql> INSERT INTO forums VALUES (1, '1', '', '', 1, 0);
mysql> INSERT INTO forums VALUES (2, '2', '', '', 2, 0);
mysql> INSERT INTO forums VALUES (3, '3', '', '', 3, 0);
mysql> INSERT INTO forums VALUES (4, '4', '', '', 4, 0);
mysql> INSERT INTO forums VALUES (5, '5', '', '', 5, 0);

      forums   :

mysql> SELECT * FROM forums;

     forums  .    :


     ,   ,        :

mysql> SELECT id_forum, name FROM forums;

   MySQL        id_forum    name


 LIMIT     ,   SELECT.  :

mysql> SELECT * FROM forums LIMIT 3;

        3   5


 LIMIT      ,     .          ,    MySQL        

mysql> SELECT * FROM forums LIMIT 1,3;

      2, 3  4


 WHERE    SELECT   ,     DELETE.      ,    id_forum  2:

mysql> SELECT * FROM forums WHERE id_forum > 2;

    :


         ORDER BY:

mysql> SELECT * FROM forums WHERE id_forum > 2 ORDER BY pos;

         id_forum   ,        pos.      :




UPDATE
UPDATE table
  SET col_name1=expr1 [, col_name2=expr2 ...]
  [WHERE definition]
  [LIMIT rows]

 UPDATE    table          .   SET ,             .   WHERE,   , ,    .      .   LIMIT     .

        2    (PHP)    hide  1,   .

UPDATE forums SET name='PHP', hide=1 WHERE id_forum=2;




SHOW
      ,     show databases  show tables           .       .

          :

mysql> SHOW FIELDS FROM authors;

    :


        :

mysql> SHOW INDEX FROM authors;

  SHOW PROCESSLIST        :
