Basic SQL: Manipulation (DML) Basic SQL: Manipulation (DML)
Retrieving, inserting, updating, and deleting data. Mastering the SELECT-FROM-WHERE block. استرجاع، إدراج، تحديث، وحذف البيانات. إتقان كتلة SELECT-FROM-WHERE.
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) إلى صيغة بسيطة.
Example: Simple Retrieval مثال: استرجاع بسيط
Retrieve birthdate and address of 'John B. Smith'. استرجاع تاريخ الميلاد والعنوان لـ 'John B. Smith'.
Pattern Matching & Sorting Pattern Matching & Sorting
Pattern Matching (LIKE) Pattern Matching (LIKE)
Used for string comparison.
تستخدم لمقارنة النصوص.
% = Zero or more characters. صفر أو أكثر من الأحرف.
_ = Exactly one character. حرف واحد بالضبط.
Sorting (ORDER BY) Sorting (ORDER BY)
Sorts the result set. Default is ASC (Ascending). Use DESC for descending.
يفرز مجموعة النتائج. الافتراضي هو ASC (تصاعدي). استخدم DESC للتنازلي.
Modification Statements Modification Statements (جمل التعديل)
INSERT INSERT
Adds new tuple(s) to a table. يضيف صفوفاً جديدة إلى الجدول.
DELETE DELETE
Removes tuple(s) from a table based on a condition. يحذف صفوفاً من الجدول بناءً على شرط.
*Warning: Omitting WHERE deletes ALL rows. *تحذير: إهمال WHERE يحذف جميع الصفوف.
UPDATE UPDATE
Modifies attribute values of existing tuples. يعدل قيم السمات للصفوف الموجودة.
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).
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`.