Please enable JavaScript.
Coggle requires JavaScript to display documents.
MySQL - Coggle Diagram
MySQL
การใช้งานคำสั่งใน MySQL
การสร้างฐานข้อมูล ด้วยคำสั่ง Create Database
รูปแบบ create database [if not exists] db_name
สร้างฐานข้อมูลชื่อ db_name ในกรณีที่มี database นั้นอยู่แล้ว และไม่ได้ใช้ออปชัน if not exists จะปรากฏข่าวสารแสดงความผิดพลาด (Error Message)
แสดง database ที่มีในระบบทั้งหมด show databases;
สร้างฐานข้อมูลใหม่ ชื่อ mydb create database mydb;
การลบฐานข้อมูล ด้วยคำสั่ง Create Database
รูปแบบ drop database [if not exists] db_name
ทดสอบสร้างฐานข้อมูลใหม่ ชื่อ test001 create database test001;
แสดงฐานข้อมูลที่มีอยู่ show databases;
ลบฐานข้อมูลชื่อ test001 drop database test001;
แสดงฐานข้อมูลอีกครั้ง(ฐานข้อมูล test001 จะหายไป)
การเลือกใช้ฐานข้อมูลด้วยคำสั่ง Use
รูปแบบ Use db_name เลือกใช้ฐานข้อมูลชื่อ db_name
เลือกใช้ database ชื่อ mydb use mydb;
การตารางใหม่ ด้วยคำสั่ง Create table
แสดงรายละเอียดคำสั่งที่สร้างตารางข้อมูล dept show create table dept;
แสดงรายละเอียดคำสั่งที่สร้างตารางข้อมูล personshow create table person;
แสดงรายละเอียดของโครงสร้างหรือฟิลด์ในตาราง dept desc dept; หรือ show columns from dept; หรือ explain dept;
แสดงรายละเอียดสถานะของตารางข้อมูล Show table status;
การแก้ไขโครงสร้างตาราง ด้วยคำสั่ง Alter table
รูปแบบ Alter [IGNORE] TABLE tbl_name alter_spec [, alter_spec…]
เปลี่ยนให้ฟิลด์ ID ในตาราง Person เป็นข้อมูลประเภท auto_increment (ควรแสดงรายละเอียดของฟิลด์ในตารางก่อน)desc person; alter table person modify id int(5) not null default ‘1’ auto_increment;
เปลี่ยนขนาดของฟิลด์ Lastname เป็น varchar(40) alter table person modify lastname varchar(40);
เพิ่มฟิลด์ start_date ซึ่งเป็นข้อมูลชนิด date เข้าไปในโครงสร้าง alter table person add start_date date;
ลบฟิลด์ birthday ทิ้ง alter table person drop birthday;
เปลี่ยนชนิดของฟิลด์ dept_name ในตาราง dept เป็น tinytext alter table dept modify dept_name tinytext;
ลบตารางข้อมูลด้วยคำสั่ง Drop table
รูปแบบ drop table [if exists] tbl_name [, tbl_name,…]
เปลี่ยนชื่อตารางข้อมูลด้วยคำสั่ง Rename table
รูปแบบrename table tbl_name to new_table_name [,tbl_name2 to new_table_name2,…]
ลบตารางข้อมูลด้วยคำสั่ง Drop table
แสดงตารางทั้งหมดในฐานข้อมูล show table status;
สร้างตารางใหม่ชื่อ Example create table example (id int(2));
เปลี่ยนชื่อตารางข้อมูลจาก Example เป็น Newexample
Rename table example to newexample
ลบตารางข้อมูล NewExample drop table newexample;
การออกจากโปรแกรม MySQL
รูปแบบ quit ใช้สำหรับออกจากโปรแกรม MySQL
ออกจากโปรแกรม MySQL quit
การใช้งานคำสั่งในMySQL การสืบค้นข้อมูล
การสืบค้นข้อมูล ด้วยคำสั่ง Select
รูปแบบ select…from table_name where condition [order by…] select…from table_name group by…[having condition[order by…]]
การสืบค้นจากตารางเดียว
แสดงข้อมูลทั้งหมดในตาราง Personselect * from person;
แสดงข้อมูลทั้งหมดในตาราง Deptselect * from dept;
การสืบค้นโดยกำหนดเงื่อนไข โดยใช้
Where clause
Operator ทางคณิตศาสตร์ = <> <= >= in between not like
Operator ทางตรรกะ or and not
ค้นหาข้อมูลในตาราง Person ที่มี id เป็น 5select * from person where id=5;
ค้นหาข้อมูลของคนที่มีตำแหน่ง Manager select * from person where position=‘manager’;
ค้นหาข้อมูลของคนที่มีเงินเดือนมากกว่า 20000 บาทselect * from person where salary>20000;
ค้นหาข้อมูลในตาราง Dept ที่มีรหัสแผนกเป็น 13select * from dept where dept_no=13;
ค้นหาข้อมูลของคนที่มีเงินเดือนระหว่าง 10000 ถึง 20000 select * from person where salary between 10000and 20000
การสืบค้นจากหลายตาราง
แสดงชื่อ (firstname) นามสกุล (lastname) และชื่อแผนก (dept_name) ของคนที่ทำงานในรหัสแผนก 13 พร้อมทั้งแสดงชื่อแผนก (dept_name) ว่าคือแผนกอะไร select person.firstname, person.lastname, person.dept_no, dept.dept_name from dept, person where dept.dept_no=person.dept_noand dept.dept_no=13
การใช้ Order by clause
ทำการจัดเรียงข้อมูล จากน้อยไปมาก และจากมากไปน้อย ถ้าไม่กำหนด default จะเป็นน้อยไปมาก
แสดงชื่อ นามสกุลและเงินเดือน จากตาราง person โดยเรียงเงินเดือนจากมากไปน้อย select firstname, lastname, salary from person order by salary desc;
แสดงข้อมูลจากตาราง dept โดยเรียงชื่อแผนกจากน้อยไปมาก (อักษร A ไป Z)select * from dept order by dept_name asc;
การใช้งาน group aggregate function
ได้แก่ฟังก์ชัน count(), min(), max(), sum(), avg(), std()
นับจำนวนพนักงานที่เป็น manager select count(*) from person where position=‘manager’;
แสดงค่าเงินเดือนที่น้อยที่สุดselect min(salary) from person;
แสดงค่าเงินเดือนมากที่สุดของรหัสแผนก 13 select max(salary) from person where dept_no=13;
การใช้งาน group by clause
ใช้ในการจัดกลุ่มข้อมูล เช่น จัดกลุ่มตามรหัสแผนก
นับจำนวนพนักงานในแต่ละตำแหน่งselect position, count(*) from person group by position;
รวมเงินเดือนของพนักงานแต่ละรหัสแผนกselect dept_no, sum(salary) from person group by dept_no;
การใช้งานคำสั่งใน MySQL การจัดการระเบียนข้อมูล
การเพิ่มระเบียนข้อมูลใหม่ ด้วยคำสั่ง Insert
รูปแบบ insert into tbl_name [(col_name,…)] value … insert into tbl_name [(col_name,…)] select … Insert … value …
เพิ่มข้อมูลเข้าสู่ตาราง Person ในฐานข้อมูล Mydb (แบบไม่ระบุฟิลด์) insert into person values (null, ‘Somchai’,’Sandee’,’Clerk’,8000,11,’2005/12/25’);
เพิ่มข้อมูลเข้าสู่ตาราง Person (แบบระบุฟิลด์) โดยการเพิ่มระเบียนใหม่ เฉพาะฟิลด์ firstname ว่า Somsri เท่านั้น insert into person (firstname) values (‘Somsri’);
ป้อนข้อมูลใน ฟิลด์ start_date เป็นวันที่ 29 ธ.ค. 2005 รูปแบบ (‘yyyy/mm/dd’) insert into person (start_date) values (‘2005/12/29’);
การเพิ่มระเบียนข้อมูลใหม่ ด้วยคำสั่ง Insert Dept_no dept_name 11 Computer 12 Information IT 13 Marketing
การแก้ไข เปลี่ยนแปลงข้อมูล ด้วยคำสั่ง Update
รูปแบบ update tbl_name set col_name1=expr1, [col_name2=expr2, …] [where condition] Update … set … where
แก้ไขข้อมูล ID=2 (ซึ่งปัจจุบันมีแค่ firstname เท่านั้น) ให้มีข้อมูลดังต่อไปนี้ update person set lastname=‘Jaidee’, position=‘Clerk’,salary=8500, dept_no=13, start_date=‘2005/10/14’ where id=9; select * from person;
การลบระเบียนข้อมูล ด้วยคำสั่ง Delete
รูปแบบdelete from tbl_name [where condition]
ลบข้อมูลในตารางข้อมูล Person เฉพาะข้อมูลที่ 8 delete from person where id=8; select * from person;