Chapter 3

စတင်ခြင်း

Database ထဲက data ကို ဆွဲမထုတ်ခင်မှာ MySQL မှာ database ဦးစွာ ဆောက်ဖို့ လိုပါတယ်။

MySQL ကို ဖွင့်ပါ။​ Terminal သို့မဟုတ် command line မှာ

mysql -uroot -p

Password တောင်းပါလိမ့်မယ်။ MySQL မှာ သုံးထားသည့် root password နဲ့ ဝင်လိုက်ပါ။

Create Database

create database school;

ဆိုပြီး ရိုက်ပါ။ ဒါဆိုရင် school database ဆောက်ပြီးပါပြီ။

show databases;

ဆိုရင် လက်ရှိ ဆောက်ထားသည့် table တွေကို ဖော်ပြပေးပါလိမ့်မယ်။ ကျွန်တော်တို့ school database ကို သုံးမှာ ဖြစ်သည့် အတွက်

use school;

ဆိုပြီး school database သုံးဖို့ ရွေးလိုက်ပါမယ်။

Create Table

create table students (
id int auto_increment primary key,
name varchar(255) not null,
join_date DATE,
bio Text,
room_id int,
created_at timestamp default current_timestamp);

အခု ဆိုရင် student table တစ်ခု တည်ဆောက်သွားပါလိမ့်မယ်။ အထက်ပါ code ကို ရှင်းပြပါမယ်။

SQL မှာ create table ဆိုတာကတော့ Table တစ်ခု တည်ဆောက်တာပါ။

ပြီးလျှင် [column name] [data type] [option], ဆိုပြီး လာပါတယ်။

id int auto_increment primary key,

ဆိုတာကတော့ column name က id ဖြစ်ပြီးတော့ int data type ပါ။​ auto_increment ဆိုတာကတော့ အလိုအလျောက် နံပတ် ထည့်သွားမယ် လို့ ပြောထားတာပါ။ primary key ဆိုတာကတော့ PRIMARY Key ဖြစ်ကြောင်း ကြေငြာထားတာပါ။

name varchar(255) not null,

not null ဆိုတာကတော့ NULL ဖြစ်ခွင့်မရှိဘူး။ data ထည့်သည့် အခါမှာ information တစ်ခုခု ဖြည့်ဖို့ လိုတယ် လို့ ကြေငြာထားတာပါ။

created_at timestamp default current_timestamp

timestamp ကတော့ timestamp value ဖြစ်ပါတယ်။ default ကတော့ ဘာ data မှ​ မထည့်ခဲ့လျှင် default နောက်မှ ရေးထားသည့် value ကို ထည့်မယ်။​ default current_timestamp လို့ ရေးထားသည့် အတွက်ကြောင့် current timestamp ကို ထည့်သွားမယ်လို့ ဆိုပါတယ်။

describe students;

ဆိုပြီး ရေးလိုက်ရင်် students table မှာ ပါသည့် columns တွေကို တွေ့ရပါမည်။

PRIMARY KEY

PRIMARY KEY ဆိုတာကတော့ table တစ်ခု တည်ဆောက်တိုင်း အမြဲထည့်သွင်းဖို့ လိုပါတယ်။ PRIMARY KEY က မထပ်ရဘူး။ Studen ID လိုပါပဲ။ တကျောင်းလုံးမှာ Studen ID 1 က တစ်ယောက်ပဲ ရှိပါတယ်။ နောက်ပြီး Customer ID လိုပါပဲ။​ Customer စာရင်းကို မှတ်ရင် Customer ID 1 က နောက်တစ်ယောက် ထပ်မရှိပါဘူး။ အခု Students table မှာ row တစ်ကြောင်းထည့်လိုက်ရင် id က 1 နဲ့ ဝင်သွားမယ်။ နောက်တစ်ကြောင်း ထပ်ဖြည့်ရင် 2 နဲ့ ဝင်ပါမယ်။ အလိုလို ၁ တိုးသွားမယ်။ auto_increment ထည့်ထားသည့် အတွက် အလိုလို ၁ တိုးသွားတာပါ။

Data Type

SQL မှာ ပါသည့် Data type တွေကို အောက်မှာ ဖော်ပြထားပါတယ်။

Data Type Spec Data Type Spec
CHAR String (0-255) INT Integer (-2147483648 to 2147483647)
VARCHAR String (0-255) BIGINT Big Integer (-9223372036854775808 to 9223372036854775807)
TINYTEXT String (0-255) FLOAT Decimal (precise to 23 digits)
TEXT String (0-65535) DOUBLE Decimal (24 to 53 digits)
BLOB String (0-65535) DECIMAL "DOUBLE" store as String
MEDIUMTEXT String (0-16777215) DATE YYYY-MM-DD
MEDIUMBLOB String (0-16777215) DATETIME YYYY-MM-DD HH:MM:SS
LONGTEXT String (0-4294967295) TIMESTAMP YYYYMMDDHHMMSS
LONGBLOB String (0-4294967295) TIME HH:MM:SS
TINYINT String (-128 to 127) ENUM One of preset options
SMALLINT Integer (0-4294967295) SET Selection of preset options
MEDIUMINT Integer (-8388608 to 8388607) BOOLEAN TINYINT(1)

အထက်ပါ data type တွေက လိုအပ်သလို အသုံးပြုနိုင်ပါတယ်။ သတိပြုရမှာကတော့ size ကြီးလေလေ storage များလေလေပါပဲ။ နောက်ပိုင်း search နဲ့ ပတ်သက်ပြီး index ထောက်ရာမှာလည်း size သေးလေလေ ပို အဆင်ပြေလေလေ ပါပဲ။ စာလုံး အရေအတွက် က 255 ထက် မကျော်နိုင်လျှင် varchar ကို သုံးတာ ပို အဆင်ပြေပါမယ်။

Data type ထဲမှာ BLOB ဆိုတာကတော့ Binary data တွေ သိမ်းဖို့ပါ။

Insert Row

ကျွန်တော်တို့တွေ students table ထဲကို row တစ်ကြောင်းထည့်ပါမယ်။

INSERT INTO students (name,join_date,bio,room_id)
VALUES ("Saturngod","2020-06-19","Student to learn SQL",221);

အခု ဆိုရင် row တစ်ကြောင်းထည့်သွင်းပြီးပါပြီ။

Row ကို ထည့်ချင်ရင်တော့

INSERT INTO [table name] ([COLUMN],[COLUMN],[COLUMN])
VALUES ([VALUE],[VALUE],[VALUE]);

ဆိုပြီး ထည့်ရပါမယ်။ Columns က တစ်ခု သို့မဟုတ် တစ်ခု ထက်မက ဖြစ်နိုင်ပါတယ်။ VALUES မှာ ထည့်ရမည့် value က အရှေ့က column order နဲ့ ညီမှ ရပါမယ်။

Retrieve Row

အခု database ထဲက ထည့်ထားသည့် value ကို ထုတ်ကြည့်ရအောင်။

SELECT * FROM students;

ထည့်ထားသည့် data တွေ ထွက်လာပါမယ်။ Row တစ်ကြောင်းတည်း ဆိုရင် တစ်ကြောင်းပဲ ထွက်လာပါမယ်။

+----+-----------+------------+----------------------+---------+---------------------+
| id | name   | join_date  | bio     | room_id | created_at |
+----+-----------+------------+----------------------+---------+---------------------+
|  1 | Saturngod | 2020-06-19 | Student to learn SQL | 221 | 2020-06-22 01:17:59 |
+----+-----------+------------+----------------------+---------+---------------------+
1 row in set (0.000 sec)

အခု ဆိုရင်တော့ create database , create table, insert row , retrieve data တို့ ကို လေ့လာပြီးပါပြီ။