diff --git a/【練習問題】 SQL/Question2/【+α】ER図/MySQL 8.0 Command Line Client.txt b/【練習問題】 SQL/Question2/【+α】ER図/MySQL 8.0 Command Line Client.txt new file mode 100644 index 0000000..3b7a5a9 --- /dev/null +++ b/【練習問題】 SQL/Question2/【+α】ER図/MySQL 8.0 Command Line Client.txt @@ -0,0 +1,391 @@ +Enter password: ********* +Welcome to the MySQL monitor. Commands end with ; or \g. +Your MySQL connection id is 13 +Server version: 8.0.32 MySQL Community Server - GPL + +Copyright (c) 2000, 2023, Oracle and/or its affiliates. + +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> SELECT DATABASE(); ++------------+ +| DATABASE() | ++------------+ +| rezodb | ++------------+ +1 row in set (0.00 sec) + +mysql> CREATE TABLE emp_table (emp_no INT, emp_name VARCHAR(50), salary INT); +Query OK, 0 rows affected (0.03 sec) + +mysql> INSERT INTO emp_table VALUES (1, '井上', 20000); +Query OK, 1 row affected (0.01 sec) + +mysql> INSERT INTO emp_table VALUES (2, '梅田', 100000); +Query OK, 1 row affected (0.01 sec) + +mysql> SELECT *FROM emp_table; ++--------+----------+--------+ +| emp_no | emp_name | salary | ++--------+----------+--------+ +| 1 | 井上 | 20000 | +| 2 | 梅田 | 100000 | ++--------+----------+--------+ +2 rows in set (0.00 sec) + +mysql> CREATE TABLE book_table (book_id INT, book_name VARCHAR(50), author_name VARCHAR(50), price INT, sales_flag INT); +Query OK, 0 rows affected (0.02 sec) + +mysql> INSERT INTO book_table VALUES (10001, 'TwoPeace', '三田英一郎', 420, 1); +ERROR 1054 (42S22): Unknown column ' 420' in 'field list' +mysql> INSERT INTO book_table VALUES (10001, 'TwoPeace', '三田英一郎', 420, 1); +Query OK, 1 row affected (0.01 sec) + +mysql> INSERT INTO book_table VALUES (10002, '名探偵金田二', '青山ふみや', 460, 1); +ERROR 1054 (42S22): Unknown column ' 460' in 'field list' +mysql> INSERT INTO book_table VALUES (10002, '名探偵金田二', '青山ふみや', 460, 1); +Query OK, 1 row affected (0.00 sec) + +mysql> INSERT INTO book_table VALUES (10003, '坊ちゃま', '冬目漱石', 460, 1); +Query OK, 1 row affected (0.00 sec) + +mysql> INSERT INTO book_table VALUES (10004, '西方見聞録', '不明', 1000, 1); +Query OK, 1 row affected (0.01 sec) + +mysql> INSERT INTO book_table VALUES (10005, '吾輩は犬である', '冬目漱石', 420, 1); +Query OK, 1 row affected (0.01 sec) + +mysql> SELECT * FROM book_table; ++---------+----------------+-------------+-------+------------+ +| book_id | book_name | author_name | price | sales_flag | ++---------+----------------+-------------+-------+------------+ +| 10001 | TwoPeace | 三田英一郎 | 420 | 1 | +| 10002 | 名探偵金田二 | 青山ふみや | 460 | 1 | +| 10003 | 坊ちゃま | 冬目漱石 | 460 | 1 | +| 10004 | 西方見聞録 | 不明 | 1000 | 1 | +| 10005 | 吾輩は犬である | 冬目漱石 | 420 | 1 | ++---------+----------------+-------------+-------+------------+ +5 rows in set (0.00 sec) + +mysql> SELECT book_name FROM book_table; ++----------------+ +| book_name | ++----------------+ +| TwoPeace | +| 名探偵金田二 | +| 坊ちゃま | +| 西方見聞録 | +| 吾輩は犬である | ++----------------+ +5 rows in set (0.00 sec) + +mysql> SELECT book_name author_name FROM book_table; ++----------------+ +| author_name | ++----------------+ +| TwoPeace | +| 名探偵金田二 | +| 坊ちゃま | +| 西方見聞録 | +| 吾輩は犬である | ++----------------+ +5 rows in set (0.00 sec) + +mysql> SELECT book_name, author_name FROM book_table; ++----------------+-------------+ +| book_name | author_name | ++----------------+-------------+ +| TwoPeace | 三田英一郎 | +| 名探偵金田二 | 青山ふみや | +| 坊ちゃま | 冬目漱石 | +| 西方見聞録 | 不明 | +| 吾輩は犬である | 冬目漱石 | ++----------------+-------------+ +5 rows in set (0.00 sec) + +mysql> UPDATE book_table SET author_name = 'マール=コー=ポロ' SET sales_flag = 0 where book_id = 10004; +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' SET sales_flag = 0 where book_id = 10004' at line 1 +mysql> UPDATE book_table SET author_name = 'マール=コー=ポロ' where book_id = 10004; +Query OK, 1 row affected (0.01 sec) +Rows matched: 1 Changed: 1 Warnings: 0 + +mysql> UPDATE book_table ]SET sales_flag = 0 where book_id = 10004; +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ']SET sales_flag = 0 where book_id = 10004' at line 1 +mysql> UPDATE book_table SET sales_flag = 0 where book_id = 10004; +Query OK, 1 row affected (0.01 sec) +Rows matched: 1 Changed: 1 Warnings: 0 + +mysql> SELECT * FROM book_table; ++---------+----------------+--------------------+-------+------------+ +| book_id | book_name | author_name | price | sales_flag | ++---------+----------------+--------------------+-------+------------+ +| 10001 | TwoPeace | 三田英一郎 | 420 | 1 | +| 10002 | 名探偵金田二 | 青山ふみや | 460 | 1 | +| 10003 | 坊ちゃま | 冬目漱石 | 460 | 1 | +| 10004 | 西方見聞録 | マール=コー=ポロ | 1000 | 0 | +| 10005 | 吾輩は犬である | 冬目漱石 | 420 | 1 | ++---------+----------------+--------------------+-------+------------+ +5 rows in set (0.00 sec) + +mysql> SELECT * FROM book_table where sales_flag = 1; ++---------+----------------+-------------+-------+------------+ +| book_id | book_name | author_name | price | sales_flag | ++---------+----------------+-------------+-------+------------+ +| 10001 | TwoPeace | 三田英一郎 | 420 | 1 | +| 10002 | 名探偵金田二 | 青山ふみや | 460 | 1 | +| 10003 | 坊ちゃま | 冬目漱石 | 460 | 1 | +| 10005 | 吾輩は犬である | 冬目漱石 | 420 | 1 | ++---------+----------------+-------------+-------+------------+ +4 rows in set (0.00 sec) + +mysql> SELECT * FROM book_table where author_name = '冬目漱石' AND price = 420; +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' AND price = 420' at line 1 +mysql> SELECT * FROM book_table where author_name = '冬目漱石' AND price = 420; ++---------+----------------+-------------+-------+------------+ +| book_id | book_name | author_name | price | sales_flag | ++---------+----------------+-------------+-------+------------+ +| 10005 | 吾輩は犬である | 冬目漱石 | 420 | 1 | ++---------+----------------+-------------+-------+------------+ +1 row in set (0.00 sec) + +mysql> DELETE FROM book_table WHERE book_id = 10003; +Query OK, 1 row affected (0.00 sec) + +mysql> SELECT * FROM book_table; ++---------+----------------+--------------------+-------+------------+ +| book_id | book_name | author_name | price | sales_flag | ++---------+----------------+--------------------+-------+------------+ +| 10001 | TwoPeace | 三田英一郎 | 420 | 1 | +| 10002 | 名探偵金田二 | 青山ふみや | 460 | 1 | +| 10004 | 西方見聞録 | マール=コー=ポロ | 1000 | 0 | +| 10005 | 吾輩は犬である | 冬目漱石 | 420 | 1 | ++---------+----------------+--------------------+-------+------------+ +4 rows in set (0.00 sec) + +mysql> CREATE TABLE item_category (category_id INT, category_name VARCHAR(256)); +Query OK, 0 rows affected (0.03 sec) + +mysql> INSERT INTO item_category (1, '家具'); +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1, '家具')' at line 1 +mysql> INSERT INTO item_category (1, '家具'); +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1, '家具')' at line 1 +mysql> INSERT INTO item_category VALUES (1, '家具'); +Query OK, 1 row affected (0.01 sec) + +mysql> INSERT INTO item_category VALUES (2, '飲料'); +Query OK, 1 row affected (0.01 sec) + +mysql> INSERT INTO item_category VALUES (3, '食品'); +Query OK, 1 row affected (0.00 sec) + +mysql> SELECT * FROM item_table + -> ^C +mysql> SELECT * FROM item_table; +ERROR 1146 (42S02): Table 'rezodb.item_table' doesn't exist +mysql> SELECT * FROM item_category; ++-------------+---------------+ +| category_id | category_name | ++-------------+---------------+ +| 1 | 家具 | +| 2 | 飲料 | +| 3 | 食品 | ++-------------+---------------+ +3 rows in set (0.00 sec) + +mysql> CREATE TABLE items(item_id INT, name VARCHAR(256), price INT, category_id INT); +Query OK, 0 rows affected (0.02 sec) + +mysql> INSERT INTO items VALUES (1, 'ローマのテーブル' 150000, 1); +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 150000, 1)' at line 1 +mysql> INSERT INTO items VALUES (1, 'ローマのテーブル', 150000, 1); +Query OK, 1 row affected (0.00 sec) + +mysql> INSERT INTO items VALUES (2, 'げーたるーど2000 500ml', 370, 2); +Query OK, 1 row affected (0.01 sec) + +mysql> INSERT INTO items VALUES (3, '原子肉', 2500, 3); +Query OK, 1 row affected (0.01 sec) + +mysql> INSERT INTO items VALUES (4, '魅惑の椅子', 100000, 1); +Query OK, 1 row affected (0.01 sec) + +mysql> SELECT * FROM item; +ERROR 1146 (42S02): Table 'rezodb.item' doesn't exist +mysql> SELECT * FROM items; ++---------+------------------------+--------+-------------+ +| item_id | name | price | category_id | ++---------+------------------------+--------+-------------+ +| 1 | ローマのテーブル | 150000 | 1 | +| 2 | げーたるーど2000 500ml | 370 | 2 | +| 3 | 原子肉 | 2500 | 3 | +| 4 | 魅惑の椅子 | 100000 | 1 | ++---------+------------------------+--------+-------------+ +4 rows in set (0.00 sec) + +mysql> SELECT * FROM item_category INNER JOIN item ON item_category.category_id = item.category_id; +ERROR 1146 (42S02): Table 'rezodb.item' doesn't exist +mysql> SELECT * FROM item_category INNER JOIN item ON item_category.category_id = items.category_id; +ERROR 1146 (42S02): Table 'rezodb.item' doesn't exist +mysql> SELECT * FROM item_category INNER JOIN items ON item_category.category_id = items.category_id; ++-------------+---------------+---------+------------------------+--------+-------------+ +| category_id | category_name | item_id | name | price | category_id | ++-------------+---------------+---------+------------------------+--------+-------------+ +| 1 | 家具 | 1 | ローマのテーブル | 150000 | 1 | +| 2 | 飲料 | 2 | げーたるーど2000 500ml | 370 | 2 | +| 3 | 食品 | 3 | 原子肉 | 2500 | 3 | +| 1 | 家具 | 4 | 魅惑の椅子 | 100000 | 1 | ++-------------+---------------+---------+------------------------+--------+-------------+ +4 rows in set (0.00 sec) + +mysql> SELECT item.category.category_id AS items.category_id FROM item_category INNER JOIN item ON item_category.category_id = items.category_id; +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.category_id FROM item_category INNER JOIN item ON item_category.category_id = i' at line 1 +mysql> SELECT * FROM item_category INNER JOIN items ON item_category.category_id = items.category_id; ++-------------+---------------+---------+------------------------+--------+-------------+ +| category_id | category_name | item_id | name | price | category_id | ++-------------+---------------+---------+------------------------+--------+-------------+ +| 1 | 家具 | 1 | ローマのテーブル | 150000 | 1 | +| 2 | 飲料 | 2 | げーたるーど2000 500ml | 370 | 2 | +| 3 | 食品 | 3 | 原子肉 | 2500 | 3 | +| 1 | 家具 | 4 | 魅惑の椅子 | 100000 | 1 | ++-------------+---------------+---------+------------------------+--------+-------------+ +4 rows in set (0.00 sec) + +mysql> SELECT * FROM item_category INNER JOIN items ON USING (category_id); +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'USING (category_id)' at line 1 +mysql> SELECT * FROM item_category INNER JOIN items ON USING (category_id); +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'USING (category_id)' at line 1 +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id); ++-------------+---------+------------------------+--------+---------------+ +| category_id | item_id | name | price | category_name | ++-------------+---------+------------------------+--------+---------------+ +| 1 | 1 | ローマのテーブル | 150000 | 家具 | +| 2 | 2 | げーたるーど2000 500ml | 370 | 飲料 | +| 3 | 3 | 原子肉 | 2500 | 食品 | +| 1 | 4 | 魅惑の椅子 | 100000 | 家具 | ++-------------+---------+------------------------+--------+---------------+ +4 rows in set (0.00 sec) + +mysql> SELECT * FROM items; ++---------+------------------------+--------+-------------+ +| item_id | name | price | category_id | ++---------+------------------------+--------+-------------+ +| 1 | ローマのテーブル | 150000 | 1 | +| 2 | げーたるーど2000 500ml | 370 | 2 | +| 3 | 原子肉 | 2500 | 3 | +| 4 | 魅惑の椅子 | 100000 | 1 | ++---------+------------------------+--------+-------------+ +4 rows in set (0.00 sec) + +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id) OEDER BY price ASC; +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OEDER BY price ASC' at line 1 +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id) ORDER BY price ASC; ++-------------+---------+------------------------+--------+---------------+ +| category_id | item_id | name | price | category_name | ++-------------+---------+------------------------+--------+---------------+ +| 2 | 2 | げーたるーど2000 500ml | 370 | 飲料 | +| 3 | 3 | 原子肉 | 2500 | 食品 | +| 1 | 4 | 魅惑の椅子 | 100000 | 家具 | +| 1 | 1 | ローマのテーブル | 150000 | 家具 | ++-------------+---------+------------------------+--------+---------------+ +4 rows in set (0.00 sec) + +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id) WHERE category_name = '家具'; ++-------------+---------+------------------+--------+---------------+ +| category_id | item_id | name | price | category_name | ++-------------+---------+------------------+--------+---------------+ +| 1 | 1 | ローマのテーブル | 150000 | 家具 | +| 1 | 4 | 魅惑の椅子 | 100000 | 家具 | ++-------------+---------+------------------+--------+---------------+ +2 rows in set (0.00 sec) + +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id) WHERE price > 1000 AND price < 50000; ++-------------+---------+--------+-------+---------------+ +| category_id | item_id | name | price | category_name | ++-------------+---------+--------+-------+---------------+ +| 3 | 3 | 原子肉 | 2500 | 食品 | ++-------------+---------+--------+-------+---------------+ +1 row in set (0.00 sec) + +mysql> SELECT COUNT (*) AS data_count from items; +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) AS data_count from items' at line 1 +mysql> SELECT COUNT () AS data_count from items; +ERROR 1630 (42000): FUNCTION rezodb.COUNT does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual +mysql> SELECT COUNT (*) AS data_count from items; +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) AS data_count from items' at line 1 +mysql> SELECT COUNT (*) AS data_count FROM items; +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) AS data_count FROM items' at line 1 +mysql> SELECT COUNT (*) FROM items; +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) FROM items' at line 1 +mysql> SELECT COUNT(*) FROM items; ++----------+ +| COUNT(*) | ++----------+ +| 4 | ++----------+ +1 row in set (0.01 sec) + +mysql> SELECT COUNT(*) AS data_count FROM items; ++------------+ +| data_count | ++------------+ +| 4 | ++------------+ +1 row in set (0.00 sec) + +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id) s + -> ^C +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id) WHERE score = MIN (score) + -> ^C +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id) WHERE score = MIN (score); +ERROR 1054 (42S22): Unknown column 'score' in 'where clause' +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id) WHERE score = (MIN (score) FROM items); +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM items)' at line 1 +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id) WHERE score = (MIN(score) FROM items); +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM items)' at line 1 +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id) WHERE score = (MAX(price) FROM items); +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM items)' at line 1 +mysql> mysql> SELECT * FROM items WHERE price = (SELECT MAX(price) FROM items); +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql> SELECT * FROM items WHERE price = (SELECT MAX(price) FROM items)' at line 1 +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id); ++-------------+---------+------------------------+--------+---------------+ +| category_id | item_id | name | price | category_name | ++-------------+---------+------------------------+--------+---------------+ +| 1 | 1 | ローマのテーブル | 150000 | 家具 | +| 2 | 2 | げーたるーど2000 500ml | 370 | 飲料 | +| 3 | 3 | 原子肉 | 2500 | 食品 | +| 1 | 4 | 魅惑の椅子 | 100000 | 家具 | ++-------------+---------+------------------------+--------+---------------+ +4 rows in set (0.00 sec) + +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id) WHERE price = (MAX(price) FROM items); +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM items)' at line 1 +mysql> SELECT * FROM items WHERE price = (SELECT MAX(price) FROM items); ++---------+------------------+--------+-------------+ +| item_id | name | price | category_id | ++---------+------------------+--------+-------------+ +| 1 | ローマのテーブル | 150000 | 1 | ++---------+------------------+--------+-------------+ +1 row in set (0.00 sec) + +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id) WHERE price = (SELECT MAX(price) FROM items); ++-------------+---------+------------------+--------+---------------+ +| category_id | item_id | name | price | category_name | ++-------------+---------+------------------+--------+---------------+ +| 1 | 1 | ローマのテーブル | 150000 | 家具 | ++-------------+---------+------------------+--------+---------------+ +1 row in set (0.00 sec) + +mysql> SELECT * FROM items INNER JOIN item_category USING (category_id) WHERE name LIKE '%の%'; ++-------------+---------+------------------+--------+---------------+ +| category_id | item_id | name | price | category_name | ++-------------+---------+------------------+--------+---------------+ +| 1 | 4 | 魅惑の椅子 | 100000 | 家具 | +| 1 | 1 | ローマのテーブル | 150000 | 家具 | ++-------------+---------+------------------+--------+---------------+ +2 rows in set (0.00 sec) + +mysql>