كل المقررات

الرئيسية / مقرر قاعدة البيانات / الفصل 3 الجزء 1

الفصل 3 (الجزء 1): The Relational Model

Relations, keys and integrity constraints

على طول: الجدول بالمصطلحات العلمية، والـ keys، وقواعد سلامة الداتا.

فيديو المحاضرة

فيديو المحاضرة

بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.

رسوم متحركة

جدول وأعمدة وصفوف وـ keys

أمثلة 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)

افتح الموضوع المقابل في صفحة LiU TDDD12

اختبار قصير

اختبر فهمك

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)

التمرين 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)

مشروع صغير في آخر المحاضرة

جدول بـ 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

الخطوات

  1. اعمل dept بـ primary key، وstudent بـ primary key وـ email UNIQUE وـ foreign key للـ dept.
  2. ضيف الداتا الصح.
  3. جرّب تضيف طالب بـ dept مش موجود وطالب بإيميل متكرر.
  4. اعرض كام طالب في كل قسم. المتوقع: 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.