目录
第1关:从概念模型到MySQL实现
任务描述
将已建好的概念模型,变成MySQL物理实现。
sql
# 请将你实现flight_booking数据库的语句写在下方:
# 请将你实现flight_booking数据库的语句写在下方:
drop database if exists flight_booking;
create database flight_booking;
use flight_booking;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS airline ; CREATE TABLE airline ( airline_id int NOT NULL AUTO_INCREMENT, name varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, iata char(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, airport_id int NOT NULL, PRIMARY KEY ( airline_id ) USING BTREE, UNIQUE INDEX iata_unq ( iata ) USING BTREE, INDEX base_airport_idx ( airport_id ) USING BTREE, CONSTRAINT airline_ibfk_1 FOREIGN KEY ( airport_id ) REFERENCES airport ( airport_id ) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
DROP TABLE IF EXISTS airplane ; CREATE TABLE airplane ( airplane_id int(0) NOT NULL AUTO_INCREMENT, type varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, capacity smallint(0) NOT NULL, identifier varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, airline_id int(0) NOT NULL,
PRIMARY KEY ( airplane_id ) USING BTREE, INDEX airplane_ibfk_1 ( airline_id ) USING BTREE, CONSTRAINT airplane_ibfk_1 FOREIGN KEY ( airline_id ) REFERENCES airline ( airline_id ) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
DROP TABLE IF EXISTS airport ; CREATE TABLE airport ( airport_id int NOT NULL AUTO_INCREMENT, iata char(3) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, icao char(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, name varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, city varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, country varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, latitude decimal(11, 8) NULL DEFAULT NULL, longitude decimal(11, 8) NULL DEFAULT NULL, PRIMARY KEY ( airport_id ) USING BTREE, UNIQUE INDEX iata_unq ( iata ) USING BTREE, UNIQUE INDEX icao_unq ( icao ) USING BTREE, INDEX name_idx ( name ) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
DROP TABLE IF EXISTS passenger ; CREATE TABLE passenger ( passenger_id int NOT NULL AUTO_INCREMENT, id char(18) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, firstname varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, lastname varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, mail varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, phone varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, sex char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, dob date NULL DEFAULT NULL, PRIMARY KEY ( passenger_id ) USING BTREE, UNIQUE INDEX id_unq ( id ) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
DROP TABLE IF EXISTS ticket ; CREATE TABLE ticket ( ticket_id int NOT NULL AUTO_INCREMENT, flight_id int NOT NULL, seat char(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, passenger_id int NOT NULL, price decimal(10, 2) NOT NULL, user_id int NOT NULL, PRIMARY KEY ( ticket_id ) USING BTREE, INDEX flight_idx ( flight_id ) USING BTREE, INDEX passenger_idx ( passenger_id ) USING BTREE, INDEX ticket_ibfk_3 ( user_id ) USING BTREE, CONSTRAINT ticket_ibfk_1 FOREIGN KEY ( flight_id ) REFERENCES flight ( flight_id ) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT ticket_ibfk_2 FOREIGN KEY ( passenger_id ) REFERENCES passenger ( passenger_id ) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT ticket_ibfk_3 FOREIGN KEY ( user_id ) REFERENCES user ( user_id ) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
DROP TABLE IF EXISTS user ; CREATE TABLE user ( user_id int NOT NULL AUTO_INCREMENT, firstname varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, lastname varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, dob date NOT NULL, sex char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, email varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
phone varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, username varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, password char(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, admin_tag tinyint NOT NULL DEFAULT 0, PRIMARY KEY ( user_id ) USING BTREE, UNIQUE INDEX user_unq ( username ) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
DROP TABLE IF EXISTS flightschedule ; CREATE TABLE flightschedule ( flight_no char(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, `from` int NOT NULL, `to` int NOT NULL, departure time NOT NULL, arrival time NOT NULL, duration smallint NOT NULL,
airline_id int NOT NULL, monday tinyint NULL DEFAULT 0,
tuesday tinyint NULL DEFAULT 0,
wednesday tinyint NULL DEFAULT 0,
thursday tinyint NULL DEFAULT 0, friday tinyint NULL DEFAULT 0, saturday tinyint NULL DEFAULT 0, sunday tinyint NULL DEFAULT 0,
PRIMARY KEY ( flight_no ) USING BTREE,
INDEX from_idx ( `from` ) USING BTREE,
INDEX to_idx ( `to` ) USING BTREE,
INDEX airline_idx ( airline_id ) USING BTREE,
CONSTRAINT flightschedule_ibfk_1 FOREIGN KEY ( `from` ) REFERENCES airport ( airport_id ) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT flightschedule_ibfk_2 FOREIGN KEY ( `to` ) REFERENCES airport ( airport_id ) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT flightschedule_ibfk_3 FOREIGN KEY ( airline_id ) REFERENCES airline ( airline_id ) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
DROP TABLE IF EXISTS flight ; CREATE TABLE flight ( flight_id int NOT NULL AUTO_INCREMENT, flight_no char(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, `from` int NOT NULL, `to` int NOT NULL,
departure datetime NOT NULL, arrival datetime NOT NULL, duration smallint NOT NULL, airline_id int NOT NULL, airplane_id int NOT NULL,
PRIMARY KEY ( flight_id ) USING BTREE,
INDEX from_idx ( `from` ) USING BTREE,
INDEX to_idx ( `to` ) USING BTREE,
-- INDEX departure_idx ( departure ) USING BTREE,
-- INDEX arrivals_idx ( arrival ) USING BTREE,
INDEX airline_idx ( airline_id ) USING BTREE,
INDEX airplane_idx ( airplane_id ) USING BTREE,
INDEX flightno ( flight_no ) USING BTREE,
CONSTRAINT flight_ibfk_1 FOREIGN KEY ( `from` ) REFERENCES airport ( airport_id ) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT flight_ibfk_2 FOREIGN KEY ( `to` ) REFERENCES airport ( airport_id ) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT flight_ibfk_3 FOREIGN KEY ( airline_id ) REFERENCES airline ( airline_id ) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT flight_ibfk_4 FOREIGN KEY ( airplane_id ) REFERENCES airplane ( airplane_id ) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT flight_ibfk_5 FOREIGN KEY ( flight_no ) REFERENCES flightschedule ( flight_no ) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
第2关:从需求分析到逻辑模型
任务描述
本关任务: 根据应用场景业务需求描述,完成ER图,并转换成关系模式。 提交设计文档
sql
请给出ER图文件存放的URL:
https://s1.wzznft.com/i/2023/12/01/ersolution.png
以下给出关系模式:
电影(movie)(movie_ID, title, type, runtime, release_date, director, starring), 主码:(movie_ID)
顾客(customer)(c_ID, name, phone), 主码:(c_ID)
放映厅(hall)(hall_ID, mode, capacity, location), 主码:(hall_ID)
排场(schedule)(schedule_ID, date, time, price, number, hall_ID, movie_ID), 主码:(schedule_ID),外码:(hall_ID) 参照放映厅(hall),(movie_ID) 参照电影(movie)
电影票(ticket)(ticket_ID, seat_num, c_ID, schedule_ID), 主码:(ticket_ID),外码:(c_ID) 参照顾客(customer),(schedule_ID) 参照排场(schedule)
# 请将利用MySQL Workbench软件的Modeling工具,经forward engineering 导出的创建schema的SQL语句完整粘到此处:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE,
SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
CREATE SCHEMA IF NOT EXISTS rbac DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci ;
USE rbac ;
CREATE TABLE IF NOT EXISTS rbac . aprole (
RoleNo INT NOT NULL COMMENT '角色编号',
RoleName CHAR(20) NOT NULL COMMENT '角色名',
Comment VARCHAR(50) NULL DEFAULT NULL COMMENT '角色描述',
Status SMALLINT NULL DEFAULT NULL COMMENT '角色状态',
PRIMARY KEY ( RoleNo ))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci
COMMENT = '角色表';
CREATE TABLE IF NOT EXISTS rbac . apuser(
UserID CHAR(8) NOT NULL COMMENT '用户工号',
UserName CHAR(8) NULL DEFAULT NULL COMMENT '用户姓名',
Comment VARCHAR(50) NULL DEFAULT NULL COMMENT '用户描述',
PassWord CHAR(32) NULL DEFAULT NULL COMMENT '口令',
Status SMALLINT NULL DEFAULT NULL COMMENT '状态',
PRIMARY KEY ( UserID ),
UNIQUE INDEX ind_username ( UserName ASC) VISIBLE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci
COMMENT = '用户表';
CREATE TABLE IF NOT EXISTS rbac . apgroup (
UserID CHAR(8) NOT NULL COMMENT '用户编号',
RoleNo INT NOT NULL COMMENT '角色编号',
PRIMARY KEY ( UserID , RoleNo ),
INDEX FK_apGroup_apRole ( RoleNo ASC) VISIBLE,
CONSTRAINT FK_apGroup_apRole FOREIGN KEY ( RoleNo ) REFERENCES rbac . aprole ( RoleNo ),
CONSTRAINT FK_apGroup_apUser FOREIGN KEY ( UserID ) REFERENCES rbac . apuser ( UserID ))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci
COMMENT = '角色分配表';
CREATE TABLE IF NOT EXISTS rbac . apmodule (
ModNo BIGINT NOT NULL COMMENT '模块编号',
ModID CHAR(10) NULL DEFAULT NULL COMMENT '系统或模块的代码',
ModName CHAR(20) NULL DEFAULT NULL COMMENT
第3关:建模工具的使用
任务描述
本关任务: 将一个建好的模型文件,利用MySQL Workbench的forward engineering功能,自动转换成SQL脚本。
sql
# 请将利用MySQL Workbench软件的Modeling工具,经forward engineering 导出的创建schema的SQL语句完整粘到此处:
# 请将利用MySQL Workbench软件的Modeling工具,经forward engineering 导出的创建schema的SQL语句完整粘到此处:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE,
SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
CREATE SCHEMA IF NOT EXISTS rbac DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci ;
USE rbac ;
CREATE TABLE IF NOT EXISTS rbac . aprole (
RoleNo INT NOT NULL COMMENT '角色编号',
RoleName CHAR(20) NOT NULL COMMENT '角色名',
Comment VARCHAR(50) NULL DEFAULT NULL COMMENT '角色描述',
Status SMALLINT NULL DEFAULT NULL COMMENT '角色状态',
PRIMARY KEY ( RoleNo ))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci
COMMENT = '角色表';
CREATE TABLE IF NOT EXISTS rbac . apuser(
UserID CHAR(8) NOT NULL COMMENT '用户工号',
UserName CHAR(8) NULL DEFAULT NULL COMMENT '用户姓名',
Comment VARCHAR(50) NULL DEFAULT NULL COMMENT '用户描述',
PassWord CHAR(32) NULL DEFAULT NULL COMMENT '口令',
Status SMALLINT NULL DEFAULT NULL COMMENT '状态',
PRIMARY KEY ( UserID ),
UNIQUE INDEX ind_username ( UserName ASC) VISIBLE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci
COMMENT = '用户表';
CREATE TABLE IF NOT EXISTS rbac . apgroup (
UserID CHAR(8) NOT NULL COMMENT '用户编号',
RoleNo INT NOT NULL COMMENT '角色编号',
PRIMARY KEY ( UserID , RoleNo ),
INDEX FK_apGroup_apRole ( RoleNo ASC) VISIBLE,
CONSTRAINT FK_apGroup_apRole FOREIGN KEY ( RoleNo ) REFERENCES rbac . aprole ( RoleNo ),
CONSTRAINT FK_apGroup_apUser FOREIGN KEY ( UserID ) REFERENCES rbac . apuser ( UserID ))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci
COMMENT = '角色分配表';
CREATE TABLE IF NOT EXISTS rbac . apmodule (
ModNo BIGINT NOT NULL COMMENT '模块编号',
ModID CHAR(10) NULL DEFAULT NULL COMMENT '系统或模块的代码',
ModName CHAR(20) NULL DEFAULT NULL COMMENT '系统或模块的名称', PRIMARY KEY ( ModNo ))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci
COMMENT = '功能模块登记表';
CREATE TABLE IF NOT EXISTS rbac . apright (
RoleNo INT NOT NULL COMMENT '角色编号',
ModNo BIGINT NOT NULL COMMENT '模块编号',
PRIMARY KEY ( RoleNo , ModNo ),
INDEX FK_apRight_apModule ( ModNo ASC) VISIBLE,
CONSTRAINT FK_apRight_apModule
FOREIGN KEY ( ModNo )
REFERENCES rbac . apmodule ( ModNo ),
CONSTRAINT FK_apRight_apRole
FOREIGN KEY ( RoleNo )
REFERENCES rbac . aprole ( RoleNo ))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci
COMMENT = '角色权限表';
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;