มายเอสคิวแอล (mysql, phpMyAdmin, SQL:Structured Query Language)
เว็บเพจสำรอง (Backup Webpages) : perlphpasp.com | thaiall.com
ปรับปรุง : 2550-02-13 (เพิ่ม functions)
การใช้ MySQL ให้สำเร็จ ควรเรียนวิชาด้านฐานข้อมูล(Database) มาให้เข้าใจก่อนครับ
ไม่งั้นท่านจะว่าที่ผมเขียนมานี่ .. ไม่รู้เรื่องเลย
SQL : Structured Query Language
SQL เป็นภาษาสอบถามข้อมูล หรือภาษาจัดการข้อมูลอย่างเป็นโครงสร้าง มีการพัฒนาโปรแกรมเพื่อรองรับ SQL ขึ้นมากมาย และระบบฐานข้อมูลในปัจจุบันเกือบทุกระบบ รองรับ SQL ทั้งสิ้น รวมถึง MySQL, MsSQL, PostgreSQL, หรือ MS Access เป็นต้น สำหรับโปรแกรมฐานข้อมูลที่รองรับ SQL ในอันดับต้น ๆ คือ MySQL เพราะเป็น Open source ที่ใช้งานได้ทั้งใน Linux และ Windows
MySQL คือ โปรแกรมฐานข้อมูล มีหน้าที่เก็บข้อมูลอย่างมีโครงสร้าง และรองรับคำสั่ง SQL เป็นเครื่องมือสำหรับเก็บข้อมูลอย่างมืออาชีพ ยังมีเครื่องมืออีกหลายอย่าง ที่ท่านต้องใช้ร่วมกันอย่างสอดคล้อง จึงจะนำไปพัฒนาระบบฐานข้อมูลซับซ้อน ตามความต้องการของผู้ใช้ได้สำเร็จสมประสงค์ เช่น การบริการเว็บ ภาษาสำหรับพัฒนาเว็บ ระบบปฏิบัติการ และคอมพิวเตอร์ที่เหมาะสม การใช้ MySQL ในฐานะนักเรียน เป็นเพียงจุดเริ่มต้นของการสร้างระบบที่สมบูรณ์ แม้นักเรียนจะพัฒนาระบบฐานข้อมูลเป็นโครงงานก่อนจบได้สมบูรณ์ แต่นั่นก็เป็นเพียงระบบหนึ่ง การหาเวลาศึกษาหลาย ๆ ระบบจะทำให้นักเรียนเข้าใจระบบฐานข้อมูลมากขึ้น

สิ่งที่ท่านควรเข้าใจก่อนใช้ MySQL
- การบริการเว็บ เช่น apache หรือ iis หรือ pws เป็นต้น
- ภาษาสำหรับพัฒนาระบบฐานข้อมูล และใช้งาน MySQL
เช่น Perl, PHP, ASP, VB และ JSP เป็นต้น
- ระบบปฏิบัติการ เช่น Windows หรือ Linux เป็นต้น

แหล่ง Download MySQL
:: http://www.MySQL.com/downloads/index.html ซึ่งมี version เยอะแยะ ลองอ่านว่าท่านจะใช้ version ไหนให้เหมาะกับท่านนะครับ ถ้าท่านมี Redhat ขอแนะนำว่าไม่ต้องไปหาที่ไหน เพราะในแผ่น Redhat มี MySQL อยู่แล้ว แต่ถ้าเป็น windows ต้อง download MySQL ในรุ่นที่เหมาะสมกับเครื่องของท่านนะครับ
สารบัญ
1. เข้าไปใน MySQL ผ่าน command prompt เบื้องต้น
2. เข้าไปใน MySQL ผ่าน command prompt ปรับปรุงข้อมูล
3. select แบบต่าง ๆ
4. โปรแกรม phpMyAdmin สำหรับจัดการ MySQL (phpMyAdmin.net)
    4.1 ตัวอย่างการ config ให้ใช้งาน phpMyAdmin
    4.2 ตัวอย่างการ config เมื่อมีรหัสผู้ใช้สำหรับเข้าใช้ MySQL
    4.3 php4 กับ MySQL4 อาจมีปัญหา
    4.4 แสดงรายชื่อตารางในฐานข้อมูลชื่อ b
    4.5 แสดงรายชื่อ field ในตาราง orderd ของฐานข้อมูลชื่อ b
    4.6 อธิบายเพิ่มเติมเพราะรับ mail จาก kemmmx@hotmail.com, khem@wassana.com
    4.7 ปัญหาที่ผมไม่พยายามแก้ต่อไป คือ การใช้ phpMyAdmin แบบ http
    4.8 แก้ปัญหาภาษาไทยเป็น ??? เมื่ออ่านข้อมูลจาก MySQL
5. โปรแกรม MySQL-Front สำหรับจัดการ MySQL จาก http://www.MySQLFront.de
6. ภาษาสำหรับจัดการ MySQL เช่น php, asp, perl หรือ jsp เป็นต้น
Source Script : memmysql.php คือ ระบบรับสมาชิกอย่างง่าย
โปรแกรมที่สั่งเปิดบริการ MySQL ก่อนที่จะใช้ phpmyadmin หรือ php script เข้าไปจัดการข้อมูล
mysqld : Compiled with full debugging and automatic memory allocation checking, and InnoDB and BDB tables.
mysqld-opt : Optimized binary. From version 4.0 on, InnoDB is enabled.
mysqld-nt : Optimized binary for Windows NT, 2000, and XP with support for named pipes.
mysqld-max : Optimized binary with InnoDB and BDB support.
mysqld-max-nt : Like mysqld-max, but compiled with support for named pipes.
ต.ย.คำสั่ง SQL อย่างง่าย
:: สอนอย่างง่ายใน แบบสอบถาม(Query) แล้วให้ประมวลผล เห็นผลทันที
create table a (a1 int, a2 char(20));
insert into a (a1, a2) values (1, "abc");
select * from a;
delete from a where a1=1;
update a set a2="def" where a1=1;
select * from a order by a2;
ตัวอย่าง Command Line
c:\mysql\bin>mysql
c:\mysql\bin>mysql -u root -pxxx
c:\mysql\bin>mysql -?
c:\mysql\bin>mysqld-nt
c:\mysql\bin>mysqld-nt --console
c:\mysql\bin>mysqladmin -u root shutdown
c:\mysql\bin>mysqladmin status
c:\mysql\bin>mysqladmin ping
c:\mysql\bin>mysqladmin variables
c:\mysql\bin>mysqld-nt -install
c:\mysql\bin>mysqld-nt -remove
c:\mysql\bin>mysqlcheck -V
c:\mysql\bin>net start
c:\mysql\bin>net start mysql
c:\mysql\bin>net stop mysql
แนะนำเว็บ (Web Guides)
- http://dev.mysql.com/doc/refman/5.0/en/string-functions.html ตัวอย่างฟังก์ชัน เช่น left, ucase
- http://micro.se-ed.com/Content/mc234/MC234_108.asp การติดตั้ง MySQL 4.1
- http://www.MySQL.org :: Official of MySQL in OSDN group
- http://www.phpMyAdmin.net :: เป็นโปรแกรมที่ใช้จัดการกับฐานข้อมูล MySQL
- http://www.thaiall.com/webserver :: เนื้อหาเน้นด้านการติดตั้ง Server แบบต่าง ๆ
- http://www.isinthai.com :: เนื้อหาเน้นด้าน Redhat Linux server
- http://www.juicystudio.com/tutorial/MySQL/index.asp :: MySQL Tutorial [ Author: Gez Lemon ]
- http://th.php.net/mysql
- http://www.devshed.com/c/a/MySQL/MySQL-Configuration-and-Installation/
- http://www.thaihosttalk.com/index.php?showtopic=1439
- http://dev.mysql.com/doc/mysql/en/mysql-config-wizard-file-location.html
- http://dev.mysql.com/doc/mysql/en/show-status.html
- คิดอะไร หรือนึกอะไรไม่ออกเกี่ยวกับ MySQL เปิด C:\mysql\Docs\manual.html อาจช่วยได้ เพราะละเอียดสุดสุด

    เทคนิคต่าง ๆ
  1. สั่ง start mysql รุ่น 4.1 สามารถกำหนดตำแหน่ง my.ini ขณะ เปิด service
    โดยเติม --defaults-file="C:\Program Files\MySQL\MySQL Server 4.1\my.ini" ขณะสั่ง start ดูได้ใน service
  2. Data Type ของ mysql มี 27 แบบ ดูตัวอย่างการสร้างจาก หัวข้อ 2.5 ได้แก่
    VARCHAR TINYINT TEXT DATE SMALLINT MEDIUMINT INT BIGINT FLOAT DOUBLE DECIMAL DATETIME TIMESTAMP TIME YEAR CHAR TINYBLOB TINYTEXT BLOB MEDIUMBLOB MEDIUMTEXT LONGBLOB LONGTEXT ENUM SET BINARY VARBINARY
mysqldatabase.gif
1. เข้าไปใน MySQL ผ่าน command prompt เบื้องต้น
    หลังติดตั้ง MySQL ครั้งแรก จะไม่มี user อื่นนอกจาก root และไม่มีรหัสผ่านของ root ท่านจึงเข้าไปได้สบาย ก่อนเข้าไปต้องสั่ง run MySQL เพื่อเปิดบริการด้วย c:\mysql\bin\mysqladmin.exe จะพบ icon รูปสัญญาณไฟเขียวไฟแดงมุมล่างขวาของจอภาพ แต่ถ้าเรียก c:\mysql\bin\mysqld.exe จะเป็นการเรียก MySQL server ให้ทำงานใน DOS mode และไม่แสดง icon ที่มุมล่างขวาด้วย
    ถ้าเป็น Linux ที่ลง MySQL แล้ว สามารถเปิดบริการด้วยการเลือก #setup และstart sql ด้วย #/etc/init.d/MySQLd start จากนั้นพิมพ์ว่า MySQL หลังเครื่องหมาย # เพื่อเข้าไปจัดการใน MySQL ถ้าเป็น windows ก็พิมพ์คำว่า c:\mysql\bin\mysql ใน start, run หรือภายใน command หรือ cmd ขั้นตอนนี้เป็นการเข้าไปเฉย ๆ ยังไม่ทำอะไร แค่ให้รู้ว่าเข้าไปได้เท่านั้น
    
    1.1 หัดเข้า และหัดออกจาก MySQL
    1.2 แสดงรายชื่อ database ในโปรแกรม MySQL
    1.3 เรียกใช้ หรือเข้าไปในฐานข้อมูลชื่อ MySQL
    1.4 แสดงรายชื่อ table ในระบบฐานข้อมูล MySQL
    1.5 ลบผู้ใช้ที่ไม่ระบุตัวตน และคนเข้า MySQL ต้องมี user และ password พร้อมกำหนดรหัสให้ root
    1.6 สร้างฐานข้อมูลใหม่ชื่อ oho
    1.7 เรียกใช้ฐานข้อมูล oho และสร้างตารางชื่อ wow
    1.8 เพิ่มระเบียนใหม่ 3 ระเบียนเข้าไปใน wow
    1.9 เรียกข้อมูลบาง field มาแสดง
    1.10 แสดง field ในตาราง wow
    1.11 หาผลรวมของบาง field
    1.12 ลบระเบียนที่ 2
    1.13 เปลี่ยนชื่อระเบียนที่ 1 เป็น thaiall thailand
    
    1.1 หัดเข้า และหัดออกจาก MySQL
      เขาว่า การเริ่มต้นที่ดี สำเร็จไปแล้วครึ่งหนึ่ง จึงให้ท่านเข้าไป แล้วออกมาเลย ไม่ต้องทำอะไร เพื่อดูว่าเข้าเป็น และออกเป็น เท่านั้น Click Start, Run and type COMMAND c:\windows>cd\mysql\bin c:\mysql\bin>mysql หรือกำหนด user ตามบรรทัดข้างล่างนี้ c:\mysql\bin>mysql -u root บรรทัดข้างบนนี้ใช้ได้ในการเข้า MySQL ครั้งแรก เพราะยังไม่มี password สำหรับ root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 to server version: 3.23.54 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> mysql> mysql> exit หรือใช้ quit ก็ได้ พิมพ์ exit เพื่อออกจาก MySQL เพราะ MySQL ยังไม่มีรหัสผ่านของ root ท่านจึงเข้า MySQL ได้ ถ้ามีการตั้งรหัสผ่านของ root ท่านจะต้องมีรหัส จึงจะ MySQL ได้ ถ้าท่าน หรือใคร แอบกำหนดรหัสผ่านให้ root โดยท่านไม่รู้ ก็ไปหาแฟ้มจาก c:\mysql\data\mysql\*.* ในเครื่องอื่น มาทับซะก็เรียบร้อย
    1.2 แสดงรายชื่อ database ในโปรแกรม MySQL
    
      mysql> show databases; คำสั่งนี้ ทำในขั้น 1.1 ได้ ถ้าไม่ exit ไปซะก่อน | Database | | mysql | | test | 2 rows in set (0.00 sec) mysql>
    1.3 เรียกใช้ หรือเข้าไปในฐานข้อมูลชื่อ MySQL
    
      mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql>
    1.4 แสดงรายชื่อ table ในระบบฐานข้อมูล MySQL
    
      mysql> show tables; | Tables_in_mysql | | columns_priv | | db | | func | | host | | tables_priv | | user | 6 rows in set (0.00 sec) mysql>
    1.5 ลบผู้ใช้ที่ไม่ระบุตัวตน และคนเข้า MySQL ต้องมี user และ password พร้อมกำหนดรหัสให้ root
    
      mysql> delete from user where user=''; Query OK, 2 rows affected (0.00 sec) mysql> update user set password = password('yourpassword') where user='root'; Query OK, 2 rows affected (0.00 sec) Rows matched: 2 changed: 2 warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> mysql> exit เมื่อเข้า MySQL อีกครั้งต้องพิมพ์ mysql -u root -p แล้วพิมพ์รหัสผ่านให้ถูกอีกครั้ง ตัวอย่างนี้ ผมกำหนดรหัสผ่านให้ root เป็น yourpassword นะครับ บ่อยครั้งที่แนะนำว่าสร้าง user ใหม่ให้มี username เป็น u และ password เป็น p จะได้จำได้ง่าย ๆ ไม่ลืม
    1.6 สร้างฐานข้อมูลใหม่ชื่อ oho
    
      mysql> create database oho; mysql> show databases; | Database | | mysql | | oho | | test | 2 rows in set (0.00 sec) mysql> ครั้งแรก MySQL สร้างฐานข้อมูลชื่อ MySQL และ test มาให้ ถ้าอยากมีฐานข้อมูลเป็นสัดส่วนไว้เก็บตารางต่าง ๆ ก็ควรสร้างไว้ใช้เอง ตัวอย่างนี้สร้างขึ้นใหม่ 1 database ชื่อ oho
    1.7 เรียกใช้ฐานข้อมูล oho และสร้างตารางชื่อ wow
    
      mysql> use oho; mysql> create table wow( ->id int primary key not null auto_increment , ->ename varchar(50), ->salary double ->); mysql> ระวังเรื่อง , และเครื่องหมาย ; นะครับ เพราะ sql ปิดคำสั่งด้วย ; เสมอ
    1.8 เพิ่มระเบียนใหม่ 3 ระเบียนเข้าไปใน wow
    
      mysql> insert into wow values('','yonok lampang',80.25); mysql> insert into wow values('','atichart hancharnchai',0.75); mysql> insert into wow values('','yaowalak ketsarin',19); mysql> รูปแบบคำสั่ง insert INSERT [INTO] { table_name WITH (<table_hint_limited> [..n]) | view_name | rowset_function_limited } { [(column_list)] { VALUES ( { DEFAULT | NULL | expression } [,..n] ) | derived_table | execute_statement } }
    1.9 เรียกข้อมูลบาง field มาแสดง
    
      mysql> select salary from wow; (ถ้าต้องการทุก field ก็ใช้ select * from wow;) | salary | | 80.25 | | 0.75 | | 19 | mysql> รูปแบบคำสั่ง select SELECT [ALL | DISTINCT] select_list [INTO new_table] FROM table_source [WHERE search_condition] [GROUP BY group_by_expression] [HAVING search_condition] [ORDER BY order_expression [ASC | DESC]]
    1.10 แสดง field ในตาราง wow
    
    หลังใช้คำสั่งสร้างตาราง หากดูผลการสร้างก็ใช้คำสั่ง show columns นี่หละครับ
      mysql> show columns from wow; | Field | Type | Null | Key | Default | Extra | | id | int(11) | | PRI | NULL | auto_increment | | ename | varchar(50) | YES | | NULL | | | salary | double | YES | | NULL | | 3 rows in set (0.05 sec) mysql> ถ้าคำสั่ง show columns ยาวไป จะใช้คำสั่ง mysql> desc wow; แทนก็ได้นะครับ ให้ผลเหมือนกัน
    1.11 หาผลรวมของบาง field
    
      mysql> select sum(salary) from wow; |sum(salary)| | 100 | mysql>
    1.12 ลบระเบียนที่ 2
    
      mysql> delete from wow where id = 2; mysql> รูปแบบคำสั่ง delete DELETE { table_name WITH (<table_hint_limited> [..n]) | view_name | rowset_function_limited } [ FROM { <table_source> } [,..n]] [ WHERE <search_condition> ]
    1.13 เปลี่ยนชื่อระเบียนที่ 1 เป็น thaiall thailand
    
      mysql> update wow set ename='thaiall thailand' where id = 1; mysql> รูปแบบคำสั่ง update UPDATE { table_name WITH (<table_hint_limited> [..n]) | view_name | rowset_function_limited } SET column_name = { expression | default | null } { [ FROM { <table_source> } [,..n]] [ WHERE <search_condition> ] }

2. เข้าไปใน MySQL ผ่าน command prompt ปรับปรุงข้อมูล
    
    2.1 เข้า MySQL โดยพิมพ์ password ผ่าน command prompt
    2.2 ดูสถานะของ MySQL
    2.3 สร้างผู้ใช้อื่นนอกจาก root อีก 3 users
    2.4 แสดงรายชื่อผู้ใช้ที่สามารถเข้า MySQL
    2.5 สร้างตาราง และลบตาราง
    2.6 เรียกใช้ฐานข้อมูล test และสร้างตารางชื่อ orderm, orderd, pro, cust
    2.7 เพิ่มระเบียนใหม่ใน 4 ตาราง
    2.8 สร้างฐานข้อมูลใหม่ชื่อ a และ b พร้อม user ใหม่ชื่อ a และ b
    2.9 คัดลอกทุกตารางจากฐานข้อมูล test ใส่ในฐานข้อมูล b
    2.10 สร้าง user และ ฐานข้อมูลใหม่ ให้นักเรียนแต่ละคน
    2.11 ลบสมาชิกออกจากระบบ 1 คน
    
    2.1 เข้า MySQL โดยพิมพ์ password ผ่าน command prompt
      C:\mysql\bin>mysql -u root -p Enter password: ************ Welcome to the MySQL monitor. Your MySQL connection id is 6 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> exit
      C:\mysql\bin>mysql -u root -pyourpassword Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 to server version: 3.23.57 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
    2.2 ดูสถานะของ MySQL
    
      mysql> status -------------- C:\mysql\bin\mysql.EXE Ver 11.15 Distrib 3.23.44, for Win95/Win98 (i32) Connection id: 2 Current database: test Current user: root@localhost Server version: 3.23.44 Protocol version: 10 Connection: localhost via TCP/IP Client characterset: latin1 Server characterset: latin1 TCP port: 3306 Uptime: 20 min 18 sec Threads: 2 Questions: 145 Slow queries: 0 Opens: 9 Flush tables: 1 Open tales: 3 Queries per second avg: 0.119 -------------- mysql>
    2.3 สร้างผู้ใช้อื่นนอกจาก root อีก 3 users
    
    burin เป็น full superuser เข้า server จากที่ไหนก็ได้ และกำหนดรหัสผ่านเป็น some_pass
    admin เข้าจัดการกับฐานข้อมูล จาก localhost ไม่ต้องใช้รหัสผ่าน สามารถ reload หรือ process งานเกี่ยวกับ admin ได้
    tom เข้าจัดการกับฐานข้อมูล จาก localhost ไม่ต้องใช้รหัสผ่าน สามารถจัดการกับฐานข้อมูล dtom ได้เต็มที่
    dummy เข้าไม่ต้องใช้รหัสผ่าน จาก localhost แต่ไม่มีสิทธิใด ๆ เพราะมีแผนจะกำหนดสิทธิให้กับ database ที่เหมาะสมภายหลัง
      mysql> GRANT ALL PRIVILEGES ON *.* TO burin@localhost IDENTIFIED BY 'some_pass' WITH GRANT OPTION; mysql> GRANT ALL PRIVILEGES ON *.* TO burin@"%" IDENTIFIED BY 'some_pass' WITH GRANT OPTION; mysql> GRANT ALL PRIVILEGES ON dtom.* TO dtom@localhost; mysql> GRANT RELOAD,PROCESS ON *.* TO admin@localhost; mysql> GRANT USAGE ON *.* TO dummy@localhost; mysql> FLUSH PRIVILEGES; อ.อิทธิพล แซ่จิว ใช้เว็บเพจนี้ เสริมการสอนเรื่องฐานข้อมูล .. ผมจึงต้องปรับปรุงให้เหมาะสม
    สิทธิของผู้ใช้ แบบโกลบอล (Mouse over จะแสดงคำอธิบาย)
    ตารางนี้ copy มาจาก phpMyAdmin ซึ่ง download ได้จาก http://www.phpMyAdmin.net
    ข้อมูลโครงสร้างการดูแลระบบ
    + SELECT + CREATE + GRANT
    + INSERT + ALTER + PROCESS
    + UPDATE + INDEX + RELOAD
    + DELETE + DROP + SHUTDOWN
    + FILE + REFERENCES
    2.4 แสดงรายชื่อผู้ใช้ที่สามารถเข้า MySQL
    
      mysql> use MySQL; Database changed mysql> select user,password from user; | user | password | | root | 606718756665bfe6 | | u | 606718756665bfe6 | 2 rows in set (0.00 sec) mysql>
    2.5 สร้างตาราง และลบตาราง
    
      mysql> use test; mysql> create table x(x int primary key); mysql> drop table x; mysql> drop table if exists x;

      แสดง Data type ของการใช้ phpmyadmin 25 แบบ ส่งเข้า SQL แล้วใช้ได้ทันที mysql> CREATE TABLE `samtable` (
      `f01` varchar(5) NOT NULL default '',
      `f02` tinyint(5) NOT NULL default '0',
      `f03` text NOT NULL,
      `f04` date NOT NULL default '0000-00-00',
      `f05` smallint(5) NOT NULL default '0',
      `f06` mediumint(5) NOT NULL default '0',
      `f07` int(5) NOT NULL default '0',
      `f08` bigint(5) NOT NULL default '0',
      `f09` float(5,2) NOT NULL default '0.00',
      `f10` double(5,2) NOT NULL default '0.00',
      `f11` decimal(5,0) NOT NULL default '0',
      `f12` datetime NOT NULL default '0000-00-00 00:00:00',
      `f13` timestamp(14) NOT NULL,
      `f14` time NOT NULL default '00:00:00',
      `f15` year(4) NOT NULL default '0000',
      `f16` varchar(5) NOT NULL default '',
      `f17` tinyblob NOT NULL,
      `f18` tinytext NOT NULL,
      `f19` blob NOT NULL,
      `f20` mediumblob NOT NULL,
      `f21` mediumtext NOT NULL,
      `f22` longblob NOT NULL,
      `f23` longtext NOT NULL,
      `f24` enum('0','1','2') NOT NULL default '0',
      `f25` set('M','F') NOT NULL default ''
      ) TYPE=MyISAM;
    2.6 เรียกใช้ฐานข้อมูล test และสร้างตารางชื่อ orderm, orderd, pro, cust
    
      mysql> use test; mysql> create table orderm(orderid int primary key,cust int,orderdate date,ordertime time); mysql> create table orderd(orderid int not null,pro int not null, primary key(orderid,pro)); mysql> create table pro(pro int primary key,proname varchar(50),price double,prorest int); mysql> create table cust(cust int primary key,custname varchar(50)); ฐานข้อมูลนี้ print screen มาจาก http://www.thaiall.com/teachaccess/db4order.mdb orderm : คำว่า m ย่อมาจาก main หมายถึง ข้อมูลการสั่งซื้อของแต่ละใบ ถ้ามี 3 ใบ ก็จะมี 3 ระเบียนในตารางนี้ orderd : คำว่า d ย่อมาจาก detail หมายถึง ข้อมูลรายละเอียดการสั่งซื้อ เช่นใบสั่ง 3 ใบ อาจสั่งสินค้า 6 รายการ แฟ้มนี้อาจมี 6 ระเบียน pro : คำว่า pro ย่อมาจาก product หมายถึง ข้อมูลจำนวนสินค้า อาจมีสินค้าเป็น 1000 รายการ แฟ้มนี้ก็จะเก็บ 1000 ระเบียน cust : คำว่า cust ย่อมาจาก customer หมายถึง ข้อมูลจำนวนลูกค้า อาจมีลูกค้าเป็น 1000 รายการ แฟ้มนี้ก็จะเก็บ 1000 ระเบียน
    2.7 เพิ่มระเบียนใหม่ใน 4 ตาราง
    
      mysql> use test; mysql> insert into `orderm` (`orderid`,`cust`,`orderdate`,`ordertime`)values( '1001','101','1/7/2004','13:35'); mysql> insert into orderm values('1002','101','1/7/2004','13:35'); mysql> insert into orderm values(1003,103,'15/7/2004','10:12'); mysql> insert into orderd values(1001,201,5,10); mysql> insert into orderd values(1001,202,6,100) mysql> insert into orderd values(1001,203,1,30); mysql> insert into orderd values(1002,204,3,50); mysql> insert into orderd values(1003,202,4,50); mysql> insert into orderd values(1003,204,1,50); mysql> insert into pro values(201,'pen',10,200); mysql> insert into pro values(202,'book',100,10); mysql> insert into pro values(203,'ink',30,5); mysql> insert into pro values(204,'knight',50,20) mysql> insert into cust values(101,'Mr.Boy'); mysql> insert into cust values(102,'Ms.Girl') mysql> insert into cust values(103,'Mr.Man');
    2.8 สร้างฐานข้อมูลใหม่ชื่อ a และ b พร้อม user ใหม่ชื่อ a และ b
    
      mysql> show databases; mysql> create database a; mysql> create database b; mysql> GRANT USAGE ON *.* TO a@localhost; mysql> GRANT ALL PRIVILEGES ON b.* TO 'b'@'localhost' identified by 'bpassword'; mysql> exit user ชื่อ a และ b เข้าฐานข้อมูลใด ๆ ไม่ได้เลย ถ้าไม่กำหนดฐานข้อมูลให้กับ user นั้นโดยเฉพาะ ตัวอย่างนี้ user a เข้า MySQL โดยไม่มีรหัสผ่านก็ได้ แต่ไม่สามารถจัดการข้อมูลในฐานข้อมูลใด ๆ ได้ สำหรับ user b เป็นเจ้าของฐานข้อมูล b โดยสมบูรณ์ และมีรหัสผ่านเข้า MySQL คือ bpassword ถ้าไม่มีสิทธิ แต่พยายามใช้ฐานข้อมูลนั้น ก็จะถูกปฏิเสธ ดังตัวอย่างด้านล่าง mysql> use a; ERROR 1044: Access denied for user: 'a@localhost' to database 'a'
    2.9 คัดลอกทุกตารางจากฐานข้อมูล test ใส่ในฐานข้อมูล b
      1. ใช้ explorer copy แฟ้มทุกแฟ้มในห้อง c:\mysql\data\test\*.* ไปไว้ในห้อง c:\mysql\data\b\*.* 2. เท่านี้ก็เรียบร้อย ข้อมูลทั้งหมดในฐานข้อมูล test เป็นของ b แล้วครับ
    ผม zip แฟ้มทั้ง 15 แฟ้ม ของ 5 ตาราง
    ไว้ในแฟ้ม 5table.zip [4 KB]
    ตาราง : cust
    ตาราง : pro
    ตาราง : orderm
    ตาราง : orderd
    ตาราง : wow
    ถ้าท่านมีฐานข้อมูลชื่อ xxx และอยากมีตาราง 5 ตารางนี้
    ท่านก็คลาย zip ลงไปในห้อง c:\mysql\data\xxx ก็เรียบร้อย
    ประหยัดเวลาสร้างตารางอีก 5 ตาราง ตามที่ผมทำให้ดูนี้
    2.10 สร้าง user และ ฐานข้อมูลใหม่ ให้นักเรียนแต่ละคน
    
      mysql> show databases; mysql> GRANT ALL PRIVILEGES ON s01.* TO 's01'@'%' identified by 's01'; mysql> GRANT ALL PRIVILEGES ON s02.* TO 's02'@'%' identified by 's02'; :: :: mysql> GRANT ALL PRIVILEGES ON s30.* TO 's30'@'%' identified by 's30'; mysql> exit สร้างสมาชิกใหม่ 30 สมาชิก แต่ตอนสร้าง database ชื่อ s01 ถึง s30 ให้ใช้ explorer copy ห้องต้นแบบ เป็นห้องต่าง ๆ ไว้ใน c:\mysql\data\s01 ถึง c:\mysql\data\s30 ผลการสร้างสมาชิก s01 จะเพิ่มระเบียนในแฟ้ม user และ db ของ ฐานข้อมูลชื่อ MySQL ส่วนรหัสผ่าน จะเหมือนกับชื่อ user เช่นสมาชิกชื่อ s01 ก็จะมีรหัสผ่านคือ s01 สมาชิกใหม่สามารถเขียนโปรแกรมไว้ใน server ใดก็ได้ เพราะอนุญาตไว้ว่าเข้ามาจากที่ใดก็ได้ ไม่จำกัดเฉพาะ localhost
    2.11 ลบสมาชิกออกจากระบบ 1 คน
    
      mysql> DELETE FROM `user` WHERE `User` = "s01" AND `Host` = "%"; mysql> DELETE FROM `db` WHERE `User` = "s01" AND `Host` = "%"; mysql> DELETE FROM `tables_priv` WHERE `User` = "s01" AND `Host` = "%"; mysql> DELETE FROM `columns_priv` WHERE `User` = "s01" AND `Host` = "%"; # ปรับปรุงสิทธิเข้าถึงใหม่อีกรอบ ... mysql> FLUSH PRIVILEGES ; mysql>
      คำสั่งเหล่านี้ copy มาจาก phpMyAdmin ในส่วนของการลบสมาชิก ถ้าสมาชิกคนนี้ถูกสร้างขึ้นมาด้วยคำสั่งข้างล่างนี้
      GRANT ALL PRIVILEGES ON s01.* TO 's01'@'%' identified by 's01';

3. Select แบบต่าง ๆ
    3.1 แสดงข้อมูลทั้งหมดจากใบสั่งซื้อละเอียด(orderd) ในฐานข้อมูล s01
    3.2 นับจำนวนระเบียนในแฟ้มสั่งซื้อละเอียด
    3.3 แสดงข้อมูล โดยเปลี่ยนชื่อ field เพื่อแสดงผล หรือใช้อ้างอิง
    3.4 แสดงข้อมูล โดยเปลี่ยนชื่อ field และใช้ function length
    3.5 แสดงข้อมูล โดยตัดตัวอักษรมาแสดงผล
    3.6 แสดงข้อมูลเลขที่ใบสั่งซื้อ เฉพาะที่ไม่ซ้ำ
    3.7 เลือกแสดงรายการสั่งซื้อละเอียด เฉพาะสินค้ารหัส 204
    3.8 แสดงรายชื่อลูกค้า และจัดเรียงตามชื่อ
    3.9 แสดงข้อมูลสินค้า เรียงตามราคาจากมากไปน้อย
    3.10 แสดงข้อมูลสินค้า ทั้งการจัดเรียง และเลือกข้อมูลพร้อมกัน
    3.11 แสดงข้อมูลสินค้า ยกเว้นรายการที่มีชื่อว่า ink
    3.12 แสดงข้อมูลสินค้า ยกเว้น(ที่ชื่อว่า pen หรือ ราคามากกว่า 90)
    3.13 แสดงข้อมูล เฉพาะเขตข้อมูลที่ต้องการ
    3.14 แสดงข้อมูล เลือกเฉพาะวันที่น้อยกว่า ปี 2010 เดือนธันวาคม วันที่ 13
    3.15 แสดงข้อมูล เลือกเฉพาะปี และเดือนที่ต้องการ
    3.16 เลือกข้อมูลด้วยคำสั่ง in เลือกเฉพาะ pen และ book
    3.17 เลือกข้อมูลด้วยคำสั่ง like เลือกเฉพาะขึ้นต้นด้วย Mr
    3.18 แสดงข้อมูลตั้งแต่ระเบียนแรกไป 3 ระเบียน
    3.19 นับแยกกลุ่มในแฟ้มใบสั่งละเอียด
    3.20 คำนวณค่าใช้จ่ายของแต่ละรายการ
    3.21 คำนวณยอดรวมในแฟ้มใบสั่งละเอียดแต่ละใบ
    3.22 คำนวณยอดรวม เลือกด้วยคำสั่ง having ภายหลัง group แล้ว
    3.23 แสดงข้อมูลใบสั่ง และชื่อผู้ซื้อแต่ละใบ
    3.24 แสดงข้อมูลใบสั่งละเอียด และชื่อสินค้าแต่ละรายการ
    3.25 การเชื่อม orderm, orderd, cust และ pro
    3.26 เลือกข้อมูลด้วย between
    
    ถ้าไม่มี MySQL จะใช้ access เพื่อศึกษา sql ก็ได้นะครับ มีตัวอย่างข้อมูล และตารางใน access ให้ศึกษา
    สามารถ download แฟ้มนี้ได้จาก http://www.thaiall.com/teachaccess/db4order.mdb
    3.1 แสดงข้อมูลทั้งหมดจากใบสั่งซื้อละเอียด(orderd) ในฐานข้อมูล s01
      mysql> use s01; Database changed mysql> select * from orderd; | 1001 | 201 | 5 | 10 | | 1001 | 202 | 6 | 100 | | 1001 | 203 | 1 | 30 | | 1002 | 204 | 3 | 50 | | 1003 | 202 | 4 | 50 | | 1003 | 204 | 1 | 50 |
    3.2 นับจำนวนระเบียนในแฟ้มสั่งซื้อละเอียด
    
      mysql> select count(*) from orderd; | count(*) | | 6 | มีอีกหลายคำสั่ง เช่น sum avg max min length mid left right instr replace เป็นต้น
    3.3 แสดงข้อมูล โดยเปลี่ยนชื่อ field เพื่อแสดงผล หรือใช้อ้างอิง
    
      mysql> select cust as f1, custname as f2 from cust; | f1 | f2 | | 101 | Mr.Boy | | 102 | Ms.Girl | | 103 | Mr.Man |
    3.4 แสดงข้อมูล โดยเปลี่ยนชื่อ field และใช้ function length
    
      mysql> select cust as f1, length(custname) as f2 from cust; | f1 | f2 | | 101 | 6 | | 102 | 7 | | 103 | 6 |
    3.5 แสดงข้อมูล โดยตัดตัวอักษรมาแสดงผล
    
      mysql> select cust, mid(custname,1,2) from cust; | cust | mid(custname,1,2) | | 101 | Mr | | 102 | Ms | | 103 | Mr | mysql>select mid(dt,1,10) as x, count(dt) as y from ynlog ->group by mid(dt,1,10) order by mid(dt,1,10) desc; | x | y | | 2007/01/03 | 19 | | 2007/01/02 | 2 |
    3.6 แสดงข้อมูลเลขที่ใบสั่งซื้อ เฉพาะที่ไม่ซ้ำ
    
      mysql> select distinct orderid from orderd; | orderid | | 1001 | | 1002 | | 1003 |
    3.7 เลือกแสดงรายการสั่งซื้อละเอียด เฉพาะสินค้ารหัส 204
    
      mysql> select * from orderd where pro =204; | orderid | pro | price | quan | | 1002 | 204 | 3 | 50 | | 1003 | 204 | 1 | 50 |
    3.8 แสดงรายชื่อลูกค้า และจัดเรียงตามชื่อ
    
      mysql> select * from cust order by custname; | cust | custname | | 101 | Mr.Boy | | 103 | Mr.Man | | 102 | Ms.Girl |
    3.9 แสดงข้อมูลสินค้า เรียงตามราคาจากมากไปน้อย
    
      mysql> select * from pro order by price desc; | pro | proname | price | prorest | | 202 | book | 100 | 10 | | 204 | knight | 50 | 20 | | 203 | ink | 30 | 5 | | 201 | pen | 10 | 200 |
    3.10 แสดงข้อมูลสินค้า ทั้งการจัดเรียง และเลือกข้อมูลพร้อมกัน
    
      mysql> select * from pro where price<100 and price>20 order by price desc; | pro | proname | price | prorest | | 204 | knight | 50 | 20 | | 203 | ink | 30 | 5 |
    3.11 แสดงข้อมูลสินค้า ยกเว้นรายการที่มีชื่อว่า ink
    
      mysql> select * from pro where not(proname = 'ink'); | pro | proname | price | prorest | | 201 | pen | 10 | 200 | | 202 | book | 100 | 10 | | 204 | knight | 50 | 20 |
    3.12 แสดงข้อมูลสินค้า ยกเว้น(ที่ชื่อว่า pen หรือ ราคามากกว่า 90)
    
      mysql> select * from pro where not(proname='pen' or price >90); | pro | proname | price | prorest | | 203 | ink | 30 | 5 | | 204 | knight | 50 | 20 |
    3.13 แสดงข้อมูล เฉพาะเขตข้อมูลที่ต้องการ
    
      mysql> select orderid,cust,orderdate from orderm; | orderid | cust | orderdate | | 1002 | 101 | 2001-07-20 | | 1003 | 103 | 2015-07-20 | | 1001 | 101 | 2001-07-20 |
    3.14 แสดงข้อมูล เลือกเฉพาะวันที่น้อยกว่า ปี 2010 เดือนธันวาคม วันที่ 13
    
      mysql> select * from orderm where orderdate < '2010-12-13'; | orderid | cust | orderdate | ordertime | | 1002 | 101 | 2001-07-20 | 13:35:00 | | 1001 | 101 | 2001-07-20 | 13:35:00 |
    3.15 แสดงข้อมูล เลือกเฉพาะปี และเดือนที่ต้องการ
    
      mysql> select * from orderm where year(orderdate)='2001' and month(orderdate)='07'; | orderid | cust | orderdate | ordertime | | 1002 | 101 | 2001-07-20 | 13:35:00 | | 1001 | 101 | 2001-07-20 | 13:35:00 |
    3.16 เลือกข้อมูลด้วยคำสั่ง in เลือกเฉพาะ pen และ book
    
      mysql> select * from pro where proname in ('pen','book'); | pro | proname | price | prorest | | 201 | pen | 10 | 200 | | 202 | book | 100 | 10 |
    3.17 เลือกข้อมูลด้วยคำสั่ง like เลือกเฉพาะขึ้นต้นด้วย Mr
    
      mysql> select * from cust where custname like 'Mr%'; | cust | custname | | 101 | Mr.Boy | | 103 | Mr.Man |
    3.18 แสดงข้อมูลตั้งแต่ระเบียนแรกไป 3 ระเบียน
    
      mysql> select * from orderd limit 0,3; | orderid | pro | price | quan | | 1001 | 201 | 5 | 10 | | 1001 | 202 | 6 | 100 | | 1001 | 203 | 1 | 30 |
    3.19 นับแยกกลุ่มในแฟ้มใบสั่งละเอียด
    
      mysql> select orderid, count(pro) from orderd group by orderid; | orderid | count(pro) | | 1001 | 3 | | 1002 | 1 | | 1003 | 2 |
    3.20 คำนวณค่าใช้จ่ายของแต่ละรายการ
    
      mysql> select orderid, pro, price * quan from orderd; | orderid | pro | price * quan | | 1001 | 201 | 50 | | 1001 | 202 | 600 | | 1001 | 203 | 30 | | 1002 | 204 | 150 | | 1003 | 202 | 200 | | 1003 | 204 | 50 |
    3.21 คำนวณยอดรวมในแฟ้มใบสั่งละเอียดแต่ละใบ
    
      mysql> select orderid, sum(price * quan) from orderd group by orderid; | orderid | sum(price * quan) | | 1001 | 680 | | 1002 | 150 | | 1003 | 250 |
    3.22 คำนวณยอดรวม เลือกด้วยคำสั่ง having ภายหลัง group แล้ว
    
    เพราะไม่สามารถใช้ where ได้ เนื่องจากค่าที่ต้องการ เป็นค่าหลังการ group ถ้าท่านใช้ having price > 100 ก็จะผิด
      mysql> select orderid, sum(price * quan) from orderd group by orderid having -> sum(price * quan) > 200; | orderid | sum(price * quan) | | 1001 | 680 | | 1003 | 250 |
    3.23 แสดงข้อมูลใบสั่ง และชื่อผู้ซื้อแต่ละใบ
    
      mysql> select orderm.orderid, cust.custname from orderm,cust where orderm.cust=cust.cust; | orderid | custname | | 1002 | Mr.Boy | | 1001 | Mr.Boy | | 1003 | Mr.Man |
    3.24 แสดงข้อมูลใบสั่งละเอียด และชื่อสินค้าแต่ละรายการ
    
    ตัวอย่างนี้ใช้คำสั่ง inner join ตามตัวอย่างใน MS Access
      mysql> select orderd.*, pro.* -> from (orderd inner join pro on orderd.pro = pro.pro) order by orderd.orderid; | orderid | pro | price | quan | pro | proname | price | prorest | | 1001 | 201 | 5 | 10 | 201 | pen | 10 | 200 | | 1001 | 203 | 1 | 30 | 203 | ink | 30 | 5 | | 1001 | 202 | 6 | 100 | 202 | book | 100 | 10 | | 1002 | 204 | 3 | 50 | 204 | knight | 50 | 20 | | 1003 | 202 | 4 | 50 | 202 | book | 100 | 10 | | 1003 | 204 | 1 | 50 | 204 | knight | 50 | 20 |
    3.25 การเชื่อม orderm, orderd, cust และ pro
    
    ตัวอย่างนี้ copy sql มาจาก MS Access ครับ แต่ใช้ใน MySQL ไม่ได้ เพราะบรรทัดคำสั่งรับ 255 ตัวอักษร แม้ลดแล้วก็ยังไม่ได้ครับ
    ตัวอย่างที่ 3 ใช้ได้ เพราะผมเขียนตาม concept ของ MySQL ไม่ได้ใช้หลักการของ MS Access (แต่ผมยังก็ไม่ได้ทดสอบว่าทำไมใช้แบบ MS Access ไม่ได้ .. ท่านหาดูลองสิครับ)
      Type 1 : MS Access but error on MySQL SELECT orderm.orderid, orderm.orderdate, orderm.ordertime, cust.cust, cust.custname, orderd.pro, pro.proname, orderd.price, orderd.quan, pro.price, pro.prorest FROM (orderd INNER JOIN pro ON orderd.pro = pro.pro) INNER JOIN (cust INNER JOIN orderm ON cust.cust = orderm.cust) ON orderd.orderid = orderm.orderid; Type 2 : change from type 1 but error on MySQL mysql> SELECT orderm.orderid,orderdate,custname,proname,orderd.price,quan,prorest -> FROM(orderd INNER JOIN pro ON orderd.pro=pro.pro)INNER JOIN(cust INNER JOIN orderm -> ON cust.cust = orderm.cust) ON orderd.orderid = orderm.orderid; Type 3 : ok on MySQL mysql> select orderm.orderid, orderm.orderdate, orderm.ordertime, cust.cust, cust.custname, -> orderd.pro, pro.proname, orderd.price, orderd.quan, pro.price, pro.prorest -> from orderm,orderd,cust,pro -> where orderm.cust=cust.cust -> and orderm.orderid=orderd.orderid -> and orderd.pro=pro.pro order by orderm.orderid; | orderid | orderdate | ordertime | cust | custname | pro | proname | price | quan | price | prorest | | 1001 | 2001-07-20 | 13:35:00 | 101 | Mr.Boy | 202 | book | 6 | 100 | 100 | 10 | | 1001 | 2001-07-20 | 13:35:00 | 101 | Mr.Boy | 201 | pen | 5 | 10 | 10 | 200 | | 1001 | 2001-07-20 | 13:35:00 | 101 | Mr.Boy | 203 | ink | 1 | 30 | 30 | 5 | | 1002 | 2001-07-20 | 13:35:00 | 101 | Mr.Boy | 204 | knight | 3 | 50 | 50 | 20 | | 1003 | 2015-07-20 | 10:12:00 | 103 | Mr.Man | 202 | book | 4 | 50 | 100 | 10 | | 1003 | 2015-07-20 | 10:12:00 | 103 | Mr.Man | 204 | knight | 1 | 50 | 50 | 20 | Type 4 : อีกตัวอย่างของ sql ใน MS Access select distinct sindexs.*,scomponents.* from (users inner join scomponents on users.sarindex = scomponents.sarindex) inner join sindexs on sindexs.sarindex = scomponents.sarindex and sindexs.cseq = scomponents.cseq where scomponents.sarindex = 1 order by sindexs.cseq, sindexs.iseq
    3.26 เลือกข้อมูลด้วย between
    
    เพราะคุณพงศ์เดช ศิริพันธุ์ กลุ่มงานรังสี โรงพยาบาลลำปาง ถามมา ผมจึงทดสอบคำสั่งนี้
    ตัวอย่างนี้ใช้คำสั่ง เลือกข้อมูลที่อยู่ในช่วงที่ต้องการ และผมก็ทดสอบกับตัวเลขได้ผล
    ทดสอบใน MS Access โดยใช้ query สร้าง แล้วดู code ของ SQL และเชื่อแน่ว่าใช้ได้กับ MySQL
      SELECT empl.salary FROM empl WHERE (((empl.salary) Between 5000 And 10000));

4. โปรแกรม phpMyAdmin สำหรับจัดการ MySQL
    phpMyAdmin (phpMyAdmin is intended to handle the adminstration of MySQL over the WWW.) คือ php script ที่ run อยู่บน Web Server ใช้สำหรับบริหารข้อมูลใน MySQL บน host ใด ๆ ถ้า MySQL จากเครื่องเป้าหมายยอมให้เข้าไปจัดการข้อมูลจากเครื่อง Remote เช่น
    - create and drop databases
    - create, copy, drop and alter tables
    - delete, edit and add fields
    - execute any SQL-statement
    - export and import CSV data
แนะนำเว็บ (Web Guides)
+ http://www.phpMyAdmin.net
+ http://sourceforge.net/projects/phpMyAdmin/
+ http://www.devside.net/web/server/windows/phpMyAdmin
+ http://www.212cafe.com/setup/setup_phpMyAdmin.htm
+ http://dev.mysql.com/doc/mysql/en/old-client.html
    4.1 ตัวอย่างการ config ให้ใช้งาน phpMyAdmin
    ในกรณีที่ท่านยังไม่กำหนดรหัสผ่านให้กับ root ใน mysql .. (เป็นการเริ่มต้นใช้งานที่ง่าย)
    เปิดแฟ้ม config.inc.php ด้วย notepad แล้วหา 3 บรรทัดด้านล่างนี้
    $cfg['PmaAbsoluteUri'] = '';
    $cfg['Servers'][$i]['user'] = 'root'; // MySQL user
    $cfg['Servers'][$i]['password'] = ''; // MySQL password
    4.2 ตัวอย่างการ config เมื่อมีรหัสผู้ใช้สำหรับเข้าใช้ MySQL
    ถ้าใน MySQL มี user ชื่อ burin และ password คือ saveworld ให้แก้ไข 3 บรรทัด ดังนี้
    $cfg['PmaAbsoluteUri'] = 'http://localhost/phpMyAdmin/';
    $cfg['Servers'][$i]['user'] = 'burin'; // MySQL user
    $cfg['Servers'][$i]['password'] = 'saveworld'; // MySQL password
    4.3 php4 กับ MySQL4 อาจมีปัญหา
    ถ้าพบปัญหา #1251 - Client does not support authentication protocol requested by server; consider upgrading MySQL client
    วิธีแก้ไขคือเปลี่ยนรุ่นของ php4 แต่ถ้าไม่ต้องการเปลี่ยนเป็น php5 ก็ให้เปลี่ยนการเข้ารหัสของ password เป็นแบบเก่า
    ในกรณีสร้างรหัสผู้ใช้ด้วย mysql ใน Text mode จะไม่พบปัญหาอะไร แต่ถ้าพบปัญหาเรื่อง not support authentication ใน phpMyAdmin ก็ต้องเปลี่ยนวิธีเข้ารหัส
    - ถ้าใช้ php4 กับ mysql4 จะมีปัญหาเรื่อง mysql_connect( ... ) ถ้าต้องการใช้ให้ได้ ต้องใช้ฟังก์ชัน old_password
    - ถ้ากำหนด $cfg['Servers'][$i]['auth_type'] = 'config'; ก็ยังใช้งานได้ ถ้าใช้ old_password
    - ถ้ากำหนด $cfg['Servers'][$i]['auth_type'] = 'http'; จะพิมพ์รหัสผ่านไม่ผ่าน แม้จะใช้ old_password แล้วก็ตาม
    mysql> set password for 'burin'@'localhost' = old_password('saveworld');
    วิธีแก้ปัญหามีอีก 2 ทาง (http://th.php.net/manual/en/function.mysql-connect.php)
    1. Start mysql server ด้วย --old-passwords option เช่น mysql --old-passwords
    2. ใส่ old_passwords ใน my.ini หรือ my.cnf (ref)
    4.4 แสดงรายชื่อตารางในฐานข้อมูลชื่อ b

    4.5 แสดงรายชื่อ field ในตาราง orderd ของฐานข้อมูลชื่อ b


    4.6 อธิบายเพิ่มเติมเพราะรับ mail จาก kemmmx@hotmail.com, khem@wassana.com
    - ประสบการณ์การใช้ MySQL ในหลายสถานการณ์ผมมีน้อย ส่วนใหญ่ผมจะใช้ MySQL ของ root ที่ไม่มีรหัสผ่าน คือติดตั้งเสร็จก็ใช้เลย ถ้าสร้างผู้ใช้ก็ใช้ MySQL 3.23 ซึ่งไม่มีปัญหา แต่ปัญหาเกิดเมื่อใช้กับ MySQL 4.1 ขึ้นไป เพราะรหัสผ่านไม่สามารถ Verify ได้ เพราะการเข้ารหัสใน MySQL 4.1 แตกต่างไปจากเดิม จึงต้องใช้ฟังก์ชัน old_password เปลี่ยนรหัสใฟ้ผู้ใช้ทีละคน
    - ถ้าต้องการใช้ phpMyAdmin จากหลายเครื่องต้องกำหนด ip ถ้าเครื่องของท่านมี ip เป็น 202.203.204.205
    แนะนำว่าครั้งแรกไม่กำหนด ให้ลองใช้ก่อน ถ้าพอใจเรื่อง security ก็ไม่ต้องกำหนด .. อาจมีบางเหตุผลที่ต้องการกำหนด
       old $cfg['PmaAbsoluteUri'] = '';
       new $cfg['PmaAbsoluteUri'] = 'http://202.203.204.205/phpmyadmin/';
    - การสร้าง user ในส่วนของ Host ก็ต้องกำหนดเป็น % เพื่อให้เข้ามาจาก Host ใด ๆ ก็ได้
       mysql> grant all privileges on *.* to boy@"%" identified by 'girl' with grant option;
       mysql> set password for 'boy'@'%' = old_password('girl');
       mysql> flush privileges;

    4.7 ปัญหาที่ผมไม่พยายามแก้ต่อไป คือ การใช้ phpMyAdmin แบบ http
    ถ้าต้องการให้ phpMyAdmin ถ้ารหัสผ่าน auth_type แบบ http จะไม่พบปัญหากับ MySQL 3.23 แต่พอใช้กับ MySQL 4.1 ขึ้นไป พบว่าการ Verify รหัสไม่ผ่าน .. ถ้าไม่เปลี่ยน version ของ php เป็น php5 ผมยังหาวิธีแก้ไขไม่ได้
    มีอีกวิธีที่ช่วยให้การเรียน phpMyAdmin ถามรหัสผ่านได้ คือการใช้ .htaccess ของ apache ตรวจสอบรหัสผ่านแทนได้ (http://www.thaiall.com/cgi/htpasswd.pl)

    4.8 แก้ปัญหาภาษาไทยเป็น ??? เมื่ออ่านข้อมูลจาก MySQL
    - ทุกครั้งที่อ่านข้อมูล จึงต้องเพิ่ม mysql_query .. เพื่อกำหนดภาษาเป็น tis620 (ทราบจาก phpconcept.com)
    - Copy SQL ที่เปิดใน Notepad ไปใส่ใน MySQL-Front เป็น ??? พอเปลี่ยนเป็น wordpad ไม่มีปัญหาเรื่อง ???? (win2003)
      <?
      // http://www.phpconcept.com/content/view/21/28/
      $host     = "localhost";
      $db       = "db";
      $tb       = "phpbb_users";
      $user     = "admin";
      $password = "p";
      $query    = "select * from $tb";
      ###########################
      $connect = mysql_connect("$host","$user","$password");
      // $charset = "SET character_set_results=tis620";
      $charset = "set names tis620";
      mysql_query($charset) or die('Invalid query: ' . mysql_error());
      $result = mysql_db_query($db,$query);
      while ($object = mysql_fetch_object($result)) {
         echo $object->username . "<br>";
      }
      mysql_close($connect);
      ?>
      
    4.9 แก้ปัญหาภาษาไทย เมื่อพิมพ์ภาษาไทยใน phpmyadmin แล้วออกเป็น ???
    - ให้ทำตอนเริ่มต้นสร้างตาราง หรือ backup ข้อมูลไว้ก่อน เพราะถ้ามีข้อมูลมาก ๆ อาจมีปัญหา
    - เปลี่ยน การเรียงลำดับในแต่ละ field จาก latin1_swedish_ci เป็น tis620_thai_ci
    - ใน php script ต้องเพิ่ม คำสั่งข้างล่างนี้ ต่อจาก mysql_connect( .. );
    mysql_query("set names tis620");
    หรือ mysql_query("SET character_set_results=tis620");
    - วิธีนี้ไม่ได้แก้ปัญหาภาษาไทยในข้อมูลเดิม แต่จะไม่มีปัญหากับข้อมูลใหม่ที่เพิ่ม หรือแก้ไข

5. โปรแกรม MySQL-Front สำหรับจัดการ MySQL
จาก http://www.MySQLFront.de/download.htm
รุ่นล่าสุด Release Version 3.1, Build: 7.11 (1,394 KB, 01 Aug 2004):
ถ้าต้องการใช้หลังหมดอายุ สามารถเลือก help, info, license, Testing until ดูวันที่หมดอายุ แล้วไปเปลี่ยนวันที่กลับไปก่อนวันหมดอายุ สัก 7 วัน
    ภาพแสดงหน้าตาของโปรแกรม MySQL-Front ::
    โปรแกรม MySQL-Front for windows ใช้เชื่อมต่อเข้า MySQL เพื่อจักการข้อมูลได้โดยง่าย สามารถ download โปรแกรมจาก http://www.MySQLFront.de version ที่ได้มาเป็น shareware กำหนดเวลาการใช้ เมื่อหมดอายุ จะใช้ความสามารถหลายอย่างไม่ได้ เช่น import export แต่ถ้าใช้วิธีย้อนวันที่ก็ยังใช้งาน option เหล่านั้นได้

      5.1 แสดงเมื่อเริ่มต้น connect (MySQLFront1.gif)
      5.2 แสดงตารางในฐานข้อมูล moodle (MySQLFront2.gif)
      5.3 แสดงการเปิด table จาก database ที่กำหนด (version 3.0) (MySQLFront3.gif)

6. ภาษาสำหรับจัดการ MySQL เช่น php, asp, perl หรือ jsp
    6.1 ถ้าไม่เปิดบริการ MySQL แต่ขอเชื่อมต่อจากภาษา php
    
      ถ้าไม่ได้ install หรือ install แล้วไม่เปิดบริการ MySQL ท่านอาจพบข้อความข้างล่างนี้
      Warning: mysql_connect(): Can't connect to mysql server on 'localhost' (10061) in c:\httpd\htdocs\test.php on line 2
      สามารถใช้คำสั่ง เพื่อตรวจดูว่า php ในเครื่องสามารถติดต่อกับ MySQL ได้หรือไม่
    6.2 โปรแกรมภาษา php สำหรับอ่านข้อมูลจาก MySQL
      หาข้อมูลเรื่อง php เพิ่มเติมได้จาก http://www.thaiall.com/php <? $host = "localhost"; $db = "test"; $tb = "cust"; $user = "root"; $password = "yourpassword"; $query = "select * from $tb"; ########################### $connect = mysql_connect("$host","$user","$password"); $result = mysql_db_query($db,$query); if ($result) echo "OK<br>"; else exit; while ($object = mysql_fetch_object($result)) { foreach ($object as $o) echo $o; # echo $object->cust . " " . $object->custname; echo "<br>"; } echo "Total records : ".mysql_num_rows($result); mysql_close($connect); ?> ตัวอย่างผลลัพธ์ OK 101Mr.Boy 102Ms.Girl 103Mr.Man Total records : 3
    6.3 โปรแกรมภาษา php สำหรับสร้างฐาน สร้างตาราง ส่งข้อมูล และอ่านออกมา
    โปรแกรมนี้ชื่อ c:\httpd\htdocs\testmysql.php จะนำไปใช้ทันทีไม่ได้ จะต้องนำเครื่องหมาย ## ออก ส่วน # ไม่ต้องเอาออก เมื่อนำ ## ออกแล้ว ให้เปิดเว็บชื่อ http://localhost/testmysql.php ผลที่ได้ จะเป็นดังข้างล่าง เมื่อเห็นผลแล้วจะต้องใส่ เครื่องหมาย ## ลงไปตามตัวอย่างข้างล่าง มิเช่นนั้นการ run ครั้งต่อไปจะผิดพลาด เนื่องจากจำนวนข้อมูลที่เพิ่มขึ้น ผิดจากที่ต้องการ
      <body>Start of testmysql.php<hr> <? $db = "thaiall_db"; $host = "localhost"; $uname = "u"; $passwd = "p"; $connect=mysql_connect($host,$uname,$passwd); $tb="worker"; # === # This area for create database in first time. ## $result=mysql_create_db($db,$connect); ## echo "Create database perlphpasp (table $tb)"; ## $query="create table $tb (eid char(4),ename char(40))"; ## $result=mysql_db_query($db,$query); ## $sql="insert into $tb values('1001','Tom')"; ## $result=mysql_db_query($db,$sql); ## $sql="insert into $tb values('1002','Dang')"; ## $result=mysql_db_query($db,$sql); ## $sql="insert into $tb values('1003','Pom')"; ## $result=mysql_db_query($db,$sql); ## mysql_close($connect); # === echo "Display records : "; $query="select * from $tb"; $result = mysql_db_query($db,$query); if ($result) { echo "OK<br>"; } else { exit; } while ($object = mysql_fetch_object($result)) { echo $object->eid . " " . $object->ename . "<br>"; } echo "Total records : ".mysql_num_rows($result); ?> <hr>End of testmysql.php</body> ตัวอย่างผลลัพธ์ Start of testmysql.php ---------------------- Display records : OK 1001 Tom 1002 Dang 1003 Pom Total records : 3 ---------------------- End of testmysql.php
    6.4 โปรแกรมภาษา asp สำหรับอ่านข้อมูลจาก MySQL
      ถ้าจะใช้ MySQL ด้วย asp ใน windows98 ต้องติดตั้ง Connector/ODBC 3.51 -- production release
      Download Driver : MyODBC-3.51.02.exe 420 KB จาก http://pubnet.moph.go.th/download.html ในประเทศไทย
      Download Driver (new) : http://dev.mysql.com/get/Downloads/MyODBC3/MyODBC-standard-3.51.9-win.exe/from/pick
      Information : http://dev.MySQL.com/downloads/connector/odbc/3.51.html
      แสดงการกำหนด DSN name และเชื่อมต่อด้วย ASP
        <% set conn = server.createobject("adodb.connection") ' conn.open "burincust", "changetousername", "changetopassword" conn.open "burincust", "", "" sql = "select * from cust" set rs = conn.execute(sql) do while not rs.eof response.write(rs("cust") & " " & rs("custname") & "<br>") rs.MoveNext loop conn.close set rs = nothing set conn = nothing %>
        <% ConnString = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=test;uid=u;password=p;" Set conn = Server.CreateObject("ADODB.Connection") conn.Open ConnString Set rs = conn.Execute("SELECT * FROM cust") do while not rs.eof response.write(rs("cust") & " " & rs("custname") & "<br>") rs.MoveNext loop conn.close set rs = nothing set conn = nothing %>
    6.5 นับระเบียนด้วย mysql_num_rows หรือ mysql_num_fields
      อาจารย์ที่อุบล ถามผมใช้ว่า mysql_num_rows แล้วมีปัญหาใน RH9 .. ก่อนท่านส่ง code หาตัวอย่างมาดูก่อน
      ตัวอย่างนี้จาก http://th.php.net/manual/en/function.mysql-num-rows.php
        <? $link = mysql_connect("localhost", "mysql_user", "mysql_password"); mysql_select_db("database", $link); $result = mysql_query("SELECT * FROM table1", $link); $num_rows = mysql_num_rows($result); echo "$num_rows Rows\n"; ?>
ข้อมูล DATA TYPE จาก http://www.bc.msu.ac.th/wicha/Database/mysql_doc.htm ของ อ.วิชา
ประเภทขนาดที่จัดเก็บค่าที่จัดเก็บ
TINYINIT1 ไบต์ เป็นค่าจำนวนเต็มขนาดเล็กมาก ถ้าเป็นค่าบวกอย่างเดียว (unsigned) จะมีค่าตั้งแต่ 0 ถึง 255 แต่ถ้าเป็นค่าบวกและลบ ( signed ) จะมีค่าตั้งแต่ –128 ถึง 127
SMALLINT2 ไบต์ เป็นค่าจำนวนเต็มขนาดเล็ก ถ้าเป็นค่าบวกอย่างเดียว (unsigned) จะมีค่าตั้งแต่ 0 ถึง 65535 แต่ถ้าเป็นค่าบวกและลบ ( signed ) จะมีค่าตั้งแต่ –32768 ถึง 32767
MEDIUMINT3 ไบต์ เป็นค่าจำนวนเต็มขนาดกลาง ถ้าเป็นค่าบวกอย่างเดียว (unsigned) จะมีค่าตั้งแต่ 0 ถึง 16777215 แต่ถ้าเป็นค่าบวกและลบ ( signed ) จะมีค่าตั้งแต่ –8388608 ถึง 8388607
INT หรือ INTEGER4 ไบต์ เป็นค่าจำนวนเต็มขนาดปกติ ถ้าเป็นค่าบวกอย่างเดียว (unsigned) จะมีค่าตั้งแต่ 0 ถึง 4294967295 แต่ถ้าเป็นค่าบวกและลบ ( signed ) จะมีค่าตั้งแต่ –2147483648 ถึง 2147483647
BIGINT หรือ INTEGER8 ไบต์ เป็นค่าจำนวนเต็มขนาดใหญ่ ถ้าเป็นค่าบวกอย่างเดียว (unsigned) จะมีค่าตั้งแต่ 0 ถึง 18446744073709551615 แต่ถ้าเป็นค่าบวกและลบ ( signed ) จะมีค่าตั้งแต่ –9223372036854775808 ถึง 9223372036854775807
FLOAT(X)4 ไบต์ (ถ้า x มีค่าไม่เกิน 24) หรือ 8 ไบต์ ( ถ้า x มีค่าตั้งแต่ 25-53 ) ปกติแล้วจะมีค่า x เป็น 2 เป็นค่าจำนวนจริง
FLOAT4 ไบต์ เก็บค่าจำนวนจริงขนาดเล็ก ตั้งแต่ –3.402823466E+38 ถึง –1.175494351E-38,0 และ 1.175494351E-38 ถึง 3.402823466E+38
DOUBLE หรือ REAL8 ไบต์ เก็บค่าจำนวนจริงขนาดปกติ ตั้งแต่ –1.7976931348623157E+308 ถึง –2.2250738585072014E-308,0 และ 2.2250738585072014E-308 ถึง 1.7976931348623157E+308
DECIMAL(M,D) หรือ NUMBERIC(M,D) M+2 ไบต์ ถ้า D มากกว่า 0 หรือ M+1 ไบต์ ถ้า D = 0 เก็บเลขทศนิยม เช่น 12345.67
ข้อมูลที่เป็นวันที่
ประเภทขนาดที่จัดเก็บ ค่าที่จัดเก็บ
DATE3 ไบต์ เก็บวันที่และเวลาในรูปแบบ ค.ศ.-เดือน-วัน (YYYY-MM-DD) โดยมีค่าตั้งแต่ 1000-01-01 ถึง 9999-12-31
DATETIME8 ไบต์ เก็บวันที่และเวลาในรูปแบบ ค.ศ.-เดือน-วัน ชั่วโมง-นาที-วินาที (YYYY-MM-DD HH:MM:SS) โดยมีค่าตั้งแต่ 1000-01-01 00:00:00 ถึง 9999-12-31 23:59:59
TIMESTAMP [(M)]4 ไบต์ เก็บวันที่และเวลาโดยมีค่าตั้งแต่ 1970-01-01 00:00:00 ถึงปี ค.ศ 2037 ส่วนรูปแบบที่เก็บจะขึ้นอยู่กับค่า M ดังนี้ ถ้าไม่กำหนดค่า M หรือ M = 14 -> YYYY-MM-DD HH:MM:SS ถ้า M = 12 -> YY-MM-DD HH:MM:SS ถ้า M = 10 ->YY-MM-DD HH:MM ถ้า M = 8 ->YY-MM-DD ถ้า M = 6 ->YY-MM ถ้า M = 4 ->YY-MM ถ้า M = 2 ->YY
TIME3 ไบต์ เก็บวันที่และเวลาในรูปแบบ ค.ศ.-เดือน-วัน ชั่วโมง-นาที-วินาที (YYYY-MM-DD HH:MM:SS) โดยมีค่าตั้งแต่ 1000-01-01 00:00:00 ถึง 999-12-31 23:59:59
YEAR [(2 หรือ 4)]1 ไบต์ ถ้าระบุค่าเป็น 2 จะเก็บค่า 70-69 หมายถึงปี ค.ศ. 1970-2069 ถ้าระบุค่าเป็น 4 จะเก็บค่าปี ค.ศ.1901-2155
ข้อมูลที่เป็นตัวอักษร
ประเภทขนาดที่จัดเก็บ ค่าที่จัดเก็บ
CHAR(M)ขนาดตามค่า M แต่ไม่เกิน 255 ไบต์ อักษรตามรหัส ascii
VARCHAR(M) ขนาดตามข้อมูลจริง แต่ไม่เกิน 255 ไบต์ อักษรตามรหัส ascii
TINYBLOB หรือ TINYTEXT เก็บตามขนาดจริง +1 ไบต์ แต่ไม่เกิน 255 ไบต์ อักษรตามรหัส ascii
BLOB หรือ TEXT เก็บตามขนาดจริง +2 ไบต์ แต่ไม่เกิน 65,535 ไบต์ อักษรตามรหัส ascii
MEDIUMBLOB หรือ MEDIUMTEXT เก็บตามขนาดจริง +3 ไบต์ แต่ไม่เกิน 16,777,215 ไบต์ อักษรตามรหัส ascii
LONGBLOB หรือ LONGTEXT เก็บตามขนาดจริง +4 ไบต์ แต่ไม่เกิน 4,294,967,295 ไบต์ อักษรตามรหัส ascii
ENUM(‘value1’,’value2’,…) 1 ไบต์ หรือ 2 ไบต์ แล้วแต่จำนวนค่า value ที่กำหนดซึ่งกำหนดได้ถึง 65,535 ค่า ค่าที่กำหนดเอาไว้
SET(‘value1’,’value2’,…) 1,2,3,4 หรือ 8 ไบต์ แล้วแต่จำนวนสมาชิกในเซ็ตของ SET (สูงสุดไม่เกิน 64) ค่าที่อยู่ในรูปของเซ็ต

แนะนำเว็บใหม่ : ผลการจัดอันดับ
รักลำปาง : thcity.com : korattown.com : topsiam.com : มหาวิทยาลัยโยนก
ศูนย์สอบ : รวมบทความ : ไอทีในชีวิตประจำวัน : ดาวน์โหลด : yourname@thaiall.com
ติดต่อ ทีมงาน ชาวลำปาง มีฝันเพื่อการศึกษา Tel.08-1992-7223