Skip to content · ⁨الانتقال إلى المحتوى⁩
Subjects · ⁨المواد⁩
  • 1 Querying basics · ⁨أساسيات الاستعلام⁩
    1.1

    SELECT و FROM

    English

    A database 数据库 keeps data in tables 表. A query 查询 reads data with SELECT. SELECT * returns every column; FROM names the table.

    • Each row 行 is one record 记录. SQL keywords are written in UPPERCASE by habit.
    • A statement ends with a semicolon ;.
    العربية

    يحفظ قاعدة البيانات البيانات في جداول. استعلام قراءة البيانات باستخدام SELECT. SELECT * يعيد كل عمود؛ FROM يحدد اسم الجدول.

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    SELECT * FROM student;
    
    جدول يخزن سجلات: الصف الواحد هو سجل واحد، والعمود الواحد هو حقل واحد
    جدول يخزن سجلات: الصف الواحد هو سجل واحد، والعمود الواحد هو حقل واحد
    • الصف الواحد هو سجل واحد. تُكتب كلمات SQL الرئيسية بأحرف كبيرة معتاداً.
    • ينتهي الأمر بفاصلة منقوطة ;.
    1.2

    اختيار الأعمدة

    English

    List the columns 列 you want, separated by commas. Use AS to rename a column in the result (an alias 别名).

    A selected column can also be a calculation — pair it with AS to name the new column:

    • SELECT DISTINCT col removes duplicate values from the result.
    العربية

    عدد الأعمدة المطلوبة، مفصولة بفاصلة. استخدم AS لإعادة تسمية عمود في النتائج (اسم مستعار).

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    SELECT name, score AS mark FROM student;
    

    يمكن أن يكون العمود المختار عملية حسابية أيضاً — ضمه مع AS لتسمية العمود الجديد:

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    SELECT name, score + 5 AS bonus FROM student;
    
    • SELECT DISTINCT col يزيل القيم المكررة من النتائج.
    1.3

    التصفية باستخدام WHERE

    English

    WHERE keeps only the rows that match a condition 条件. Compare with =, <> (not equal), <, >, <=, >=. Put text in single quotes.

    • SQL runs the parts in this order: FROM → WHERE → SELECT.

    Common mistakes

    • Put text values in single quotes: WHERE name = 'Ann'; column names have no quotes.
    • SELECT * returns every column — name only the columns you need.
    • WHERE filters rows; it comes after FROM.
    العربية

    WHERE يحتفظ فقط بالصفوف التي تطابق شرطاً ما. قارن مع =، <> (غير يساوي)، <، >، <=، >=. ضع النصوص بين علامات اقتباس مفردة.

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71), (3, 'Ana', 95);
    SELECT name, score FROM student WHERE score >= 80;
    
    • تنفذ SQL الأجزاء بهذا الترتيب: FROM → WHERE → SELECT.

    أخطاء شائعة

    • ضع قيم النص بين علامات اقتباس مفردة: WHERE name = 'Ann'؛ أسماء الأعمدة لا تحمل علامات اقتباس.
    • SELECT * يعيد كل عمود — حدد فقط الأعمدة التي تحتاجها.
    • WHERE يصفي الصفوف؛ يأتي بعد FROM.
  • 2 Filtering & logic · ⁨التصفية والمنطق⁩
    2.1

    AND, OR, NOT

    English

    Combine conditions with AND, OR, NOT. AND needs all sides true; OR needs any side true; NOT reverses one. Use brackets to set the precedence 优先级.

    العربية

    اجمع الشروط باستخدام AND، OR، NOT. AND يتطلب صحة جميع الجوانب؛ OR يكفي صحة أي جانب؛ NOT يعكس أحد الجانبين. استخدم الأقواس لتحديد الأولوية.

    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT name FROM student WHERE form = '11A' AND score >= 90;
    
    AND يتطلب صحتها جميعاً؛ OR يكفي صحتها أي منها؛ NOT يعكس أحدها
    AND يتطلب صحتها جميعاً؛ OR يكفي صحتها أي منها؛ NOT يعكس أحدها
    2.2

    LIKE و IN و BETWEEN

    English

    LIKE matches a text pattern 模式: % stands for any text and _ for one character — these are wildcards 通配符.

    Pattern Matches
    'M%' starts with M
    '%a' ends with a
    '%an%' contains "an"
    'M_i' M, then exactly one character, then i

    IN matches a set 集合 of values; BETWEEN matches a range 范围 (both ends included).

    العربية

    LIKE يطابق نمط نصي: % يمثل أي نص و_ يمثل حرفاً واحداً — هذه رموز عاملة (wildcards).

    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT name FROM student WHERE name LIKE 'M%';        -- starts with M
    
    النمط يطابق
    'M%' يبدأ بحرف M
    '%a' ينتهي بحرف a
    '%an%' يحتوي على "an"
    'M_i' M، ثم حرف واحد بالضبط، ثم i

    IN يطابق مجموعة من القيم؛ BETWEEN يطابق نطاقاً (يشمل الطرفين).

    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT name, score FROM student
    WHERE form IN ('11A', '11C') AND score BETWEEN 80 AND 100;
    
    2.3

    NULL

    English

    NULL marks a missing value 缺失值 — nothing was stored in that cell. NULL is not 0 and not an empty string. A comparison with = or <> never matches it; test with IS NULL or IS NOT NULL.

    • WHERE score = NULL returns no rows at all — not even Sam's row.
    • A NULL in arithmetic gives NULL: score + 5 stays NULL for Sam.

    Common mistakes

    • Test for an empty value with IS NULL, never = NULL.
    • In LIKE, % matches any text and _ matches one character: 'A%' means "starts with A".
    • IN (1, 2, 3) is shorter than many ORs; BETWEEN a AND b includes both ends.
    العربية

    NULL يشير إلى قيمة مفقودة — لم يتم تخزين شيء في تلك الخلية. NULL ليس 0 وليس سلسلة فارغة. المقارنة مع = أو <> لا تطابقه أبداً؛ اختبره باستخدام IS NULL أو IS NOT NULL.

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', NULL), (3, 'Ana', 95);
    SELECT name FROM student WHERE score IS NULL;
    
    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', NULL), (3, 'Ana', 95);
    SELECT name, score FROM student WHERE score IS NOT NULL;
    
    • لا تُرجع WHERE score = NULL أي صف على الإطلاق — حتى صف سام.
    • NULL في العمليات الحسابية يعطي NULL: score + 5 يبقى NULL لسام.

    أخطاء شائعة

    • اختبر القيمة الفارغة باستخدام IS NULL، ولا تستخدم = NULL أبداً.
    • في LIKE، % يطابق أي نص و_ يطابق حرفاً واحداً: 'A%' تعني "يبدأ بحرف A".
    • IN (1, 2, 3) أقصر من عدة ORs؛ BETWEEN a AND b يشمل الطرفين.
  • 3 Sorting & limiting · ⁨الفرز والتقييد⁩
    3.1

    ORDER BY و LIMIT

    English

    ORDER BY sorts 排序 the result rows. Add DESC for descending 降序 (high to low); the default is ascending 升序 (low to high).

    Sort by more than one column

    List several columns. A tie 平局 in the first column is broken by the next.

    LIMIT (top-N)

    LIMIT n keeps only the first n rows — pair it with ORDER BY for a top-N 前 N 名 list.

    • ORDER BY can also sort by an alias or a calculation: ORDER BY avg_score DESC.
    • Text sorts alphabetically: ORDER BY name runs A → Z.

    Common mistakes

    • ORDER BY sorts ascending by default; add DESC for descending.
    • ORDER BY comes near the end, after WHERE and GROUP BY.
    • LIMIT caps how many rows come back, but only after sorting.
    العربية

    ORDER BY يرتب صفوف النتائج. أضف DESC للتصاعد التنازلي (من الأعلى إلى الأسفل)؛ الافتراضي هو التصاعد التنازلي (من الأدنى إلى الأعلى).

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei',88),(2,'Sam',71),(3,'Ana',95);
    SELECT name, score FROM student ORDER BY score DESC;
    

    الترتيب بناءً على أكثر من عمود

    عدد عدة أعمدة. التعادل في العمود الأول يُحل بواسطة التالي.

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei',88),(2,'Sam',88),(3,'Ana',95);
    SELECT name, score FROM student ORDER BY score DESC, name ASC;
    

    LIMIT (أعلى-N)

    LIMIT n يحتفظ فقط بأول n صفوف — ضمه مع ORDER BY للحصول على قائمة أعلى-N.

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei',88),(2,'Sam',71),(3,'Ana',95);
    SELECT name, score FROM student ORDER BY score DESC LIMIT 2;
    
    • ORDER BY يمكنه أيضاً الترتيب حسب اسم مستعار أو عملية حسابية: ORDER BY avg_score DESC.
    • النص يُرتب أبجدياً: ORDER BY name يشغل A → Z.

    أخطاء شائعة

    • ORDER BY يرتب تصاعدياً بشكل افتراضي؛ أضف DESC للتنازلي.
    • ORDER BY يأتي في النهاية تقريباً، بعد WHERE وGROUP BY.
    • LIMIT يحدد الحد الأقصى لعدد الصفوف المُرجعة، ولكن فقط بعد الترتيب.
    ORDER BY يرتب؛ LIMIT يحتفظ بأول n بعد الترتيب
    ORDER BY يرتب؛ LIMIT يحتفظ بأول n بعد الترتيب
  • 4 Aggregates & grouping · ⁨الوظائف المجمّعة والتجميع⁩
    4.1

    الدوال المجمعة

    English

    An aggregate function 聚合函数 turns many rows into one summary 汇总 value. Wrap an average in ROUND(x, 2) to tidy it.

    Function Gives
    COUNT(*) how many rows
    SUM(col) the total
    AVG(col) the mean average
    MIN(col) / MAX(col) the smallest / largest value
    العربية

    الدالة المجمعة تحول عدة صفوف إلى قيمة ملخص واحدة. غلف المتوسط في ROUND(x, 2) لتنسيقه.

    الدالة تعطيك
    COUNT(*) عدد الصفوف
    SUM(col) المجموع الكلي
    AVG(col) المتوسط الحسابي
    MIN(col) / MAX(col) أصغر قيمة / أكبر قيمة
    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT COUNT(*), ROUND(AVG(score), 2), MAX(score) FROM student;
    
    4.2

    GROUP BY و HAVING

    English

    GROUP BY makes one summary row per group 组. HAVING filters those groups — it is like WHERE, but it runs after grouping.

    Whatever order you write them in, SQL always runs the clauses of a query in the same fixed order:

    Step Clause
    1 FROM (and any JOIN)
    2 WHERE — filter rows
    3 GROUP BY — form groups
    4 HAVING — filter groups
    5 SELECT — compute the output columns
    6 ORDER BY, then LIMIT

    Common mistakes

    • You cannot select a plain column beside an aggregate unless it is in GROUP BY.
    • Filter rows with WHERE (before grouping) and filter groups with HAVING (after).
    • COUNT(*) counts rows; COUNT(col) skips NULLs in that column.
    العربية

    GROUP BY يجعل صف ملخص واحد لكل مجموعة. HAVING يصفي تلك المجموعات — إنه يشبه WHERE، لكنه يعمل بعد التجميع.

    CREATE TABLE student (id INTEGER, name TEXT, form TEXT, score INTEGER);
    INSERT INTO student VALUES (1,'Mei','11A',88),(2,'Sam','11B',71),(3,'Ana','11A',95);
    SELECT form, COUNT(*) AS n, ROUND(AVG(score), 2) AS avg_score
    FROM student
    GROUP BY form
    HAVING COUNT(*) > 1;
    
    GROUP BY يجمع الصفوف في سلة واحدة لكل قيمة؛ كل سلة تصبح صف ملخص واحد
    يجمع GROUP BY الصفوف في صندوق واحد لكل قيمة؛ يصبح كل صندوق سطراً ملخصاً واحداً

    بغض النظر عن الترتيب الذي تكتبه به، تنفذ SQL دائماً جمل الاستعلام بنفس الترتيب الثابت:

    الخطوة الجملة
    1 FROM (وأي JOIN)
    2 WHERE — تصفية الصفوف
    3 GROUP BY — تكوين المجموعات
    4 HAVING — تصفية المجموعات
    5 SELECT — حساب أعمدة المخرجات
    6 ORDER BY، ثم LIMIT

    أخطاء شائعة

    • لا يمكنك تحديد عمود عادي بجانب دالة تجميعية إلا إذا كان ضمن GROUP BY.
    • قم بتصفية الصفوف باستخدام WHERE (قبل التجميع)، وتصفية المجموعات باستخدام HAVING (بعد التجميع).
    • COUNT(*) يحسب عدد الصفوف؛ بينما COUNT(col) يتجاهل القيم الفارغة NULL في ذلك العمود.
  • 5 Joins · ⁨الوصل (Joins)⁩
    5.1

    Keys & relationships

    English

    A primary key 主键 uniquely names each row in a table. A foreign key 外键 in one table points to the primary key of another — that builds a relationship 关系 between them.

    العربية

    A primary key 主键 uniquely names each row in a table. A foreign key 外键 in one table points to the primary key of another — that builds a relationship 关系 between them.

    CREATE TABLE class (id INTEGER, name TEXT);
    CREATE TABLE student (id INTEGER, name TEXT, class_id INTEGER);
    INSERT INTO class VALUES (1, 'Maths'), (2, 'Art');
    INSERT INTO student VALUES (1, 'Mei', 1), (2, 'Sam', 2);
    SELECT * FROM student;
    
    5.2

    INNER JOIN

    English

    A join 连接 combines rows from two tables. INNER JOIN ... ON ... keeps rows where the keys match. A short alias 别名 (s, c) keeps the query readable.

    العربية

    A join 连接 combines rows from two tables. INNER JOIN ... ON ... keeps rows where the keys match. A short alias 别名 (s, c) keeps the query readable.

    CREATE TABLE class (id INTEGER, name TEXT);
    CREATE TABLE student (id INTEGER, name TEXT, class_id INTEGER);
    INSERT INTO class VALUES (1, 'Maths'), (2, 'Art');
    INSERT INTO student VALUES (1, 'Mei', 1), (2, 'Sam', 2);
    SELECT s.name, c.name AS class
    FROM student s INNER JOIN class c ON s.class_id = c.id;
    
    INNER JOIN matches each foreign key to a primary key and combines the matched rows
    INNER JOIN matches each foreign key to a primary key and combines the matched rows
    5.3

    Joining with grouping

    Join first, then GROUP BY to summarise across the joined rows.

    CREATE TABLE class (id INTEGER, name TEXT);
    CREATE TABLE student (id INTEGER, name TEXT, class_id INTEGER);
    INSERT INTO class VALUES (1, 'Maths'), (2, 'Art');
    INSERT INTO student VALUES (1, 'Mei', 1), (2, 'Sam', 2), (3, 'Ana', 1);
    SELECT c.name AS class, COUNT(*) AS n
    FROM student s INNER JOIN class c ON s.class_id = c.id
    GROUP BY c.name;
    
    5.4

    LEFT JOIN

    English

    An INNER JOIN keeps only matched rows. A LEFT JOIN 左连接 keeps every row of the left (first) table; where there is no match, the right table's columns come back as NULL.

    • Sam has no class, but the row still appears — with class as NULL.
    • To find only the unmatched rows, add WHERE c.id IS NULL.

    Common mistakes

    • A join with no ON condition pairs every row with every row (a cross join).
    • Match the foreign key to the primary key: ON orders.customer_id = customers.id.
    • An INNER JOIN drops rows that have no match on the other side; use a LEFT JOIN to keep them.
    العربية

    An INNER JOIN keeps only matched rows. A LEFT JOIN 左连接 keeps every row of the left (first) table; where there is no match, the right table's columns come back as NULL.

    CREATE TABLE class (id INTEGER, name TEXT);
    CREATE TABLE student (id INTEGER, name TEXT, class_id INTEGER);
    INSERT INTO class VALUES (1, 'Maths'), (2, 'Art');
    INSERT INTO student VALUES (1, 'Mei', 1), (2, 'Sam', NULL);
    SELECT s.name, c.name AS class
    FROM student s LEFT JOIN class c ON s.class_id = c.id;
    
    • Sam has no class, but the row still appears — with class as NULL.
    • To find only the unmatched rows, add WHERE c.id IS NULL.

    Common mistakes

    • A join with no ON condition pairs every row with every row (a cross join).
    • Match the foreign key to the primary key: ON orders.customer_id = customers.id.
    • An INNER JOIN drops rows that have no match on the other side; use a LEFT JOIN to keep them.
  • 6 Modifying data · ⁨تعديل البيانات⁩
    6.1

    INSERT

    English

    INSERT INTO ... VALUES ... adds new rows 行. Name the columns, then give the values in the same order. (Each block below ends with a SELECT so you can see the result.)

    • INSERT INTO t VALUES (...), (...); adds several rows in one statement.
    العربية

    INSERT INTO ... VALUES ... adds new rows 行. Name the columns, then give the values in the same order. (Each block below ends with a SELECT so you can see the result.)

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student (id, name, score) VALUES (1, 'Mei', 88);
    INSERT INTO student VALUES (2, 'Sam', 71);
    SELECT * FROM student;
    
    • INSERT INTO t VALUES (...), (...); adds several rows in one statement.
    INSERT adds a new row to a table
    INSERT adds a new row to a table
    6.2

    UPDATE

    UPDATE ... SET ... WHERE ... changes existing rows. Always add WHERE, or every row changes.

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    UPDATE student SET score = 75 WHERE name = 'Sam';
    SELECT * FROM student;
    
    6.3

    DELETE

    English

    DELETE FROM ... WHERE ... removes rows. Without WHERE it empties the whole table 表.

    Common mistakes

    • UPDATE and DELETE without a WHERE change every row — always add the WHERE.
    • In INSERT, the values must line up with the column list in order and type.
    • Test a risky DELETE first as a SELECT with the same WHERE.
    العربية

    DELETE FROM ... WHERE ... removes rows. Without WHERE it empties the whole table 表.

    CREATE TABLE student (id INTEGER, name TEXT, score INTEGER);
    INSERT INTO student VALUES (1, 'Mei', 88), (2, 'Sam', 71);
    DELETE FROM student WHERE score < 80;
    SELECT * FROM student;
    

    Common mistakes

    • UPDATE and DELETE without a WHERE change every row — always add the WHERE.
    • In INSERT, the values must line up with the column list in order and type.
    • Test a risky DELETE first as a SELECT with the same WHERE.
  • 7 Defining tables · ⁨تعريف الجداول⁩
    7.1

    إنشاء جدول (CREATE TABLE) وأنواع البيانات

    English

    CREATE TABLE defines a table's schema 表结构: the column names and their data types 数据类型. The main SQLite types are INTEGER, TEXT, and REAL (a decimal number).

    العربية

    يحدد CREATE TABLE مخطط الجدول: أسماء الأعمدة وأنواع بياناتها. الأنواع الرئيسية في SQLite هي INTEGER، وTEXT، وREAL (رقم عشري).

    CREATE TABLE student (id INTEGER, name TEXT, score REAL);
    INSERT INTO student VALUES (1, 'Mei', 88.5);
    SELECT * FROM student;
    
    يُعرّف CREATE TABLE أسماء الأعمدة وأنواعها
    إنشاء جدول بأسماء الأعمدة وأنواعها
    7.2

    المفاتيح والقيود

    English

    A constraint 约束 is a rule on a column: PRIMARY KEY (a unique id), NOT NULL (must have a value), UNIQUE, and DEFAULT (a fallback value).

    Declare a foreign key with REFERENCES — it records that the column points at another table's primary key:

    • The long form is FOREIGN KEY (class_id) REFERENCES class(id) on its own line.
    العربية

    القيد هو قاعدة على عمود: PRIMARY KEY (معرف فريد)، NOT NULL (يجب أن يحتوي على قيمة)، UNIQUE، وDEFAULT (قيمة افتراضية).

    CREATE TABLE student (
        id INTEGER PRIMARY KEY,
        name TEXT NOT NULL,
        score INTEGER DEFAULT 0
    );
    INSERT INTO student (id, name) VALUES (1, 'Mei');
    SELECT * FROM student;
    

    إعلان مفتاح أجنبي باستخدام REFERENCES — يسجل أن العمود يشير إلى المفتاح الأساسي لجدول آخر:

    CREATE TABLE class (id INTEGER PRIMARY KEY, name TEXT);
    CREATE TABLE student (
        id INTEGER PRIMARY KEY,
        name TEXT NOT NULL,
        class_id INTEGER REFERENCES class(id)
    );
    INSERT INTO class VALUES (1, 'Maths');
    INSERT INTO student VALUES (1, 'Mei', 1);
    SELECT s.name, c.name AS class
    FROM student s INNER JOIN class c ON s.class_id = c.id;
    
    • الصيغة الطويلة هي FOREIGN KEY (class_id) REFERENCES class(id) في سطر منفصل.
    7.3

    تعديل الجدول

    English

    ALTER TABLE ... ADD COLUMN ... changes the schema of a table that already exists. A DEFAULT fills the new column in the old rows.

    • ALTER TABLE student RENAME TO pupil; renames the whole table.
    • DROP TABLE student; deletes the table completely — structure and data.

    Common mistakes

    • Every column needs a data type (e.g. INTEGER, TEXT).
    • A PRIMARY KEY must be unique and cannot be NULL.
    • A foreign key value must exist in the table it points to.
    العربية

    ALTER TABLE ... ADD COLUMN ... يغير مخطط جدول موجود بالفعل. يقوم DEFAULT بملء العمود الجديد في الصفوف القديمة.

    CREATE TABLE student (id INTEGER, name TEXT);
    INSERT INTO student VALUES (1, 'Mei');
    ALTER TABLE student ADD COLUMN score INTEGER DEFAULT 0;
    SELECT * FROM student;
    
    • ALTER TABLE student RENAME TO pupil; يعيد تسمية الجدول بالكامل.
    • DROP TABLE student; يحذف الجدول تمامًا — الهيكل و البيانات.

    أخطاء شائعة

    • يحتاج كل عمود إلى نوع بيانات (مثلاً INTEGER، TEXT).
    • يجب أن يكون المفتاح الأساسي فريدًا ولا يمكن أن يكون فارغًا.
    • يجب أن توجد قيمة المفتاح الأجنبي في الجدول الذي تشير إليه.
  • 8 Database design · ⁨تصميم قواعد البيانات⁩
    8.1

    العلاقات ومخططات الكيان-العلاقة

    English

    Tables connect through relationships 关系. One-to-many 一对多 is the most common: one class has many students. Many-to-many 多对多 needs a join table 连接表 in the middle. An entity-relationship diagram 实体关系图 (ER diagram) draws each entity 实体 as a box and each relationship as a line.

    Relationship Example
    one-to-one a person and their passport
    one-to-many a class and its students
    many-to-many students and clubs

    The join table holds one row per link. Here Mei is in two clubs, and Chess has two members:

    العربية

    ترتبط الجداول من خلال العلاقات. علاقة واحد إلى متعدد هي الأكثر شيوعًا: فصل واحد له العديد من الطلاب. تتطلب علاقة متعدد إلى متعدد جدول وسيط في المنتصف. يرسم مخطط الكيان-العلاقة (ER diagram) كل كيان كمربع وكل علاقة كخط.

    العلاقة مثال
    واحد إلى واحد شخص وجواز سفره
    واحد إلى متعدد فصل وطلابه
    متعدد إلى متعدد طلاب وأندية
    الجانب "الكثير" يحمل المفتاح الأجنبي؛ يتطلب الرابط من كثير إلى كثير جدول ربط
    الجانب "متعدد" يحمل المفتاح الأجنبي؛ يتطلب رابط متعدد إلى متعدد جدول وسيط

    يحتوي الجدول الوسيط على صف واحد لكل رابط. هنا مي في ناديين، والشطرنج لديه عضوان:

    CREATE TABLE student (id INTEGER PRIMARY KEY, name TEXT);
    CREATE TABLE club (id INTEGER PRIMARY KEY, name TEXT);
    CREATE TABLE membership (student_id INTEGER, club_id INTEGER);
    INSERT INTO student VALUES (1, 'Mei'), (2, 'Sam');
    INSERT INTO club VALUES (1, 'Chess'), (2, 'Art');
    INSERT INTO membership VALUES (1, 1), (1, 2), (2, 1);
    SELECT s.name, c.name AS club
    FROM membership m
    INNER JOIN student s ON m.student_id = s.id
    INNER JOIN club c ON m.club_id = c.id;
    
    8.2

    التطبيع

    English

    Normalisation 范式化 organises tables to avoid redundancy 冗余 (the same data repeated) and the update mistakes it causes. The first three normal forms 范式:

    • 1NF: every cell holds one atomic 原子 value — no lists inside a cell.
    • 2NF: no column depends on only part of a composite key 复合主键.
    • 3NF: no column depends on another non-key column.
    Unnormalised (bad) Normalised (better)
    student(name, club1, club2) student(name) + membership(student, club)

    Common mistakes

    • Split repeating groups into their own table (normalisation) instead of many similar columns.
    • Each table should describe ONE kind of thing.
    • Link tables with a foreign key that points to another table's primary key.
    العربية

    الطبيعية (التنظيم) ترتب الجداول لتجنب التكرار (تكرار نفس البيانات) والأخطاء التي تسببها في التحديث. الأشكال الطبيعية الثلاثة الأولى:

    • 1NF: كل خلية تحتوي على قيمة ذرية واحدة — لا توجد قوائم داخل الخلية.
    • 2NF: لا يعتمد أي عمود على جزء فقط من مفتاح مركب.
    • 3NF: لا يعتمد أي عمود على عمود آخر غير مفتاحي.
    غير مُنظَّمة (سيئة) مُنظَّمة (أفضل)
    student(name, club1, club2) student(name) + membership(student, club)

    أخطاء شائعة

    • فصل المجموعات المتكررة إلى جدول خاص بها (التنظيم) بدلاً من وجود أعمدة مشابهة عديدة.
    • يجب أن يصف كل جدول نوعاً واحداً من الأشياء.
    • ربط الجداول بمفتاح أجنبي يشير إلى المفتاح الأساسي لجدول آخر.

Log in or create account · ⁨تسجيل الدخول أو إنشاء حساب⁩

IGCSE, A-Level & AP