เพิ่มคุณภาพข้อมูลด้วย Constraints

SQL เบื้องต้น: ฐานข้อมูลเชิงสัมพันธ์

Timo Grossenbacher

Data Journalist

Integrity Constraints

  1. Attribute constraints เช่น ชนิดข้อมูลของคอลัมน์ (บทที่ 2)
  2. Key constraints เช่น primary keys (บทที่ 3)
  3. Referential integrity constraints บังคับใช้ผ่าน foreign keys (บทที่ 4)
SQL เบื้องต้น: ฐานข้อมูลเชิงสัมพันธ์

ทำไมต้องใช้ Constraints?

  • Constraints ช่วยกำหนดโครงสร้างข้อมูล
  • Constraints ช่วยให้ข้อมูลสอดคล้องกัน และเพิ่มคุณภาพข้อมูล
  • คุณภาพข้อมูลคือข้อได้เปรียบทางธุรกิจและเงื่อนไขพื้นฐานของ data science
  • การบังคับใช้อาจยุ่งยาก แต่ PostgreSQL ช่วยได้
SQL เบื้องต้น: ฐานข้อมูลเชิงสัมพันธ์

ชนิดข้อมูลในฐานะ Attribute Constraints

SQL เบื้องต้น: ฐานข้อมูลเชิงสัมพันธ์

การจัดการชนิดข้อมูล (Casting)

CREATE TABLE weather (
 temperature integer,
 wind_speed text);

SELECT temperature * wind_speed AS wind_chill FROM weather;
operator does not exist: integer * text
HINT: No operator matches the given name and argument type(s). 
You might need to add explicit type casts.
SELECT temperature * CAST(wind_speed AS integer) AS wind_chill
FROM weather;
SQL เบื้องต้น: ฐานข้อมูลเชิงสัมพันธ์

มาฝึกกันเถอะ!

SQL เบื้องต้น: ฐานข้อมูลเชิงสัมพันธ์

Preparing Video For Download...