SQL — DDL and DML
הוצג
17:15
מאגרי נתונים והמודל היחסיתי
לפני מאגרי הנתונים, כל תוכנה שמרה את הקבצים הפשוטים שלה — קובץ אחד לכל תוכנה. דמיין חנות. תוכנת המכירות, תוכנת החשבונית ותוכנת המשלוח…
קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
בשיעור הזה
| English | משמעות |
|---|---|
| query language | A language used to request and work with data in a database. |
| tables/ˈteɪblz/ | A collection of data organised into rows and columns. |
| SQL/ˌes kjuː ˈel/ | שפת שאלות מבנית |
| statement/ˈsteɪtmənt/ | A line of code that tells a program what to do. |
| Data Definition Language/ˈdeɪtə ˌdefɪˈnɪʃn ˈlæŋɡwɪdʒ/ | שפת הגדרת נתונים |
| Data Manipulation Language/ˈdeɪtə məˌnɪpjʊˈleɪʃn ˈlæŋɡwɪdʒ/ | שפת עיבוד נתונים |
| DBMS/ˌdiː biː em ˈes/ | Database management system: software for defining, storing, querying, updating and controlling access to a database. |
| database/ˈdeɪtəbeɪs/ | An organised collection of data that can be accessed and managed. |
| keys | Fields that identify rows or link rows in different tables. |
| queries | Requests for data from a database that may include conditions. |
| rows | Horizontal lines in a table, each containing one record. |
| Data types/ˈdeɪtə taɪps/ | Categories that define what kind of values a field can hold. |
| attribute/ˈætrɪbjuːt/ | A named property or characteristic associated with an object, element or record. |
| foreign key/ˈfɒrən kiː/ | A field or group of fields that refers to a candidate key in another or the same database table. |
| primary key/ˈpraɪməri kiː/ | A field or combination of fields that uniquely identifies each record in a database table. |
| field/fiːld/ | A named component of a record, with its own value and type. |
| condition/kənˈdɪʃn/ | A requirement or circumstance that affects what happens or whether something is true. |
| Strings/strɪŋz/ | Sequences of characters used as text in a program. |
| single quotes | Pairs of marks used around text values in SQL statements. |
| Comparisons | Operations that check the relationship between two values. |
| SQL script | One or more SQL statements written to perform database actions. |
| INNER JOIN/ˈɪnə dʒɔɪn/ | חיבור פנימי |
| clause/klɔːz/ | A part of an SQL statement that performs a particular function. |
| result set | The collection of records returned when a query is run. |
| Aggregate functions/ˈæɡrɪɡeɪt ˈfʌŋkʃnz/ | Functions that produce a single summary value from multiple rows of data. |
השפה שמתכתיgrandparents גם השתמשו בה
- בשנת 1974 שני חוקרים של IBM עיצבו שפת שאלות לטבלאות של Codd וכינו לה SEQUEL: Structured English Query Language. השם קוצר ל-SQL, והשפה לעולם לא נעלמה.
- חמישים שנה לאחר מכן, כל בנק, חברת תעופה, בית חולים ואתר אינטרנט שתשתמש בהם פועל עליה. תלמיד שמקליד
SELECTהיום כותב את אותו פקודה שכתב מתכנת לפני שהוריו נולדו. - יש לה שני חלקים, ובמבחן מבקשים לקרוא את האחד ולכתוב את השניים: שפת הגדרת הנתונים הבונה את המבנה, ושפת עיבוד הנתונים שממלא, משנה ושואלת את הנתונים.
- שיעור זה הוא תת-קבוצת של SQL בתוכנית הלימודים, פקודה לפי פקודה, עם הציון שכל אחת נושאת.
DDL ו-DML
- ה-DBMS מבצע את כל יצירת ועריכה של המבנה של מסד הנתונים דרך ה-DDL: יצירת מסד נתונים, יצירת ושנוי טבלאות, הוספת מקדים.
- היא מבצעת כל השאלות ואת תחזוקת ה-נתונים דרך ה-DML: בחירה, הכנסה, עדכון ומחיקת שורות.
- SQL הוא הסטנדרט בתעשייה לשניהם. סיווג משפט לתחום הנכון הוא שאלה נפוצה בעלת סימן אחד:
CREATE TABLEהוא DDL,SELECTהוא DML.

מבנה בצד אחד, נתונים בצד השני

Which is part of the Data Definition Language (DDL)?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
DDL changes the structure (CREATE, ALTER, DROP). SELECT/INSERT/UPDATE/DELETE are DML (working with data).
DDL defines the structure (e.g. CREATE TABLE), while DML works with the data inside it (SELECT, INSERT, UPDATE, DELETE).
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Definition vs Manipulation: DDL shapes the tables; DML reads and changes the rows.
DDL: יצירת המבנה
CREATE DATABASE Shop;
CREATE TABLE CUSTOMER (
CustomerID INTEGER,
Name VARCHAR(50),
Town VARCHAR(30),
Joined DATE,
Active BOOLEAN,
PRIMARY KEY (CustomerID)
);
ALTER TABLE CUSTOMER ADD Email VARCHAR(100);
- סוגי נתונים במסלול לימודים:
CHARACTER(מספר קבוע של תווים),VARCHAR(n)(עד n תווים),BOOLEAN,INTEGER,REAL,DATE,TIME. PRIMARY KEY (field)קורא שם למפתח;ALTER TABLE … ADDמוסיף מאפיין לטבלה קיימת.
Match each item of data to the SQL data type for it.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Two states, variable-length text, a number with a fractional part, a calendar date. INTEGER is for whole counts and TIME for a time of day.
דוגמה פתורה: שתי טבלאות עם מפתח זר
- כתוב את ה-SQL ליצירת טבלת
ORDERSעםOrderIDכמפתח ראשי,CustomerIDכמפתח זר ל-CUSTOMER, ושורתOrderDate.
CREATE TABLE ORDERS (
OrderID INTEGER,
CustomerID INTEGER,
OrderDate DATE,
PRIMARY KEY (OrderID),
FOREIGN KEY (CustomerID) REFERENCES CUSTOMER(CustomerID)
);
- הסימונים:
CREATE TABLEעם השם; כל מאפיין עם סוג מתאים; ה-PRIMARY KEY; ה-FOREIGN KEY … REFERENCESהקורא שם לטבלה גם לשדה שלה.
DML: שאלה באמצעות SELECT
SELECT Name, Town
FROM CUSTOMER
WHERE Town = 'London'
ORDER BY Name ASC;
SELECTמפרט את השדות להוצאה (*לכל),FROMקורא שם לטבלה,WHEREמשאיר רק שורות העונות לתנאי,ORDER BYמסדר את התוצאה,ASCאוDESC.- מחרוזות נכתבות בתוך סימני ציטוט יחיד; מספרים אינם. השוואות:
=,<,>,<=,>=,<>; תנאים מחוברים עםAND,OR,NOT;LIKE 'A%'מתאים טקסט שמתחיל ב-A;BETWEEN 10 AND 20נותן טווח.

רק השורות שעוברות את WHERE מגיעות לתוצאות

Which SQL keyword retrieves data from a table? (one word)
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
SELECT lists the fields to retrieve; FROM names the table.
The WHERE clause in a SELECT statement:
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
WHERE filters rows by a condition; ORDER BY sorts; the SELECT list chooses columns.
דוגמה פתורה: כתיבת השאלה
- כיתוב סקריפט SQL להדפסת שמות ומיילים של כל לקוחות פעילים במנצ'סטר, בסדר אלפביתי לפי שם.
SELECT Name, Email
FROM CUSTOMER
WHERE Town = 'Manchester' AND Active = TRUE
ORDER BY Name;
- נקודה אחת לכל: השדות הנכונים לאחר
SELECT; הטבלה הנכונה לאחרFROM; הWHEREעם שני התנאים והמחרוזת בתוך סימני הוצאה;ORDER BY Name. השתמש בשמות המדויקים של הטבלה והשדה שנתנה בשאלה.

Put the clauses of a SELECT statement in the order they are written.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
Fields, table, filter, sort. ORDER BY is always last, and the statement ends with a semicolon.
שתי טבלות: INNER JOIN
SELECT CUSTOMER.Name, ORDERS.OrderDate
FROM CUSTOMER INNER JOIN ORDERS
ON CUSTOMER.CustomerID = ORDERS.CustomerID
WHERE ORDERS.OrderDate >= '2024-01-01';
- INNER JOIN משלב שורות משתי טבלות כאשר המקור הזר בטבלה האחת תואם למפתח הראשי בטבלה השנייה, המצוינים ב-clause ה
ON. - Prefix a field with its table when the same name appears in both. The syllabus asks for queries over at most two tables.
Stitch two tables with INNER JOIN
A join matches rows where the foreign key equals the primary key — here Orders.CustomerID = Customer.CustomerID — and combines each matching pair into one wider row.
An INNER JOIN is used to:
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
A JOIN combines two tables on a relationship (usually a foreign key matching a primary key).
פונקציות אגרגט ו-GROUP BY
SELECT CustomerID, COUNT(*) AS NumOrders
FROM ORDERS
GROUP BY CustomerID;
SELECT AVG(Price) FROM PRODUCT;
SELECT SUM(Quantity) FROM ORDER_LINE WHERE OrderID = 1042;
- פונקציות אגרגט מסכמות מספר שורות לערך אחד:
COUNTספירת השורות,SUMחיבור כולל,AVGממוצע. GROUP BYיוצר שורת סיכום אחת לכל ערך בשדה: מספר ההזמנות לכל לקוח. ללא שימוש בזה, הפונקציה האגגרטי מסכמת את כל הטבלה.

What does COUNT(*) return?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
COUNT(*) counts rows; SUM/AVG/MIN/MAX are the other aggregate functions.
To output the number of orders placed by each customer, the query needs: select כולם that apply.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Count the rows, one group per customer, from the orders table. Sorting is optional.
שינוי נתונים: INSERT, UPDATE, DELETE
INSERT INTO CUSTOMER (CustomerID, Name, Town, Joined, Active)
VALUES (101, 'Ada Lovelace', 'London', '2024-03-01', TRUE);
UPDATE CUSTOMER SET Town = 'Bristol' WHERE CustomerID = 101;
DELETE FROM CUSTOMER WHERE CustomerID = 101;
INSERT INTO … VALUESמוסיף שורה: רשימת השדות, ולאחר מכן הערכים באותו סדר.UPDATE … SET … WHEREמעריך שורות התואמות.DELETE FROM … WHEREמוריד אותן.- תמיד יש לתת ל
UPDATEולDELETEclauseWHERE, אלא השינוי יפגע בכל שורה בטבלה.

What happens if you run UPDATE or DELETE without a WHERE clause?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
With no WHERE, the operation affects all rows — a common and dangerous mistake.
Match each DML statement to what it does.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
SELECT reads; INSERT adds; UPDATE changes; DELETE removes — the four core DML verbs.
דוגמה פותרת: קריאת הפקודה
SELECT Name
FROM CUSTOMER INNER JOIN ORDERS
ON CUSTOMER.CustomerID = ORDERS.CustomerID
WHERE OrderDate = '2024-05-01'
ORDER BY Name DESC;
- ציינו מה התוכנית הזו מציגה. שמות כל הלקוחות שיצעו הזמנה ב-1 במאי 2024, שורה אחת לכל הזמנה כזו, בסדר אלפביתי הפוך.
- קרא אותו בסדר ביצוע: חבר את הטבלות לפי מזהה לקוח, שמור את השורות ליום זה, הפק את השם, מיין יורד. לקוח עם שתי הזמנות באותו יום יופיע פעמיים.

סימונים נופלים מהיד
- מחרוזות בסימני הוצאה, מספרים ללא סימנים:
Town = 'London',CustomerID = 101. ORDER BYבא אחריWHERE; חיבור (join) דורש את clause הON; כל פקודה מסתיימת בנקודת-פסקה.COUNT(*)סופר שורות, לא ערכים ייחודיים; השאלה הקבוצתית דורשתGROUP BY.CREATE,ALTERוPRIMARY KEYהם DDL;SELECT,INSERT,UPDATE,DELETEהם DML. השתמש בשמות המדויקים שהשאלה נותנת.
הבנתך מושלמת
- DDL יוצר ומשנה את המבנה:
CREATE DATABASE,CREATE TABLEעם מאפיינים מסוגים,PRIMARY KEY,FOREIGN KEY … REFERENCES,ALTER TABLE … ADD· DML עובד עם הנתונים - סוגים:
CHARACTER,VARCHAR(n),BOOLEAN,INTEGER,REAL,DATE,TIME SELECT fields FROM table WHERE condition ORDER BY field;INNER JOIN … ONלשתי טבלות;COUNT,SUM,AVGעםGROUP BYלשורה אחת לכל קבוצהINSERT INTO … VALUES,UPDATE … SET … WHERE,DELETE FROM … WHERE: לעולם עדכון או מחיקה ללאWHERE
מה השתבש? רק צוות האתר רואה זאת.