File notes/00_sum.md changed (mode: 100644) (index 5e57a7d..b1c63c6) |
... |
... |
keywords: |
18 |
18 |
- MySQL programming |
- MySQL programming |
19 |
19 |
header-includes: |
header-includes: |
20 |
20 |
- \usepackage{latex/packages} |
- \usepackage{latex/packages} |
|
21 |
|
pandoc-numbering: |
|
22 |
|
problem: |
|
23 |
|
general: |
|
24 |
|
listing-title: List of problems |
21 |
25 |
--- |
--- |
22 |
26 |
|
|
23 |
27 |
<!-- |
<!-- |
|
... |
... |
These lecture notes are written in an elusive style: their are a support for the |
33 |
37 |
They are not designed to be self-contained: they are rather a list of topics and reminders, along with handy examples, code and drawings. |
They are not designed to be self-contained: they are rather a list of topics and reminders, along with handy examples, code and drawings. |
34 |
38 |
Reading them before coming to the lecture will help you getting a sense of the next topic we will be discussing, but you may sometimes have trouble deciphering their … *unique* style. |
Reading them before coming to the lecture will help you getting a sense of the next topic we will be discussing, but you may sometimes have trouble deciphering their … *unique* style. |
35 |
39 |
|
|
|
40 |
|
When it comes to code, you can normally type it or copy-and-paste it from the document and use it as it: the colors and the <!-- ↪ --> ${\tiny\hookrightarrow}$, |
|
41 |
|
symbols are here to ease the reading, but you can ignore them safely. |
|
42 |
|
|
36 |
43 |
On top of the notes, you will find in this document: |
On top of the notes, you will find in this document: |
37 |
44 |
|
|
38 |
45 |
- [References, at the very end of this document](#ref) |
- [References, at the very end of this document](#ref) |
|
... |
... |
Solution +.# |
292 |
299 |
|
|
293 |
300 |
## Problems {-} |
## Problems {-} |
294 |
301 |
|
|
295 |
|
Problem (Campus Database) +.#campus |
|
|
302 |
|
Problem (A database catalog for a campus) +.#campus |
296 |
303 |
|
|
297 |
304 |
We want to define a `CAMPUS` database organized into three files as follows: |
We want to define a `CAMPUS` database organized into three files as follows: |
298 |
305 |
|
|
|
... |
... |
Question -.# |
320 |
327 |
8. Can you tell which professor has the highest evaluations? |
8. Can you tell which professor has the highest evaluations? |
321 |
328 |
|
|
322 |
329 |
|
|
323 |
|
## Solution to @problem:campus |
|
|
330 |
|
## Solution to Selected Problems |
|
331 |
|
|
|
332 |
|
Solution to @problem:campus |
324 |
333 |
|
|
325 |
334 |
The database catalog should be similar to the following: |
The database catalog should be similar to the following: |
326 |
335 |
|
|
|
... |
... |
Solution +.# |
676 |
685 |
|
|
677 |
686 |
## Problems |
## Problems |
678 |
687 |
|
|
679 |
|
Problem +.#cinema |
|
|
688 |
|
Problem (Relational model for a cinema company) +.#cinema |
680 |
689 |
|
|
681 |
690 |
A cinema company wants you to design a relational model for the following set-up: |
A cinema company wants you to design a relational model for the following set-up: |
682 |
691 |
|
|
|
... |
... |
TICKETS(Id (PK), ShowTimeId (FK to SHOWTIME.Id), Price) |
706 |
715 |
## Resources {-} |
## Resources {-} |
707 |
716 |
|
|
708 |
717 |
- [@Textbook6, Ch. 4--5], [@Textbook7, Ch. 6--7] describes `SQL`, not one of its implementation. |
- [@Textbook6, Ch. 4--5], [@Textbook7, Ch. 6--7] describes `SQL`, not one of its implementation. |
709 |
|
- @problem:installation and @problem:sqldoc contain a list of useful links. |
|
710 |
718 |
- To compare DBMS, you can look at their features, at <https://en.wikipedia.org/wiki/Comparison_of_relational_database_management_systems>, and at their popularity at <https://db-engines.com/en/ranking> and <https://www.eversql.com/most-popular-databases-in-2018-according-to-stackoverflow-survey/> (if you sum up MySQL and MariaDB, they are first in both). |
- To compare DBMS, you can look at their features, at <https://en.wikipedia.org/wiki/Comparison_of_relational_database_management_systems>, and at their popularity at <https://db-engines.com/en/ranking> and <https://www.eversql.com/most-popular-databases-in-2018-according-to-stackoverflow-survey/> (if you sum up MySQL and MariaDB, they are first in both). |
711 |
719 |
- MySQL and MariaDB have some differences, you can look them up at <https://mariadb.com/kb/en/library/mariadb-vs-mysql-features/> and <https://mariadb.com/kb/en/library/mariadb-vs-mysql-compatibility/>. |
- MySQL and MariaDB have some differences, you can look them up at <https://mariadb.com/kb/en/library/mariadb-vs-mysql-features/> and <https://mariadb.com/kb/en/library/mariadb-vs-mysql-compatibility/>. |
712 |
720 |
|
|
713 |
721 |
This chapter will be "code-driven": the code will illustrate and help you understand some concepts. |
This chapter will be "code-driven": the code will illustrate and help you understand some concepts. |
714 |
|
You may want to have a look at @problem:installation, in the [problem section](#sec:gettingstartedwithmysql), as early as possible in this lecture. |
|
|
722 |
|
You may want to have a look at the ["Setting Up Your Work Environment"](#sec:setup) Section, as early as possible in this lecture. |
|
723 |
|
On top of being a step-by-step guide to install and configure a relational database managment system, it contains a list of useful links. |
715 |
724 |
|
|
716 |
725 |
## Actors |
## Actors |
717 |
726 |
|
|
|
... |
... |
Type and domains are two different things in some implementations, cf. for insta |
774 |
783 |
|
|
775 |
784 |
## First Commands |
## First Commands |
776 |
785 |
|
|
777 |
|
~~~{.sqlmysql, .numberLines} |
|
|
786 |
|
~~~{.sqlmysql .numberLines} |
778 |
787 |
CREATE SCHEMA HW_FACULTY; |
CREATE SCHEMA HW_FACULTY; |
779 |
788 |
|
|
780 |
789 |
/* Or |
/* Or |
|
... |
... |
INSERT INTO PROF VALUES ( |
815 |
824 |
|
|
816 |
825 |
The following commands are particularly useful: |
The following commands are particularly useful: |
817 |
826 |
|
|
818 |
|
~~~{.sqlmysql, , .numberLines} |
|
|
827 |
|
~~~{.sqlmysql .numberLines} |
819 |
828 |
SHOW SCHEMAS; -- List the schema |
SHOW SCHEMAS; -- List the schema |
820 |
829 |
SHOW TABLES; -- List the tables in a schema |
SHOW TABLES; -- List the tables in a schema |
821 |
830 |
DESCRIBE <TableName>; -- Show the structure of a table |
DESCRIBE <TableName>; -- Show the structure of a table |
|
... |
... |
f. `CHECK` |
840 |
849 |
|
|
841 |
850 |
We know a. and b. from the Relational Model, here comes new constraints that can't be describe in our relations. |
We know a. and b. from the Relational Model, here comes new constraints that can't be describe in our relations. |
842 |
851 |
|
|
843 |
|
~~~{.sqlmysql, , .numberLines} |
|
|
852 |
|
~~~{.sqlmysql .numberLines} |
844 |
853 |
CREATE TABLE HURRICANE( |
CREATE TABLE HURRICANE( |
845 |
854 |
Name VARCHAR(25) PRIMARY KEY, |
Name VARCHAR(25) PRIMARY KEY, |
846 |
855 |
WindSpeed INT DEFAULT 76, |
WindSpeed INT DEFAULT 76, |
|
... |
... |
MariaDB [HW_CONSTRAINTS_PART1]> DESCRIBE STATE; |
878 |
887 |
2 rows in set (0.00 sec) |
2 rows in set (0.00 sec) |
879 |
888 |
~~~ |
~~~ |
880 |
889 |
|
|
881 |
|
~~~{.sqlmysql, .numberLines} |
|
|
890 |
|
~~~{.sqlmysql .numberLines} |
882 |
891 |
-- Adding a primary key: |
-- Adding a primary key: |
883 |
892 |
ALTER TABLE STATE ADD PRIMARY KEY (Name); |
ALTER TABLE STATE ADD PRIMARY KEY (Name); |
884 |
893 |
|
|
|
... |
... |
ALTER TABLE HURRICANE ADD FOREIGN KEY (Above) REFERENCES STATE(Name); |
913 |
922 |
|
|
914 |
923 |
A bit of testing: |
A bit of testing: |
915 |
924 |
|
|
916 |
|
~~~{.sqlmysql, .numberLines} |
|
|
925 |
|
~~~{.sqlmysql .numberLines} |
917 |
926 |
INSERT INTO STATE VALUES('Georgia', 'GA'); |
INSERT INTO STATE VALUES('Georgia', 'GA'); |
918 |
927 |
INSERT INTO STATE VALUES('Texas', 'TX'); |
INSERT INTO STATE VALUES('Texas', 'TX'); |
919 |
928 |
INSERT INTO STATE VALUES('FLORIDA', 'FL'); |
INSERT INTO STATE VALUES('FLORIDA', 'FL'); |
|
... |
... |
UPDATE HURRICANE SET Above = 'North Carolina' |
957 |
966 |
|
|
958 |
967 |
## Foreign Keys |
## Foreign Keys |
959 |
968 |
|
|
960 |
|
~~~{.sqlmysql, .numberLines} |
|
|
969 |
|
~~~{.sqlmysql .numberLines} |
961 |
970 |
CREATE TABLE STORM( |
CREATE TABLE STORM( |
962 |
971 |
Name VARCHAR(25) PRIMARY KEY, |
Name VARCHAR(25) PRIMARY KEY, |
963 |
972 |
Kind ENUM('Tropical Storm', 'Hurricane'), |
Kind ENUM('Tropical Storm', 'Hurricane'), |
|
... |
... |
INSERT INTO STORM VALUES('Irma', 'Tropical Storm', 102, DEFAULT); |
989 |
998 |
|
|
990 |
999 |
MySQL will always notify you if there is an error in a date attribute |
MySQL will always notify you if there is an error in a date attribute |
991 |
1000 |
|
|
992 |
|
~~~{.sqlmysql, .numberLines} |
|
|
1001 |
|
~~~{.sqlmysql .numberLines} |
993 |
1002 |
|
|
994 |
1003 |
INSERT INTO STATE VALUES('Georgia', 'GA', NULL); |
INSERT INTO STATE VALUES('Georgia', 'GA', NULL); |
995 |
1004 |
INSERT INTO STATE VALUES('Texas', 'TX', NULL); |
INSERT INTO STATE VALUES('Texas', 'TX', NULL); |
|
... |
... |
DELETE FROM STORM |
1006 |
1015 |
|
|
1007 |
1016 |
## Foreign Keys Restrictions |
## Foreign Keys Restrictions |
1008 |
1017 |
|
|
1009 |
|
~~~{.sqlmysql, .numberLines} |
|
|
1018 |
|
~~~{.sqlmysql .numberLines} |
1010 |
1019 |
CREATE TABLE F_Key( |
CREATE TABLE F_Key( |
1011 |
1020 |
Attribute VARCHAR(25) PRIMARY KEY |
Attribute VARCHAR(25) PRIMARY KEY |
1012 |
1021 |
); |
); |
|
... |
... |
DROP TABLE Table_default; |
1074 |
1083 |
|
|
1075 |
1084 |
--- |
--- |
1076 |
1085 |
|
|
1077 |
|
~~~{.sqlmysql, .numberLines} |
|
|
1086 |
|
~~~{.sqlmysql .numberLines} |
1078 |
1087 |
-- The following fails too, this time because of the Table_restrict table: |
-- The following fails too, this time because of the Table_restrict table: |
1079 |
1088 |
UPDATE F_Key SET Attribute = 'After Update' |
UPDATE F_Key SET Attribute = 'After Update' |
1080 |
1089 |
WHERE Attribute = 'First Test'; |
WHERE Attribute = 'First Test'; |
|
... |
... |
DEPARTMENT(Code (PK), Name, Head (FK to PROF.Login)) |
1152 |
1161 |
](img/rel_mod/PROF_DEPARTMENT.svg){ width=100% } |
](img/rel_mod/PROF_DEPARTMENT.svg){ width=100% } |
1153 |
1162 |
\ |
\ |
1154 |
1163 |
|
|
1155 |
|
~~~{.sqlmysql, .numberLines} |
|
|
1164 |
|
~~~{.sqlmysql .numberLines} |
1156 |
1165 |
CREATE TABLE PROF( |
CREATE TABLE PROF( |
1157 |
1166 |
Login VARCHAR(25) PRIMARY KEY, |
Login VARCHAR(25) PRIMARY KEY, |
1158 |
1167 |
Name VARCHAR(25), |
Name VARCHAR(25), |
|
... |
... |
Note the structure of the `ALTER TABLE` command: |
1177 |
1186 |
- … `KEY (Department) REFERENCES (Code);`⇒ error |
- … `KEY (Department) REFERENCES (Code);`⇒ error |
1178 |
1187 |
- … `KEY PROF(Department) REFERENCES DEPARTMENT(Code);` ⇒ ok |
- … `KEY PROF(Department) REFERENCES DEPARTMENT(Code);` ⇒ ok |
1179 |
1188 |
|
|
1180 |
|
~~~{.sqlmysql, .numberLines} |
|
|
1189 |
|
~~~{.sqlmysql .numberLines} |
1181 |
1190 |
CREATE TABLE STUDENT( |
CREATE TABLE STUDENT( |
1182 |
1191 |
Login VARCHAR(25) PRIMARY KEY, |
Login VARCHAR(25) PRIMARY KEY, |
1183 |
1192 |
Name VARCHAR(25), |
Name VARCHAR(25), |
|
... |
... |
INSERT INTO DEPARTMENT (Code, Name) VALUES |
1206 |
1215 |
|
|
1207 |
1216 |
And we can even specify the order (even the trivial one): |
And we can even specify the order (even the trivial one): |
1208 |
1217 |
|
|
1209 |
|
~~~{.sqlmysql, .numberLines} |
|
|
1218 |
|
~~~{.sqlmysql .numberLines} |
1210 |
1219 |
INSERT INTO PROF (Login, Department, Name) VALUES |
INSERT INTO PROF (Login, Department, Name) VALUES |
1211 |
1220 |
('caubert', 'CS', 'Clément Aubert'); |
('caubert', 'CS', 'Clément Aubert'); |
1212 |
1221 |
|
|
|
... |
... |
Solution +.# |
1735 |
1744 |
~ Database (schema), table, view, assertion, trigger, etc. |
~ Database (schema), table, view, assertion, trigger, etc. |
1736 |
1745 |
|
|
1737 |
1746 |
Solution +.# |
Solution +.# |
1738 |
|
~ ~~~{.sqlmysql} |
|
1739 |
|
ALTER TABLE STAFF ADD PRIMARY KEY(Id); |
|
1740 |
|
~~~ |
|
|
1747 |
|
~ \ |
1741 |
1748 |
|
|
1742 |
|
<!-- Bogue --> |
|
|
1749 |
|
~~~{.sqlmysql} |
|
1750 |
|
ALTER TABLE STAFF ADD PRIMARY KEY(Id); |
|
1751 |
|
~~~ |
1743 |
1752 |
|
|
1744 |
1753 |
Solution +.# |
Solution +.# |
1745 |
1754 |
|
|
1746 |
|
<!-- Bogue table et solution, Color? --> |
|
|
1755 |
|
<!-- Bogue, on ne peut avoir les tables inline. --> |
1747 |
1756 |
|
|
1748 |
1757 |
Data type | Examples | |
Data type | Examples | |
1749 |
1758 |
Int | `4`, `-32` |
Int | `4`, `-32` |
|
... |
... |
Solution +.# |
1759 |
1768 |
~ `DATE'2016-01-21'`, `'2016-01-21'`, `'2016/01/21'`, `'20160121'`. |
~ `DATE'2016-01-21'`, `'2016-01-21'`, `'2016/01/21'`, `'20160121'`. |
1760 |
1769 |
|
|
1761 |
1770 |
Solution +.# |
Solution +.# |
1762 |
|
~ |
|
|
1771 |
|
~ \ |
|
1772 |
|
|
1763 |
1773 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
1764 |
1774 |
INSERT INTO TRAINS VALUES('Thomas', 4); |
INSERT INTO TRAINS VALUES('Thomas', 4); |
1765 |
1775 |
~~~ |
~~~ |
1766 |
|
|
|
1767 |
|
<!-- Bogue! --> |
|
1768 |
1776 |
|
|
1769 |
1777 |
Solution +.# |
Solution +.# |
1770 |
1778 |
~ Yes. |
~ Yes. |
|
... |
... |
Solution +.# |
1801 |
1809 |
#. In the referencing rows, the department number is updated accordingly. |
#. In the referencing rows, the department number is updated accordingly. |
1802 |
1810 |
|
|
1803 |
1811 |
Solution +.# |
Solution +.# |
1804 |
|
~ ~~~{.sqlmysql} |
|
1805 |
|
SELECT Name, Address |
|
1806 |
|
FROM TOURIST |
|
1807 |
|
WHERE EntryDate > DATE'2012-09-15'; |
|
1808 |
|
~~~ |
|
1809 |
|
<!-- Bogue --> |
|
|
1812 |
|
~ \ |
|
1813 |
|
|
|
1814 |
|
~~~{.sqlmysql} |
|
1815 |
|
SELECT Name, Address |
|
1816 |
|
FROM TOURIST |
|
1817 |
|
WHERE EntryDate > DATE'2012-09-15'; |
|
1818 |
|
~~~ |
1810 |
1819 |
|
|
1811 |
1820 |
Solution +.# |
Solution +.# |
1812 |
1821 |
~ It selects all the attributes. |
~ It selects all the attributes. |
|
... |
... |
Solution +.# |
1842 |
1851 |
> - 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. |
> - 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. |
1843 |
1852 |
|
|
1844 |
1853 |
Solution +.# |
Solution +.# |
1845 |
|
~ ~~~{.sqlmysql} |
|
1846 |
|
UPDATE PROF SET Name = 'Hugo Pernot' |
|
1847 |
|
WHERE Login = 'caubert'; |
|
1848 |
|
~~~ |
|
|
1854 |
|
~ \ |
|
1855 |
|
|
|
1856 |
|
~~~{.sqlmysql} |
|
1857 |
|
UPDATE PROF SET Name = 'Hugo Pernot' |
|
1858 |
|
WHERE Login = 'caubert'; |
|
1859 |
|
~~~ |
1849 |
1860 |
|
|
1850 |
1861 |
Solution +.# |
Solution +.# |
1851 |
1862 |
~ Yes, we could update more than one tuple at a time. |
~ Yes, we could update more than one tuple at a time. |
|
... |
... |
Solution +.# |
1887 |
1898 |
Solution +.# |
Solution +.# |
1888 |
1899 |
~ `ALTER TABLE BOOK ALTER COLUMN Pages DROP DEFAULT;` |
~ `ALTER TABLE BOOK ALTER COLUMN Pages DROP DEFAULT;` |
1889 |
1900 |
|
|
|
1901 |
|
## Setting Up Your Work Environment {#sec:setup} |
1890 |
1902 |
|
|
1891 |
|
## Problems |
|
1892 |
|
|
|
1893 |
|
### Setting up MySQL {#sec:gettingstartedwithmysql} |
|
|
1903 |
|
This part is a short tutorial to install and configure a working relational DBMS. |
1894 |
1904 |
|
|
1895 |
|
Problem -.+.#installation |
|
|
1905 |
|
### Installation |
1896 |
1906 |
|
|
1897 |
|
In this problem, you will install the [MySQL](https://www.mysql.com/) DataBase Managment System and set it up. |
|
1898 |
|
On top of being open source and free, it is one of the most widespread and used DBMS, as well as one of the most well-documented. |
|
1899 |
|
Linux users will use its community-developed fork, \href{https://mariadb.org/}{MariaDB}, but that won't make a difference in this course (up to some minor aspects). |
|
|
1907 |
|
You will install the [MySQL](https://www.mysql.com/) DataBase Managment System, or its community-developed fork, [MariaDB](https://mariadb.org/). |
|
1908 |
|
Below are the instruction to install MySQL Community Edition on Windows 10, and MariaDB on Linux-based distribution, but both are developped for every major operating system (Mac OS, Windows, Debian, Ubuntu, etc.): feel free to pick one or the other, it won't really make a difference in this course (up to some minor aspects). MySQL is more common, MariaDB is growing, both are released under [GNU General Public License](https://www.gnu.org/licenses/licenses.html#GPL), well-documented and free of charge for their "community" versions. |
1900 |
1909 |
|
|
1901 |
|
**bogue: cross ref?** |
|
|
1910 |
|
It is perfectly acceptable to install MySQL or MariaDB on a virtual machine for this class. |
|
1911 |
|
Remember that at <http://spots.augusta.edu/tschultz/resources/SWAvailable.html>, you can have access to [VMware](https://www.vmware.com/) and Windows licences, and that using [Virtual Box](https://www.virtualbox.org/wiki/Downloads) should also work fine. |
|
1912 |
|
Once this step is completed, or if MySQL or MariaDB is already installed on your computer, go to the next step. |
1902 |
1913 |
|
|
1903 |
|
It is very important that you follow the steps below carefully. |
|
1904 |
|
Read the messages printed on your screen, make sure a step was correctly executed before moving to the next one. |
|
1905 |
|
And, remember: |
|
|
1914 |
|
I tried to give precise and up-to-date instructions below, follow them carefully, read the messages displayed on your screen, make sure a step was correctly executed before moving to the next one, and everything should be all right. |
|
1915 |
|
Also, remember: |
1906 |
1916 |
|
|
1907 |
|
1. Don't wait, set your system early. |
|
1908 |
|
2. I'm willing to help you, but there's nothing I can do with an email like "It doesn't work": if you look for help, be detailed and clear about what you think went wrong. |
|
|
1917 |
|
#. Don't wait, set your system early. |
|
1918 |
|
#. I will be happy to assist you, but there's nothing I can do with an email like "It doesn't work": if you look for help, be detailed and clear about what you think went wrong. |
1909 |
1919 |
|
|
1910 |
1920 |
The following links could be useful: |
The following links could be useful: |
1911 |
1921 |
|
|
1912 |
|
- <https://dev.mysql.com/doc/refman/5.7/en/windows-installation.html>, |
|
1913 |
|
- <https://dev.mysql.com/doc/refman/5.7/en/linux-installation-native.html>, |
|
1914 |
|
- <https://dev.mysql.com/doc/refman/5.7/en/connecting-disconnecting.html> and |
|
|
1922 |
|
- <https://mariadb.com/kb/en/library/getting-installing-and-upgrading-mariadb/> |
|
1923 |
|
- <https://dev.mysql.com/doc/refman/8.0/en/mysql-installer-workflow.html>, and particularly [the page on windows installation](https://dev.mysql.com/doc/refman/8.0/en/windows-installation.html) and [the page on Linux installation using package-managers](https://dev.mysql.com/doc/refman/8.0/en/linux-installation-native.html), |
|
1924 |
|
- <https://dev.mysql.com/doc/refman/5.7/en/connecting-disconnecting.html> |
1915 |
1925 |
- <https://www.linode.com/docs/databases/mysql/how-to-install-mysql-on-debian-8> |
- <https://www.linode.com/docs/databases/mysql/how-to-install-mysql-on-debian-8> |
1916 |
1926 |
|
|
|
1927 |
|
#### Installing MySQL on Windows 10 |
|
1928 |
|
|
|
1929 |
|
#. Visit <https://dev.mysql.com/downloads/installer/>, click on "Download" next to |
|
1930 |
|
|
|
1931 |
|
> Windows (x86, 32-bit), MSI Installer XXX YYY (mysql-installer-web-community-XXX.msi) |
|
1932 |
|
|
|
1933 |
|
where XXX is a number version (e.g., 8.0.11), and YYY is the size of the file (e.g., 15.8M). On the next page, click on the (somewhat hidden) "No thanks, just start my download." button. |
|
1934 |
|
#. Save the "mysql-installer-web-community-XXX.msi" file, and open it. If there is an updated version of the installer available, agree to download it. Accept the license term. |
|
1935 |
|
#. We will now install the various components needed for this class, leaving all the choices by defaults. This means that you need to do the following: |
|
1936 |
|
#. Leave the first option on "Developer Default" and click on "Next" |
|
1937 |
|
#. Click on "Next" even if you don't meet all the requirements |
|
1938 |
|
#. Click on "Execute". The system will download and install several softwares (this may take some time). |
|
1939 |
|
#. Click on "Next" twice, leave "Type and Networking" on "Standalone MySQL Server / Classic MySQL Replication" and click "Next", and leave the next options as they are (unless you know what you do and want to change the port, for instance) and click on "Next". |
|
1940 |
|
#. \label{choose-pswd} You now need to choose a password for the MySQL root account. |
|
1941 |
|
It can be anything, just make sure to memorize it. |
|
1942 |
|
Click on "Next". |
|
1943 |
|
#. On the "Windows Service" page, leave everything as it is and click on "Next". |
|
1944 |
|
#. On the "Plugins and Extensions" page, leave everything as it is and click on "Next". |
|
1945 |
|
#. Finally, click "Execute" on the "Apply Configuration" page, and then on "Finish". |
|
1946 |
|
#. Click on "Cancel" on the "Product Configuration" page and confirm that you don't want to add products: we only need to have MySQL Server XXX configured. |
|
1947 |
|
|
|
1948 |
|
#. We now want to make sure that MySQL is running: launch Windows' "Control Panel", then click on "Administrative Tools", and on "Services". Look for "MySQLXX", its status should be "Running". If it is not, right-click on it and click on "Start". |
|
1949 |
|
#. Open a command prompt (search for `cmd`, or use [PowerShell](https://docs.microsoft.com/en-us/powershell/scripting/getting-started/getting-started-with-windows-powershell?view=powershell-6)) and type |
1917 |
1950 |
|
|
1918 |
|
#### Installation |
|
|
1951 |
|
~~~{.powershell} |
|
1952 |
|
cd "C:\Program Files\MySQL\MySQL Server 5.7\bin" |
|
1953 |
|
~~~ |
1919 |
1954 |
|
|
1920 |
|
Below are the instructions for Windows 10 and for Linux. |
|
1921 |
|
It is perfectly acceptable to install MySQL on a virtual machine for this class (remember that on <http://spots.augusta.edu/tschultz/resources/SWAvailable.html>, you can have access to VMware and Windows licences, but using [Virtual Box](https://www.virtualbox.org/wiki/Downloads) or Linux should also work fine). |
|
1922 |
|
Once this step is completed, or if MySQL is already installed on your computer, go to the next step. |
|
|
1955 |
|
and then |
1923 |
1956 |
|
|
1924 |
|
- **For Windows 10** |
|
1925 |
|
1. Visit <https://dev.mysql.com/downloads/installer/>, click on "Download" next to "Windows (x86, 32-bit), MSI Installer 5.7.20 18.5M (mysql-installer-web-community-5.7.20.0.msi)". On the next page, click on the (somewhat hidden) "No thanks, just start my download." button. |
|
1926 |
|
2. Save the "mysql-installer-web-community-5.7.20.0.msi" file, and open it. If there is an updated version of the installer available, agree to download it. Accept the license term. |
|
1927 |
|
3. We will now install the various components needed for this class, leaving all the choices by defaults. This means that you need to do the following: |
|
1928 |
|
1. Leave the first option on "Developer Default" and click on "Next" |
|
1929 |
|
2. Click on "Next" even if you don't meet all the requirements |
|
1930 |
|
3. Click on "Execute". The system will download and install several softwares (this may take some time). |
|
1931 |
|
4. Click on "Next" twice, leave "Type and Networking" on "Standalone MySQL Server / Classic MySQL Replication" and click "Next", and leave the next options as they are (unless you know what you do and want to change the port, for instance) and click on "Next". |
|
1932 |
|
5. \label{choose-pswd} You now need to choose a password for the MySQL root account. |
|
1933 |
|
It can be anything, just make sure to memorize it. |
|
1934 |
|
Click on "Next". |
|
1935 |
|
6. On the "Windows Service" page, leave everything as it is and click on "Next". |
|
1936 |
|
7. On the "Plugins and Extensions" page, leave everything as it is and click on "Next". |
|
1937 |
|
8. Finally, click "Execute" on the "Apply Configuration" page, and then on "Finish". |
|
1938 |
|
9. Click on "Cancel" on the "Product Configuration" page and confirm that you don't want to add products: we only need to have MySQL Server 5.7.20 configured. |
|
1939 |
|
4. We now want to make sure that MySQL is running: launch Windows' "Control Panel", then click on "Administrative Tools", and on "Services". Look for "MySQL57", its status should be "Running". If it is not, right-click on it and click on "Start". |
|
|
1957 |
|
~~~{.powershell} |
|
1958 |
|
mysql -u root -p |
|
1959 |
|
~~~ |
1940 |
1960 |
|
|
1941 |
|
- **For Linux** |
|
|
1961 |
|
and enter the password you picked previously for the root account. |
|
1962 |
|
You are now logged as root in your database management system, you should see a brief message, followed by a prompt |
|
1963 |
|
|
|
1964 |
|
~~~{.powershell} |
|
1965 |
|
mysql > |
|
1966 |
|
~~~ |
1942 |
1967 |
|
|
1943 |
|
1. Install, through your standard package management system (`apt` or `aptitude` for debian-based systems, `pacman` for Arch Linux, etc.), the packages `mysql-client` and `mysql-server` as well as their dependencies. |
|
1944 |
|
2. Open a terminal and type |
|
|
1968 |
|
#### Installing MariaDB on Linux |
|
1969 |
|
|
|
1970 |
|
#. Install, through your standard package management system (`apt` or `aptitude` for debian-based systems, `pacman` for Arch Linux, etc.), the packages `mysql-client` and `mysql-server` as well as their dependencies^[Yes, the package is called `mysql-server`, but it actually install the package `mariadb-server-10.1`… So don't be confused: *we are, indeed, installing MariaDB*!]. |
|
1971 |
|
#. Open a terminal and type |
1945 |
1972 |
|
|
1946 |
1973 |
~~~{.bash} |
~~~{.bash} |
1947 |
1974 |
/etc/init.d/mysql status |
/etc/init.d/mysql status |
|
... |
... |
Click on "Next". |
1959 |
1986 |
service mysqld start |
service mysqld start |
1960 |
1987 |
~~~ |
~~~ |
1961 |
1988 |
|
|
1962 |
|
3. As root, type in your terminal |
|
|
1989 |
|
#. As root, type in your terminal |
1963 |
1990 |
|
|
1964 |
1991 |
~~~{.bash} |
~~~{.bash} |
1965 |
1992 |
mysql_secure_installation |
mysql_secure_installation |
|
... |
... |
Click on "Next". |
1967 |
1994 |
|
|
1968 |
1995 |
You'll be asked to provide the current password for the root MySQL user: this password has not be defined yet, so just hit "Enter". You'll be asked if you want to set a new password (that you can freely chose, just make sure to memorize it). Then, answer "n" to the question "Remove anonymous users?", "Y" to "Disallow root login remotely?", "n" to "Remove test database and access to it?" and finally "Y" to "Reload privilege tables now?". |
You'll be asked to provide the current password for the root MySQL user: this password has not be defined yet, so just hit "Enter". You'll be asked if you want to set a new password (that you can freely chose, just make sure to memorize it). Then, answer "n" to the question "Remove anonymous users?", "Y" to "Disallow root login remotely?", "n" to "Remove test database and access to it?" and finally "Y" to "Reload privilege tables now?". |
1969 |
1996 |
|
|
1970 |
|
#### Creating an user |
|
|
1997 |
|
#. Still as root, type in your terminal |
|
1998 |
|
|
|
1999 |
|
~~~{.bash} |
|
2000 |
|
mysql -u root -p |
|
2001 |
|
~~~ |
|
2002 |
|
|
|
2003 |
|
and enter the password you picked previously for the root account. |
|
2004 |
|
You are now logged as root in your database management system: you should see a brief message, followed by a prompt |
|
2005 |
|
|
|
2006 |
|
~~~{.bash} |
|
2007 |
|
MariaDB [(none)]> |
|
2008 |
|
~~~ |
|
2009 |
|
|
|
2010 |
|
### Creating an user |
1971 |
2011 |
|
|
1972 |
|
This step will create a non-root user and grant it some rights. Type the following three commands, one by one (that is, type the first one, hit "enter", type the second, hit "enter", etc. The colors are here to ease the reading, you can ignore them safely.): |
|
|
2012 |
|
This step will create a non-root user^[By default, MySQL and MariaDB only create a root user with all privileges and no password, but we added a password at the previous step.] and grant it some rights. Copy-and-paste or type the following three commands, one by one (that is, enter the first one, hit "enter", enter the second, hit "enter", etc.): |
|
2013 |
|
|
|
2014 |
|
We first create a new user called `testuser` on our local installation, and give it the password `password`: |
1973 |
2015 |
|
|
1974 |
2016 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
1975 |
2017 |
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password'; |
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password'; |
|
2018 |
|
~~~ |
1976 |
2019 |
|
|
|
2020 |
|
Then, we grant it all the privileges on the databases whose name starts with `HW_`: |
|
2021 |
|
|
|
2022 |
|
~~~{.sqlmysql} |
1977 |
2023 |
GRANT ALL PRIVILEGES ON `HW\_%` . * TO |
GRANT ALL PRIVILEGES ON `HW\_%` . * TO |
1978 |
2024 |
'testuser'@'localhost'; |
'testuser'@'localhost'; |
|
2025 |
|
~~~ |
|
2026 |
|
|
|
2027 |
|
Be careful: backticks (`` ` ``) are surrounding `HW\_` whereas single quotes (`'`) are surrounding `testuser` and `localhost`. |
|
2028 |
|
|
|
2029 |
|
And then we quit the DBMS, using |
1979 |
2030 |
|
|
|
2031 |
|
~~~{.sqlmysql} |
1980 |
2032 |
EXIT; |
EXIT; |
1981 |
2033 |
~~~ |
~~~ |
1982 |
2034 |
|
|
1983 |
|
The message prompted after the two first commands should be |
|
|
2035 |
|
The message displayed after the two first commands should be |
1984 |
2036 |
|
|
1985 |
2037 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
1986 |
2038 |
Query OK, 0 rows affected (0.00 sec) |
Query OK, 0 rows affected (0.00 sec) |
1987 |
2039 |
~~~ |
~~~ |
1988 |
2040 |
|
|
1989 |
|
and the message after the last command should be |
|
1990 |
|
|
|
|
2041 |
|
and the message displayed after the last command should be |
1991 |
2042 |
|
|
1992 |
2043 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
1993 |
2044 |
Bye |
Bye |
1994 |
2045 |
~~~ |
~~~ |
1995 |
2046 |
|
|
1996 |
|
Be careful: back-tics are surrounding |
|
1997 |
|
|
|
1998 |
|
~~~bat |
|
1999 |
|
HW\_% |
|
2000 |
|
~~~ |
|
2001 |
|
|
|
2002 |
|
|
|
2003 |
|
whereas single quotes are surrounding `testuser` and `localhost` (normally, copy-and-pasting from this document should work). |
|
2004 |
|
|
|
2005 |
|
#### Log-in |
|
|
2047 |
|
### Loging-in as testuser {#testuserlogin} |
2006 |
2048 |
|
|
2007 |
|
We now log as the normal user called "testuser". |
|
|
2049 |
|
We now log in as the normal user called "testuser". |
2008 |
2050 |
|
|
2009 |
2051 |
Linux users should type *as a normal user, i.e., not as root*, in their terminal the following, and Windows users should type in their command prompt the following^[Provided the working directory is still `C:\Program Files\MySQL\MySQL Server 5.7\bin`. Cf. <https://dev.mysql.com/doc/mysql-windows-excerpt/5.7/en/mysql-installation-windows-path.html> to add the MySQL bin directory to your Windows system `PATH` environment variable.]: |
Linux users should type *as a normal user, i.e., not as root*, in their terminal the following, and Windows users should type in their command prompt the following^[Provided the working directory is still `C:\Program Files\MySQL\MySQL Server 5.7\bin`. Cf. <https://dev.mysql.com/doc/mysql-windows-excerpt/5.7/en/mysql-installation-windows-path.html> to add the MySQL bin directory to your Windows system `PATH` environment variable.]: |
2010 |
2052 |
|
|
|
... |
... |
Linux users should type *as a normal user, i.e., not as root*, in their terminal |
2012 |
2054 |
mysql -u testuser -p |
mysql -u testuser -p |
2013 |
2055 |
~~~ |
~~~ |
2014 |
2056 |
|
|
2015 |
|
Enter `password` as your password. If at some point you want to know if you are logged as root or testuser, simply enter |
|
|
2057 |
|
Enter `password` as your password. |
|
2058 |
|
If you are prompted with a message |
|
2059 |
|
|
|
2060 |
|
~~~{.bash} |
|
2061 |
|
ERROR 1045 (28000): Access denied for user 'testuser'@'localhost' (using password: YES) |
|
2062 |
|
~~~ |
|
2063 |
|
|
|
2064 |
|
then you probably typed the wrong password. |
|
2065 |
|
Otherwise, you should see a welcoming message from MySQL or MariaDB and a prompt. |
|
2066 |
|
|
|
2067 |
|
If at some point you want to know if you are logged as root or testuser, simply enter |
2016 |
2068 |
|
|
2017 |
2069 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
2018 |
2070 |
\s; |
\s; |
2019 |
2071 |
~~~ |
~~~ |
2020 |
2072 |
|
|
2021 |
|
#### Creating our first database |
|
|
2073 |
|
### Creating our first database |
|
2074 |
|
|
|
2075 |
|
Now, let us create our first schema, our first table, populate it with data, and display various information. |
2022 |
2076 |
|
|
2023 |
|
Now, type the following commands one by one: |
|
|
2077 |
|
We first create the schema (or database), `HW_FirstTest`: |
2024 |
2078 |
|
|
2025 |
2079 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
2026 |
|
CREATE DATABASE HW_1Q2; |
|
2027 |
|
USE HW_1Q2; |
|
2028 |
|
CREATE TABLE t(i integer, n integer); |
|
2029 |
|
INSERT INTO t VALUES (1,1),(2,2),(3,3); |
|
2030 |
|
SELECT * FROM t; |
|
|
2080 |
|
CREATE DATABASE HW_FirstTest; |
2031 |
2081 |
~~~ |
~~~ |
2032 |
2082 |
|
|
2033 |
|
After that last command, you should see |
|
|
2083 |
|
Let us make sure that we created it: |
2034 |
2084 |
|
|
2035 |
2085 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
2036 |
|
+------+------+ |
|
2037 |
|
| i | n | |
|
2038 |
|
+------+------+ |
|
2039 |
|
| 1 | 1 | |
|
2040 |
|
| 2 | 2 | |
|
2041 |
|
| 3 | 3 | |
|
2042 |
|
+------+------+ |
|
|
2086 |
|
SHOW DATABASES; |
2043 |
2087 |
~~~ |
~~~ |
2044 |
2088 |
|
|
2045 |
|
You're all set! All you have to do is to quit, using the command |
|
|
2089 |
|
Let us use it: |
2046 |
2090 |
|
|
2047 |
2091 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |
2048 |
|
EXIT; |
|
|
2092 |
|
USE HW_FirstTest; |
2049 |
2093 |
~~~ |
~~~ |
2050 |
2094 |
|
|
2051 |
|
### Creating, listind and dropping databases |
|
|
2095 |
|
And see what it contains now: |
2052 |
2096 |
|
|
2053 |
|
Problem -.+.#creatingSchema |
|
|
2097 |
|
~~~{.sqlmysql} |
|
2098 |
|
SHOW TABLES; |
|
2099 |
|
~~~ |
2054 |
2100 |
|
|
2055 |
|
*This exercise assume you successfully completed @problem:installation.* |
|
|
2101 |
|
We now create a table called `TableTest`: |
2056 |
2102 |
|
|
2057 |
|
#. Connect to your MySQL DBMS as `testuser`: |
|
2058 |
|
- **In windows**, open a command prompt (search for "cmd") and type |
|
|
2103 |
|
~~~{.sqlmysql} |
|
2104 |
|
CREATE TABLE TableTest (Attribute1 INT, Attribute2 INT); |
|
2105 |
|
~~~ |
2059 |
2106 |
|
|
2060 |
|
~~~{.bash} |
|
2061 |
|
cd "C:\Program Files\MySQL\MySQL Server 5.7\bin" |
|
2062 |
|
~~~ |
|
|
2107 |
|
And can make sure that it was indeed created: |
2063 |
2108 |
|
|
2064 |
|
- **In Linux**, open a shell (as a normal user) |
|
|
2109 |
|
~~~{.sqlmysql} |
|
2110 |
|
SHOW TABLES; |
|
2111 |
|
~~~ |
2065 |
2112 |
|
|
2066 |
|
Then, in both cases, type |
|
|
2113 |
|
We can further ask our DBMS to display the structure of the table we just created: |
2067 |
2114 |
|
|
2068 |
|
~~~{.text} |
|
2069 |
|
mysql -u testuser -p |
|
2070 |
|
~~~ |
|
|
2115 |
|
~~~{.sqlmysql} |
|
2116 |
|
DESCRIBE TableTest; -- Can be abbreviated as DESC TableTest; |
|
2117 |
|
~~~ |
2071 |
2118 |
|
|
2072 |
|
and enter the password `password`. |
|
2073 |
|
If you are prompted with a message |
|
|
2119 |
|
And even ask to get back the code that would create the exact same structure: |
2074 |
2120 |
|
|
2075 |
|
~~~{.bash} |
|
2076 |
|
ERROR 1045 (28000): Access denied for user 'testuser'@'localhost' (using password: YES) |
|
2077 |
|
~~~ |
|
|
2121 |
|
~~~{.sqlmysql} |
|
2122 |
|
SHOW CREATE TABLE TableTest; |
|
2123 |
|
~~~ |
2078 |
2124 |
|
|
2079 |
|
then you probably typed the wrong password. |
|
2080 |
|
Otherwise, you should see a welcoming message from MySQL / MariaDB and a prompt. |
|
|
2125 |
|
Now, let us populate it with some data: |
2081 |
2126 |
|
|
2082 |
|
#. Create a new database called `HW_2Q1` |
|
|
2127 |
|
~~~{.sqlmysql} |
|
2128 |
|
INSERT INTO TableTest |
|
2129 |
|
VALUES (1,2), |
|
2130 |
|
(3,4), |
|
2131 |
|
(5,6); |
|
2132 |
|
~~~ |
2083 |
2133 |
|
|
2084 |
|
~~~{.sqlmysql} |
|
2085 |
|
CREATE DATABASE HW_2Q1; |
|
2086 |
|
~~~ |
|
|
2134 |
|
Note that the `SQL` syntax and your DBMS are completely fine with your statement spreading over multiple lines. |
|
2135 |
|
Let us now display the data stored in the table: |
2087 |
2136 |
|
|
2088 |
|
#. List the databases |
|
2089 |
|
|
|
2090 |
|
~~~{.sqlmysql} |
|
2091 |
|
SHOW DATABASES; |
|
2092 |
|
~~~ |
|
2093 |
|
|
|
2094 |
|
Make sure `HW_2Q1` is a part of it. |
|
|
2137 |
|
~~~{.sqlmysql} |
|
2138 |
|
SELECT * FROM TableTest; |
|
2139 |
|
~~~ |
2095 |
2140 |
|
|
2096 |
|
4. Remove ("drop") the database, using |
|
2097 |
|
|
|
2098 |
|
~~~{.sqlmysql} |
|
2099 |
|
DROP DATABASE HW_2Q1; |
|
2100 |
|
~~~ |
|
|
2141 |
|
After that last command, you should see |
|
2142 |
|
|
|
2143 |
|
~~~{.sqlmysql} |
|
2144 |
|
+------------+------------+ |
|
2145 |
|
| Attribute1 | Attribute2 | |
|
2146 |
|
+------------+------------+ |
|
2147 |
|
| 1 | 2 | |
|
2148 |
|
| 3 | 4 | |
|
2149 |
|
| 5 | 6 | |
|
2150 |
|
+------------+------------+ |
|
2151 |
|
~~~ |
|
2152 |
|
|
|
2153 |
|
|
|
2154 |
|
Finally, we can erasre the content of the table, then erase ("drop") the table, and finally the schema: |
|
2155 |
|
|
|
2156 |
|
~~~{.sqlmysql} |
|
2157 |
|
DELETE FROM TableTest; -- Delete the rows |
|
2158 |
|
DROP TABLE TableTest; -- Delete the table |
|
2159 |
|
DROP DATABASE HW_FirstTest; -- Delete the schema |
|
2160 |
|
~~~ |
2101 |
2161 |
|
|
2102 |
|
In the future, we will refer to the commands 1 and 2 as "log-in as `testuser` and create a database `HW_2Q1`". |
|
|
2162 |
|
You're all set! All you have to do is to quit, using the command |
2103 |
2163 |
|
|
2104 |
|
### Reading the documentation |
|
|
2164 |
|
~~~{.sqlmysql} |
|
2165 |
|
EXIT; |
|
2166 |
|
~~~ |
|
2167 |
|
|
|
2168 |
|
## Problems {-} |
2105 |
2169 |
|
|
2106 |
|
Problem -.+.#sqldoc |
|
|
2170 |
|
Problem (Discovering the documentation) +.#sqldoc |
2107 |
2171 |
|
|
2108 |
|
The goal of this problem is to learn where to find the documentation of your DBMS, and to have a first look at the syntax of SQL commands. |
|
|
2172 |
|
The goal of this problem is to learn where to find the documentation for your DBMS, and to understand how to read the syntax of SQL commands. |
2109 |
2173 |
|
|
2110 |
|
You can consult your textbook, [@Textbook6, Table 5.2, p. 140] or [@Textbook7, Table 7.2, p. 235], for a very quick summary of the most common commands. |
|
|
2174 |
|
You can consult your textbook, [@Textbook6, Table 5.2, p. 140] or [@Textbook7, Table 7.2, p. 235], for a very quick summary of the most common commands. |
2111 |
2175 |
Make sure you are familiar with the Backus–Naur form (BNF) notation commonly used: |
Make sure you are familiar with the Backus–Naur form (BNF) notation commonly used: |
2112 |
2176 |
|
|
2113 |
2177 |
- non-terminal symbols (i.e., variables, parameters) are enclosed in angled brackets, |
- non-terminal symbols (i.e., variables, parameters) are enclosed in angled brackets, |
|
... |
... |
Make sure you are familiar with the Backus–Naur form (BNF) notation commonly u |
2134 |
2198 |
(…|…|…) |
(…|…|…) |
2135 |
2199 |
~~~ |
~~~ |
2136 |
2200 |
|
|
2137 |
|
The most complete lists of commands are probably at |
|
|
2201 |
|
The most complete lists of commands are probably at |
2138 |
2202 |
|
|
2139 |
|
- <https://mariadb.com/kb/en/library/sql-statements/> and |
|
2140 |
|
- <https://dev.mysql.com/doc/refman/5.7/en/sql-syntax.html> |
|
|
2203 |
|
- <https://mariadb.com/kb/en/library/sql-statements/> and |
|
2204 |
|
- <https://dev.mysql.com/doc/refman/8.0/en/sql-syntax.html> |
2141 |
2205 |
|
|
2142 |
2206 |
Those are the commands implemented in the DBMS you are actually using. |
Those are the commands implemented in the DBMS you are actually using. |
2143 |
2207 |
Since there are small variations from one implementation to the other, it's better to take one of this link as a reference in the future. |
Since there are small variations from one implementation to the other, it's better to take one of this link as a reference in the future. |
2144 |
2208 |
|
|
2145 |
|
### Creating and using a table |
|
2146 |
|
|
|
2147 |
|
Problem -.+.#address |
|
2148 |
2209 |
|
|
2149 |
|
*This exercise, and the following ones, assume you successfully completed @problem:installation.* |
|
|
2210 |
|
Problem (SQL -- Creating and using a simple table) +.#address |
2150 |
2211 |
|
|
2151 |
|
Log in as `testuser` and create a database `HW_2Q3`. |
|
2152 |
|
|
|
2153 |
|
#### Setting up the meta-data |
|
2154 |
|
|
|
2155 |
|
We will first set-up our meta-data (i.e., the schema, the structure where our tables will be, and the tables themselves). |
|
2156 |
|
|
|
2157 |
|
Let us first tell MySQL that we want to use that database |
|
2158 |
|
|
|
2159 |
|
~~~{.sqlmysql} |
|
2160 |
|
USE HW_2Q3; |
|
2161 |
|
~~~ |
|
2162 |
|
|
|
2163 |
|
and ask what it contains |
|
2164 |
|
|
|
2165 |
|
~~~{.sqlmysql} |
|
2166 |
|
SHOW TABLES; |
|
2167 |
|
~~~ |
|
2168 |
|
|
|
2169 |
|
Now, create a first table named `NAME` |
|
|
2212 |
|
Question -.# |
|
2213 |
|
~ Log in as `testuser`, create a database named `HW_Address`, use it, and create two tables: |
2170 |
2214 |
|
|
2171 |
|
~~~{.sqlmysql} |
|
|
2215 |
|
~~~{.sqlmysql .numberLines} |
2172 |
2216 |
CREATE TABLE NAME( |
CREATE TABLE NAME( |
2173 |
2217 |
FName VARCHAR(15), |
FName VARCHAR(15), |
2174 |
2218 |
LName VARCHAR(15), |
LName VARCHAR(15), |
2175 |
2219 |
Id INT, |
Id INT, |
2176 |
2220 |
PRIMARY KEY(Id) |
PRIMARY KEY(Id) |
2177 |
2221 |
); |
); |
2178 |
|
~~~ |
|
2179 |
|
|
|
2180 |
|
Note that the `SQL` syntax and your DBMS are completely fine with your statement spreading over multiple lines. |
|
2181 |
|
Let us create a second table, named `ADDRESS` |
|
2182 |
2222 |
|
|
2183 |
|
~~~{.sqlmysql} |
|
2184 |
2223 |
CREATE TABLE ADDRESS( |
CREATE TABLE ADDRESS( |
2185 |
2224 |
StreetName VARCHAR(15), |
StreetName VARCHAR(15), |
2186 |
2225 |
Number INT, |
Number INT, |
|
... |
... |
CREATE TABLE ADDRESS( |
2189 |
2228 |
); |
); |
2190 |
2229 |
~~~ |
~~~ |
2191 |
2230 |
|
|
2192 |
|
To make sure those two tables were actually created, we can use |
|
2193 |
|
|
|
2194 |
|
~~~{.sqlmysql} |
|
2195 |
|
SHOW TABLES; |
|
2196 |
|
~~~ |
|
2197 |
|
|
|
2198 |
|
But how to make sure that you entered the attributes correctly? |
|
2199 |
|
One way to make sure is to enter the command |
|
2200 |
|
|
|
2201 |
|
~~~{.sqlmysql} |
|
2202 |
|
SHOW CREATE TABLE NAME; |
|
2203 |
|
~~~ |
|
2204 |
|
|
|
2205 |
|
or |
|
2206 |
|
|
|
2207 |
|
~~~{.sqlmysql} |
|
2208 |
|
DESC ADDRESS; |
|
2209 |
|
~~~ |
|
2210 |
|
|
|
2211 |
|
and to examine carefully the message printed. You should read |
|
2212 |
|
|
|
2213 |
|
~~~{.plain} |
|
2214 |
|
+------------+-------------+------+-----+---------+-------+ |
|
2215 |
|
| Field | Type| Null | Key | Default | Extra | |
|
2216 |
|
+------------+-------------+------+-----+---------+-------+ |
|
2217 |
|
| StreetName | varchar(15) | NO | PRI | NULL| | |
|
2218 |
|
| Number | int(11) | NO | PRI | NULL| | |
|
2219 |
|
| Habitants | int(11) | YES | | NULL| | |
|
2220 |
|
+------------+-------------+------+-----+---------+-------+ |
|
2221 |
|
~~~ |
|
2222 |
|
|
|
2223 |
|
If you believe there is a mistake, you can erase ("drop") the *table* (not the whole database, as we did in @problem:creatingSchema) using |
|
2224 |
|
|
|
2225 |
|
~~~{.sqlmysql} |
|
2226 |
|
DROP TABLE ADDRESS; |
|
2227 |
|
~~~ |
|
|
2231 |
|
Question -.# |
|
2232 |
|
~ Observe the output produced by the command |
2228 |
2233 |
|
|
2229 |
|
And then re-create it. Of course, you can do the same for the `NAME` table. |
|
2230 |
|
|
|
2231 |
|
Now, let us add a foreign key to the `ADDRESS` table: |
|
2232 |
|
|
|
2233 |
|
~~~{.sqlmysql} |
|
2234 |
|
ALTER TABLE ADDRESS |
|
2235 |
|
ADD FOREIGN KEY (Habitants) |
|
2236 |
|
REFERENCES NAME(Id); |
|
2237 |
|
~~~ |
|
|
2234 |
|
~~~{.sqlmysql} |
|
2235 |
|
DESC ADDRESS; |
|
2236 |
|
~~ |
|
2237 |
|
|
2238 |
2238 |
|
|
2239 |
|
And observe the modification: |
|
|
2239 |
|
Question -.# |
|
2240 |
|
~ Add a foreign key to the `ADDRESS` table, using |
2240 |
2241 |
|
|
2241 |
|
~~~{.sqlmysql} |
|
2242 |
|
DESC ADDRESS; |
|
2243 |
|
~~~ |
|
|
2242 |
|
~~~{.sqlmysql} |
|
2243 |
|
ALTER TABLE ADDRESS |
|
2244 |
|
ADD FOREIGN KEY (Habitants) |
|
2245 |
|
REFERENCES NAME(Id); |
|
2246 |
|
~~~ |
2244 |
2247 |
|
|
2245 |
|
#### Inserting data |
|
|
2248 |
|
And observe the new output produced by the command |
2246 |
2249 |
|
|
2247 |
|
This second part is about data, i.e., filling our tables with actual information. |
|
|
2250 |
|
~~~{.sqlmysql} |
|
2251 |
|
DESC ADDRESS; |
|
2252 |
|
~~~ |
2248 |
2253 |
|
|
2249 |
|
We begin by adding some data in the `NAME` table: |
|
|
2254 |
|
Is it what you would have expected? How informative is it? Can you think of a command that would output more detailled information? |
2250 |
2255 |
|
|
2251 |
|
~~~{.sqlmysql} |
|
2252 |
|
INSERT INTO NAME VALUES ('Barbara', 'Liskov', 003); |
|
2253 |
|
INSERT INTO NAME VALUES ('Tuong Lu', 'Kim', 004); |
|
2254 |
|
INSERT INTO NAME VALUES ('Samantha', NULL, 080); |
|
2255 |
|
~~~ |
|
|
2256 |
|
Question -.# |
|
2257 |
|
~ Draw the relations corresponding to that database, including the primary, as well as foreign, keys. |
2256 |
2258 |
|
|
2257 |
|
To display the data we just inserted, we can use: |
|
|
2259 |
|
Question -.# |
|
2260 |
|
~ Add some data in the `NAME` table: |
2258 |
2261 |
|
|
2259 |
|
~~~{.sqlmysql} |
|
2260 |
|
SELECT * FROM NAME; |
|
2261 |
|
~~~ |
|
|
2262 |
|
~~~{.sqlmysql} |
|
2263 |
|
INSERT INTO NAME VALUES ('Barbara', 'Liskov', 003); |
|
2264 |
|
INSERT INTO NAME VALUES ('Tuong Lu', 'Kim', 004); |
|
2265 |
|
INSERT INTO NAME VALUES ('Samantha', NULL, 080); |
|
2266 |
|
~~~ |
2262 |
2267 |
|
|
2263 |
|
Do you notice anything regarding the values we entered for the `Id` attribute? |
|
|
2268 |
|
What command can you use to display this infomation back? |
|
2269 |
|
Do you notice anything regarding the values we entered for the `Id` attribute? |
2264 |
2270 |
|
|
2265 |
|
We can add some data into the `ADDRESS` table (using a different syntax): |
|
2266 |
|
|
|
2267 |
|
~~~{.sqlmysql} |
|
2268 |
|
INSERT INTO ADDRESS (StreetName, Number, Habitants) |
|
2269 |
|
VALUES |
|
2270 |
|
('Armstrong Drive', 10019, 003), |
|
2271 |
|
('North Broad St.', 23, 004), |
|
2272 |
|
('Robert Lane', 120, NULL); |
|
2273 |
|
~~~ |
|
2274 |
|
|
|
2275 |
|
And let's use our first update command: |
|
2276 |
|
|
|
2277 |
|
~~~{.sqlmysql} |
|
2278 |
|
UPDATE ADDRESS SET Habitants = 3 WHERE Number = 120; |
|
2279 |
|
~~~ |
|
2280 |
|
|
|
2281 |
|
Now, answer the following questions. |
|
2282 |
|
|
|
2283 |
2271 |
Question -.# |
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/>. |
|
|
2272 |
|
~ Add some data into the `ADDRESS` table: |
2285 |
2273 |
|
|
2286 |
|
Question -.# |
|
2287 |
|
~ Draw the relations corresponding to that database, including the domains and primary, as well as foreign, keys. |
|
|
2274 |
|
~~~{.sqlmysql} |
|
2275 |
|
INSERT INTO ADDRESS |
|
2276 |
|
VALUES |
|
2277 |
|
('Armstrong Drive', 10019, 003), |
|
2278 |
|
('North Broad St.', 23, 004), |
|
2279 |
|
('Robert Lane', 120, NULL); |
|
2280 |
|
~~~ |
|
2281 |
|
|
|
2282 |
|
What difference do you note with the insertions we made in the `NAME` table. Which syntax seem more easy to you? |
2288 |
2283 |
|
|
2289 |
2284 |
Question -.# |
Question -.# |
2290 |
2285 |
~ Write a `SELECT` statement that returns the `Id` number of the person whose first name is "Samantha". |
~ Write a `SELECT` statement that returns the `Id` number of the person whose first name is "Samantha". |
|
... |
... |
Question -.# |
2298 |
2293 |
Question -.# |
Question -.# |
2299 |
2294 |
~ Write a statement that violate another kind of constraint. Explain what constraint you are violating, and explain the error message. |
~ Write a statement that violate another kind of constraint. Explain what constraint you are violating, and explain the error message. |
2300 |
2295 |
|
|
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 |
2296 |
|
|
2312 |
|
|
|
2313 |
|
### Repetiting tuples in MySQL |
|
2314 |
|
|
|
2315 |
|
Problem +.#repetition |
|
|
2297 |
|
Problem (Repetiting tuples in MySQL) +.-.#repetition |
2316 |
2298 |
|
|
2317 |
2299 |
Log in as `testuser` and create a database `HW_2Q4`. Tell MySQL that you want to use that database, and create a table (I will assume you named it `EXAMPLE` in the following, but you are free to name it the way you want) with at least two attributes that have different data types. Don't declare a primary key yet. Answer the following: |
Log in as `testuser` and create a database `HW_2Q4`. Tell MySQL that you want to use that database, and create a table (I will assume you named it `EXAMPLE` in the following, but you are free to name it the way you want) with at least two attributes that have different data types. Don't declare a primary key yet. Answer the following: |
2318 |
2300 |
|
|
|
... |
... |
Can you provide a *relational database schema* (no need to write the `SQL` code, |
2624 |
2606 |
|
|
2625 |
2607 |
## Solution to Selected Problems |
## Solution to Selected Problems |
2626 |
2608 |
|
|
2627 |
|
### |
|
|
2609 |
|
Solution for @problem:address |
|
2610 |
|
|
|
2611 |
|
Question -.# |
|
2612 |
|
~ We simply log-in as indicated in the ["Loging-in as testuser"](#testuserlogin) Section. |
|
2613 |
|
Then, we enter |
|
2614 |
|
|
|
2615 |
|
~~~{.mysql .numberLines} |
|
2616 |
|
CREATE DATABASE HW_Address; |
|
2617 |
|
USE HW_Address; |
|
2618 |
|
~~~ |
|
2619 |
|
|
|
2620 |
|
We then create the tables as in the problem. |
|
2621 |
|
|
|
2622 |
|
Question -.# |
|
2623 |
|
~ \ |
|
2624 |
|
|
|
2625 |
|
~~~{.bash} |
|
2626 |
|
MariaDB [HW_Address]> DESC ADDRESS; |
|
2627 |
|
+------------+-------------+------+-----+---------+-------+ |
|
2628 |
|
| Field | Type | Null | Key | Default | Extra | |
|
2629 |
|
+------------+-------------+------+-----+---------+-------+ |
|
2630 |
|
| StreetName | varchar(15) | NO | PRI | NULL | | |
|
2631 |
|
| Number | int(11) | NO | PRI | NULL | | |
|
2632 |
|
| Habitants | int(11) | YES | | NULL | | |
|
2633 |
|
+------------+-------------+------+-----+---------+-------+ |
|
2634 |
|
~~~ |
|
2635 |
|
|
|
2636 |
|
Question -.# |
|
2637 |
|
~ We add the foreign key, then |
|
2638 |
|
|
|
2639 |
|
~~~{.bash} |
|
2640 |
|
MariaDB [HW_Address]> DESC ADDRESS; |
|
2641 |
|
+------------+-------------+------+-----+---------+-------+ |
|
2642 |
|
| Field | Type | Null | Key | Default | Extra | |
|
2643 |
|
+------------+-------------+------+-----+---------+-------+ |
|
2644 |
|
| StreetName | varchar(15) | NO | PRI | NULL | | |
|
2645 |
|
| Number | int(11) | NO | PRI | NULL | | |
|
2646 |
|
| Habitants | int(11) | YES | MUL | NULL | | |
|
2647 |
|
+------------+-------------+------+-----+---------+-------+ |
|
2648 |
|
~~~ |
|
2649 |
|
|
|
2650 |
|
The only difference is the `MUL` value, which is a bit surprising: quoting <https://dev.mysql.com/doc/refman/8.0/en/show-columns.html>, |
|
2651 |
|
|
|
2652 |
|
> If Key is MUL, the column is the first column of a nonunique index in which multiple occurrences of a given value are permitted within the column. |
|
2653 |
|
|
|
2654 |
|
In other word, this doesn't really carry any information about the fact that `ADDRESS.Habitants` is now a foreign key referencing `NAME.Id`. |
|
2655 |
|
A way of displaying information about that foreign key is using `SHOW CREATE TABLE`: |
|
2656 |
|
|
|
2657 |
|
~~~{.bash} |
|
2658 |
|
MariaDB [HW_Address]> SHOW CREATE TABLE ADDRESS; |
|
2659 |
|
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
|
2660 |
|
| Table | Create Table | |
|
2661 |
|
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
|
2662 |
|
| ADDRESS | CREATE TABLE `ADDRESS` ( |
|
2663 |
|
`StreetName` varchar(15) NOT NULL, |
|
2664 |
|
`Number` int(11) NOT NULL, |
|
2665 |
|
`Habitants` int(11) DEFAULT NULL, |
|
2666 |
|
PRIMARY KEY (`StreetName`,`Number`), |
|
2667 |
|
KEY `Habitants` (`Habitants`), |
|
2668 |
|
CONSTRAINT `ADDRESS_ibfk_1` FOREIGN KEY (`Habitants`) REFERENCES `NAME` (`Id`) |
|
2669 |
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 | |
|
2670 |
|
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
|
2671 |
|
1 row in set (0.01 sec) |
|
2672 |
|
~~~ |
|
2673 |
|
|
|
2674 |
|
Question -.# |
|
2675 |
|
~ \ |
|
2676 |
|
|
|
2677 |
|
{ width=100% } |
|
2681 |
|
\ |
|
2682 |
|
|
|
2683 |
|
Question -.# |
|
2684 |
|
~ \ |
|
2685 |
|
|
|
2686 |
|
~~~{.sqlmysql} |
|
2687 |
|
SELECT * FROM NAME; |
|
2688 |
|
~~~ |
|
2689 |
|
|
|
2690 |
|
The `Id` attributes lost their [leading zeros](https://en.wikipedia.org/wiki/Leading_zero). |
|
2691 |
|
|
|
2692 |
|
Question -.# |
|
2693 |
|
~ This syntax is better for "bulk insertion", since it allows to write less command, and to focus on the data to insert. |
|
2694 |
|
|
|
2695 |
|
Question -.# |
|
2696 |
|
~ \ |
|
2697 |
|
|
|
2698 |
|
~~~{.sqlmysql} |
|
2699 |
|
SELECT Id FROM NAME WHERE FName = 'Samantha'; |
|
2700 |
|
~~~ |
|
2701 |
|
|
|
2702 |
|
Question -.# |
|
2703 |
|
~ \ |
|
2704 |
|
|
|
2705 |
|
~~~{.sqlmysql} |
|
2706 |
|
INSERT INTO NAME VALUES ('Maria', 'Kashi', NULL); |
|
2707 |
|
~~~ |
|
2708 |
|
|
|
2709 |
|
returns |
|
2710 |
|
|
|
2711 |
|
~~~{.bash} |
|
2712 |
|
ERROR 1048 (23000): Column 'Id' cannot be null |
|
2713 |
|
~~~ |
|
2714 |
|
|
|
2715 |
|
Another way of violating the entity integrity constraint is |
|
2716 |
|
|
|
2717 |
|
~~~{.sqlmysql} |
|
2718 |
|
INSERT INTO NAME VALUES ('Maria', 'Kashi', 80); |
|
2719 |
|
~~~ |
|
2720 |
|
|
|
2721 |
|
which returns |
|
2722 |
|
|
|
2723 |
|
~~~{.bash} |
|
2724 |
|
ERROR 1062 (23000): Duplicate entry '80' for key 'PRIMARY' |
|
2725 |
|
~~~ |
|
2726 |
|
|
|
2727 |
|
Question -.# |
|
2728 |
|
~ \ |
|
2729 |
|
|
|
2730 |
|
~~~{.sqlmysql} |
|
2731 |
|
UPDATE ADDRESS SET Habitants = 340 WHERE Number = 120; |
|
2732 |
|
~~~ |
|
2733 |
|
|
|
2734 |
|
returns |
|
2735 |
|
|
|
2736 |
|
~~~{.bash} |
|
2737 |
|
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`HW_Address`.`ADDRESS`, CONSTRAINT `ADDRESS_ibfk_1` FOREIGN KEY (`Habitants`) REFERENCES `NAME` (`Id`)) |
|
2738 |
|
~~~ |
|
2739 |
|
|
|
2740 |
|
Question -.# |
|
2741 |
|
~ \ |
|
2742 |
|
|
|
2743 |
|
~~~{.sqlmysql} |
|
2744 |
|
INSERT INTO NAME VALUE ('Hi'); |
|
2745 |
|
~~~ |
|
2746 |
|
|
|
2747 |
|
returns |
|
2748 |
|
|
|
2749 |
|
~~~{.bash} |
|
2750 |
|
ERROR 1136 (21S01): Column count doesn't match value count at row 1 |
|
2751 |
|
~~~ |
|
2752 |
|
|
|
2753 |
|
I was violating an implicit constraint, trying to insert a row with fewer values than there are attributes in the table. Note that |
|
2754 |
|
|
|
2755 |
|
~~~{.sqlmysql} |
|
2756 |
|
INSERT INTO ADDRESS VALUES ('Maria', 'Random', 98); |
|
2757 |
|
~~~ |
|
2758 |
|
|
|
2759 |
|
is a violation of explicit constraint, which is that the value must match the domain (i.e., datatype) of the attribute where they are inserted. However, MySQL or MariaDB doesn't return an error, and simply replace `'Random'` with `0`. |
|
2760 |
|
|
|
2761 |
|
|
2628 |
2762 |
Solution for @problem:repetition |
Solution for @problem:repetition |
2629 |
2763 |
~ |
~ |
2630 |
2764 |
|
|
|
... |
... |
compagny name &: String\\ |
4129 |
4263 |
\end{tikzpicture} |
\end{tikzpicture} |
4130 |
4264 |
|
|
4131 |
4265 |
|
|
4132 |
|
|
|
4133 |
4266 |
Problem +.# |
Problem +.# |
4134 |
4267 |
~ Consider the relation |
~ Consider the relation |
4135 |
4268 |
|
|
|
... |
... |
Problem +.# |
4137 |
4270 |
|
|
4138 |
4271 |
and the following functional dependencies: |
and the following functional dependencies: |
4139 |
4272 |
|
|
4140 |
|
**Bogue** |
|
4141 |
|
|
|
4142 |
4273 |
||| |
||| |
4143 |
4274 |
---: | :---: | --- | |
---: | :---: | --- | |
4144 |
4275 |
\{Zip, Brand\} | → | \{Phone\} |
\{Zip, Brand\} | → | \{Phone\} |
|
... |
... |
Problem +.# |
4146 |
4277 |
\{ Brand\} | → | \{Website\} |
\{ Brand\} | → | \{Website\} |
4147 |
4278 |
\{Phone\} | → | \{Call\_center\} |
\{Phone\} | → | \{Call\_center\} |
4148 |
4279 |
|
|
|
4280 |
|
<!-- BOGUE --> |
|
4281 |
|
|
4149 |
4282 |
Assume that \{Zip, Brand\} is the primary key. |
Assume that \{Zip, Brand\} is the primary key. |
4150 |
4283 |
Normalize this relation to the second normal form, and *then* to the third normal form. |
Normalize this relation to the second normal form, and *then* to the third normal form. |
4151 |
4284 |
Give the relations, their primary keys, and functional dependencies for both steps. |
Give the relations, their primary keys, and functional dependencies for both steps. |