Files
db-lesson/【練習問題】 SQL/Question1/MySQL SQL問題集 part2.txt
2026-09-28 16:09:47 +09:00

139 lines
7.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)