安装MySQL并且创建库和表
- 一, windows采用zip安装MySQL
- 1. 卸载已有的MySQL
- 2. 下载zip格式
- 3. 解压缩
- 4. 配置环境变量
- 5. 创建配置文件
- 6. 初始化mysql服务
- 二, 新建产品库mydb6_product,新建三张表
一, windows采用zip安装MySQL
1. 卸载已有的MySQL
注意:
- 卸载时需要通过在windows主页应用处卸载,不能直接删除文件夹;
- 卸载完毕后需要将program files和programdata文件夹里的mysql文件夹删除
2. 下载zip格式
地址:https://dev.mysql.com/downloads/mysql/
3. 解压缩
注意:地址不能有中文
4. 配置环境变量
高级系统设置->环境变量->系统变量->path->新建,然后找到mysql的bin所在文件夹地址复制过来(原来有的直接改就行)->确定。
5. 创建配置文件
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8mb4
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
[mysqld] # 服务端设置
# 设置3306端口
port=3306
# 重要,设置mysql的安装目录
basedir=D:\mysql-8.0.37-winx64
# 重要,设置mysql数据库的数据的存放目录
datadir=D:\mysql-8.0.37-winx64\data
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
6. 初始化mysql服务
二, 新建产品库mydb6_product,新建三张表
mysql> create database mydb6_product;
Query OK, 1 row affected (0.01 sec)mysql> use mydb6_product;
Database changed
mysql> select database();
+---------------+
| database() |
+---------------+
| mydb6_product |
+---------------+
1 row in set (0.00 sec)mysql> create table employee(id int primary key, name varchar(50) not null, age int, gender varchar(10) not null default 'unknown', salary float);
Query OK, 0 rows affected (0.03 sec)mysql> desc employee;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(50) | NO | | NULL | |
| age | int | YES | | NULL | |
| gender | varchar(10) | NO | | unknown | |
| salary | float | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)mysql> create table orders(id int primary key, name varchar(100) not null, price float, quantity int, category varchar(50));
Query OK, 0 rows affected (0.03 sec)mysql> desc orders;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(100) | NO | | NULL | |
| price | float | YES | | NULL | |
| quantity | int | YES | | NULL | |
| category | varchar(50) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)mysql> create table invoices(number int primary key auto_increment, order_id int, in_date date, total_amount float, check(total_amount>0), foreign key(order_id) references orders(id));
Query OK, 0 rows affected (0.03 sec)mysql> desc invoices;
+--------------+-------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------+------+-----+---------+----------------+
| number | int | NO | PRI | NULL | auto_increment |
| order_id | int | YES | MUL | NULL | |
| in_date | date | YES | | NULL | |
| total_amount | float | YES | | NULL | |
+--------------+-------+------+-----+---------+----------------+
4 rows in set (0.00 sec)