File notes/code/sql/HW_FACULTY.sql added (mode: 100644) (index 0000000..81becd9) |
|
1 |
|
/* code/sql/HW_FACULTY.sql */ |
|
2 |
|
|
|
3 |
|
CREATE SCHEMA HW_FACULTY; |
|
4 |
|
|
|
5 |
|
/* Or |
|
6 |
|
CREATE DATABASE HW_FACUTLY; |
|
7 |
|
*/ |
|
8 |
|
|
|
9 |
|
CREATE TABLE HW_FACULTY.PROF( |
|
10 |
|
Fname VARCHAR(15), -- No String! |
|
11 |
|
Room INT, -- shorthad for INTEGER, are also available: SMALLINT, FLOAT, REAL, DEC |
|
12 |
|
Title CHAR(3), -- fixed-length string, padded with blanks if needed |
|
13 |
|
Tenured BIT(1), |
|
14 |
|
Nice BOOLEAN, -- True / False (= 0) / Unknown |
|
15 |
|
Hiring DATE, |
|
16 |
|
Last_seen TIME, |
|
17 |
|
FavoriteFruit ENUM('apple','orange','pear'), |
|
18 |
|
PRIMARY KEY(Fname, Hiring) |
|
19 |
|
); |
|
20 |
|
|
|
21 |
|
/* Or, instead of using the fully qualified name HW_FACULTY.PROF, we could have: |
|
22 |
|
USE HW_FACULTY; |
|
23 |
|
CREATE TABLE PROF(…) |
|
24 |
|
*/ |
|
25 |
|
|
|
26 |
|
USE HW_FACULTY; |
|
27 |
|
|
|
28 |
|
INSERT INTO PROF VALUES ( |
|
29 |
|
"Clément" -- Or 'Clément', but ' " ' and "'" are neat! |
|
30 |
|
, 290 |
|
31 |
|
, 'PhD' |
|
32 |
|
, 0 |
|
33 |
|
, NULL |
|
34 |
|
, '19940101' -- Or '940101', '1994-01-01', '94/01/01' |
|
35 |
|
, '090500' -- Or '09:05:00', '9:05:0', '9:5:0', '090500' |
|
36 |
|
-- Note also the existence of DATETIME, with 'YYYY-MM-DD HH:MM:SS' |
|
37 |
|
, 'apple' |
|
38 |
|
); |
File notes/lectures_notes.md changed (mode: 100644) (index 289f2c1..6f4fa83) |
... |
... |
Type and domains are two different things in some implementations, cf. for insta |
981 |
981 |
|
|
982 |
982 |
## First Commands |
## First Commands |
983 |
983 |
|
|
984 |
|
~~~{.sqlmysql .numberLines} |
|
985 |
|
CREATE SCHEMA HW_FACULTY; |
|
986 |
|
|
|
987 |
|
/* Or |
|
988 |
|
CREATE DATABASE HW_FACUTLY; |
|
989 |
|
*/ |
|
990 |
|
|
|
991 |
|
CREATE TABLE HW_FACULTY.PROF( |
|
992 |
|
Fname VARCHAR(15), -- No String! |
|
993 |
|
Room INT, -- shorthad for INTEGER, are also available: SMALLINT, FLOAT, REAL, DEC |
|
994 |
|
Title CHAR(3), -- fixed-length string, padded with blanks if needed |
|
995 |
|
Tenured BIT(1), |
|
996 |
|
Nice BOOLEAN, -- True / False (= 0) / Unknown |
|
997 |
|
Hiring DATE, |
|
998 |
|
Last_seen TIME, |
|
999 |
|
FavoriteFruit ENUM('apple','orange','pear'), |
|
1000 |
|
PRIMARY KEY(Fname, Hiring) |
|
1001 |
|
); |
|
1002 |
|
|
|
1003 |
|
/* Or, instead of using the fully qualified name HW_FACULTY.PROF, we could have: |
|
1004 |
|
USE HW_FACULTY; |
|
1005 |
|
CREATE TABLE PROF(…) |
|
1006 |
|
*/ |
|
|
984 |
|
```{.sqlmysql .numberLines include=code/sql/HW_FACULTY.sql} |
|
985 |
|
``` |
1007 |
986 |
|
|
1008 |
|
USE HW_FACULTY; |
|
1009 |
|
|
|
1010 |
|
INSERT INTO PROF VALUES ( |
|
1011 |
|
"Clément" -- Or 'Clément', but ' " ' and "'" are neat! |
|
1012 |
|
, 290 |
|
1013 |
|
, 'PhD' |
|
1014 |
|
, 0 |
|
1015 |
|
, NULL |
|
1016 |
|
, '19940101' -- Or '940101', '1994-01-01', '94/01/01' |
|
1017 |
|
, '090500' -- Or '09:05:00', '9:05:0', '9:5:0', '090500' |
|
1018 |
|
-- Note also the existence of DATETIME, with 'YYYY-MM-DD HH:MM:SS' |
|
1019 |
|
, 'apple' |
|
1020 |
|
); |
|
1021 |
|
~~~ |
|
1022 |
987 |
|
|
1023 |
988 |
The following commands are particularly useful: |
The following commands are particularly useful: |
1024 |
989 |
|
|
1025 |
|
~~~{.sqlmysql .numberLines} |
|
|
990 |
|
~~~{.sqlmysql} |
1026 |
991 |
SHOW SCHEMAS; -- List the schema |
SHOW SCHEMAS; -- List the schema |
1027 |
992 |
SHOW TABLES; -- List the tables in a schema |
SHOW TABLES; -- List the tables in a schema |
1028 |
993 |
DESCRIBE <TableName>; -- Show the structure of a table |
DESCRIBE <TableName>; -- Show the structure of a table |
1029 |
994 |
SELECT * FROM <TableName> -- List all the rows in a table |
SELECT * FROM <TableName> -- List all the rows in a table |
1030 |
|
|
|
1031 |
995 |
DROP TABLE <TableName>; -- "Drop" (erase) a table |
DROP TABLE <TableName>; -- "Drop" (erase) a table |
1032 |
996 |
DROP SCHEMA <SchemaName>; -- Drop a schema |
DROP SCHEMA <SchemaName>; -- Drop a schema |
|
997 |
|
~~~ |
1033 |
998 |
|
|
|
999 |
|
<!-- |
1034 |
1000 |
DESCRIBE <SQL command>; -- Gives explanations as to how the SQL command is processed |
DESCRIBE <SQL command>; -- Gives explanations as to how the SQL command is processed |
1035 |
|
~~~ |
|
|
1001 |
|
--> |
1036 |
1002 |
|
|
1037 |
1003 |
--- |
--- |
1038 |
1004 |
|
|
|
... |
... |
ERROR 1045 (28000): Access denied for user 'testuser'@'localhost' (using passwor |
1973 |
1939 |
then you probably typed the wrong password. |
then you probably typed the wrong password. |
1974 |
1940 |
Otherwise, you should see a welcoming message from MySQL or MariaDB and a prompt. |
Otherwise, you should see a welcoming message from MySQL or MariaDB and a prompt. |
1975 |
1941 |
|
|
|
1942 |
|
To save yourself the hassle of typing the password, you can use |
|
1943 |
|
|
|
1944 |
|
~~~{.bash} |
|
1945 |
|
mysql -u testuser -ppassword |
|
1946 |
|
~~~ |
|
1947 |
|
|
|
1948 |
|
or |
|
1949 |
|
|
|
1950 |
|
~~~{.bash} |
|
1951 |
|
mysql -u testuser -p --password=password |
|
1952 |
|
~~~ |
|
1953 |
|
|
|
1954 |
|
|
1976 |
1955 |
If at some point you want to know if you are logged as root or testuser, simply enter |
If at some point you want to know if you are logged as root or testuser, simply enter |
1977 |
1956 |
|
|
1978 |
1957 |
~~~{.sqlmysql} |
~~~{.sqlmysql} |