下载MySQL8.0.37
到MySQL官网,在页面点击Downloads,然后选择最底下的社区版下载,
随后点击
出现这样的界面
然后点击Archives历史的版本,我们下载8.0.37版本
我们选择第一个下载就好。
下载好后我们找到其压缩包解压缩到任意一个盘,我解压缩到了C盘。注意解压缩的路径不能有中文。
第一步:
找到解压缩的文件夹,点击bin文件把路径复制一下。
第二步:
打开系统设置——>系统——>系统信息——>高级系统设置——>环境变量——>选择系统变量下的Path变量编辑它——>新建然后粘贴bin文件的路径,然后点击确定即可
第三步:
进入解压后的文件中,新建一个文本文档.txt(注意打开文件拓展名显示),然后重命名为my.ini
随后复制粘贴下面的内容进入my.ini里边
[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
注意:datadir和basedir中的你文件的路径,实际的和配置文件中的路径一致。
第四步:
以管理员的身份运行命令提示符
初始化mysql80服务
mysqld --initialize --console
注意不要初始化完后不要关闭窗口,找到 root@localhost:xxxxxxx. 冒号后面就是你的初始化密码,如果你关闭了窗口重新初始化会报错。
如果缺少依赖文件:下载VC开发运行库依赖进行安装,如:VisualC_Redistributable
然后安装mysql服务:
mysqld --install
随后启动服务:net start mysql
登录mysql:mysql --uroot -p
找到你的输入密码然后回车即可
为了方便,修改你的初始密码:
格式:
alter user root@localhost identified by'123456'
重新登录,输入新密码
建库格式:
create database 库名;
建完库后养成习惯使用库:
use 库名;
查看当前生效的库:
select database();
然后开始创表,格式为:
create table 表名(表选项)
表选项的格式为:(列名1 列类型1 约束, 列明2 列类型2 约束,--------)
查看表:
desc 表名;
创建employee表:
mysql> create database mydb6_product;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb1_test |
| mydb2_stuinfo |
| mydb3_employee |
| mydb4_product |
| mydb6_product |
| mysql |
| performance_schema |
| sys |
+--------------------+
9 rows in set (0.00 sec)
mysql> use mydb6_product;
Database changed
mysql> select database;
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 '' at line 1
mysql> select database();
+---------------+
| database() |
+---------------+
| mydb6_product |
+---------------+
1 row in set (0.00 sec)
mysql> create table employees(id int primary key, name varchar(50) not null, age int, gender char(10) not null default 'unknown', salary float);
Query OK, 0 rows affected (0.02 sec)
mysql> desc employees;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(50) | NO | | NULL | |
| age | int | YES | | NULL | |
| gender | char(10) | NO | | unknown | |
| salary | float | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql>
创建oredes表
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.02 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>
创建invoices表
mysql> create table invoices(number int primary key, order_id int,foreign key(order_id) references orders(id), in_date date, total_amount float not null default 0.01);
Query OK, 0 rows affected (0.04 sec)
mysql> show tables;
+-------------------------+
| Tables_in_mydb6_product |
+-------------------------+
| employees |
| invoices |
| orders |
+-------------------------+
3 rows in set (0.00 sec)
mysql> desc invoices;
+--------------+-------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------+------+-----+---------+-------+
| number | int | NO | PRI | NULL | |
| order_id | int | YES | MUL | NULL | |
| in_date | date | YES | | NULL | |
| total_amount | float | NO | | 0.01 | |
+--------------+-------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table invoices modify number int auto_increment;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mydb6_product中的全部表的信息
mysql> desc employees;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(50) | NO | | NULL | |
| age | int | YES | | NULL | |
| gender | char(10) | NO | | unknown | |
| salary | float | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.00 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> 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 | NO | | 0.01 | |
+--------------+-------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql>