Databases and SELECT
Databases and tables
A database stores information in tables. A table is a grid, a bit like a spreadsheet:
- each row is one record — for example, one student;
- each column is one field — for example, the student's name or score.
This course uses a small table called student. It has 5 records and 4 fields: id, name, form, and score.
ฐานข้อมูลและตาราง
ฐานข้อมูล เก็บข้อมูลในรูปแบบ ตาราง ตารางคือกริด คล้ายกับสเปรดชีต:
- แต่ละ แถว คือ เรคอร์ด หนึ่ง — ตัวอย่างเช่น นักเรียนหนึ่งคน;
- แต่ละ คอลัมน์ คือ ฟิลด์ หนึ่ง — ตัวอย่างเช่น ชื่อหรือนักเรียนคะแนน
หลักสูตรนี้ใช้ตารางเล็กชื่อว่า student它有 5 เรคอร์ดและ 4 ฟิลด์: id, name, form และ score
Asking for data with SELECT
To read data from a table you write a query with SELECT.
The simplest query asks for every column of every row. The star * means "all columns":
SELECTchooses the columns —*means all of them.FROM studentsays which table to read.
The database replies with a result table (also called a result set): the rows your query asked for.
การขอข้อมูลด้วย SELECT
ในการอ่านข้อมูลจากตาราง คุณเขียน query ด้วย SELECT
query ที่ง่ายที่สุดขอ ทุกคอลัมน์ ของ ทุกแถว ดาว * หมายถึง "ทุกคอลัมน์":
SELECT * FROM student;
SELECTเลือกคอลัมน์ —*หมายถึงทั้งหมดFROM studentบอกว่าจะอ่านตารางใด
ฐานข้อมูลตอบกลับด้วย ตารางผลลัพธ์ (เรียกอีกอย่างว่า result set): แถวที่คุณ query ขอมา
A few rules
- Keywords like
SELECTandFROMare often written in capitals so they stand out. - End each statement with a semicolon
;. - An empty cell is shown as
NULL, which means "no value".
Now try it. Click Run to see the output, then Check answer.
กฎบางประการ
- คำสำคัญเช่น
SELECTและFROMมักเขียนด้วยตัวพิมพ์ใหญ่เพื่อให้โดดเด่น - ทิ้งท้ายแต่ละ statement ด้วยเครื่องหมายกึ่งทศนิยม
; - เซลล์ว่างแสดงเป็น
NULLซึ่งหมายถึง "ไม่มีค่า"
ตอนนี้ลองดู吧 Click Run เพื่อดู output แล้ว Check answer
Common mistakes
- End a query with a semicolon:
SELECT ... FROM table;. SELECT *returns every column — name only the ones you need.
ข้อผิดพลาดทั่วไป
- ทิ้งท้าย query ด้วยเครื่องหมายกึ่งทศนิยม:
SELECT ... FROM table; SELECT *ส่งคืนทุกคอลัมน์ — ระบุเฉพาะชื่อที่คุณต้องการ
SELECT … FROM … WHERE
WHERE keeps rows, then SELECT keeps columns — step through it. · WHERE จะเก็บแค่แถว (rows), แล้ว SELECT จะเก็บแค่คอลัมน์ (columns) — ลองเดินผ่านโค้ดดู
Show the whole student table — every column and every row. Use SELECT * FROM ...;. · แสดงตาราง student ทั้งใบ — ทุกคอลัมน์และทุกแถว ใช้ SELECT * FROM ...;
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Now keep only some columns and some rows. Show just the name and score of students in form 11A. Pick the two columns and add a WHERE form = '11A'. · ตอนนี้เก็บแค่บางคอลัมน์และบางแถว แสดงเฉพาะ name และ score ของนักเรียนในชั้นเรียน 11A เลือกสองคอลัมน์แล้วใส่ WHERE form = '11A' เข้าไป
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่