2 Commits

Author SHA1 Message Date
793d398c06 question2です 2026-09-28 14:38:10 +09:00
3fa893447e question1です 2026-09-28 12:38:48 +09:00
5 changed files with 62 additions and 801 deletions

View File

@@ -1,179 +0,0 @@
MySQL SQL問題集 part1
MySQEnter password: *********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 23
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> USE rezodb;
Database changed
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, '井上', 200000);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO emp_table VALUES (2, '梅田', 1000000);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM emp_table;
+--------+----------+---------+
| emp_no | emp_name | salary |
+--------+----------+---------+
| 1 | 井上 | 200000 |
| 2 | 梅田 | 1000000 |
+--------+----------+---------+
2 rows in set (0.00 sec)
mysql> CREATE TABLE book_table (book_id INT,book_name VARCHAR(50), author_name VARCHAR(50), prime INT,sales_flag INT);
Query OK, 0 rows affected (0.04 sec)
mysql> INSERT INTO book_table VALUES (10001,TwoPeace,'三田英一郎',420,1);
ERROR 1054 (42S22): Unknown column 'TwoPeace' in 'field list'
mysql> INSERT INTO book_table VALUES(10001,TwoPeace,'三田英一郎',420,1);
ERROR 1054 (42S22): Unknown column 'TwoPeace' in 'field list'
mysql> DROP TABLE book_table;
Query OK, 0 rows affected (0.02 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.03 sec)
mysql> INSERT INTO book_table(10001,TwoPeace,'三田英一郎',420,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 '10001,TwoPeace,'三田英一郎',420,1)' at line 1
mysql> INSERT INTO book_table VALUES (10001,'TwoPeace','三田英一郎',420,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 'VALUES (10001,'TwoPeace','三田英一郎',420,1)' at line 1
mysql> SELECT * fROM book_table;
Empty set (0.00 sec)
mysql> INSERT INTO book_table VALUES (10001,'TwoPeace','三田英一郎',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 |
+---------+-----------+-------------+-------+------------+
1 row in set (0.00 sec)
mysql> INSERT INTO book_table VALUES(10002,'名探偵金田二','青山ふみや',420,1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO book_table VALUES(20001,'坊ちゃま','冬目漱石',460);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> INSERT INTO book_table VALUES(20001,'坊ちゃま','冬目漱石',460,1);
Query OK, 1 row affected (0.01 sec)
mysql> UPDATE book_table SET price = 460 WHERE book_id = 10002;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> INSERT INTO book_table VALUES(30001,'西方見聞録','不明',1000,1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO book_table VALIES(40010,'吾輩は犬である','冬目漱石',420,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 'VALIES(40010,'吾輩は犬である','冬目漱石',420,1)' at line 1
mysql> INSERT INTO book_table VALUES(40010,'吾輩は犬である','冬目漱石',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 |
| 20001 | 坊ちゃま | 冬目漱石 | 460 | 1 |
| 30001 | 西方見聞録 | 不明 | 1000 | 1 |
| 40010 | 吾輩は犬である | 冬目漱石 | 420 | 1 |
+---------+----------------+-------------+-------+------------+
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> UPDATA book_table SET author_name = 'マール=コー=ポロ' WHERE book_id = 30001;
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 'UPDATA book_table SET author_name = 'マール=コー=ポロ' WHERE book_id ' at line 1
mysql> UPDATA book_table SET author_name = 'マール=コー=ポロ' WHERE book_id =30001;
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 'UPDATA book_table SET author_name = 'マール=コー=ポロ' WHERE book_id ' at line 1
mysql> UPDATA book_table SET sales_flag = 0 WHERE book_id = 30001;
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 'UPDATA book_table SET sales_flag = 0 WHERE book_id = 30001' at line 1
mysql> ^C
mysql> UPDATE book_table SET author_name = 'マール=コー=ポロ' WHERE book_id = 30001;
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 = 30001;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT FROM *book_table;
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 *book_table' at line 1
mysql> SELECT FROM * book_table;
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 * book_table' at line 1
mysql> SELECT * FROM book_table;
+---------+----------------+--------------------+-------+------------+
| book_id | book_name | author_name | price | sales_flag |
+---------+----------------+--------------------+-------+------------+
| 10001 | TwoPeace | 三田英一郎 | 420 | 1 |
| 10002 | 名探偵金田二 | 青山ふみや | 460 | 1 |
| 20001 | 坊ちゃま | 冬目漱石 | 460 | 1 |
| 30001 | 西方見聞録 | マール=コー=ポロ | 1000 | 0 |
| 40010 | 吾輩は犬である | 冬目漱石 | 420 | 1 |
+---------+----------------+--------------------+-------+------------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM book_table where salse_flag = 1;
ERROR 1054 (42S22): Unknown column 'salse_flag' in 'where clause'
mysql> SELECT * FROM book_table where salse_flag = 1;
ERROR 1054 (42S22): Unknown column 'salse_flag' in 'where clause'
mysql> SELECT * FROM table WHERE sales_flag = 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 'table WHERE sales_flag = 1' at line 1
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 |
| 20001 | 坊ちゃま | 冬目漱石 | 460 | 1 |
| 40010 | 吾輩は犬である | 冬目漱石 | 420 | 1 |
+---------+----------------+-------------+-------+------------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM book_table where author_name = '冬目漱石' and price = 420;
+---------+----------------+-------------+-------+------------+
| book_id | book_name | author_name | price | sales_flag |
+---------+----------------+-------------+-------+------------+
| 40010 | 吾輩は犬である | 冬目漱石 | 420 | 1 |
+---------+----------------+-------------+-------+------------+
1 row in set (0.00 sec)
mysql> DELETE FROM book_table WHERE book_id = 20001;
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 |
| 30001 | 西方見聞録 | マール=コー=ポロ | 1000 | 0 |
| 40010 | 吾輩は犬である | 冬目漱石 | 420 | 1 |
+---------+----------------+--------------------+-------+------------+
4 rows in set (0.00 sec)

View File

@@ -1,483 +0,0 @@
MySQL SQL問題集 part2+α
mysql> INSERT INTO items VALUES (5,'十八茶 600ml',110,1);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO items VALUES (6,'いい匂いのするドリアン',5000,3);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO items VALUES (7,'太平洋の水 550ml',300,2);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO items VALUES (8,'ひたすら膨らむガム',200,2);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO items VALUES (9,'人をだめにするソファー',10000,1);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO items VALUES (10,'人を虜にする名酒',20000,2);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO items VALUES (11,'新鮮キャベツ',1000,3);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT items.category_id,items.item_id,items.name,items.price,item_category.category_name FROM item_category INNER JOIN items ON items.category_id = item_category.category_id;
+-------------+---------+--------------------------+--------+---------------+
| category_id | item_id | name | price | category_name |
+-------------+---------+--------------------------+--------+---------------+
| 1 | 1 | ローマのテーブル | 150000 | 家具 |
| 1 | 4 | 魅惑の椅子 | 100000 | 家具 |
| 1 | 5 | 十八茶 600ml | 110 | 家具 |
| 1 | 9 | 人をだめにするソファー | 10000 | 家具 |
| 2 | 2 | げーたるーど2000 500ml | 370 | 飲料 |
| 2 | 7 | 太平洋の水 550ml | 300 | 飲料 |
| 2 | 8 | ひたすら膨らむガム | 200 | 飲料 |
| 2 | 10 | 人を虜にする名酒 | 20000 | 飲料 |
| 3 | 3 | 原始肉 | 2500 | 食品 |
| 3 | 6 | いい匂いのするドリアン | 5000 | 食品 |
| 3 | 11 | 新鮮キャベツ | 1000 | 食品 |
+-------------+---------+--------------------------+--------+---------------+
11 rows in set (0.00 sec)
mysql> SELECT items.category_id,items.item_id,items.name,items.price,item_category.category_name FROM item_category INNER JOIN items ON items.category_id = item_category.category_id ORDER BY item_id ASC;
+-------------+---------+--------------------------+--------+---------------+
| category_id | item_id | name | price | category_name |
+-------------+---------+--------------------------+--------+---------------+
| 1 | 1 | ローマのテーブル | 150000 | 家具 |
| 2 | 2 | げーたるーど2000 500ml | 370 | 飲料 |
| 3 | 3 | 原始肉 | 2500 | 食品 |
| 1 | 4 | 魅惑の椅子 | 100000 | 家具 |
| 1 | 5 | 十八茶 600ml | 110 | 家具 |
| 3 | 6 | いい匂いのするドリアン | 5000 | 食品 |
| 2 | 7 | 太平洋の水 550ml | 300 | 飲料 |
| 2 | 8 | ひたすら膨らむガム | 200 | 飲料 |
| 1 | 9 | 人をだめにするソファー | 10000 | 家具 |
| 2 | 10 | 人を虜にする名酒 | 20000 | 飲料 |
| 3 | 11 | 新鮮キャベツ | 1000 | 食品 |
+-------------+---------+--------------------------+--------+---------------+
11 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 |
| 5 | 十八茶 600ml | 110 | 1 |
| 6 | いい匂いのするドリアン | 5000 | 3 |
| 7 | 太平洋の水 550ml | 300 | 2 |
| 8 | ひたすら膨らむガム | 200 | 2 |
| 9 | 人をだめにするソファー | 10000 | 1 |
| 10 | 人を虜にする名酒 | 20000 | 2 |
| 11 | 新鮮キャベツ | 1000 | 3 |
+---------+--------------------------+--------+-------------+
11 rows in set (0.00 sec)
mysql> DROP TABLE IF EXISTS order_items;
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> DROP TABLE IF EXISTS orders;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP TABLE IF EXISTS items;
Query OK, 0 rows affected (0.02 sec)
mysql> DROP TABLE IF EXISTS item_category;
Query OK, 0 rows affected (0.01 sec)
mysql> DROP TABLE IF EXISTS users;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
mysql> CREATE TABLE users(
-> user_id INT PRIMARY KEY AUTO_INCREMENT COMMENT 'ユーザID'
-> , name VARCHAR (15) NOT NULL COMMENT 'ユーザ名'
-> , password VARCHAR (50) NOT NULL COMMENT 'パスワード'
-> , age INT COMMENT '年齢'
-> , is_del CHAR (1) NOT NULL DEFAULT '0' COMMENT '削除フラグ:0:未削除、1:削除済'
-> )
-> default charset=utf8
-> COMMENT 'ユーザテーブル';
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql>
mysql> CREATE TABLE item_category(
-> category_id INT AUTO_INCREMENT PRIMARY KEY COMMENT 'カテゴリID'
-> , name VARCHAR (256) NOT NULL COMMENT 'カテゴリ名'
-> , is_del CHAR (1) NOT NULL DEFAULT '0' COMMENT '削除フラグ:0:未削除、1:削除済'
-> )
-> default charset=utf8
-> COMMENT 'カテゴリテーブル';
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql>
mysql> CREATE TABLE items(
-> item_id INT PRIMARY KEY AUTO_INCREMENT COMMENT '商品ID'
-> , name VARCHAR (255) NOT NULL COMMENT '商品名'
-> , price INT NOT NULL COMMENT '価格'
-> , category_id INT COMMENT 'カテゴリID'
-> , is_sale CHAR (1) NOT NULL DEFAULT '1' COMMENT '販売有無:1:販売中、0:販売無'
-> , is_del CHAR (1) NOT NULL DEFAULT '0' COMMENT '削除フラグ:0:未削除、1:削除済'
-> , FOREIGN KEY fk_categoryid(category_id) REFERENCES item_category(category_id)
-> )
-> default charset=utf8
-> COMMENT '商品テーブル';
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql>
mysql> CREATE TABLE orders(
-> order_id INT PRIMARY KEY AUTO_INCREMENT COMMENT '注文ID'
-> , user_id INT NOT NULL COMMENT 'ユーザID'
-> , order_time DATETIME NOT NULL COMMENT '注文日時'
-> , is_del CHAR (1) NOT NULL DEFAULT '0' COMMENT '削除フラグ:0:未削除、1:削除済'
-> , FOREIGN KEY fk_userid(user_id) REFERENCES users(user_id)
-> )
-> default charset=utf8
-> COMMENT '注文テーブル';
Query OK, 0 rows affected, 1 warning (0.04 sec)
mysql>
mysql> CREATE TABLE order_items(
-> order_id INT COMMENT '注文ID'
-> , item_id INT COMMENT '商品ID'
-> , quantity INT COMMENT '注文数'
-> , PRIMARY KEY (order_id, item_id)
-> , FOREIGN KEY fk_orderid(order_id) REFERENCES orders(order_id)
-> , FOREIGN KEY fk_itemid(item_id) REFERENCES items(item_id)
-> )
-> default charset=utf8
-> COMMENT '注文商品テーブル';
Query OK, 0 rows affected, 1 warning (0.05 sec)
mysql>
mysql>
mysql> INSERT INTO users (name, password, age) VALUES ('三浦知一','KfCQDjiZ', 53);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO users (name, password, age) VALUES ('遠藤保','V3mngzMP', 40);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO users (name, password, age) VALUES ('柴崎学','XktKCvCx', 27);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO users (name, password, age) VALUES ('本田啓介','bSCebKnP', 33);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO users (name, password, age) VALUES ('花川真司','xKetmAcH', 30);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO item_category (name) VALUES ('家具');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO item_category (name) VALUES ('飲料');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO item_category (name) VALUES ('食品');
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO items (name, price, category_id) VALUES ('ローマのテーブル', 150000, 1);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO items (name, price, category_id) VALUES ('げーたるーど2000 500ml', 370, 2);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO items (name, price, category_id) VALUES ('原始肉', 2500, 3);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO items (name, price, category_id) VALUES ('魅惑の椅子', 100000, 1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO items (name, price, category_id) VALUES ('十八茶 600ml', 110, 1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO items (name, price, category_id) VALUES ('いい匂いのするドリアン', 5000, 3);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO items (name, price, category_id) VALUES ('太平洋の水 550ml', 300, 2);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO items (name, price, category_id) VALUES ('ひたすら膨らむガム', 200, 3);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO items (name, price, category_id) VALUES ('人をだめにするソファー', 10000, 1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO items (name, price, category_id) VALUES ('人を虜にする名酒', 20000, 2);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO items (name, price, category_id) VALUES ('新鮮キャベツ', 1000, 3);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO orders (user_id, order_time) VALUES (1, '2015-02-22 12:00:00');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO orders (user_id, order_time) VALUES (4, '2015-12-01 15:30:00');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO orders (user_id, order_time) VALUES (3, '2018-03-20 18:45:00');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO orders (user_id, order_time) VALUES (2, '2018-05-11 11:28:00');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO orders (user_id, order_time) VALUES (5, '2018-09-30 10:50:00');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO orders (user_id, order_time) VALUES (1, '2018-09-12 20:13:00');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO orders (user_id, order_time) VALUES (4, '2018-09-15 21:34:00');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO orders (user_id, order_time) VALUES (1, '2019-05-06 16:00:00');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO orders (user_id, order_time) VALUES (3, '2019-12-20 10:39:00');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO orders (user_id, order_time) VALUES (2, '2020-01-01 14:11:00');
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (1, 2, 1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (1, 6, 10);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (1, 7, 1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (1, 11, 10);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (2, 4, 1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (3, 9, 1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (4, 7, 1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (4, 8, 1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (5, 2, 7);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (5, 3, 5);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (5, 10, 1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (5, 11, 5);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (5, 5, 1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (5, 7, 1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (6, 8, 10);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (7, 4, 2);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (8, 1, 1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (8, 9, 1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (9, 3, 4);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (9, 5, 2);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (9, 8, 10);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (10, 1, 2);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO order_items (order_id, item_id, quantity) VALUES (10, 10, 2);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM items;
+---------+------------------------+--------+-------------+---------+--------+
| item_id | name | price | category_id | is_sale | is_del |
+---------+------------------------+--------+-------------+---------+--------+
| 1 | ローマのテーブル | 150000 | 1 | 1 | 0 |
| 2 | げーたるーど2000 500ml | 370 | 2 | 1 | 0 |
| 3 | 原始肉 | 2500 | 3 | 1 | 0 |
| 4 | 魅惑の椅子 | 100000 | 1 | 1 | 0 |
| 5 | 十八茶 600ml | 110 | 1 | 1 | 0 |
| 6 | いい匂いのするドリアン | 5000 | 3 | 1 | 0 |
| 7 | 太平洋の水 550ml | 300 | 2 | 1 | 0 |
| 8 | ひたすら膨らむガム | 200 | 3 | 1 | 0 |
| 9 | 人をだめにするソファー | 10000 | 1 | 1 | 0 |
| 10 | 人を虜にする名酒 | 20000 | 2 | 1 | 0 |
| 11 | 新鮮キャベツ | 1000 | 3 | 1 | 0 |
+---------+------------------------+--------+-------------+---------+--------+
11 rows in set (0.00 sec)
mysql> ALTER TABLE items RENAME COLUMN item_id TO '商品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 ''商品ID'' at line 1
mysql> ALTER TABLE items RENAME COLUMN item_id TO `商品ID`;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM items;
+--------+------------------------+--------+-------------+---------+--------+
| 商品ID | name | price | category_id | is_sale | is_del |
+--------+------------------------+--------+-------------+---------+--------+
| 1 | ローマのテーブル | 150000 | 1 | 1 | 0 |
| 2 | げーたるーど2000 500ml | 370 | 2 | 1 | 0 |
| 3 | 原始肉 | 2500 | 3 | 1 | 0 |
| 4 | 魅惑の椅子 | 100000 | 1 | 1 | 0 |
| 5 | 十八茶 600ml | 110 | 1 | 1 | 0 |
| 6 | いい匂いのするドリアン | 5000 | 3 | 1 | 0 |
| 7 | 太平洋の水 550ml | 300 | 2 | 1 | 0 |
| 8 | ひたすら膨らむガム | 200 | 3 | 1 | 0 |
| 9 | 人をだめにするソファー | 10000 | 1 | 1 | 0 |
| 10 | 人を虜にする名酒 | 20000 | 2 | 1 | 0 |
| 11 | 新鮮キャベツ | 1000 | 3 | 1 | 0 |
+--------+------------------------+--------+-------------+---------+--------+
11 rows in set (0.00 sec)
mysql> ALTER TABLE items RENAME COLUMN name TO `商品名`;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE items RENAME COLUMN price TO `価格`;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE items RENAME COLUMN category_id TO `カテゴリ名`;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM items;
+--------+------------------------+--------+------------+---------+--------+
| 商品ID | 商品名 | 価格 | カテゴリ名 | is_sale | is_del |
+--------+------------------------+--------+------------+---------+--------+
| 1 | ローマのテーブル | 150000 | 1 | 1 | 0 |
| 2 | げーたるーど2000 500ml | 370 | 2 | 1 | 0 |
| 3 | 原始肉 | 2500 | 3 | 1 | 0 |
| 4 | 魅惑の椅子 | 100000 | 1 | 1 | 0 |
| 5 | 十八茶 600ml | 110 | 1 | 1 | 0 |
| 6 | いい匂いのするドリアン | 5000 | 3 | 1 | 0 |
| 7 | 太平洋の水 550ml | 300 | 2 | 1 | 0 |
| 8 | ひたすら膨らむガム | 200 | 3 | 1 | 0 |
| 9 | 人をだめにするソファー | 10000 | 1 | 1 | 0 |
| 10 | 人を虜にする名酒 | 20000 | 2 | 1 | 0 |
| 11 | 新鮮キャベツ | 1000 | 3 | 1 | 0 |
+--------+------------------------+--------+------------+---------+--------+
11 rows in set (0.00 sec)
mysql> SELECT * FROM orders;
+----------+---------+---------------------+--------+
| order_id | user_id | order_time | is_del |
+----------+---------+---------------------+--------+
| 1 | 1 | 2015-02-22 12:00:00 | 0 |
| 2 | 4 | 2015-12-01 15:30:00 | 0 |
| 3 | 3 | 2018-03-20 18:45:00 | 0 |
| 4 | 2 | 2018-05-11 11:28:00 | 0 |
| 5 | 5 | 2018-09-30 10:50:00 | 0 |
| 6 | 1 | 2018-09-12 20:13:00 | 0 |
| 7 | 4 | 2018-09-15 21:34:00 | 0 |
| 8 | 1 | 2019-05-06 16:00:00 | 0 |
| 9 | 3 | 2019-12-20 10:39:00 | 0 |
| 10 | 2 | 2020-01-01 14:11:00 | 0 |
+----------+---------+---------------------+--------+
10 rows in set (0.00 sec)
mysql> SELECT * FROM order_items;
+----------+---------+----------+
| order_id | item_id | quantity |
+----------+---------+----------+
| 1 | 2 | 1 |
| 1 | 6 | 10 |
| 1 | 7 | 1 |
| 1 | 11 | 10 |
| 2 | 4 | 1 |
| 3 | 9 | 1 |
| 4 | 7 | 1 |
| 4 | 8 | 1 |
| 5 | 2 | 7 |
| 5 | 3 | 5 |
| 5 | 5 | 1 |
| 5 | 7 | 1 |
| 5 | 10 | 1 |
| 5 | 11 | 5 |
| 6 | 8 | 10 |
| 7 | 4 | 2 |
| 8 | 1 | 1 |
| 8 | 9 | 1 |
| 9 | 3 | 4 |
| 9 | 5 | 2 |
| 9 | 8 | 10 |
| 10 | 1 | 2 |
| 10 | 10 | 2 |
+----------+---------+----------+
23 rows in set (0.00 sec)
mysql> SELECT * FROM users;
+---------+----------+----------+------+--------+
| user_id | name | password | age | is_del |
+---------+----------+----------+------+--------+
| 1 | 三浦知一 | KfCQDjiZ | 53 | 0 |
| 2 | 遠藤保 | V3mngzMP | 40 | 0 |
| 3 | 柴崎学 | XktKCvCx | 27 | 0 |
| 4 | 本田啓介 | bSCebKnP | 33 | 0 |
| 5 | 花川真司 | xKetmAcH | 30 | 0 |
+---------+----------+----------+------+--------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM item_category;
+-------------+------+--------+
| category_id | name | is_del |
+-------------+------+--------+
| 1 | 家具 | 0 |
| 2 | 飲料 | 0 |
| 3 | 食品 | 0 |
+-------------+------+--------+
3 rows in set (0.00 sec)
mysql> SELECT order.id,user_id,order_time,item_id,quantity FROM order_items INNER JOIN orders ON order_id = order_items.order_id;
ERROR 1054 (42S22): Unknown column 'order.id' in 'field list'
mysql> SELECT `商品ID`,`商品名`,`価格`,`カテゴリ名` FROM items;
+--------+------------------------+--------+------------+
| 商品ID | 商品名 | 価格 | カテゴリ名 |
+--------+------------------------+--------+------------+
| 1 | ローマのテーブル | 150000 | 1 |
| 2 | げーたるーど2000 500ml | 370 | 2 |
| 3 | 原始肉 | 2500 | 3 |
| 4 | 魅惑の椅子 | 100000 | 1 |
| 5 | 十八茶 600ml | 110 | 1 |
| 6 | いい匂いのするドリアン | 5000 | 3 |
| 7 | 太平洋の水 550ml | 300 | 2 |
| 8 | ひたすら膨らむガム | 200 | 3 |
| 9 | 人をだめにするソファー | 10000 | 1 |
| 10 | 人を虜にする名酒 | 20000 | 2 |
| 11 | 新鮮キャベツ | 1000 | 3 |
+--------+------------------------+--------+------------+
11 rows in set (0.00 sec)
mysql> items.`商品ID`,items.`商品名`,items.`価格,items,`カテゴリ名` FROM item_category INNER JOIN items.`カテゴリ名` = item_category.category_id;
`> ^C
mysql> items.`商品ID`,items.`商品名`,items.`価格,items,`カテゴリ名` FROM item_category INNER JOIN items.`カテゴリ名` = item_category.category_id;
`> ^C
mysql> SELECT items.`商品ID`, items.`商品名`, items.`価格`, item_category.`カテゴリ名` FROM items INNER JOIN item_category ON items.`カテゴリID` = item_category.category_id;
ERROR 1054 (42S22): Unknown column 'item_category.カテゴリ名' in 'field list'
mysql>

View File

@@ -1,139 +0,0 @@
MySQL SQL問題集 part2
mysql> CREATE TABLE item_category(category_id INT,category_name VARCHAR(256));
Query OK, 0 rows affected (0.03 sec)
mysql> SELECT * FROM item_category;
Empty set (0.00 sec)
mysql> alter table item_category ADD PRIMARY KEY (category_id);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table item_category MODIFY COLUMN category_name VARCHAR(256) NOT NULL;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> CREATE TABLE items (item_id INT,name VARCHAR(256), price INT,category_id INT);
Query OK, 0 rows affected (0.03 sec)
mysql> alter table items ADD PRIMARY KEY (item_id);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE items MODIFY COLUMN price INT NOT NULL;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE items ADD CONSTRAINT fk_category FOREIGN KEY (category_id) REFERENCES item_category(category_id);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> INSERT INTO item_category VALUES (1,'家具');
Query OK, 1 row affected (0.02 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.01 sec)
mysql> SELECT * FROM item_category;
+-------------+---------------+
| category_id | category_name |
+-------------+---------------+
| 1 | 家具 |
| 2 | 飲料 |
| 3 | 食品 |
+-------------+---------------+
3 rows in set (0.00 sec)
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.00 sec)
mysql> INSERT INTO items VALIES(3,'原始肉',2500,3);
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 'VALIES(3,'原始肉',2500,3)' at line 1
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> SEKECT * 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 'SEKECT * FROM items' at line 1
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.01 sec)
mysql> SELECT items.category_id,items.item_id,items.name,items.price,item_category.category_name FROM item_category INNER JOIN items ON items.category_id = item_category.category_id;
+-------------+---------+--------------------------+--------+---------------+
| category_id | item_id | name | price | category_name |
+-------------+---------+--------------------------+--------+---------------+
| 1 | 1 | ローマのテーブル | 150000 | 家具 |
| 1 | 4 | 魅惑の椅子 | 100000 | 家具 |
| 2 | 2 | げーたるーど2000 500ml | 370 | 飲料 |
| 3 | 3 | 原始肉 | 2500 | 食品 |
+-------------+---------+--------------------------+--------+---------------+
4 rows in set (0.00 sec)
mysql> SELECT items.category_id,items.item_id,items.name,items.price,item_category.category_name FROM item_category INNER JOIN items ON items.category_id = item_category.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 items.category_id,items.item_id,items.name,items.price,item_category.category_name FROM item_category INNER JOIN items ON items.category_id = item_category.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 items.category_id,items.item_id,items.name,items.price,item_category.category_name FROM item_category INNER JOIN items ON items.category_id = item_category.category_id WHERE price BETWEEN 1000 AND 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;
+------------+
| data_count |
+------------+
| 4 |
+------------+
1 row in set (0.02 sec)
mysql> SELECT items.category_id,items.item_id,items.name,items.price,item_category.category_name FROM item_category INNER JOIN items ON items.category_id = item_category.category_id WHERE items.price = (SELECT MAX(price)FROM items);
+-------------+---------+------------------+--------+---------------+
| category_id | item_id | name | price | category_name |
+-------------+---------+------------------+--------+---------------+
| 1 | 1 | ローマのテーブル | 150000 | 家具 |
+-------------+---------+------------------+--------+---------------+
1 row in set (0.01 sec)
mysql> SELECT items.category_id,items.item_id,items.name,items.price,item_category.category_name FROM item_category INNER JOIN items ON items.category_id = item_category.category_id WHERE name LIKE '%の%';
+-------------+---------+------------------+--------+---------------+
| category_id | item_id | name | price | category_name |
+-------------+---------+------------------+--------+---------------+
| 1 | 1 | ローマのテーブル | 150000 | 家具 |
| 1 | 4 | 魅惑の椅子 | 100000 | 家具 |
+-------------+---------+------------------+--------+---------------+
2 rows in set (0.01 sec)

View File

@@ -0,0 +1,25 @@
問1
CREATE TABLE book_table(book_id INT, book_name VARCHAR(50), author_name VARCHAR(50), price INT, sales_flag INT);
問2
INSERT INTO book_table VALUES (10001, 'TwoPeace', '三田英一郎', 420, 1);
INSERT INTO book_table VALUES (10002, '名探偵金田二', '青山ふみや', 460, 1);
INSERT INTO book_table VALUES (20001, '坊ちゃま', '冬目漱石', 460, 1);
INSERT INTO book_table VALUES (30001, '西方見聞録', '不明', 1000, 1);
INSERT INTO book_table VALUES (40010, '吾輩は犬である', '冬目漱石', 420, 1);
問3
SELECT book_name, author_name FROM book_table;
問4
UPDATE book_table SET author_name = 'マール=コ-=ポロ' WHERE book_id = 30001;
UPDATE book_table SET sales_flag = 0 WHERE book_id = 30001;
問5
SELECT * FROM book_table WHERE sales_flag = 1;
問6
SELECT * FROM book_table WHERE author_name = '冬目漱石' AND price = 420;
問7
DELETE FROM book_table WHERE book_id = 20001;

View File

@@ -0,0 +1,37 @@
問1
CREATE TABLE item_category(category_id INT AUTO_INCREMENT, category_name VARCHAR(256) NOT NULL, PRIMARY KEY (category_id));
INSERT INTO item_category (category_name) VALUES ('家具');
INSERT INTO item_category (category_name) VALUES ('飲料');
INSERT INTO item_category (category_name) VALUES ('食品');
問2
CREATE TABLE items(item_id INT AUTO_INCREMENT, name VARCHAR(256) NOT NULL, price INT NOT NULL, category_id INT, PRIMARY KEY (item_id));
ALTER TABLE items ADD FOREIGN KEY(category_id) REFERENCES item_category(category_id);
INSERT INTO items(name, price, category_id) VALUES('ローマのテーブル', 150000, 1);
INSERT INTO items(name, price, category_id) VALUES('げーたるーど2000 500ml', 370, 2);
INSERT INTO items(name, price, category_id) VALUES('原子肉', 2500, 3);
INSERT INTO items(name, price, category_id) VALUES('魅惑の椅子', 100000, 1);
問3
SELECT item_category.category_id, items.item_id, items.name,items.price, item_category.category_name AS name FROM item_category INNER JOIN items ON item_category.category_id = items.category_id;
問4
SELECT item_category.category_id, items.item_id, items.name,items.price, item_category.category_name AS name FROM item_category INNER JOIN items ON item_category.category_id = items.category_id ORDER BY items.price ASC;
問5
SELECT item_category.category_id, items.item_id, items.name,items.price, item_category.category_name AS name FROM item_category INNER JOIN items ON item_category.category_id = items.category_id WHERE item_category.category_name = '家具';
問6
SELECT item_category.category_id, items.item_id, items.name,items.price, item_category.category_name AS name FROM item_category INNER JOIN items ON item_category.category_id = items.category_id WHERE price >= 1000 AND price <= 50000;
問7
SELECT COUNT(*) AS data_count FROM items;
問8
SELECT item_category.category_id, items.item_id, items.name,items.price, item_category.category_name AS name FROM item_category INNER JOIN items ON item_category.category_id = items.category_id WHERE price = (SELECT MAX(price) FROM items);
問9
SELECT item_category.category_id, items.item_id, items.name,items.price, item_category.category_name AS name FROM item_category INNER JOIN items ON item_category.category_id = items.category_id WHERE name LIKE '%の%';