Skip to content

Commit d7dc48f

Browse files
authored
Merge pull request #47 from hsf-training/improvements-mysql-basics
Improvements in the MySQL basics chapter
2 parents 0b14ea6 + 624690f commit d7dc48f

File tree

1 file changed

+68
-3
lines changed

1 file changed

+68
-3
lines changed

_episodes/02-sql-basics.md

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,46 @@ We will first create a database named ``metadata`` in our mysql server.
4141
CREATE DATABASE metadata;
4242
```
4343

44+
> ## Case sensitivity in MySQL
45+
>In MySQL, SQL commands are case-insensitive. In other words, `CREATE DATABASE metadata` is the same as `create database metadata`.
46+
>
47+
>However, it is a common practice to write SQL commands in uppercase
48+
>to distinguish them from table and column names, which are case-sensitive.
49+
{: .callout}
50+
51+
You can list all the databases by using the command
52+
```sql
53+
SHOW DATABASES;
54+
```
55+
~~~
56+
+--------------------+
57+
| Database |
58+
+--------------------+
59+
| information_schema |
60+
| metadata |
61+
| mysql |
62+
| performance_schema |
63+
| sys |
64+
+--------------------+
65+
~~~
66+
{: .output}
67+
68+
It shows that the database "metadata" is created.
69+
70+
> ## What are those other databases?
71+
>
72+
>By default, MySQL comes with several databases that serve specific purposes. We will not go into details of each database,
73+
>but here is a brief overview:
74+
>
75+
> - `mysql`: This database contains user account information and privileges
76+
> - `information_schema`: Contains metadata about all the other databases in the MySQL server
77+
> - `performance_schema`: Contains performance metrics for the MySQL server
78+
> - `sys`: Used for tuning and diagnosis use cases
79+
>
80+
> You can read more about these databases in the [MySQL documentation](https://dev.mysql.com/doc/refman/8.0/en/information-schema.html).
81+
> For now it is not necessary to understand them in detail.
82+
{: .callout}
83+
4484
To work with a specific database, you can use the USE command. For instance, to select the "metadata" database:
4585
```sql
4686
USE metadata;
@@ -85,9 +125,16 @@ CREATE TABLE dataset (
85125
collision_energy INT NOT NULL
86126
);
87127
```
128+
129+
Notice we have defined `id` as the primary key with the `AUTO_INCREMENT` attribute.
130+
This means that the `id` column will automatically generate a unique value for each new record added to the table.
131+
132+
The `PRIMARY KEY` means that the `id` column will be the unique identifier for each record in the table. Additionally,
133+
a primary key enhances the performance of cross-referencing between tables (we will look at this in more detail in the next episode).
134+
88135
You can see the table and corresponding columns by using the command
89136
```sql
90-
SHOW COLUMNS FROM dataset;
137+
DESCRIBE dataset;
91138
```
92139
~~~
93140
+------------------+--------------+------+-----+---------+----------------+
@@ -185,7 +232,10 @@ SELECT filename FROM dataset WHERE run_number > 50 AND collision_type='pp';
185232
{: .challenge}
186233
187234
## UPDATE
188-
The UPDATE command is used to make changes to existing record. For example, if you want to update the "collision_type" and "collision_energy" for a specific record, you can use:
235+
236+
The UPDATE command is used to make changes to existing record.
237+
238+
For example, if you want to update the "collision_type" and "collision_energy" for a specific record, you can use:
189239
190240
```sql
191241
UPDATE dataset
@@ -219,8 +269,23 @@ WHERE filename = 'expx.myfile1.root';
219269
{: .challenge}
220270
221271
## DELETE
222-
The DELETE command is used to remove record from a table. To delete a record with a specific filename, you can use:
272+
273+
The DELETE command is used to remove a row from a table.
274+
275+
For example, to delete a record with a specific filename you can use:
223276
```sql
224277
DELETE FROM dataset
225278
WHERE filename = 'expx.myfile2.root';
226279
```
280+
281+
>## Be careful with UPDATE and DELETE without WHERE!
282+
>Very important: if you omit the `WHERE` clause in an `UPDATE` or `DELETE` statement, you will update or delete ALL records in the table!
283+
>
284+
> For example, the following command
285+
> ```sql
286+
> DELETE FROM dataset;
287+
> ```
288+
> will delete all records in the `dataset` table.
289+
>
290+
>This can have unintended consequences, so be cautious when using these commands.
291+
{: .callout}

0 commit comments

Comments
 (0)