List of commits:
Subject Hash Author Date (UTC)
Finishing exo for chapter SQL aea58f58f06d99615ae7cb85247bdc08c5ce9d96 aubert@math.cnrs.fr 2018-05-23 17:52:51
Progress on the SQL chapter. e13cddc35a709c6714a90b21d8ed8db6b037793c aubert@math.cnrs.fr 2018-05-23 05:12:21
Started type some relational model examples in latex, reviewed chapter 1 and 2. 1ddf3000e0cb4f8f02b2b2de1ffd8ae14a81c9dc aubert@math.cnrs.fr 2018-05-22 21:09:12
Finished correcting Chapter 1. Added a preamble and tweaked a lot (math, references, titles of subsubsections, etc.) acdd1bb083cd6985763afd9b77e1b7995dc1ebd7 aubert@math.cnrs.fr 2018-05-22 07:02:51
Cleaning latex dependencies: removed colors, fonts, etc., added header and footer. b4c1232d6091e12bc2cdf038529921143a55b758 aubert@math.cnrs.fr 2018-05-22 05:06:04
Putting all latex material aside, changing class. e43fb54e6ecb0ec4669ed16feba940b47ee3e35c aubert@math.cnrs.fr 2018-05-21 15:14:48
Gathering references, fixing many small typos, adding references in bib. 4f5281a0770f07d2f48f36462ab25f0a3945f4e0 aubert@math.cnrs.fr 2018-05-21 04:54:59
Finished merging exercises into lecture notes! 36e1f79080a8ac56837632bcac2c119fc3efa24f aubert@math.cnrs.fr 2018-05-20 18:50:54
Almost done with the homeworks! f82824a2ca9769a0ecf8fc75d6bf1cae1ef3c7a4 aubert@math.cnrs.fr 2018-05-20 05:23:43
Travail sur intégration des exos dans les notes. c5a6676f85953c7bec9bb2de99bed3552ecf52af aubert@math.cnrs.fr 2018-05-20 05:04:30
Les exercises compilent :-) d4230fce5210d1d8905a42d11634e49e82217541 aubert@math.cnrs.fr 2018-05-19 19:09:56
Progrès sur exercises. 40dd86cdc624209a2282d4437921f46325c2cfa1 aubert@math.cnrs.fr 2018-05-19 18:34:07
Working on exercises. 42549c8d28249f1ecf85dd21f6d004ae63c533ad aubert@math.cnrs.fr 2018-05-19 05:43:04
Working on incorporating the exercises. f32148faeb8b11425fe9a57770db9bc85ae02c34 aubert@math.cnrs.fr 2018-05-19 04:51:37
working on sum for exo. 5fb55d2c31b06161ade3a7cae707485d8c02447a aubert@math.cnrs.fr 2018-05-18 21:12:56
Adding various small notes. 5eac6e0b19441fca9a3f70f239d5cbbc4d769976 aubert@math.cnrs.fr 2018-05-17 20:37:14
First pass over! ea1e4f6ddabd91287d10384dc8f28565a762526b aubert@math.cnrs.fr 2018-05-17 19:26:28
Working on notes on normal forms. 58bfcedc6bd6336592a5f93a43ffc0aaf64fa8f6 aubert@math.cnrs.fr 2018-05-16 16:40:12
Initial commit, draft of lecture notes. 200d2739bca881d60c7c9381b16f5a4d6384ff29 aubert@math.cnrs.fr 2018-05-16 15:35:36
Commit aea58f58f06d99615ae7cb85247bdc08c5ce9d96 - Finishing exo for chapter SQL
Author: aubert@math.cnrs.fr
Author date (UTC): 2018-05-23 17:52
Committer name: aubert@math.cnrs.fr
Committer date (UTC): 2018-05-23 17:52
Parent(s): e13cddc35a709c6714a90b21d8ed8db6b037793c
Signer:
Signing key:
Signing status: N
Tree: eacfd3c8987d7fd97d8b63817d8f05489b6ef62a
File Lines added Lines deleted
notes/00_sum.md 225 239
File notes/00_sum.md changed (mode: 100644) (index 43c6cc9..5e57a7d)
... ... On top of the [references](references) and of the "resources" listed at the begi
105 105 - Check with `jhove 00_sum.pdf`, `pdfinfo 00_sum.pdf` the validity of the document. - Check with `jhove 00_sum.pdf`, `pdfinfo 00_sum.pdf` the validity of the document.
106 106 - Compress / optimize output? - Compress / optimize output?
107 107 - Make better makefile (compile images in latex, clean up, etc.). - Make better makefile (compile images in latex, clean up, etc.).
108 - Check that order of exercises / problems match introduction in lecture.
109 - Check that solution of exercises numbering match exercise numbering.
108 110
109 111 # Introduction # Introduction
110 112
 
... ... You can test if a value is `NULL` with `IS NULL`.
1403 1405
1404 1406 ## More Select Queries ## More Select Queries
1405 1407
1406 For select-project-join, cf. [@Textbook6, 4.3.1].
1407 For aliases, cf. [@Textbook6, 4.3.2].
1408 For nested queries, cf. [@Textbook6, 5.1.2].
1408 For select-project-join, cf. [@Textbook6, 4.3.1] or [@Textbook7, 6.3.1].
1409 For aliases, cf. [@Textbook6, 4.3.2] or [@Textbook7, 6.3.2],
1410 For nested queries, cf. [@Textbook6, 5.1.2] or [@Textbook7, 7.1.2].
1409 1411
1410 ### Select-project-join (4.3.1)
1412 ### Select-project-join
1411 1413
1412 1414 ~~~{.sqlmysql} ~~~{.sqlmysql}
1413 1415 SELECT Login FROM PROF, DEPARTMENT SELECT Login FROM PROF, DEPARTMENT
 
... ... SELECT Login FROM PROF, DEPARTMENT
1417 1419
1418 1420 - `Department.Name = 'Mathematics'` is the selection condition - `Department.Name = 'Mathematics'` is the selection condition
1419 1421 - `Department = Code` is the join condition, because it combines two tuples. - `Department = Code` is the join condition, because it combines two tuples.
1420 - Why do we use the fully qualified Name attribute for `Name`?
1422 - Why do we use the fully qualified name attribute for `Name`?
1421 1423 - We have to list all the tables we want to consult, even if we use fully qualified names. - We have to list all the tables we want to consult, even if we use fully qualified names.
1422 1424
1423 1425 ~~~{.sqlmysql} ~~~{.sqlmysql}
 
... ... SELECT PROF.Name FROM PROF, DEPARTMENT, STUDENT
1440 1442
1441 1443 ### Aliasing tuples ### Aliasing tuples
1442 1444
1445 We can use aliases to shorten the previous query:
1446
1447 ~~~{.sqlmysql}
1448 SELECT PROF.Name FROM PROF, DEPARTMENT, STUDENT AS B
1449 WHERE B.Name = 'Ava Alyx
1450 AND B.Major = DEPARTMENT.Code
1451 AND DEPARTMENT.Head = PROF.Login;
1452 ~~~
1453
1454 We can use multiple aliases:
1455
1443 1456 ~~~{.sqlmysql} ~~~{.sqlmysql}
1444 1457 SELECT A.Name FROM PROF AS A, DEPARTMENT, STUDENT AS B SELECT A.Name FROM PROF AS A, DEPARTMENT, STUDENT AS B
1445 1458 WHERE B.Name = 'Ava Alyx' WHERE B.Name = 'Ava Alyx'
 
... ... SELECT A.Name FROM PROF AS A, DEPARTMENT, STUDENT AS B
1447 1460 AND DEPARTMENT.Head = A.Login; AND DEPARTMENT.Head = A.Login;
1448 1461 ~~~ ~~~
1449 1462
1463 For those two, aliases were convenient, but not required to write the query.
1464 In some cases, we can't do without aliases, for instance if we want to compare two rows in the same table:
1465
1450 1466 ~~~{.sqlmysql} ~~~{.sqlmysql}
1451 1467 SELECT Others.Login FROM GRADE AS Mine, GRADE AS Others SELECT Others.Login FROM GRADE AS Mine, GRADE AS Others
1452 1468 WHERE Mine.Login = 'aalyx' WHERE Mine.Login = 'aalyx'
 
... ... WHERE Me.Name = 'Ava Alyx'
1463 1479 AND NOT Fellow.Name = 'Ava Alyx'; AND NOT Fellow.Name = 'Ava Alyx';
1464 1480 ~~~ ~~~
1465 1481
1482 `AND NOT Me = Fellow` would *not* work.
1483 Note that `AS 'Fellow of Ava'` is *another* kind of aliasing, mentioned in a previous section.
1484
1466 1485 ### Nested queries ### Nested queries
1467 1486
1468 1487 ~~~{.sqlmysql} ~~~{.sqlmysql}
1469 SELECT Login FROM GRADE WHERE Grade >
1488 SELECT Login FROM GRADE
1489 WHERE Grade >
1470 1490 (SELECT AVG(Grade) FROM GRADE); (SELECT AVG(Grade) FROM GRADE);
1471 1491 ~~~ ~~~
1472 1492
1473 Outer query, inner query.
1474 (Average of all non NULL values.)
1493 A nested query is made of an outer query (`SELECT Login`…) and an inner query (`SELECT AVG(Grade)`…).
1494
1495 (Average of all non-`NULL` values.)
1475 1496
1476 1497 ~~~{.sqlmysql} ~~~{.sqlmysql}
1477 SELECT Login FROM GRADE WHERE Grade >= ALL (SELECT Grade FROM GRADE WHERE Grade IS NOT NULL);
1498 SELECT Login FROM GRADE
1499 WHERE Grade >=
1500 ALL (SELECT Grade FROM GRADE WHERE Grade IS NOT NULL);
1478 1501
1479 1502 SELECT Login SELECT Login
1480 1503 FROM PROF FROM PROF
 
... ... WHERE DEPARTMENT IN ( SELECT Major
1483 1506 WHERE Login LIKE '%a'); WHERE Login LIKE '%a');
1484 1507 ~~~ ~~~
1485 1508
1509 Why can't we use `=`?
1486 1510
1487 ## More Select Queries
1488
1489 ### Select-project-join (4.3.1)
1490
1491 ~~~{.sqlmysql}
1492 SELECT Login FROM PROF, DEPARTMENT WHERE DEPARTMENT.Name = 'Mathematics' AND Department = Code;
1493 ~~~
1494
1495 - `Department.Name = 'Mathematics'` is the selection condition
1496 - `Department = Code` is the join condition, because it combines two tuples.
1497 - Why do we use the fully qualified Name attribute for `Name`?
1498
1499 ~~~{.sqlmysql}
1500 SELECT Name FROM STUDENT, GRADE WHERE Grade > 3.0 AND STUDENT.Login = GRADE.Login;
1501 ~~~
1502
1503 - `Grade > 3.0` is the selection condition
1504 - `STUDENT.Login = GRADE.Login` is the join condition
1505
1506 We can have two join conditions!
1507
1508 ~~~{.sqlmysql}
1509 SELECT PROF.Name FROM PROF, DEPARTMENT, STUDENT WHERE STUDENT.Name = 'Ava Alyx' AND STUDENT.Major = DEPARTMENT.Code AND DEPARTMENT.Head = PROF.Login;
1510 ~~~
1511
1512 ### Aliasing Tuples
1511 Actually, nested query that uses `=` can often be rewritten without being nested:
1513 1512
1514 1513 ~~~{.sqlmysql} ~~~{.sqlmysql}
1515 SELECT A.Name FROM PROF AS A, DEPARTMENT, STUDENT AS B WHERE B.Name = 'Ava Alyx' AND B.Major = DEPARTMENT.Code AND DEPARTMENT.Head = A.Login;
1514 SELECT Login
1515 FROM PROF
1516 WHERE DEPARTMENT = ( SELECT Major
1517 FROM STUDENT
1518 WHERE Login = "cjoella");
1516 1519 ~~~ ~~~
1517 1520
1518 ~~~{.sqlmysql}
1519 SELECT Others.Login FROM GRADE AS Mine, GRADE as Others WHERE Mine.Login = 'aalyx' and Mine.Grade < Others.Grade;
1520 ~~~
1521 becomes
1521 1522
1522 1523 ~~~{.sqlmysql} ~~~{.sqlmysql}
1523 SELECT Fellow.Name AS 'Fellow of Ava'
1524 FROM STUDENT AS Me, STUDENT AS Fellow
1525 WHERE Me.Name = 'Ava Alyx' AND Fellow.Major = Me.Major AND NOT Fellow.Name = 'Ava Alyx';
1524 SELECT STUDENT.Login
1525 FROM PROF, STUDENT
1526 WHERE DEPARTMENT = Major AND STUDENT.Login = "cjoella";
1526 1527 ~~~ ~~~
1527 1528
1528 ### Nested Queries
1529 Conversly, you can sometimes write select-project-join as nested queries
1530 For instance,
1529 1531
1530 1532 ~~~{.sqlmysql} ~~~{.sqlmysql}
1531 SELECT Login FROM GRADE WHERE Grade >
1532 (SELECT AVG(Grade) FROM GRADE);
1533 SELECT Name FROM STUDENT, GRADE
1534 WHERE Grade > 3.0
1535 AND STUDENT.Login = GRADE.Login;
1533 1536 ~~~ ~~~
1534 1537
1535 Outer query, inner query.
1536 (Average of all non NULL values.)
1538 becomes
1537 1539
1538 1540 ~~~{.sqlmysql} ~~~{.sqlmysql}
1539 SELECT Login FROM GRADE WHERE Grade >= ALL (SELECT Grade FROM GRADE WHERE Grade IS NOT NULL);
1540
1541 SELECT Login
1542 FROM PROF
1543 WHERE DEPARTMENT IN ( SELECT Major
1544 FROM STUDENT
1545 WHERE Login LIKE '%a');
1541 SELECT Name FROM STUDENT
1542 WHERE Login IN (SELECT Login FROM GRADE WHERE Grade > 3.0);
1546 1543 ~~~ ~~~
1547 1544
1548 Why can't we use `=`?
1549 Here, we can, because a student will have only 1 major: need to improve this example!
1550 Also, if `=` can be used, then a nested query isn't needed.
1551
1552 1545 ## Exercises {-} ## Exercises {-}
1553 1546
1554 1547 Exercise +.# Exercise +.#
 
... ... Exercise +.#
1561 1554 ~ Write a `SQL` statement that adds a primary key constraint to an attribute named `Id` in an already existing table named `STAFF`. ~ Write a `SQL` statement that adds a primary key constraint to an attribute named `Id` in an already existing table named `STAFF`.
1562 1555
1563 1556 Exercise +.# Exercise +.#
1564 ~ Complete the following table wo different examples when asked for examples:
1557 ~ Complete each row of the following table with either a datatype or two different examples:
1565 1558
1566 1559 <!-- Color? --> <!-- Color? -->
1567 1560
1568 Data Type | Examples |
1561 Data type | Examples |
1569 1562 --- | --- | --- | --- |
1570 1563   | `4`, `-32`   | `4`, `-32`
1571 1564 Char(4) |   Char(4) |  
 
... ... Exercise +.#
1577 1570 ~ Explain what the following `SQL` statement does ~ Explain what the following `SQL` statement does
1578 1571
1579 1572 ~~~{.sqlmysql} ~~~{.sqlmysql}
1580 CREATE SCHEMA Faculty;
1573 CREATE SCHEMA FACULTY;
1581 1574 ~~~ ~~~
1582 1575
1583 1576 Exercise +.# Exercise +.#
 
... ... Exercise +.#
1588 1581
1589 1582
1590 1583 Exercise +.# Exercise +.#
1591 ~ If `PkgName` is a primary key in the table `MYTABLE`, what can you tell about the number of rows returned by the following statement?
1584 ~ If `PkgName` is the primary key in the table `MYTABLE`, what can you tell about the number of rows returned by the following statement?
1592 1585 ```{.sqlmysql} ```{.sqlmysql}
1593 1586 SELECT * FROM MYTABLE WHERE PkgName = 'MySQL'; SELECT * FROM MYTABLE WHERE PkgName = 'MySQL';
1594 1587 ``` ```
 
... ... Exercise +.#
1597 1590 ~ What is the difference between an implicit, an explicit, and a semantic constraint? ~ What is the difference between an implicit, an explicit, and a semantic constraint?
1598 1591
1599 1592 Exercise +.# Exercise +.#
1600 ~ If you want every tuple referencing the tuple you're about to delete to be deleted as well, what mechanism should you use?
1593 ~ If you want that every time a referenced row is delted, all the refering rows are deleted as well, what mechanism should you use?
1601 1594
1602 1595 Exercise +.# Exercise +.#
1603 1596 ~ If a database designer is using the `ON UPDATE SET NULL` for a foreign key, what mechanism is he implementing (i.e., describe how the database will react a certain operation)? ~ If a database designer is using the `ON UPDATE SET NULL` for a foreign key, what mechanism is he implementing (i.e., describe how the database will react a certain operation)?
 
... ... Exercise +.#
1606 1599 ~ If the following is part of the design of a table: ~ If the following is part of the design of a table:
1607 1600
1608 1601 ~~~{.sqlmysql} ~~~{.sqlmysql}
1609 FOREIGN KEY (DptNumber) REFERENCES DEPARTMENT(DptNumber) ON DELETE SET DEFAULT ON UPDATE CASCADE
1602 FOREIGN KEY (DptNumber) REFERENCES DEPARTMENT(Number)
1603 ON DELETE SET DEFAULT
1604 ON UPDATE CASCADE
1610 1605 ~~~ ~~~
1611 1606
1612 What happen to the row whose foreign key `DptNumber` is set to `3` if
1607 What happen to the rows whose foreign key `DptNumber` are set to `3` if the row in the `DEPARTEMENT` table with primary key `Number` set to `3` is…
1613 1608
1614 #. the row in the `DEPARTEMENT` table with primary key `DptNumber` set to `3` is deleted?
1615 #. the row in the `DEPARTEMENT` table with primary key `DptNumber` set to `3` as its value for `DptNumber` updated to `5`?
1609 #. … deleted?
1610 #. …updated to `5`?
1616 1611
1617 1612 Exercise +.# Exercise +.#
1618 1613 ~ If the following is part of the design of a `WORKER` table: ~ If the following is part of the design of a `WORKER` table:
1619 1614
1620 1615 ~~~{.sqlmysql} ~~~{.sqlmysql}
1621 FOREIGN KEY WORKER(DptNumber) REFERENCES DEPARTMENT(DptNumber) ON UPDATE CASCADE
1616 FOREIGN KEY WORKER(DptNumber) REFERENCES DEPARTMENT(DptNumber)
1617 ON UPDATE CASCADE
1622 1618 ~~~ ~~~
1623 1619
1624 Admitting there is a row in the `WORKER` table whose foreign key `DptNumber` is set to `3`, what would happen if
1620 What happen to the rows whose foreign key `DptNumber` are set to `3` if the row in the `DEPARTMENT` table with primary key `Number` set to `3` is…
1625 1621
1626 #. We tried to delete the row in the `DEPARTEMENT` table with primary key `DptNumber` set to `3`?
1627 #. We tried to change the value of `DptNumber` of the row in the `DEPARTEMENT` table with primary key `DptNumber` set to `3`?
1622 #. … deleted?
1623 #. … updated to `5`?
1628 1624
1629 1625 Exercise +.# Exercise +.#
1630 1626 ~ Given a relation `TOURIST(Name, EntryDate, Address)`, write a `SQL` statement printing the name and address of all the tourists who entered the territory after the 15 September, 2012. ~ Given a relation `TOURIST(Name, EntryDate, Address)`, write a `SQL` statement printing the name and address of all the tourists who entered the territory after the 15 September, 2012.
1631 1627
1628 Exercise +.#
1629 ~ Describe what the star do in the statement
1630
1631 ~~~{.sqlmysql}
1632 SELECT ALL * FROM MYTABLE;
1633 ~~~
1634
1632 1635 Exercise +.# Exercise +.#
1633 1636 ~ What is the fully qualified name of an attribute? Give an example. ~ What is the fully qualified name of an attribute? Give an example.
1634 1637
 
... ... SELECT DISTINCT * FROM MYTABLE;
1653 1656
1654 1657 How are the results the same? How are they different? How are the results the same? How are they different?
1655 1658
1656
1657 Exercise +.#
1658 ~ Describe what the star do in the statement `SELECT ALL * FROM MYTABLE;`.
1659
1660 1659 Exercise +.# Exercise +.#
1661 1660 ~ What is wrong with the statement ~ What is wrong with the statement
1662 1661
 
... ... Exercise +.#
1664 1663 SELECT * WHERE Name = 'CS' FROM DEPARTMENT; SELECT * WHERE Name = 'CS' FROM DEPARTMENT;
1665 1664 ~~~ ~~~
1666 1665
1666 Exercise +.#
1667 ~ Write a query that returns the number of row (i.e., of entries, of tuples) in a table named `BOOK`.
1668
1667 1669 Exercise +.# Exercise +.#
1668 1670 ~ When is it useful to use a select-project-join query? ~ When is it useful to use a select-project-join query?
1669 1671
 
... ... Exercise +.#
1671 1673 ~ When is a tuple variable useful? ~ When is a tuple variable useful?
1672 1674
1673 1675 Exercise +.# Exercise +.#
1674 ~ Write a query that changes the name of the professor whose login is 'caubert' to 'Hugo Pernot' in the table `PROF`.
1676 ~ Write a query that changes the name of the professor whose `Login` is `'caubert'` to `'Hugo Pernot'` in the table `PROF`.
1675 1677
1676 1678 Exercise +.# Exercise +.#
1677 1679 ~ Can an `UPDATE` statement have a `WHERE` condition using an attribute that isn't the primary key? If no, justify, if yes, tell what could happen. ~ Can an `UPDATE` statement have a `WHERE` condition using an attribute that isn't the primary key? If no, justify, if yes, tell what could happen.
1678 1680
1679 Exercise +.#
1680 ~ What is a multi-set? What does it mean to say that `SQL` treats tables as multisets?
1681
1682
1683 Exercise +.#
1684 ~ What is the difference between those two queries?
1685
1686 ~~~{.sqlmysql}
1687 SELECT ALL * FROM MYTABLE;
1688 ~~~
1689
1690 and
1691
1692 ~~~{.sqlmysql}
1693 SELECT DISTINCT * FROM MYTABLE;
1694 ~~~
1695
1696 How are the results the same? How are they different?
1697
1698
1699 1681 Exercise +.# Exercise +.#
1700 1682 ~ What are the possible meanings or interpretations for a `NULL` value? ~ What are the possible meanings or interpretations for a `NULL` value?
1701 1683
1702 1684 Exercise +.# Exercise +.#
1703 1685 ~ What are the values of the following expressions (i.e., do they evaluate to `TRUE`, `FALSE`, or `UNKNOWN`)? ~ What are the values of the following expressions (i.e., do they evaluate to `TRUE`, `FALSE`, or `UNKNOWN`)?
1704 1686
1705 `TRUE AND FALSE`\hspace{2.2em}
1706 `TRUE AND UNKNOWN`\hspace{2.2em}
1707 `NOT UNKNOWN`\hspace{2.2em}
1708 `FALSE OR UNKNOWN`
1687 - `TRUE AND FALSE`
1688 - `TRUE AND UNKNOWN`
1689 - `NOT UNKNOWN`
1690 - `FALSE OR UNKNOWN`
1709 1691
1710 1692 Exercise +.# Exercise +.#
1711 1693 ~ What comparison expression should you use to test if a value is different from `NULL`? ~ What comparison expression should you use to test if a value is different from `NULL`?
1712 1694
1713
1714 1695 Exercise +.# Exercise +.#
1715 1696 ~ Explain this query: ~ Explain this query:
1716 1697
 
... ... Exercise +.#
1722 1703 WHERE Login = 'jrakesh'); WHERE Login = 'jrakesh');
1723 1704 ~~~ ~~~
1724 1705
1725 Exercise +.#
1706 Can you rewrite it without nesting queries?
1707
1708 Exercise +.#
1726 1709 ~ What is wrong with this query? ~ What is wrong with this query?
1727 1710
1728 1711 ~~~{.sqlmysql} ~~~{.sqlmysql}
1729 SELECT Name
1730 FROM STUDENT
1731 WHERE Login IN ( SELECT Code
1732 FROM Department
1733 WHERE head = 'aturing');
1712 SELECT Name FROM STUDENT
1713 WHERE Login IN
1714 ( SELECT Code FROM Department WHERE head = 'aturing');
1734 1715 ~~~ ~~~
1735 1716
1736 1717
1737 Exercise +.#
1738 ~ Write a query that returns the number of row (i.e., of entries, of tuples) in a table named `BOOK`.
1739
1740 Exercise +.#
1741 ~ Consider the following `SQL` code:
1742
1743 ~~~{.sqlmysql}
1744 CREATE TABLE COMPUTER(
1745 Id VARCHAR(20) PRIMARY KEY,
1746 Model VARCHAR(20)
1747 );
1748
1749 CREATE TABLE PRINTER(
1750 Id VARCHAR(20) PRIMARY KEY,
1751 Model VARCHAR(20)
1752 );
1753
1754 CREATE TABLE CONNEXION(
1755 Computer VARCHAR(20),
1756 Printer VARCHAR(20),
1757 PRIMARY KEY(Computer, Printer),
1758 FOREIGN KEY (Computer) REFERENCES COMPUTER(Id),
1759 FOREIGN KEY (Printer) REFERENCES PRINTER(Id)
1760 );
1761
1762 INSERT INTO COMPUTER VALUES
1763 ('A', 'DELL A'),
1764 ('B', 'HP X'),
1765 ('C', 'ZEPTO D'),
1766 ('D', 'MAC Y');
1767
1768 INSERT INTO PRINTER VALUES
1769 ('12', 'HP-140'),
1770 ('13', 'HP-139'),
1771 ('14', 'HP-140'),
1772 ('15', 'HP-139');
1773
1774 INSERT INTO CONNEXION VALUES
1775 ('A', '12'),
1776 ('A', '13'),
1777 ('B', '13'),
1778 ('C', '14');
1779 ~~~
1780
1781 Write a query that returns … (in parenthesis, the values returned *in this set-up*, but you have to be general)
1782
1783 #. … the number of computers connected to the printer whose id is '13' (i.e., $2$ ).
1784 #. … the number of different models of printers (i.e., $2$).
1785 #. … the model(s) of the printer connected to the computer whose id is 'A' (i.e., 'HP-140' and 'HP-139').
1786 #. … the id of the computer(s) not connected to any printer (i.e., 'D').
1787
1788 1718 Exercise +.# Exercise +.#
1789 1719 ~ Write a query that returns the sum of all the values stored in the `Pages` attribute of a `BOOK` table. ~ Write a query that returns the sum of all the values stored in the `Pages` attribute of a `BOOK` table.
1790 1720
 
... ... Exercise +.#
1794 1724 Exercise +.# Exercise +.#
1795 1725 ~ Write a query that removes the default value for a `Pages` attribute in a `BOOK` table. ~ Write a query that removes the default value for a `Pages` attribute in a `BOOK` table.
1796 1726
1797 **Bogue, some kind of duplicate?**
1798
1799 1727 #### Exercises {-} #### Exercises {-}
1800 1728
1801 Question -.#
1802 ~ Have a look at the formal definition of the
1803
1804 ~~~{.sqlmysql}
1805 ALTER
1806 ~~~
1807
1808 command, at <https://dev.mysql.com/doc/refman/5.7/en/alter-table.html> or <https://mariadb.com/kb/en/library/alter-table/>.
1809
1810 Question -.#
1811 ~ Draw the relations corresponding to that database, including the domains and primary, as well as foreign, keys.
1812
1813 Question -.#
1814 ~ Write a
1815
1816 ~~~{.sqlmysql}
1817 SELECT
1818 ~~~
1819
1820 statement that returns the `Id` number of the person whose first name is "Samantha".
1821
1822 Question -.#
1823 ~ Write a statement that violate the entity integrity constraint. What is the error message returned?
1824
1825 Question -.#
1826 ~ Execute an `UPDATE` statement that violate the referential integrity constraint. What is the error message returned?
1827
1828 Question -.#
1829 ~ Write a statement that violate another kind of constraint. Explain what constraint you are violating, and explain the error message.
1830
1831 Solutions in comment.
1832
1833 <!--
1834 SELECT Id FROM NAME WHERE FName = 'Samantha';
1835 % ERROR 1062 (23000): Duplicate entry '80' for key 'PRIMARY'
1836 % ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`HW_2Q3`.`ADDRESS`, CONSTRAINT `fk_id` FOREIGN KEY (`Habitants`) REFERENCES `name` (`Id`))
1837 % ERROR 1136 (21S01): Column count doesn't match value count at row 1
1838 -->
1839
1840 1729 ## Solution to Exercises {-} ## Solution to Exercises {-}
1841 1730
1842 1731 Solution +.# Solution +.#
 
... ... Solution +.#
1856 1745
1857 1746 <!-- Bogue table et solution, Color? --> <!-- Bogue table et solution, Color? -->
1858 1747
1859 Data Type | Examples |
1748 Data type | Examples |
1860 1749 Int | `4`, `-32` Int | `4`, `-32`
1861 1750 Char(4) | `'trai'`, `'plol'` Char(4) | `'trai'`, `'plol'`
1862 1751 VarChar(10) | `'Train'`, `'Michelle'` VarChar(10) | `'Train'`, `'Michelle'`
 
... ... Solution +.#
1867 1756 ~ It creates a schema, i.e., a database, named "Faculty". ~ It creates a schema, i.e., a database, named "Faculty".
1868 1757
1869 1758 Solution +.# Solution +.#
1870 ~ `DATE`'2016-01-21'
1759 ~ `DATE'2016-01-21'`, `'2016-01-21'`, `'2016/01/21'`, `'20160121'`.
1871 1760
1872 1761 Solution +.# Solution +.#
1873 1762 ~ ~
 
... ... Solution +.#
1881 1770 ~ Yes. ~ Yes.
1882 1771
1883 1772 Solution +.# Solution +.#
1884 ~ The textbook reads, pp. 67 - 69:
1773 ~ [@Textbook6, pp. 67--69] or [@Textbook7, pp. 157--158] reads:
1885 1774
1886 #. Constraints that are inherent in the data model. We call these inherent model-based constraints or implicit constraints.
1887 #. Constraints that can be directly expressed in schemas of the data model, typically by specifying them in the DDL (data definition language, see Section 2.3.1). We call these schema-based constraints or explicit constraints.
1888 #. Constraints that cannot be directly expressed in the schemas of the data model, and hence must be expressed and enforced by the application programs. We call these application-based or semantic constraints or business rules.
1775 > Constraints on databases can generally be divided into three main categories:
1776 >
1777 >#. Constraints that are inherent in the data model. We call these **inherent model-based constraints** or **implicit constraints**.
1778 >#. Constraints that can be directly expressed in schemas of the data model, typically by specifying them in the DDL (data definition language, see Section 2.3.1). We call these **schema-based constraints** or **explicit constraints**.
1779 >#. Constraints that *cannot* be directly expressed in the schemas of the data model, and hence must be expressed and enforced by the application programs. We call these **application-based** or **semantic constraints** or **business rules**.
1889 1780
1890 1781 Solution +.# Solution +.#
1891 1782 ~ We should use a referential triggered action clause, ~ We should use a referential triggered action clause,
 
... ... Solution +.#
1895 1786 ~~~ ~~~
1896 1787
1897 1788 Solution +.# Solution +.#
1898 ~ If the referenced tuple is updated, then the attribute of the referencing relation are set to NULL.
1789 ~ If the referenced row is updated, then the attribute of the referencing rows are set to `NULL`.
1899 1790
1900 1791 Solution +.# Solution +.#
1901 ~ The department number is set to the default value. The department number is updated accordingly.
1792 ~ In the referencing rows,
1793
1794 #. the department number is set to the default value.
1795 #. the department number is updated accordingly.
1796
1797 Solution +.#
1798 ~
1799
1800 #. This operation is rejected: the row in the `DEPARTMENT` table with primary key `Number` set to `3` can't be deleted if a row in the `WORKER` table references it.
1801 #. In the referencing rows, the department number is updated accordingly.
1902 1802
1903 1803 Solution +.# Solution +.#
1904 1804 ~ ~~~{.sqlmysql} ~ ~~~{.sqlmysql}
 
... ... Solution +.#
1906 1806 FROM TOURIST FROM TOURIST
1907 1807 WHERE EntryDate > DATE'2012-09-15'; WHERE EntryDate > DATE'2012-09-15';
1908 1808 ~~~ ~~~
1909
1910 1809 <!-- Bogue --> <!-- Bogue -->
1911 1810
1912 1811 Solution +.# Solution +.#
1913 ~ The name of the relation with the name of its schema and a period beforehand. myDB.MYTABLE, EMPLOYEE.name, etc.
1812 ~ It selects all the attributes.
1813
1814 Solution +.#
1815 ~ The name of the relation with the name of its schema and a period beforehand. An example would be `EMPLOYEE.Name`.
1914 1816
1915 1817 Solution +.# Solution +.#
1916 1818 ~ All the tables in that database. ~ All the tables in that database.
1917 1819
1918 1820 Solution +.# Solution +.#
1919 ~ A set where the same value can occur twice. The same tuple can occur twice in a table.
1821 ~ A set where the same value can occur twice. The same row can occur twice in a table.
1920 1822
1921 1823 Solution +.# Solution +.#
1922 ~ All will print the duplicate, distinct will eliminate them.
1824 ~ They both select all the rows in the `MYTABLE` table, but `ALL` will print the duplicate values, whereas `DISTINCT` will print them only once.
1923 1825
1924 Solution +.#
1925 ~ It selects all the attributes.
1926 1826
1927 1827 Solution +.# Solution +.#
1928 1828 ~ You can't have the `WHERE` before `FROM`. ~ You can't have the `WHERE` before `FROM`.
1929 1829
1930 1830 Solution +.# Solution +.#
1931 ~ We use those query that projects on attributes using a selection and join conditions when we need to look for information gathered in multiple tables.
1831 ~ `SELECT COUNT(*) FROM BOOK;`
1932 1832
1933 1833 Solution +.# Solution +.#
1934 ~ It makes the distinction between two different copies of the same relation, it is useful when we want to select a tuple in a relation that is in a particular relation with a tuple in the same relation.
1834 ~ We use those query that projects on attributes using a selection and join conditions when we need to construct for information based on pieces of data spread in multiple tables.
1935 1835
1936 Cf. <https://stackoverflow.com/a/7698796/2657549>: They are useful for saving typing, but there are other reasons to use them:
1836 Solution +.#
1837 ~ It makes the distinction between two different rows of the same table, it is useful when we want to select a tuple in a relation that is in a particular relation with a tuple in the same relation. Quoting <https://stackoverflow.com/a/7698796/>:
1937 1838
1938 - If you join a table to itself you must give it two different names otherwise referencing the table would be ambiguous.
1939 - It can be useful to give names to derived tables, and in some database systems it is required… even if you never refer to the name.
1839 > They are useful for saving typing, but there are other reasons to use them:
1840 >
1841 > - If you join a table to itself you must give it two different names otherwise referencing the table would be ambiguous.
1842 > - It can be useful to give names to derived tables, and in some database systems it is required… even if you never refer to the name.
1940 1843
1941 1844 Solution +.# Solution +.#
1942 1845 ~ ~~~{.sqlmysql} ~ ~~~{.sqlmysql}
1943 UPDATE PROF SET Name = 'Hugo Pernot' WHERE Login = 'caubert';
1846 UPDATE PROF SET Name = 'Hugo Pernot'
1847 WHERE Login = 'caubert';
1944 1848 ~~~ ~~~
1945 1849
1946 1850 Solution +.# Solution +.#
1947 1851 ~ Yes, we could update more than one tuple at a time. ~ Yes, we could update more than one tuple at a time.
1948 1852
1949 1853 Solution +.# Solution +.#
1950 ~ A set where the same value can occur twice.
1951 The same tuple can occur twice in a table.
1854 ~ Unknown value, unavailable / withheld, N/A.
1952 1855
1953 1856 Solution +.# Solution +.#
1954 ~ `ALL` will print the duplicate, `DISTINCT` will eliminate them.
1857 ~
1955 1858
1956 Solution +.#
1957 ~ Unknown value, unavailable / withheld, N/A.
1859 - `TRUE AND FALSE` → `FALSE`
1860 - `TRUE AND UNKNOWN` → `UNKNOWN`
1861 - `NOT UNKNOWN` → `UNKNOWN`
1862 - `FALSE OR UNKNOWN` → `FALSE`
1958 1863
1959 Solution +.#
1960 ~ `FALSE`, `UNKNOWN`, `UNKNOWN`, `FALSE`
1961 1864
1962 1865 Solution +.# Solution +.#
1963 1866 ~ `IS NOT` ~ `IS NOT`
1964 1867
1965 1868 Solution +.# Solution +.#
1966 ~ It list the login of the professors teaching in the department where the student whose login is "jrakesh" is majoring.
1869 ~ It list the login of the professors teaching in the department where the student whose login is "jrakesh" is majoring. It can be rewritten as
1967 1870
1871 ~~~{.sqlmysql}
1872 SELECT PROF.Login
1873 FROM PROF, STUDENT
1874 WHERE Department = Major
1875 AND STUDENT.Login = 'jrakesh';
1876 ~~~
1968 1877
1969 1878 Solution +.# Solution +.#
1970 1879 ~ It tries to find a `Login` in a `Code`! ~ It tries to find a `Login` in a `Code`!
1971 1880
1972 1881 Solution +.# Solution +.#
1973 ~ `SELECT COUNT(*) FROM BOOK;`
1974
1975 Solution +.#
1976 ~ This solution is missing
1977
1978
1979 Solution +.#
1980 ~ `SELECT SUM(Pages)FROM BOOK;`
1882 ~ `SELECT SUM(Pages) FROM BOOK;`
1981 1883
1982 1884 Solution +.# Solution +.#
1983 1885 ~ `ALTER TABLE BOOK ADD COLUMN Pages INT;` ~ `ALTER TABLE BOOK ADD COLUMN Pages INT;`
 
... ... Since there are small variations from one implementation to the other, it's bett
2242 2144
2243 2145 ### Creating and using a table ### Creating and using a table
2244 2146
2147 Problem -.+.#address
2148
2245 2149 *This exercise, and the following ones, assume you successfully completed @problem:installation.* *This exercise, and the following ones, assume you successfully completed @problem:installation.*
2246 2150
2247 2151 Log in as `testuser` and create a database `HW_2Q3`. Log in as `testuser` and create a database `HW_2Q3`.
 
... ... And let's use our first update command:
2374 2278 UPDATE ADDRESS SET Habitants = 3 WHERE Number = 120; UPDATE ADDRESS SET Habitants = 3 WHERE Number = 120;
2375 2279 ~~~ ~~~
2376 2280
2281 Now, answer the following questions.
2282
2283 Question -.#
2284 ~ Have a look at the formal definition of the `ALTER` command, at <https://dev.mysql.com/doc/refman/5.7/en/alter-table.html> or <https://mariadb.com/kb/en/library/alter-table/>.
2285
2286 Question -.#
2287 ~ Draw the relations corresponding to that database, including the domains and primary, as well as foreign, keys.
2288
2289 Question -.#
2290 ~ Write a `SELECT` statement that returns the `Id` number of the person whose first name is "Samantha".
2291
2292 Question -.#
2293 ~ Write a statement that violate the entity integrity constraint. What is the error message returned?
2294
2295 Question -.#
2296 ~ Execute an `UPDATE` statement that violate the referential integrity constraint. What is the error message returned?
2297
2298 Question -.#
2299 ~ Write a statement that violate another kind of constraint. Explain what constraint you are violating, and explain the error message.
2300
2301 Solutions in comment.
2302
2303 Solution to @problem:address
2304
2305 ~~~{.sqlmysql}
2306 SELECT Id FROM NAME WHERE FName = 'Samantha';
2307 ERROR 1062 (23000): Duplicate entry '80' for key 'PRIMARY'
2308 ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`HW_2Q3`.`ADDRESS`, CONSTRAINT `fk_id` FOREIGN KEY (`Habitants`) REFERENCES `name` (`Id`))
2309 ERROR 1136 (21S01): Column count doesn't match value count at row 1
2310 ~~~
2311
2377 2312
2378 2313 ### Repetiting tuples in MySQL ### Repetiting tuples in MySQL
2379 2314
 
... ... Problem +.#
2503 2438
2504 2439 Suppose we have the relational model depicted below, with the indicated data in it: Suppose we have the relational model depicted below, with the indicated data in it:
2505 2440
2441 **Bogue: draw the actual relational model.**
2506 2442
2507 2443 **COFFEE** **COFFEE**
2508 2444
2509
2510 2445 \underline{Ref} | Origin | TypeOfRoast | PricePerPound \underline{Ref} | Origin | TypeOfRoast | PricePerPound
2511 2446 :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
2512 2447 $001$ | Brazil | Light | 8.90 $001$ | Brazil | Light | 8.90
 
... ... Johns \| Co. | $221$
2538 2473 :---: | :---: | :---: | :---: |
2539 2474 Coffee Unl. | bob@cofunl.com Coffee Unl. | bob@cofunl.com
2540 2475 Coffee Exp. | pat@coffeex.dk Coffee Exp. | pat@coffeex.dk
2541 Johns \| Co. | NULL
2476 Johns & Co. | NULL
2542 2477
2543 2478 In the following, we will assume that this model was implemented in a DBMS (MySQL or MariaDB), the primary keys being the underlined attributes, and the foreign keys as follow: In the following, we will assume that this model was implemented in a DBMS (MySQL or MariaDB), the primary keys being the underlined attributes, and the foreign keys as follow:
2544 2479
 
... ... Write queries that answer the following questions (the last two are bonus):
2593 2528 #. "How many coffees does Johns \& co. provide us with?" #. "How many coffees does Johns \& co. provide us with?"
2594 2529 #. "What are the names of the providers of my dark coffees?" #. "What are the names of the providers of my dark coffees?"
2595 2530
2531
2532 Problem +.#
2533 ~ Consider the following `SQL` code:
2534
2535
2536 ~~~{.sqlmysql}
2537 CREATE TABLE COMPUTER(
2538 Id VARCHAR(20) PRIMARY KEY,
2539 Model VARCHAR(20)
2540 );
2541
2542 CREATE TABLE PRINTER(
2543 Id VARCHAR(20) PRIMARY KEY,
2544 Model VARCHAR(20)
2545 );
2546
2547 CREATE TABLE CONNEXION(
2548 Computer VARCHAR(20),
2549 Printer VARCHAR(20),
2550 PRIMARY KEY(Computer, Printer),
2551 FOREIGN KEY (Computer) REFERENCES COMPUTER(Id),
2552 FOREIGN KEY (Printer) REFERENCES PRINTER(Id)
2553 );
2554
2555 INSERT INTO COMPUTER VALUES
2556 ('A', 'DELL A'),
2557 ('B', 'HP X'),
2558 ('C', 'ZEPTO D'),
2559 ('D', 'MAC Y');
2560
2561 INSERT INTO PRINTER VALUES
2562 ('12', 'HP-140'),
2563 ('13', 'HP-139'),
2564 ('14', 'HP-140'),
2565 ('15', 'HP-139');
2566
2567 INSERT INTO CONNEXION VALUES
2568 ('A', '12'),
2569 ('A', '13'),
2570 ('B', '13'),
2571 ('C', '14');
2572 ~~~
2573
2574
2575 Write a query that returns … (in parenthesis, the values returned *in this set-up*, but you have to be general)
2576
2577 #. … the number of computers connected to the printer whose `Id` is `13` (i.e., 2 ).
2578 #. … the number of different models of printers (i.e., 2).
2579 #. … the model(s) of the printer connected to the computer whose `Id` is 'A' (i.e., `'HP-140'` and `'HP-139'`).
2580 #. … the id of the computer(s) not connected to any printer (i.e., `'D'`).
2581
2596 2582 Problem +.# Problem +.#
2597 2583
2598 2584 A friend of yours want you to review and improve the code for a role-playing game. A friend of yours want you to review and improve the code for a role-playing game.
 
... ... We will use multiple models:
2738 2724 Foreign Keys   ✔ ✔ Foreign Keys   ✔ ✔
2739 2725 Table Names     ✔ Table Names     ✔
2740 2726 Column Names     ✔ Column Names     ✔
2741 Column Data Types     ✔
2727 Column Data types     ✔
2742 2728 ---------------------- ------------ --------- ---------- ---------------------- ------------ --------- ----------
2743 2729
2744 2730 Remember that in Relational models, relations were representing entities and relationships, here the distinction is made in this table (entity vs relationship). Remember that in Relational models, relations were representing entities and relationships, here the distinction is made in this table (entity vs relationship).
 
... ... public class MyProg{
4837 4823
4838 4824 /* Errors after this point.*/ /* Errors after this point.*/
4839 4825
4840 String strSelect = "SELECT title WHERE qty > 40 FROM disks;";
4826 String strSelect = "SELECT title FROM disks WHERE qty > 40;";
4841 4827 ResultSet rset = stmt.executeUpdate(strSelect); ResultSet rset = stmt.executeUpdate(strSelect);
4842 4828
4843 4829 System.out.println("The records selected are: (listed last first):"); System.out.println("The records selected are: (listed last first):");
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/caubert/CSCI_3410

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/caubert/CSCI_3410

Clone this repository using git:
git clone git://git.rocketgit.com/user/caubert/CSCI_3410

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main