Changing the schema · การแก้ไขโครงสร้างฐานข้อมูล (Schema)
Changing a table's structure
A table's design is not fixed. ALTER TABLE … ADD adds a new column to an existing table:
Existing rows get NULL in the new column until you fill them in.
การเปลี่ยนโครงสร้างตาราง
การออกแบบตารางไม่ได้ตายตัว ALTER TABLE … ADD เพิ่มคอลัมน์ใหม่ลงในตารางที่มีอยู่แล้ว
ALTER TABLE product ADD weight DECIMAL(5, 2);
แถวที่มีอยู่จะถูกเติม NULL ในคอลัมน์ใหม่จนกว่าคุณจะกรอกข้อมูล
Removing a table
DROP TABLE deletes a whole table — its structure and all its rows:
This cannot be undone, so use it with care. Dropping a table that other tables reference can break referential integrity.
การลบตาราง
DROP TABLE ลบทั้งตาราง — โครงสร้าง และ แถวทั้งหมด ของมัน
DROP TABLE product;
สิ่งนี้ไม่สามารถย้อนกลับได้ ดังนั้นต้องใช้ด้วยความระมัดระวัง การลบตารางที่ตารางอื่นอ้างอิงอาจทำให้ referential integrity บิดเบี้ยว
Common mistakes
ALTER TABLEadds or changes a column.- Existing rows get NULL in a new column unless you set a default.
ข้อผิดพลาดทั่วไป
ALTER TABLEเพิ่มหรือแก้ไขคอลัมน์- แถวเดิมจะได้ NULL ในคอลัมน์ใหม่เว้นแต่คุณจะกำหนดค่าเริ่มต้น
Add a new column weight of type DECIMAL(5, 2) to the product table using ALTER TABLE. · เพิ่มคอลัมน์ใหม่ weight ประเภท DECIMAL(5, 2) เข้าไปในตาราง product โดยใช้ ALTER TABLE
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Delete the entire product table with DROP TABLE. (Afterwards only customer and orders remain.) · ลบตาราง product ทั้งหมดด้วย DROP TABLE (หลังจากนั้นจะมีเพียง customer และ orders เหลืออยู่)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่