Creating tables · יצירת טבלות
Two kinds of SQL command
SQL has two halves:
- DDL (Data Definition Language) changes the structure — the tables themselves.
- DML (Data Manipulation Language) works with the data inside the tables — reading it with
SELECT, and adding, changing, or removing it withINSERT/UPDATE/DELETE. Everything so far (SELECT) is DML.
CREATE TABLE is the main DDL command. It lists each column with a data type and marks the primary key:
שני סוגי פקודות SQL
ל-SQL יש שני חצאים:
- DDL (שפת הגדרת נתונים) משנה את המבנה – את הטבלאות עצמן.
- DML (שפת עיבוד נתונים) עובדת עם הנתונים בתוך הטבלאות – קורא אותם עם
SELECT, ומוסיף, משנה או מוסר אותם עםINSERT/UPDATE/DELETE. הכל עד כה (SELECT) הוא DML.
CREATE TABLE היא הפקודה המרכזית ב-DDL. היא מפרטת כל עמודה עם סוג נתונים ומסמנת אתמפתח ראשי:
CREATE TABLE customer (
id INTEGER PRIMARY KEY,
name VARCHAR(20),
city VARCHAR(15)
);
Data types and foreign keys
Common data types:
INTEGER— whole numbersVARCHAR(n)/CHAR(n)— text, up to / exactlyncharactersDECIMAL(p, s)— a number withsdecimal placesDATE,BOOLEAN
To link to another table, add a FOREIGN KEY:
The checker reads back your table's structure to see which columns you created.
סוגי נתונים ומפתחות זרים
סוגי נתונים נפוצים:
INTEGER– מספרים שלמיםVARCHAR(n)/CHAR(n)– טקסט, עד / בדיוקnתוויםDECIMAL(p, s)– מספר עםsמקומות עשרוניותDATE,BOOLEAN
כדי לקשר לטבלה אחרת, הוסף FOREIGN KEY:
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
FOREIGN KEY (customer_id) REFERENCES customer(id)
);
הבוחן קורא מחדש את מבנה הטבלה כדי לראות אילו עמודות יצרת.
Common mistakes
- Every column needs a data type (e.g.
INTEGER,TEXT). - Declare the PRIMARY KEY when you create the table.
טעויות נפוצות
- לכל עמודה יש צורך בסוג נתונים (למשל
INTEGER,TEXT). - הגדר את המפתח הראשי PRIMARY KEY בזמן יצירת הטבלה.
Create a table called teacher with three columns: id (INTEGER, the primary key), name (VARCHAR(20)), and subject (VARCHAR(15)). · צרו טבלה בשם teacher עם שלושה עמודות: id (INTEGER, המפתח הראשי), name (VARCHAR(20)), וsubject (VARCHAR(15)).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Design another table. Create event with id (INTEGER PRIMARY KEY), title (VARCHAR(30)), and price (DECIMAL(6,2) — a money column with 2 decimal places). · עצבו טבלה נוספת. צרו event עם id (INTEGER PRIMARY KEY), title (VARCHAR(30)), וprice (DECIMAL(6,2) — עמודת כסף עם 2 מקומות עשרוניים).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.