Basic SQL: Data Definition (DDL) Basic SQL: Data Definition (DDL)

Building the structure. Creating schemas, tables, and enforcing constraints with SQL. بناء الهيكل. إنشاء المخططات (schemas)، والجداول، وفرض القيود باستخدام SQL.

Schema Schema CREATE TABLE CREATE TABLE Data Types (CHAR, VARCHAR) Data Types (CHAR, VARCHAR) Constraints (PK, FK) Constraints (PK, FK) ALTER & DROP ALTER & DROP

SQL Structure & Types هيكلية SQL وأنواعها

SQL (Structured Query Language) is the standard for relational databases. It is comprehensive, covering DDL (Definition), DML (Manipulation), and VDL (View Definition). SQL (لغة الاستعلام الهيكلية) هي المعيار لقواعد البيانات العلائقية. وهي شاملة، تغطي DDL (التعريف)، DML (المعالجة)، و VDL (تعريف الـ View).

Schema & Catalog Schema & Catalog

  • Schema:Schema: Group of tables/views owned by a user. مجموعة من الجداول/الـ views يملكها مستخدم.
  • Catalog:Catalog: Collection of schemas. مجموعة من الـ schemas.
  • Environment:Environment: Installation of SQL DBMS. تثبيت نظام إدارة قواعد البيانات SQL.

Key Data Types Key Data Types (أنواع البيانات الرئيسية)

  • CHAR(n): Fixed length string. نص ثابت الطول.
  • VARCHAR(n): Variable length string. نص متغير الطول.
  • INT/SMALLINT: Integers. أعداد صحيحة.
  • FLOAT/REAL: Floating point. أرقام عشرية.
  • DATE/TIME: YYYY-MM-DD / HH:MM:SS.YYYY-MM-DD / HH:MM:SS.

The CREATE Statement The CREATE Statement

Example: Creating 'EMPLOYEE' مثال: إنشاء 'EMPLOYEE'
CREATE TABLE EMPLOYEE ( Fname VARCHAR(15) NOT NULL, Minit CHAR, Lname VARCHAR(15) NOT NULL, Ssn CHAR(9) NOT NULL, Bdate DATE, Address VARCHAR(30), Sex CHAR, Salary DECIMAL(10,2), Super_ssn CHAR(9), Dno INT NOT NULL, PRIMARY KEY (Ssn), FOREIGN KEY (Super_ssn) REFERENCES EMPLOYEE(Ssn), FOREIGN KEY (Dno) REFERENCES DEPARTMENT(Dnumber) );

Primary Key Primary Key (المفتاح الأساسي)

Uniquely identifies rows. Can be single or composite. يحدد الصفوف بشكل فريد. يمكن أن يكون مفرداً أو مركباً.
PRIMARY KEY (Ssn)
PRIMARY KEY (Pno, Essn)

Foreign Key Foreign Key (المفتاح الأجنبي)

Links to another table (Referential Integrity). يربط بجدول آخر (التكامل المرجعي).
FOREIGN KEY (Dno) REFERENCES DEPARTMENT(Dnumber)

Handling Violations معالجة الانتهاكات (Handling Violations)

What happens when you delete a parent record (e.g., a Department) that is referenced by children (e.g., Employees)? You must specify the action. ماذا يحدث عندما تحذف سجلاً أباً (مثل قسم) يتم الإشارة إليه من قبل أبناء (مثل موظفين)؟ يجب عليك تحديد الإجراء.

RESTRICT RESTRICT (Default). Reject the delete. (الافتراضي). رفض الحذف.
CASCADE CASCADE Delete the children too. (Dangerous!) حذف الأبناء أيضاً. (خطر!)
SET NULL SET NULL Set the FK in children to NULL. تعيين الـ FK في الأبناء إلى NULL.
FOREIGN KEY (Dnumber) REFERENCES DEPARTMENT(Dnumber) ON DELETE SET NULL ON UPDATE CASCADE;

Modifying the Schema تعديل الـ Schema

ALTER TABLE ALTER TABLE

Add or drop columns/constraints. إضافة أو حذف أعمدة/قيود.

ALTER TABLE COMPANY.EMPLOYEE ADD COLUMN Job VARCHAR(12);
ALTER TABLE COMPANY.EMPLOYEE DROP COLUMN Address CASCADE;

DROP TABLE DROP TABLE

Deletes the table and data permanently. حذف الجدول والبيانات بشكل دائم.

DROP TABLE DEPENDENT CASCADE;

*CASCADE removes constraints referencing this table. *CASCADE يزيل القيود التي تشير إلى هذا الجدول.

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

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

TRAP: Circular References فخ: المراجع الدائرية (Circular References)

If Table A refers to Table B, and Table B refers to Table A (e.g., Employee has Dept, Dept has Manager), you cannot create both with FKs immediately.
Solution: Create tables without FKs first, then use `ALTER TABLE` to add the FK constraints later.
إذا كان جدول A يشير إلى جدول B، وجدول B يشير إلى جدول A (مثل: موظف لديه قسم، وقسم لديه مدير)، لا يمكنك إنشاء كليهما مع FKs فوراً.
الحل: أنشئ الجداول بدون FKs أولاً، ثم استخدم `ALTER TABLE` لإضافة قيود الـ FK لاحقاً.

TRAP: Default Action فخ: الإجراء الافتراضي

If you don't specify `ON DELETE` or `ON UPDATE`, the default behavior is RESTRICT (REJECT). The DBMS will throw an error if you try to delete a parent record that has children. إذا لم تحدد `ON DELETE` أو `ON UPDATE`، فإن السلوك الافتراضي هو RESTRICT (رفض). سيرمي الـ DBMS خطأ إذا حاولت حذف سجل أب لديه أبناء.

SECRET: Constraint Naming سر: تسمية القيود

Always name your constraints (e.g., `CONSTRAINT EMP_PK PRIMARY KEY...`). If you let the system auto-name them (e.g., `SYS_C00123`), it will be a nightmare to `DROP` or `ALTER` them later because you won't know the name. قم دائماً بتسمية قيودك (مثل `CONSTRAINT EMP_PK PRIMARY KEY...`). إذا تركت النظام يسميها تلقائياً (مثل `SYS_C00123`)، سيكون من الصعب جداً حذفها (`DROP`) أو تعديلها (`ALTER`) لاحقاً لأنك لن تعرف الاسم.

KEY CONCEPT: The Catalog مفهوم أساسي: The Catalog

All the `CREATE TABLE` commands don't just create storage buckets; they update the System Catalog (Data Dictionary). The DBMS checks this catalog every time a query is run to ensure valid syntax and permissions. جميع أوامر `CREATE TABLE` لا تنشئ فقط أوعية للتخزين؛ بل تقوم بتحديث System Catalog (قاموس البيانات). يتحقق الـ DBMS من هذا الكتالوج في كل مرة يتم فيها تشغيل استعلام لضمان صحة الصيغة والصلاحيات.