فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة SQL
أمثلة قصيرة وبسيطة
الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.
المثال ١: أول جدول
Sample output (press Run to produce your own): id | name | year ---+------+----- 1 | Mona | 2 2 | Omar | 3 (2 rows)
المثال ٢: الـ schema والـ instance
Sample output (press Run to produce your own): rows_now -------- 2 (1 row) rows_now -------- 3 (1 row) name | type -----+-------- id | INTEGER name | TEXT year | INTEGER (3 rows)
المثال ٣: الـ DBMS بيحمي الداتا
Sample output (press Run to produce your own): Error: UNIQUE constraint failed: student.id Error: NOT NULL constraint failed: student.name id | name ---+----- 1 | Mona (1 row)
اختبار قصير
اختبر فهمك
1. Which is a problem of keeping data in separate plain files?
2. A DBMS is:
3. The data stored in a database right now is called:
4. In the three-schema architecture, which level describes files and indexes?
5. Data independence means we can:
تمارين برمجة
جرّب بإيدك
التمرين 1
اعمل جدول course فيه code و title، وضيف مادتين، واعرض كل الصفوف.
Create a table course(code, title), insert two courses and show all rows.
(output appears here)
sql('''
CREATE TABLE course (code TEXT PRIMARY KEY, title TEXT);
INSERT INTO course VALUES ('DB1', 'Databases'), ('ML1', 'Machine Learning');
SELECT * FROM course;
''')
Output
code | title -----+----------------- DB1 | Databases ML1 | Machine Learning (2 rows)
التمرين 2
جرّب تضيف صف تاني بنفس الـ code. إيه اللي بيحصل وليه؟
Try to insert a second row with the same code. What happens and why?
(output appears here)
sql('''
CREATE TABLE course (code TEXT PRIMARY KEY, title TEXT);
INSERT INTO course VALUES ('DB1', 'Databases');
INSERT INTO course VALUES ('DB1', 'Duplicate');
SELECT COUNT(*) AS n FROM course;
''')
Output
Error: UNIQUE constraint failed: course.code n - 1 (1 row)
مشروع صغير في آخر المحاضرة
أول قاعدة بيانات: جامعة صغيرة
الداتا: University sample data: 5 students with program and year. Written for this course (our own data, free to reuse).
License / source: Our own sample data; the SQL runs on SQLite, which is in the public domain. Dataset page
الخطوات
- اعمل جدول student فيه id و name و program و year، والـ id يبقى primary key.
- ضيف الخمس طلاب اللي في الـ starter.
- اعرض الطلاب اللي في برنامج CS بس.
- عدّ كام طالب في كل برنامج. المتوقع: CS = 3 و Math = 2.
Expected output (press Run to produce your own): name | year ------+----- Mona | 2 Omar | 3 Salma | 1 (3 rows) program | students --------+--------- CS | 3 Math | 2 (2 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, introduction chapters.