Demo: Exercise 1: Installation 1610_mysql_basics1.zip
Demo: Exercise 2: “mysql” 1610_mysql_basics1.zip
mysql> CREATE DATABASE mydb;
Query OK, 1 row affected (0.01 sec)
mysql> USE mydb;
Database changed
mysql> DROP DATABASE temp_db;
Query OK, 0 rows affected (0.01 sec)
mysql> DROP DATABASE IF EXISTS temp_db;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
|Database |
+--------------------+
|information_schema |
| mydb |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> CREATE TABLE person (
-> person_id SMALLINT UNSIGNED NOT NULL,
-> first_name VARCHAR(45) NOT NULL,
-> last_name VARCHAR(45) NOT NULL,
-> PRIMARY KEY (person_id)
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.14 sec)
mysql> SHOW TABLES;
+----------------+
| Tables_in_mydb |
+----------------+
| person |
+----------------+
1 row in set (0.00 sec)
mysql> ALTER TABLE person rename to person1;
Query OK, 0 rows affected (0.06 sec)
mysql> SHOW TABLES;
+----------------------+
| Tables_in_mydb |
+----------------------+
| person1 |
+----------------------+
1 row in set (0.00 sec)
mysql> RENAME TABLE person1 TO whatever;
Query OK, 0 rows affected (0.05 sec)
mysql> SHOW TABLES;
+----------------------+
| Tables_in_mydb |
+----------------------+
| whatever |
+----------------------+
1 row in set (0.00 sec)
mysql> ALTER TABLE person CHANGE last_name surname varchar(30);
Query OK, 0 rows affected (0.62 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESCRIBE person;
+------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------------------+------+-----+---------+----------------+
| person_id | smallint(5) unsigned | NO | PRI | NULL |
| first_name | varchar(45) | NO | | NULL | |
| surname | varchar(30) | YES | | NULL | |
+------------+----------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
mysql> ALTER TABLE person ADD age smallint(3) unsigned not null;
Query OK, 0 rows affected (0.42 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESCRIBE person;
+------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------------------+------+-----+---------+----------------+
| person_id | smallint(5) unsigned | NO | PRI | NULL |
| first_name | varchar(45) | NO | | NULL | |
| surname | varchar(30) | YES | | NULL | |
| age | smallint(3) unsigned | NO | | NULL | |
+------------+----------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
mysql> ALTER TABLE person DROP first_name;
Query OK, 0 rows affected (0.25 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW TABLES;
+-------------------+
| Tables_in_temp_db |
+-------------------+
| temp_table |
+-------------------+
1 row in set (0.00 sec)
mysql> DROP TABLE temp_table;
Query OK, 0 rows affected (0.06 sec)
mysql> DROP TABLE IF EXISTS temp_table;
Query OK, 0 rows affected, 1 warning (0.12 sec)
mysql> SHOW TABLES;
Empty set (0.00 sec)
mysql> SELECT * FROM temp_db.temp_table;
+---------+---------------------+
| temp_id | temp_whatever |
+---------+---------------------+
| 1 | life is good |
| 2 | life is even better |
+---------+---------------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM mydb.student;
+------------+------------+-----------+-----+-------+
| student_id | first_name | last_name | age | grade |
+------------+------------+-----------+-----+-------+
| 1 | yuna | kim | 19 | 4 |
| 2 | kelly | jones | 22 | 5 |
+------------+------------+-----------+-----+-------+
2 rows in set (0.00 sec)
Demo: Exercise 3: Databases, Tables, Fields 1610_mysql_basics1.zip
mysql> INSERT INTO person (person_id, first_name, last_name, age)
-> VALUES (1, 'sang', 'shin', 88);
Query OK, 1 row affected (0.10 sec)
mysql> SELECT * FROM person;
+-----------+------------+-----------+-----+
| person_id | first_name | last_name | age |
+-----------+------------+-----------+-----+
| 1 | sang | shin | 88 |
+-----------+------------+-----------+-----+
1 row in set (0.00 sec)
mysql> INSERT INTO person (person_id, first_name, last_name, age)
-> VALUES
-> (2, 'kelly', 'jones', 22),
-> (3, 'jack', 'kennedy', 56),
-> (4, 'paul', 'kennedy', 34),
-> (5, 'daniel', 'song', 24),
-> (6, 'nichole', 'scott', 9);
Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> DELETE FROM person WHERE age < 10;
Query OK, 1 row affected (0.07 sec)
mysql> UPDATE person SET age = 88
-> WHERE age = 99 OR first_name = 'paul';
Query OK, 1 row affected, 2 warnings (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 2
mysql> SELECT * FROM person;
+-----------+------------+-----------+-----+
| person_id | first_name | last_name | age |
+-----------+------------+-----------+-----+
| 1 | sang | shin | 88 |
| 2 | kelly | jones | 22 |
| 3 | jack | kennedy | 56 |
| 4 | paul | kennedy | 88 |
+-----------+------------+-----------+-----+
4 rows in set (0.00 sec)
Demo: Exercise 4: INSERT/UPDATE/DELETE 1610_mysql_basics1.zip
mysql> SELECT last_name, age FROM person;
+-----------+-----+
| last_name | age |
+-----------+-----+
| shin | 88 |
| jones | 22 |
| kennedy | 56 |
| kennedy | 34 |
| song | 24 |
+-----------+-----+
5 rows in set (0.00 sec)
mysql> SELECT first_name, age FROM person
-> WHERE age > 50;
+------------+-----+
| first_name | age |
+------------+-----+
| sang | 88 |
| jack | 56 |
+------------+-----+
2 rows in set (0.00 sec)
mysql> SELECT first_name, last_name, age FROM person
-> WHERE age < 50 AND first_name LIKE '%niel';
+------------+-----------+-----+
| first_name | last_name | age |
+------------+-----------+-----+
| daniel | song | 24 |
+------------+-----------+-----+
1 row in set (0.00 sec)
mysql> SELECT last_name, age FROM person
-> ORDER BY age ASC;
+-----------+-----+
| last_name | age |
+-----------+-----+
| jones | 22 |
| song | 24 |
| kennedy | 34 |
| kennedy | 56 |
| shin | 88 |
+-----------+-----+
5 rows in set (0.00 sec)
mysql> SELECT * FROM person
-> ORDER BY age DESC;
+-----------+------------+-----------+-----+
| person_id | first_name | last_name | age |
+-----------+------------+-----------+-----+
| 1 | sang | shin | 88 |
| 3 | jack | kennedy | 56 |
| 4 | paul | kennedy | 34 |
| 5 | daniel | song | 24 |
| 2 | kelly | jones | 22 |
+-----------+------------+-----------+-----+
5 rows in set (0.00 sec)
mysql> SELECT * from person
-> ORDER BY age DESC
-> LIMIT 3;
+-----------+------------+-----------+-----+
| person_id | first_name | last_name | age |
+-----------+------------+-----------+-----+
| 1 | sang | shin | 88 |
| 3 | jack | kennedy | 56 |
| 4 | paul | kennedy | 34 |
+-----------+------------+-----------+-----+
3 rows in set (0.00 sec)
Demo: Exercise 5: SELECT 1610_mysql_basics1.zip
mysql> SELECT 3 + 6;
+-------+
| 3 + 6 |
+-------+
| 9 |
+-------+
1 row in set (0.00 sec)
mysql> SELECT 45 * (1+2);
+------------+
| 45 * (1+2) |
+------------+
| 135 |
+------------+
1 row in set (0.00 sec)
mysql> SELECT COUNT(age) FROM person;
+------------+
| COUNT(age) |
+------------+
| 5 |
+------------+
1 row in set (0.04 sec)
mysql> SELECT AVG(age) from person;
+----------+
| AVG(age) |
+----------+
| 44.8000 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT SUM(age) FROM person;
+----------+
| SUM(age) |
+----------+
| 224 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT MIN(age) FROM person;
+----------+
| MIN(age) |
+----------+
| 22 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT MAX(age) FROM person;
+----------+
| MAX(age) |
+----------+
| 88 |
+----------+
1 row in set (0.00 sec)
Demo: Exercise 6: Arithmetic Functions 1610_mysql_basics1.zip
mysql> SOURCE c:/tmp/student.sql
Query OK, 0 rows affected (0.10 sec)
Demo: Exercise 7: SQL File 1610_mysql_basics1.zip
Download course content
Download PDF and Lab Zip files