题目要求:
1、安装MySQL:
共有三种方式:
(1)mis安装:MySQL :: Download MySQL Installer (Archived Versions)(版本选择8.0.37)
(2)zip格式安装:MySQL :: Download MySQL Community Server(版本选择8.0.37)
(3)二进制包安装:MySQL :: Download MySQL Community Server(版本选择8.0.37)
注意:通过mis安装MySQL后需要先卸载,再使用zip格式安装,二进制包安装则需要在Linux上安装)
2、创建mydb6_product库
create database mydb6_product;
创建成功后使用 show databases; 查看创建的数据库如下图:
3、创建employees表
(1)use mydb6_product;
(2)
create table employees(
-> id int primary key,
-> name varchar(50) not null,
-> age int,
-> gender varchar(10) not null default 'unknown',
-> salary float(5,2)) ;
desc employees;
使用desc查看表结构如下图:
4、创建orders表
(1)use mydb6_product;
(2)
create table orders(
-> id int primary key,
-> name varchar(100) not null,
-> price float,
-> quantity int,
-> category varchar(50));
(3)使用desc orders如下图:
5、创建oinvoices表
(1)use mydb6_product;
(2)
create table invoices(
-> number int primary key auto_increment,
-> order_id int not null,
-> in_date date,
-> total_amount float(5,2) check(total_amount>0),
-> foreign key(order_id) references orders(id));
使用desc invoices;
总结:注意创建表之前要使用库 use 库名字 创建invoices表时,关联外键要等到最后一步,在执行命令。