MySQL for the Internet of Things

(Steven Felgate) #1
Chapter 5 ■ MySQL priMer

Listing 5-1 shows examples of several SQL commands in action using the mysql client.

Listing 5-1. Commands Using the mysql Client


$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.8-rc-log MySQL Community Server (GPL)


Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> CREATE DATABASE plant_monitoring;
Query OK, 1 row affected (0.01 sec)


mysql> CREATE TABLE plant_monitoring.plants (plant_name char(50), sensor_value int,
sensor_event timestamp);
Query OK, 0 rows affected (0.06 sec)


mysql> INSERT INTO plant_monitoring.plants VALUES ('living room', 23, NULL);
Query OK, 1 row affected (0.04 sec)


mysql> SELECT * FROM plant_monitor.plants;
+-------------+--------------+---------------------+
| plant_name | sensor_value | sensor_event |
+-------------+--------------+---------------------+
| living room | 23 | 2015-09-22 19:54:01 |
+-------------+--------------+---------------------+
1 row in set (0.01 sec)


mysql> SET @@global.server_id = 111;
Query OK, 0 rows affected (0.00 sec)


mysql>


In this example, you see DML in the form of the CREATE DATABASE and CREATE TABLE statements, DDL
in the form of the INSERT and SELECT statements, and a simple administrative command to set a global
server variable. Next you see the creation of a database and a table to store the data, the addition of a row in
the table, and finally the retrieval of the data in the table. Notice how I used capital letters for SQL command
keywords. This is a common practice and helps make the SQL commands easier to read and easier for find
user-supplied options or data.
You can exit the MySQL client by typing the command quit. On Linux and Unix systems, you can press
Ctrl+D to exit the client.

Free download pdf