Skip to content · ⁨ข้ามไปยังเนื้อหา⁩
Subjects · ⁨รายวิชา⁩
  • 1 Querying basics
    1.1

    SELECT และ FROM

    English

    A database 数据库 keeps data in tables 表. A query 查询 reads data with SELECT. SELECT * returns every column; FROM names the table.

    • Each row 行 is one record 记录. SQL keywords are written in UPPERCASE by habit.
    • A statement ends with a semicolon ;.
    ไทย

    ฐานข้อมูลเก็บข้อมูลในตาราง Query อ่านข้อมูลด้วย SELECT. SELECT * กลับมาทุกคอลัมน์; FROM ชื่อบท

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    SELECT * FROM student;
    
    ตารางเก็บเรคอร์ด: แต่ละแถวคือเรคอร์ดหนึ่ง แต่ละคอลัมน์คือฟิลด์หนึ่ง
    ตารางเก็บเรคอร์ด: แต่ละแถวคือเรคอร์ดหนึ่ง แต่ละคอลัมน์คือฟิลด์หนึ่ง
    • แต่ละแถวคือเรคอร์ดหนึ่ง คำสั่ง SQL เขียนเป็นตัวพิมพ์ใหญ่ตามความเคยชิน
    • คำสั่งสิ้นสุดด้วยเครื่องหมายอัฒปจ ➟ ;
    1.2

    เลือกคอลัมน์

    English

    List the columns 列 you want, separated by commas. Use AS to rename a column in the result (an alias 别名).

    A selected column can also be a calculation — pair it with AS to name the new column:

    • SELECT DISTINCT col removes duplicate values from the result.
    ไทย

    ระบุคอลัมน์ที่ต้องการ แยกด้วยลูกน้ำ ใช้ AS เพื่อเปลี่ยนชื่อคอลัมน์ในผลลัพธ์ (alias)

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    SELECT name, score AS mark FROM student;
    

    คอลัมน์ที่เลือกสามารถเป็นการคำนวณได้ — จับคู่กับ AS เพื่อตั้งชื่อคอลัมน์ใหม่:

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    SELECT name, score + 5 AS bonus FROM student;
    
    • SELECT DISTINCT col จะลบค่าซ้ำออกจากผลลัพธ์.
    1.3

    กรองด้วย WHERE

    English

    WHERE keeps only the rows that match a condition 条件. Compare with =, <> (not equal), <, >, <=, >=. Put text in single quotes.

    • SQL runs the parts in this order: FROM → WHERE → SELECT.

    Common mistakes

    • Put text values in single quotes: WHERE name = 'Ann'; column names have no quotes.
    • SELECT * returns every column — name only the columns you need.
    • WHERE filters rows; it comes after FROM.
    ไทย

    WHERE เก็บเฉพาะแถวที่ตรงกับเงื่อนไข เปรียบเทียบกับ =, <> (ไม่เท่ากับ), <, >, <=, >=. ใส่ข้อความในอัญลักษณ์

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71), (3, 'Ana', 95);
    SELECT name, score FROM student WHERE score >= 80;
    
    • SQL รันส่วนต่างๆ ตามลำดับนี้: FROM → WHERE → SELECT.

    ข้อผิดพลาดที่พบบ่อย

    • ใส่ค่าข้อความในอัญลักษณ์: WHERE name = 'Ann'; ชื่อคอลัมน์ไม่มีอัญลักษณ์
    • SELECT * กลับมาทุกคอลัมน์ — ระบุเฉพาะคอลัมน์ที่คุณต้องการ
    • WHERE กรองแถว; มาหลัง FROM
  • 2 Filtering & logic
    2.1

    AND, OR, NOT

    English

    Combine conditions with AND, OR, NOT. AND needs all sides true; OR needs any side true; NOT reverses one. Use brackets to set the precedence 优先级.

    ไทย

    รวมเงื่อนไขด้วย AND, OR, NOT. AND ต้องทุกด้านเป็นจริง; OR ต้องด้านใดด้านหนึ่งเป็นจริง; NOT สลับค่าหนึ่ง ใช้วงเล็บเพื่อกำหนดลำดับความสำคัญ

    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT name FROM student WHERE form = '11A' AND score >= 90;
    
    AND ต้องทุกด้านเป็นจริง; OR ต้องด้านใดด้านหนึ่ง; NOT สลับค่าหนึ่ง
    AND ต้องทุกด้านเป็นจริง; OR ต้องด้านใดด้านหนึ่ง; NOT สลับค่าหนึ่ง
    2.2

    LIKE, IN และ BETWEEN

    English

    LIKE matches a text pattern 模式: % stands for any text and _ for one character — these are wildcards 通配符.

    Pattern Matches
    'M%' starts with M
    '%a' ends with a
    '%an%' contains "an"
    'M_i' M, then exactly one character, then i

    IN matches a set 集合 of values; BETWEEN matches a range 范围 (both ends included).

    ไทย

    LIKE ตรงกับรูปแบบข้อความ: % แทนข้อความใดๆ และ _ แทนหนึ่งอักขระ — สิ่งเหล่านี้เป็น wildcards

    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT name FROM student WHERE name LIKE 'M%';        -- starts with M
    
    รูปแบบ ตรงกับ
    'M%' เริ่มต้นด้วย M
    '%a' สิ้นสุดด้วย a
    '%an%' มี "an" อยู่ภายใน
    'M_i' M, ตามด้วยหนึ่งอักขระพอดี, ตามด้วย i

    IN ตรงกับชุดของค่า; BETWEEN ตรงกับช่วง (ทั้งสองข้างรวมอยู่ด้วย)

    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT name, score FROM student
    WHERE form IN ('11A', '11C') AND score BETWEEN 80 AND 100;
    
    2.3

    NULL

    English

    NULL marks a missing value 缺失值 — nothing was stored in that cell. NULL is not 0 and not an empty string. A comparison with = or <> never matches it; test with IS NULL or IS NOT NULL.

    • WHERE score = NULL returns no rows at all — not even Sam's row.
    • A NULL in arithmetic gives NULL: score + 5 stays NULL for Sam.

    Common mistakes

    • Test for an empty value with IS NULL, never = NULL.
    • In LIKE, % matches any text and _ matches one character: 'A%' means "starts with A".
    • IN (1, 2, 3) is shorter than many ORs; BETWEEN a AND b includes both ends.
    ไทย

    NULL ทำเครื่องหมายว่าขาดค่า — ไม่มีอะไรถูกบันทึกในเซลล์นั้น NULL ไม่ใช่ 0 และไม่ใช่สตริงว่าง การเปรียบเทียบกับ = หรือ <> จะไม่ตรงกับมันเลย; ทดสอบด้วย IS NULL หรือ IS NOT NULL

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', NULL), (3, 'Ana', 95);
    SELECT name FROM student WHERE score IS NULL;
    
    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', NULL), (3, 'Ana', 95);
    SELECT name, score FROM student WHERE score IS NOT NULL;
    
    • WHERE score = NULL กลับมาไม่มีแถวนักเลย —แม้แต่แถวของ Sam ก็ไม่มี
    • ค่า NULL ในการคำนวณจะทำให้ได้ NULL: score + 5 ยังคงเป็น NULL สำหรับ Sam.

    ข้อผิดพลาดที่พบบ่อย

    • ทดสอบค่าว่างด้วย IS NULL, ห้ามใช้ = NULL
    • ใน LIKE, % ตรงกับข้อความใดๆ และ _ ตรงกับหนึ่งอักขระ: 'A%' หมายถึง "เริ่มต้นด้วย A"
    • IN (1, 2, 3) สั้นกว่า OR หลายรายการ; BETWEEN a AND b รวมถึงจุดเริ่มต้นและจุดสิ้นสุด
  • 3 Sorting & limiting
    3.1

    ORDER BY และ LIMIT

    English

    ORDER BY sorts 排序 the result rows. Add DESC for descending 降序 (high to low); the default is ascending 升序 (low to high).

    Sort by more than one column

    List several columns. A tie 平局 in the first column is broken by the next.

    LIMIT (top-N)

    LIMIT n keeps only the first n rows — pair it with ORDER BY for a top-N 前 N 名 list.

    • ORDER BY can also sort by an alias or a calculation: ORDER BY avg_score DESC.
    • Text sorts alphabetically: ORDER BY name runs A → Z.

    Common mistakes

    • ORDER BY sorts ascending by default; add DESC for descending.
    • ORDER BY comes near the end, after WHERE and GROUP BY.
    • LIMIT caps how many rows come back, but only after sorting.
    ไทย

    ORDER BY เรียงลำดับแถวผลลัพธ์ เพิ่ม DESC为了降序 (สูงไปต่ำ); ค่าเริ่มต้นคือ ascending (ต่ำไปสูง)

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei',88),(2,'Sam',71),(3,'Ana',95);
    SELECT name, score FROM student ORDER BY score DESC;
    

    เรียงตามหลายคอลัมน์

    ระบุหลายคอลัมน์ หากมีค่าเท่ากันในคอลัมน์แรก จะใช้คอลัมน์ถัดไปตัดสิน

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei',88),(2,'Sam',88),(3,'Ana',95);
    SELECT name, score FROM student ORDER BY score DESC, name ASC;
    

    LIMIT (Top-N)

    LIMIT n เก็บเฉพาะ n แถวแรก — จับคู่กับ ORDER BY เพื่อสร้างรายการ Top-N N

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei',88),(2,'Sam',71),(3,'Ana',95);
    SELECT name, score FROM student ORDER BY score DESC LIMIT 2;
    
    • ORDER BY สามารถเรียงตาม alias หรือการคำนวณได้: ORDER BY avg_score DESC
    • ข้อความเรียงตามตัวอักษร: ORDER BY name รัน A → Z

    ข้อผิดพลาดที่พบบ่อย

    • ORDER BY เรียง ascending โดยค่าเริ่มต้น; เพิ่ม DESC เพื่อ descending
    • ORDER BY มาใกล้ท้าย หลัง WHERE และ GROUP BY
    • LIMIT จำกัดจำนวนแถวที่กลับมา แต่หลังจากการเรียงลำดับแล้วเท่านั้น
    ORDER BY เรียง; LIMIT เก็บ n แถวแรกหลังการเรียง
    ORDER BY เรียง; LIMIT เก็บ n แถวแรกหลังการเรียง
  • 4 Aggregates & grouping
    4.1

    ฟังก์ชันรวมกลุ่ม (Aggregate functions)

    English

    An aggregate function 聚合函数 turns many rows into one summary 汇总 value. Wrap an average in ROUND(x, 2) to tidy it.

    Function Gives
    COUNT(*) how many rows
    SUM(col) the total
    AVG(col) the mean average
    MIN(col) / MAX(col) the smallest / largest value
    ไทย

    ฟังก์ชันรวมกลุ่มแปลงหลายแถวให้เป็นค่าสรุปเดียว ห่อค่าเฉลี่ยด้วย ROUND(x, 2) เพื่อจัดรูป

    ฟังก์ชัน ให้ผล
    COUNT(*) จำนวนแถว
    SUM(col) ผลรวม
    AVG(col) ค่าเฉลี่ย
    MIN(col) / MAX(col) ค่าต่ำสุด / สูงสุด
    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT COUNT(*), ROUND(AVG(score), 2), MAX(score) FROM student;
    
    4.2

    GROUP BY และ HAVING

    English

    GROUP BY makes one summary row per group 组. HAVING filters those groups — it is like WHERE, but it runs after grouping.

    Whatever order you write them in, SQL always runs the clauses of a query in the same fixed order:

    Step Clause
    1 FROM (and any JOIN)
    2 WHERE — filter rows
    3 GROUP BY — form groups
    4 HAVING — filter groups
    5 SELECT — compute the output columns
    6 ORDER BY, then LIMIT

    Common mistakes

    • You cannot select a plain column beside an aggregate unless it is in GROUP BY.
    • Filter rows with WHERE (before grouping) and filter groups with HAVING (after).
    • COUNT(*) counts rows; COUNT(col) skips NULLs in that column.
    ไทย

    GROUP BY สร้างแถวสรุปหนึ่งแถวต่อกลุ่ม HAVING กรองกลุ่มเหล่านั้น — เหมือนกับ WHERE แต่รันหลังการจัดกลุ่ม

    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT form, COUNT(*) AS n, ROUND(AVG(score), 2) AS avg_score
    FROM student
    GROUP BY form
    HAVING COUNT(*) > 1;
    
    GROUP BY รวมแถวเข้าเป็นถังเดียวต่อค่า; แต่ละถังกลายเป็นแถวสรุปหนึ่งแถว
    GROUP BY รวบรวมแถวเข้าเป็นกลุ่มเดียวสำหรับแต่ละค่า; แต่ละกลุ่มจะกลายเป็นแถวสรุปผลหนึ่ง

    ไม่ว่าคุณเขียนคำสั่งตามลำดับใด SQL จะดำเนินการตามลำดับของ_clause_ที่แน่นอนเสมอ:

    ขั้นตอน Clause
    1 FROM (และ JOIN อื่นๆ)
    2 WHERE — กรองแถว
    3 GROUP BY — สร้างกลุ่ม
    4 HAVING — กรองกลุ่ม
    5 SELECT — คำนวณคอลัมน์ผลลัพธ์
    6 ORDER BY, แล้ว LIMIT

    ข้อผิดพลาดที่พบบ่อย

    • คุณไม่สามารถเลือกคอลัมน์ธรรมดาข้างเคียงกับ aggregate ได้ เว้นแต่จะอยู่ใน GROUP BY
    • กรองแถวด้วย WHERE (ก่อนการจัดกลุ่ม) และกรองกลุ่มด้วย HAVING (หลังการจัดกลุ่ม)
    • COUNT(*) นับจำนวนแถว; COUNT(col) ข้าม NULL ในคอลัมน์นั้น
  • 5 Joins
    5.1

    คีย์และความสัมพันธ์

    English

    A primary key 主键 uniquely names each row in a table. A foreign key 外键 in one table points to the primary key of another — that builds a relationship 关系 between them.

    ไทย

    Primary key ชี้ระบุแถวในตารางอย่างชัดเจน Each row has a unique primary key. Foreign key ในตารางหนึ่งชี้ไปยัง primary key ของอีกตารางหนึ่ง — ซึ่งสร้างความสัมพันธ์ระหว่างตารางทั้งสอง

    CREATE TABLE class (id INTEGER, name TEXT);
    CREATE TABLE student (id INTEGER, name TEXT, class_id INTEGER);
    INSERT INTO class VALUES (1, 'Maths'), (2, 'Art');
    INSERT INTO student VALUES (1, 'Mei', 1), (2, 'Sam', 2);
    SELECT * FROM student;
    
    5.2

    INNER JOIN

    English

    A join 连接 combines rows from two tables. INNER JOIN ... ON ... keeps rows where the keys match. A short alias 别名 (s, c) keeps the query readable.

    ไทย

    การ JOIN เชื่อมต่อแถวจากตารางสองตารางเข้าด้วยกัน การ INNER JOIN ... ON ... จะเก็บเฉพาะแถวที่คีย์ตรงกัน การตั้งชื่อย่อสั้นๆ (s, c) จะช่วยให้โค้ดอ่านง่ายขึ้น

    CREATE TABLE class (id INTEGER, name TEXT);
    CREATE TABLE student (id INTEGER, name TEXT, class_id INTEGER);
    INSERT INTO class VALUES (1, 'Maths'), (2, 'Art');
    INSERT INTO student VALUES (1, 'Mei', 1), (2, 'Sam', 2);
    SELECT s.name, c.name AS class
    FROM student s INNER JOIN class c ON s.class_id = c.id;
    

    INNER JOIN จับคู่ foreign key กับ primary key และรวมแถวที่จับคู่กันได้ *INNER JOIN จับคู่ foreign key กับ primary key และรวมแถวที่จับคู่กันได้

    5.3

    การ Join พร้อมการจัดกลุ่ม

    English

    Join first, then GROUP BY to summarise across the joined rows.

    ไทย

    Join ก่อน แล้วใช้ GROUP BY เพื่อสรุปผลข้ามแถวที่ Join แล้ว

    CREATE TABLE class (id INTEGER, name TEXT);
    CREATE TABLE student (id INTEGER, name TEXT, class_id INTEGER);
    INSERT INTO class VALUES (1, 'Maths'), (2, 'Art');
    INSERT INTO student VALUES (1, 'Mei', 1), (2, 'Sam', 2), (3, 'Ana', 1);
    SELECT c.name AS class, COUNT(*) AS n
    FROM student s INNER JOIN class c ON s.class_id = c.id
    GROUP BY c.name;
    
    5.4

    LEFT JOIN

    English

    An INNER JOIN keeps only matched rows. A LEFT JOIN 左连接 keeps every row of the left (first) table; where there is no match, the right table's columns come back as NULL.

    • Sam has no class, but the row still appears — with class as NULL.
    • To find only the unmatched rows, add WHERE c.id IS NULL.

    Common mistakes

    • A join with no ON condition pairs every row with every row (a cross join).
    • Match the foreign key to the primary key: ON orders.customer_id = customers.id.
    • An INNER JOIN drops rows that have no match on the other side; use a LEFT JOIN to keep them.
    ไทย

    การ INNER JOIN จะเก็บเฉพาะแถวที่จับคู่กันได้เท่านั้น การ LEFT JOIN จะเก็บ ทุก แถวของตารางซ้าย (ตารางแรก) และหากไม่พบแถวที่จับคู่ได้ คอลัมน์ของตารางขวาจะถูกแทนที่ด้วยค่า NULL

    CREATE TABLE class (id INTEGER, name TEXT);
    CREATE TABLE student (id INTEGER, name TEXT, class_id INTEGER);
    INSERT INTO class VALUES (1, 'Maths'), (2, 'Art');
    INSERT INTO student VALUES (1, 'Mei', 1), (2, 'Sam', NULL);
    SELECT s.name, c.name AS class
    FROM student s LEFT JOIN class c ON s.class_id = c.id;
    
    • Sam ไม่มีชั้นเรียน แต่แถวยังคงปรากฏอยู่ — โดย class มีค่าเป็น NULL
    • เพื่อค้นหาเฉพาะแถวที่ไม่จับคู่กันได้ ให้เพิ่ม WHERE c.id IS NULL เข้าไป

    ข้อผิดพลาดที่พบบ่อย

    • การ Join ที่ไม่มีเงื่อนไข ON จะจับคู่แถวทั้งหมดกับแถวทั้งหมด (cross join)
    • จับคู่ foreign key กับ primary key: ON orders.customer_id = customers.id
    • INNER JOIN จะตัดแถวที่ไม่มีคู่ในอีกฝั่งออก; ให้ใช้ LEFT Join เพื่อบรรจุไว้
  • 6 Modifying data
    6.1

    INSERT

    English

    INSERT INTO ... VALUES ... adds new rows 行. Name the columns, then give the values in the same order. (Each block below ends with a SELECT so you can see the result.)

    • INSERT INTO t VALUES (...), (...); adds several rows in one statement.
    ไทย

    INSERT INTO ... VALUES ... เพิ่มแถวใหม่ ตั้งชื่อคอลัมน์แล้วใส่ค่าตามลำดับเดียวกัน (บล็อกด้านล่างแต่ละจบด้วย SELECT เพื่อให้เห็นผลลัพธ์)

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student (id, name, score) VALUES (1, 'Mei', 88);
    INSERT INTO student VALUES (2, 'Sam', 71);
    SELECT * FROM student;
    
    • คำสั่ง INSERT INTO t VALUES (...), (...); สามารถเพิ่มหลายแถวได้ในคำสั่งเดียว

    INSERT เพิ่มแถวใหม่ลงในตาราง *INSERT เพิ่มแถวใหม่ลงในตาราง

    6.2

    UPDATE

    English

    UPDATE ... SET ... WHERE ... changes existing rows. Always add WHERE, or every row changes.

    ไทย

    UPDATE ... SET ... WHERE ... ใช้สำหรับแก้ไขข้อมูลที่มีอยู่เดิม ควรใช้คู่กับ WHERE เสมอ มิฉะนั้นจะมีการเปลี่ยนแปลงข้อมูลทั้งหมดในตาราง

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    UPDATE student SET score = 75 WHERE name = 'Sam';
    SELECT * FROM student;
    
    6.3

    DELETE

    English

    DELETE FROM ... WHERE ... removes rows. Without WHERE it empties the whole table 表.

    Common mistakes

    • UPDATE and DELETE without a WHERE change every row — always add the WHERE.
    • In INSERT, the values must line up with the column list in order and type.
    • Test a risky DELETE first as a SELECT with the same WHERE.
    ไทย

    DELETE FROM ... WHERE ... ลบแถวออก หากไม่มี WHERE จะลบข้อมูลทั้งตาราง

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    DELETE FROM student WHERE score < 80;
    SELECT * FROM student;
    

    ข้อผิดพลาดที่พบบ่อย

    • UPDATE และ DELETE โดยไม่มี WHERE จะเปลี่ยนทุกแถว — ต้องใส่ WHERE เสมอ
    • ใน INSERT ค่าต้องเรียงตรงกับรายการคอลัมน์ตามลำดับและประเภท
    • ทดสอบ DELETE ที่เสี่ยงภัยก่อนโดยใช้ SELECT ที่มี WHERE เดียวกัน
  • 7 Defining tables
    7.1

    CREATE TABLE & ประเภทข้อมูล

    English

    CREATE TABLE defines a table's schema 表结构: the column names and their data types 数据类型. The main SQLite types are INTEGER, TEXT, and REAL (a decimal number).

    ไทย

    CREATE TABLE กำหนดโครงสร้างตาราง: ชื่อคอลัมน์และประเภทข้อมูลหลักของ SQLite คือ INTEGER, TEXT และ REAL (ตัวเลขทศนิยม)

    CREATE TABLE student (id INTEGER, name TEXT, score REAL);
    INSERT INTO student VALUES (1, 'Mei', 88.5);
    SELECT * FROM student;
    

    CREATE TABLE ระบุชื่อคอลัมน์และประเภทข้อมูล *CREATE TABLE ระบุชื่อคอลัมน์และประเภทข้อมูล

    7.2

    คีย์และข้อจำกัด

    English

    A constraint 约束 is a rule on a column: PRIMARY KEY (a unique id), NOT NULL (must have a value), UNIQUE, and DEFAULT (a fallback value).

    Declare a foreign key with REFERENCES — it records that the column points at another table's primary key:

    • The long form is FOREIGN KEY (class_id) REFERENCES class(id) on its own line.
    ไทย

    Constraint เป็นกฎบนคอลัมน์: PRIMARY KEY (รหัสประจำตัวที่ไม่ซ้ำ), NOT NULL (ต้องมีค่า), UNIQUE, และ DEFAULT (ค่าสำรอง)

    CREATE TABLE student (
        id INTEGER PRIMARY KEY,
        name TEXT NOT NULL,
        score INTEGER DEFAULT 0
    );
    INSERT INTO student (id, name) VALUES (1, 'Mei');
    SELECT * FROM student;
    

    ประกาศ foreign key ด้วย REFERENCES — บันทึกว่าคอลัมน์นี้ชี้ไปยัง primary key ของตารางอื่น:

    CREATE TABLE class (id INTEGER PRIMARY KEY, name TEXT);
    CREATE TABLE student (
        id INTEGER PRIMARY KEY,
        name TEXT NOT NULL,
        class_id INTEGER REFERENCES class(id)
    );
    INSERT INTO class VALUES (1, 'Maths');
    INSERT INTO student VALUES (1, 'Mei', 1);
    SELECT s.name, c.name AS class
    FROM student s INNER JOIN class c ON s.class_id = c.id;
    
    • รูปแบบยาวคือ FOREIGN KEY (class_id) REFERENCES class(id) บนบรรทัดเดียว
    7.3

    ALTER TABLE

    English

    ALTER TABLE ... ADD COLUMN ... changes the schema of a table that already exists. A DEFAULT fills the new column in the old rows.

    • ALTER TABLE student RENAME TO pupil; renames the whole table.
    • DROP TABLE student; deletes the table completely — structure and data.

    Common mistakes

    • Every column needs a data type (e.g. INTEGER, TEXT).
    • A PRIMARY KEY must be unique and cannot be NULL.
    • A foreign key value must exist in the table it points to.
    ไทย

    ALTER TABLE ... ADD COLUMN ... เปลี่ยนโครงสร้างตารางที่มีอยู่แล้ว การเพิ่ม DEFAULT จะเติมค่าให้คอลัมน์ใหม่ในแถวเก่า

    CREATE TABLE student (id INTEGER, name TEXT);
    INSERT INTO student VALUES (1, 'Mei');
    ALTER TABLE student ADD COLUMN score INTEGER DEFAULT 0;
    SELECT * FROM student;
    
    • ALTER TABLE student RENAME TO pupil; เปลี่ยนชื่อทั้งตาราง
    • DROP TABLE student; ลบตารางออกทั้งหมด — ทั้งโครงสร้างและข้อมูล

    ข้อผิดพลาดที่พบบ่อย

    • ทุกคอลัมน์ต้องมี data type (เช่น INTEGER, TEXT)
    • PRIMARY KEY ต้องไม่ซ้ำและไม่เป็น NULL
    • ค่า foreign key ต้องมีอยู่ในตารางที่ชี้ไป
  • 8 Database design
    8.1

    ความสัมพันธ์และแผนภาพ ER

    English

    Tables connect through relationships 关系. One-to-many 一对多 is the most common: one class has many students. Many-to-many 多对多 needs a join table 连接表 in the middle. An entity-relationship diagram 实体关系图 (ER diagram) draws each entity 实体 as a box and each relationship as a line.

    Relationship Example
    one-to-one a person and their passport
    one-to-many a class and its students
    many-to-many students and clubs

    The join table holds one row per link. Here Mei is in two clubs, and Chess has two members:

    ไทย

    ตารางเชื่อมต่อกันผ่านความสัมพันธ์ One-to-many เป็นที่พบบ่อยที่สุด: ชั้นเรียนหนึ่งมีนักเรียนหลายคน Many-to-many ต้องการตาราง Join กลาง แผนภาพ entity-relationship (ER diagram) วาดแต่ละ entity เป็นกล่องและแต่ละความสัมพันธ์เป็นเส้น

    ความสัมพันธ์ ตัวอย่าง
    one-to-one บุคคลและหนังสือเดินทาง
    one-to-many ชั้นเรียนและนักเรียน
    many-to-many นักเรียนและชมรม

    ด้าน 'many' คือฝั่งที่มี foreign key; การเชื่อมโยงแบบ many-to-many จำเป็นต้องใช้ตารางกลางในการ JOIN *ด้าน "many" พก foreign key; ความสัมพันธ์ many-to-many ต้องการตาราง Join

    ตาราง Join เก็บแถวหนึ่งต่อหนึ่งความเชื่อมโยงที่นี่ Mei อยู่ในสองชมรม และ Chess มีสมาชิกสองคน:

    CREATE TABLE student (id INTEGER PRIMARY KEY, name TEXT);
    CREATE TABLE club (id INTEGER PRIMARY KEY, name TEXT);
    CREATE TABLE membership (student_id INTEGER, club_id INTEGER);
    INSERT INTO student VALUES (1, 'Mei'), (2, 'Sam');
    INSERT INTO club VALUES (1, 'Chess'), (2, 'Art');
    INSERT INTO membership VALUES (1, 1), (1, 2), (2, 1);
    SELECT s.name, c.name AS club
    FROM membership m
    INNER JOIN student s ON m.student_id = s.id
    INNER JOIN club c ON m.club_id = c.id;
    
    8.2

    การทำให้เป็นมาตรฐาน (Normalisation)

    English

    Normalisation 范式化 organises tables to avoid redundancy 冗余 (the same data repeated) and the update mistakes it causes. The first three normal forms 范式:

    • 1NF: every cell holds one atomic 原子 value — no lists inside a cell.
    • 2NF: no column depends on only part of a composite key 复合主键.
    • 3NF: no column depends on another non-key column.
    Unnormalised (bad) Normalised (better)
    student(name, club1, club2) student(name) + membership(student, club)

    Common mistakes

    • Split repeating groups into their own table (normalisation) instead of many similar columns.
    • Each table should describe ONE kind of thing.
    • Link tables with a foreign key that points to another table's primary key.
    ไทย

    Normalization จัดระเบียบตารางเพื่อหลีกเลี่ยงความซ้ำซ้อน (ข้อมูลเดิมซ้ำ) และความผิดพลาดในการอัปเดตที่มันก่อให้เกิด Normal forms แรกสาม:

    • 1NF: แต่ละเซลล์มีค่า atomic เดียว — ไม่มีการวางรายการภายในเซลล์
    • 2NF: ไม่มีคอลัมน์ขึ้นอยู่กับเพียงส่วนหนึ่งของ composite key
    • 3NF: ไม่มีคอลัมน์ขึ้นอยู่กับ non-key column อื่น
    ไม่ทำ Normalization (แย่) ทำ Normalization (ดีกว่า)
    student(name, club1, club2) student(name) + membership(student, club)

    ข้อผิดพลาดที่พบบ่อย

    • แยกกลุ่มซ้ำซ้อนออกเป็นตาราง riêng (normalization) แทนการใช้คอลัมน์คล้ายกันจำนวนมาก
    • แต่ละตารางควรอธิบายสิ่งชนิดเดียว
    • เชื่อมโยงตารางด้วย foreign key ที่ชี้ไปยัง primary key ของตารางอื่น

Log in or create account · ⁨เข้าสู่ระบบหรือสร้างบัญชี⁩

IGCSE, A-Level & AP