Creating tables · إنشاء الجداول
Two kinds of SQL command
SQL has two halves:
- DDL (Data Definition Language) changes the structure — the tables themselves.
- DML (Data Manipulation Language) works with the data inside the tables — reading it with
SELECT, and adding, changing, or removing it withINSERT/UPDATE/DELETE. Everything so far (SELECT) is DML.
CREATE TABLE is the main DDL command. It lists each column with a data type and marks the primary key:
نوعان من أوامر SQL
يحتوي SQL على نصفين:
- DDL (لغة تعريف البيانات) يغير الهيكل — أي الجداول نفسها.
- DML (لغة معالجة البيانات) يعمل مع البيانات داخل الجداول — بقراءتها عبر
SELECT، وإضافتها أو تغييرها أو حذفها عبرINSERT/UPDATE/DELETE. كل ما سبق (SELECT) هو DML.
CREATE TABLE هو الأمر الرئيسي لـ DDL. يعرض كل عمود مع نوع بيانات ويحدد المفتاح الأساسي:
CREATE TABLE customer (
id INTEGER PRIMARY KEY,
name VARCHAR(20),
city VARCHAR(15)
);
Data types and foreign keys
Common data types:
INTEGER— whole numbersVARCHAR(n)/CHAR(n)— text, up to / exactlyncharactersDECIMAL(p, s)— a number withsdecimal placesDATE,BOOLEAN
To link to another table, add a FOREIGN KEY:
The checker reads back your table's structure to see which columns you created.
أنواع البيانات والمفاتيح الأجنبية
أنواع بيانات شائعة:
INTEGER— أعداد صحيحةVARCHAR(n)/CHAR(n)— نص، حتى / بالضبطnأحرفDECIMAL(p, s)— رقم يحتوي علىsمنازل عشريةDATE,BOOLEAN
للربط بجدول آخر، أضف FOREIGN KEY:
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
FOREIGN KEY (customer_id) REFERENCES customer(id)
);
يقوم الفاحص بإعادة قراءة هيكل جدولك للتحقق من الأعمدة التي أنشأتها.
Common mistakes
- Every column needs a data type (e.g.
INTEGER,TEXT). - Declare the PRIMARY KEY when you create the table.
أخطاء شائعة
- يحتاج كل عمود إلى نوع بيانات (مثلاً
INTEGER،TEXT). - أعلن PRIMARY KEY عند إنشاء الجدول.
Create a table called teacher with three columns: id (INTEGER, the primary key), name (VARCHAR(20)), and subject (VARCHAR(15)). · أنشئ جدولاً باسم teacher بثلاثة أعمدة: id (INTEGER، مفتاح رئيسي)، name (VARCHAR(20))، و subject (VARCHAR(15)).
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Design another table. Create event with id (INTEGER PRIMARY KEY), title (VARCHAR(30)), and price (DECIMAL(6,2) — a money column with 2 decimal places). · صمم جدولاً آخر. أنشئ event مع id (INTEGER PRIMARY KEY)، وtitle (VARCHAR(30))، وprice (DECIMAL(6,2) — عمود نقدي يحتوي على 2 خانات عشرية).
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.