Basic SQL: Manipulation (DML) Basic SQL: Manipulation (DML)

Retrieving, inserting, updating, and deleting data. Mastering the SELECT-FROM-WHERE block. استرجاع، إدراج، تحديث، وحذف البيانات. إتقان كتلة SELECT-FROM-WHERE.

SELECT-FROM-WHERE SELECT-FROM-WHERE LIKE (Pattern Matching) LIKE (Pattern Matching) ORDER BY ORDER BY INSERT / DELETE / UPDATE INSERT / DELETE / UPDATE Joins (Implied) Joins (ضمنية)

The SELECT Statement The SELECT Statement

The core of SQL retrieval. The basic form is mapping conceptual operations (Projection, Selection, Cartesian Product) into a simple syntax. جوهر استرجاع البيانات في SQL. الشكل الأساسي هو تحويل العمليات المفاهيمية (Projection, Selection, Cartesian Product) إلى صيغة بسيطة.

SELECT <attribute_list> FROM <table_list> WHERE <condition> ORDER BY <sort_list>;
SELECT (Projection) SELECT (Projection) Lists the columns to be retrieved. Use `*` for all columns. تسرد الأعمدة المراد استرجاعها. استخدم `*` لجميع الأعمدة.
FROM (Join Source) FROM (Join Source) Lists the tables involved. If >1 table, it implies a Cartesian Product. تسرد الجداول المعنية. إذا كان >1 جدول، فهذا يعني Cartesian Product (ضرب ديكارتي).
WHERE (Selection) WHERE (Selection) Filters the rows based on a boolean condition. تصفي الصفوف بناءً على شرط منطقي (boolean).

Example: Simple Retrieval مثال: استرجاع بسيط

Retrieve birthdate and address of 'John B. Smith'. استرجاع تاريخ الميلاد والعنوان لـ 'John B. Smith'.

SELECT Bdate, Address FROM EMPLOYEE WHERE Fname='John' AND Minit='B' AND Lname='Smith';

Pattern Matching & Sorting Pattern Matching & Sorting

Pattern Matching (LIKE) Pattern Matching (LIKE)

Used for string comparison. تستخدم لمقارنة النصوص.
% = Zero or more characters. صفر أو أكثر من الأحرف.
_ = Exactly one character. حرف واحد بالضبط.

SELECT Fname, Lname FROM EMPLOYEE WHERE Address LIKE '%Houston, TX%';

Sorting (ORDER BY) Sorting (ORDER BY)

Sorts the result set. Default is ASC (Ascending). Use DESC for descending. يفرز مجموعة النتائج. الافتراضي هو ASC (تصاعدي). استخدم DESC للتنازلي.


SELECT * FROM EMPLOYEE ORDER BY Dno ASC, Salary DESC;

Modification Statements Modification Statements (جمل التعديل)

INSERT INSERT

Adds new tuple(s) to a table. يضيف صفوفاً جديدة إلى الجدول.

INSERT INTO EMPLOYEE (Fname, Lname, Ssn, Dno) VALUES ('Richard', 'Marini', '653298653', 4);

DELETE DELETE

Removes tuple(s) from a table based on a condition. يحذف صفوفاً من الجدول بناءً على شرط.

DELETE FROM EMPLOYEE WHERE Lname = 'Brown';

*Warning: Omitting WHERE deletes ALL rows. *تحذير: إهمال WHERE يحذف جميع الصفوف.

UPDATE UPDATE

Modifies attribute values of existing tuples. يعدل قيم السمات للصفوف الموجودة.

UPDATE PROJECT SET PLOCATION = 'Bellaire', DNUM = 5 WHERE PNUMBER = 10;

Aliases & Implicit Joins Aliases & Implicit Joins

When querying multiple tables, we use the `WHERE` clause to specify the join condition (Foreign Key = Primary Key). عند الاستعلام من جداول متعددة، نستخدم جملة `WHERE` لتحديد شرط الربط (Foreign Key = Primary Key).

SELECT E.Fname, E.Lname, S.Fname, S.Lname FROM EMPLOYEE AS E, EMPLOYEE AS S WHERE E.Super_ssn = S.Ssn;
Self-Join Concept: مفهوم Self-Join: In the example above, we join `EMPLOYEE` to itself to find Supervisors. We MUST use Aliases (E, S) to distinguish the two roles of the same table. في المثال أعلاه، نربط `EMPLOYEE` بنفسه للعثور على المشرفين. يجب علينا استخدام الأسماء المستعارة (Aliases E, S) لتمييز الدورين لنفس الجدول.

The Exam Vault خزنة الاختبار

Professor's Secrets & Trap Avoidance أسرار البروفيسور وتجنب الفخاخ

TRAP: Missing Join Condition فخ: شرط الربط المفقود

If you list 2 tables in `FROM` but forget the `WHERE` condition linking them, you get a Cartesian Product (Every row in Table A combined with Every row in Table B).
Result: Massive, incorrect output.
إذا أدرجت جدولين في `FROM` ولكن نسيت شرط `WHERE` الذي يربط بينهما، ستحصل على Cartesian Product (ضرب ديكارتي) (كل صف في الجدول أ مدمج مع كل صف في الجدول ب).
النتيجة: مخرجات ضخمة وغير صحيحة.

TRAP: Quote Confusion فخ: ارتباك الاقتباس (Quotes)

Strings and Dates must be enclosed in single quotes ('Value'). Numbers must NOT be quoted (10, 50.5).
Example: `WHERE Salary > '30000'` might work in some SQL dialects but is conceptually wrong.
النصوص (Strings) و التواريخ (Dates) يجب وضعها بين علامات اقتباس مفردة ('Value'). الأرقام يجب ألا توضع بين علامات اقتباس (10, 50.5).
مثال: `WHERE Salary > '30000'` قد يعمل في بعض لهجات SQL ولكنه خاطئ مفاهيمياً.

SECRET: The `*` Wildcard سر: الرمز `*` (Wildcard)

`SELECT *` retrieves ALL columns. It is useful for quick checks but bad for production code (performance waste + code breaks if schema changes). In exams, `SELECT *` is acceptable unless asked for specific attributes. `SELECT *` يسترجع جميع الأعمدة. هو مفيد للفحص السريع ولكنه سيء للكود الإنتاجي (هدر للأداء + كسر الكود إذا تغير الـ schema). في الاختبارات، `SELECT *` مقبول ما لم يطلب سمات محددة.

KEY CONCEPT: Distinct مفهوم أساسي: Distinct

SQL `SELECT` creates a Bag (Multiset), allowing duplicates (e.g., listing all employee salaries might show '30000' twice). To get a Set (unique values), use `SELECT DISTINCT Salary`. جملة `SELECT` في SQL تنشئ Bag (مجموعة متعددة)، مما يسمح بالتكرار (مثلاً: سرد رواتب الموظفين قد يظهر '30000' مرتين). للحصول على Set (قيم فريدة)، استخدم `SELECT DISTINCT Salary`.