init
This commit is contained in:
114
【練習問題】 SQL/Question2/【+α】SQL/MySQL_question2.sql
Normal file
114
【練習問題】 SQL/Question2/【+α】SQL/MySQL_question2.sql
Normal file
@@ -0,0 +1,114 @@
|
||||
DROP TABLE IF EXISTS order_items;
|
||||
DROP TABLE IF EXISTS orders;
|
||||
DROP TABLE IF EXISTS items;
|
||||
DROP TABLE IF EXISTS item_category;
|
||||
DROP TABLE IF EXISTS users;
|
||||
|
||||
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 'ユーザテーブル';
|
||||
|
||||
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 'カテゴリテーブル';
|
||||
|
||||
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 '商品テーブル';
|
||||
|
||||
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 '注文テーブル';
|
||||
|
||||
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 '注文商品テーブル';
|
||||
|
||||
|
||||
INSERT INTO users (name, password, age) VALUES ('三浦知一','KfCQDjiZ', 53);
|
||||
INSERT INTO users (name, password, age) VALUES ('遠藤保','V3mngzMP', 40);
|
||||
INSERT INTO users (name, password, age) VALUES ('柴崎学','XktKCvCx', 27);
|
||||
INSERT INTO users (name, password, age) VALUES ('本田啓介','bSCebKnP', 33);
|
||||
INSERT INTO users (name, password, age) VALUES ('花川真司','xKetmAcH', 30);
|
||||
|
||||
INSERT INTO item_category (name) VALUES ('家具');
|
||||
INSERT INTO item_category (name) VALUES ('飲料');
|
||||
INSERT INTO item_category (name) VALUES ('食品');
|
||||
|
||||
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);
|
||||
INSERT INTO items (name, price, category_id) VALUES ('十八茶 600ml', 110, 1);
|
||||
INSERT INTO items (name, price, category_id) VALUES ('いい匂いのするドリアン', 5000, 3);
|
||||
INSERT INTO items (name, price, category_id) VALUES ('太平洋の水 550ml', 300, 2);
|
||||
INSERT INTO items (name, price, category_id) VALUES ('ひたすら膨らむガム', 200, 3);
|
||||
INSERT INTO items (name, price, category_id) VALUES ('人をだめにするソファー', 10000, 1);
|
||||
INSERT INTO items (name, price, category_id) VALUES ('人を虜にする名酒', 20000, 2);
|
||||
INSERT INTO items (name, price, category_id) VALUES ('新鮮キャベツ', 1000, 3);
|
||||
|
||||
INSERT INTO orders (user_id, order_time) VALUES (1, '2015-02-22 12:00:00');
|
||||
INSERT INTO orders (user_id, order_time) VALUES (4, '2015-12-01 15:30:00');
|
||||
INSERT INTO orders (user_id, order_time) VALUES (3, '2018-03-20 18:45:00');
|
||||
INSERT INTO orders (user_id, order_time) VALUES (2, '2018-05-11 11:28:00');
|
||||
INSERT INTO orders (user_id, order_time) VALUES (5, '2018-09-30 10:50:00');
|
||||
INSERT INTO orders (user_id, order_time) VALUES (1, '2018-09-12 20:13:00');
|
||||
INSERT INTO orders (user_id, order_time) VALUES (4, '2018-09-15 21:34:00');
|
||||
INSERT INTO orders (user_id, order_time) VALUES (1, '2019-05-06 16:00:00');
|
||||
INSERT INTO orders (user_id, order_time) VALUES (3, '2019-12-20 10:39:00');
|
||||
INSERT INTO orders (user_id, order_time) VALUES (2, '2020-01-01 14:11:00');
|
||||
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (1, 2, 1);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (1, 6, 10);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (1, 7, 1);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (1, 11, 10);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (2, 4, 1);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (3, 9, 1);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (4, 7, 1);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (4, 8, 1);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (5, 2, 7);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (5, 3, 5);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (5, 10, 1);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (5, 11, 5);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (5, 5, 1);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (5, 7, 1);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (6, 8, 10);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (7, 4, 2);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (8, 1, 1);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (8, 9, 1);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (9, 3, 4);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (9, 5, 2);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (9, 8, 10);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (10, 1, 2);
|
||||
INSERT INTO order_items (order_id, item_id, quantity) VALUES (10, 10, 2);
|
||||
Reference in New Issue
Block a user