การ สร้าง table

       ก่อนที่เราจะสร้าง table เราจะต้องสร้าง file database เสียก่อน และในตอนที่ผ่านมาเราก็สร้าง file database ที่ชื่อ "datatest" ไว้แล้ว ให้เรานำ file "datatest" มาใช้อีกครั้งโดย ใช้คำสั่ง USE database

       การสร้าง table จำเป็นต้องทำความเข้าใจ ถึง Table Option เสียก่อน เพราะว่า table ต้องมี Option เป็นส่วนประกอบของโครงสร้างดังนี้

Table option

Option

Description

AUTO_INCREMENT The next auto increment value you want to set for your table
(MyISAM)
AVG_ROW_LENGTH An approximation of the average row length for your table. You only
need to set this for tables with variable size records.
CHECKSUM Set this to 1 if you want MySQL to maintain a checksum for all rows
(makes the table a little slower to update but makes it easier to nd
corrupted tables) (MyISAM)
COMMENT A 60 character comment for your table
MAX_ROWS Max number of rows you plan to store in the table
MIN_ROWS Minimum number of rows you plan to store in the table
PACK_KEYS Set this to 1 if you want to have smaller index. This usually makes
updates slower and reads faster (MyISAM, ISAM).
PASSWORD Encrypt the .frm le with a password. This option doesn't do anything
in the standard MySQL version.
DELAY_KEY_WRITE Set this to 1 if want to delay key table updates until the table is closed
(MyISAM).
ROW_FORMAT De nes how the rows should be stored (for the future).
PRIMARY KEY Difference Record must not equals 
NOT NULL Must have any thing  in record not null

       การสร้าง table  มีรูปแบบคำสั่งดังนี้
 

       รูปแบบคำสั่งสร้าง table
        CREATE TABLE data1 (field1, filed2, filed3, filedn)
หรือ
       create_definition:
       col_name type [NOT NULL | NULL] [DEFAULT default_value] 
                               [AUTO_INCREMENT][PRIMARY KEY] 
                               [reference_definition]
        or    PRIMARY KEY (index_col_name,...)
        or    KEY [index_name] (index_col_name,...)
        or    INDEX [index_name] (index_col_name,...)
        or    UNIQUE [INDEX] [index_name] (index_col_name,...)

        คำอธิบาย
        CREATE TABLE data1
        CREATE TABLE เป็นคำสั่งสร้าง table
       data1 เป็นชื่อ table ที่ต้องการสร้าง
       field1 เป็นชนิดของ column

       ตัวอย่าง
       mysql>CREATE TABLE  phonebook(
       -> name VARCHAR(25),
       -> email VARCHAR(30),
       -> phone INT,
       -> ID INT NOT NULL AUTO_INCREMENT,
       -> PRIMARY KEY(ID));
 

       ให้สร้างตารางที่ชื่อว่า phone book โดยมีรายละเอียดดังตัวอย่างข้างต้น 
       ผลลัพธ์ที่ได้ดังภาพข้างล่าง
 


mysql> CREATE TABLE phonebook(

    -> name VARCHAR(25),

    -> email VARCHAR(30),

    -> phone INT,

    -> ID INT NOT NULL AUTO_INCREMENT,

    -> PRIMARY KEY(ID));

Query OK, 0 rows affected (0.00 sec)

mysql>

      Tip คุณสามารถกดปุ่มลูกศรชี้ขึ้นได้ โดยโปรแกรมจะทวนคำสั่งจากคำสั่งสุดท้ายไปหาคำสั่งแรก ตามลำดับ ซึ่งจะสร้างความสะดวกให้คุณ โดยไม่ต้องพิมพ์คำสั่งซ้ำๆอีก  แต่สำหรับผู้เริ่มต้นศึกษาแล้ว ผู้เขียนไม่แนะนำให้ใช้วิธีนี้ เนื่องจากจะไม่เป็นการช่วยฝึกทบทวนการใช้คำสั่งเลย...


Copyright By Passkorn Roungrong 2000