การสร้างตารางข้อมูลใหม่ด้วยคำสั่ง CREATE TABLE ใน MariaDB

   หลังจากเรียนรู้การสร้างฐานข้อมูลที่จะใช้งานกันไปแล้วก็มาถึงคราวการสร้างตารางข้อมูลกันบ้าง  ในการสร้างตารางฐานข้อมูลควรจะมีการออกแบบที่ดีเพื่อให้รองรับการใช้งานที่ตรงตามความต้องการ  แต่ก็สามารถที่จะเพิ่ม ลบ แก้ไขฟิลด์ได้ เดี๋ยวจะมาเรียนรู้กันอีกทีนะครับ

Syntax

CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
    (create_definition,...) [table_options    ]... [partition_options]
CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
    [(create_definition,...)] [table_options   ]... [partition_options]
    select_statement
CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
   { LIKE old_table_name | (LIKE old_table_name) }

select_statement:
    [IGNORE | REPLACE] [AS] SELECT ...   (Some legal select statement)


ในที่นี้เราจะใช้งานฐานข้อมูลที่สร้างไว้ชื่อ mydb ก็จะใช้คำสั่งเข้าใช้งาน
use mydb


จากนั้นก็ใช้คำสั่ง CREATE TABLE เพื่อสร้างตารางข้อมูลดังนี้
create table dept
    -> (
    -> dept_no int(2) not null default '0',
    -> dept_name text default NULL,
    -> primary key (dept_no)
    -> );


ทดสอบผลการสร้างตารางข้อมูลด้วยคำสั่ง SHOW TABLES ดังนี้
show tables;


จะได้ผลได้ภาพดังนี้

คำสั่งในแบบ command line แบบชัดๆ กัน
C:\Program Files\MariaDB 10.1\bin>mysql -u root -p
Enter password: ********
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.1.16-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use mydb
Database changed
MariaDB [mydb]> create table dept
    -> (
    -> dept_no int(2) not null default '0',
    -> dept_name text default NULL,
    -> primary key (dept_no)
    -> );
Query OK, 0 rows affected (0.29 sec)

MariaDB [mydb]> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| dept                 |
+----------------+
1 row in set (0.00 sec)

MariaDB [mydb]>





ไม่มีความคิดเห็น

ขับเคลื่อนโดย Blogger.