فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة SQL
أمثلة قصيرة وبسيطة
الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.
المثال ١: Primary key وقيد الـ NOT NULL
Sample output (press Run to produce your own): Error: UNIQUE constraint failed: dept.dept_id Error: NOT NULL constraint failed: dept.name dept_id | name --------+------ 10 | IT 20 | Sales (2 rows)
المثال ٢: Candidate keys بـ UNIQUE
Sample output (press Run to produce your own): Error: UNIQUE constraint failed: student.email id | email | name ---+------------------+----- 1 | mona@example.com | Mona (1 row)
المثال ٣: Referential integrity
Sample output (press Run to produce your own): Error: FOREIGN KEY constraint failed Error: FOREIGN KEY constraint failed id | name | dept_id ---+------+-------- 1 | Mona | 10 (1 row)
اختبار قصير
اختبر فهمك
1. Rows of a relation are called:
2. Does the order of rows in a relation matter?
3. A minimal set of attributes that is unique for every tuple is a:
4. A foreign key must:
5. Entity integrity says:
تمارين برمجة
جرّب بإيدك
التمرين 1
اعمل جدول product فيه id كـ primary key وسعر لازم يكون أكبر من صفر (CHECK)، وجرّب تضيف سعر سالب.
Create a product table with id as primary key and a price that must be greater than zero (CHECK). Try inserting a negative price.
(output appears here)
sql('''
CREATE TABLE product (id INTEGER PRIMARY KEY, name TEXT, price REAL CHECK (price > 0));
INSERT INTO product VALUES (1, 'Pen', 2.5);
INSERT INTO product VALUES (2, 'Free', -1);
SELECT * FROM product;
''')
Output
Error: CHECK constraint failed: price > 0 id | name | price ---+------+------ 1 | Pen | 2.5 (1 row)
التمرين 2
جدول enrollment فيه student_id و course_code. اجعل الاتنين مع بعض primary key وجرّب تكرر نفس التسجيل.
Make (student_id, course_code) a composite primary key of enrollment and try to insert the same enrollment twice.
(output appears here)
sql('''
CREATE TABLE enrollment (student_id INTEGER, course_code TEXT, PRIMARY KEY (student_id, course_code));
INSERT INTO enrollment VALUES (1, 'DB1');
INSERT INTO enrollment VALUES (1, 'DB1');
SELECT COUNT(*) AS n FROM enrollment;
''')
Output
Error: UNIQUE constraint failed: enrollment.student_id, enrollment.course_code n - 1 (1 row)
مشروع صغير في آخر المحاضرة
جدول بـ keys وقيود سلامة
الداتا: University sample data: 3 departments and 5 students. Written for this course (our own data, free to reuse).
License / source: Our own sample data; SQLite is in the public domain. Dataset page
الخطوات
- اعمل dept بـ primary key، وstudent بـ primary key وـ email UNIQUE وـ foreign key للـ dept.
- ضيف الداتا الصح.
- جرّب تضيف طالب بـ dept مش موجود وطالب بإيميل متكرر.
- اعرض كام طالب في كل قسم. المتوقع: IT = 2 و Math = 1 و Physics = 0 (استخدم LEFT JOIN).
Expected output (press Run to produce your own): Error: UNIQUE constraint failed: student.email Error: FOREIGN KEY constraint failed name | students --------+--------- IT | 2 Math | 1 Physics | 0 (3 rows)
Credits: the topic order follows the lecture list of the course TDDD12 Database Technology at Linköping University (https://www.ida.liu.se/~TDDD12/fo/), whose reading guide is based on the textbook Fundamentals of Database Systems by Elmasri and Navathe. We found no reuse licence on that page, so we link to it and copy nothing from it: the explanations, diagrams, data and code in this lecture are our own. Reference: Elmasri and Navathe, Fundamentals of Database Systems, the chapter on the relational model.