1. 下载MySQL-server
Windows(x86, 32-bit), MSI Installer下载地址:
MySQL :: MySQL Community Downloads
2. 安装
2.1 case选择
Server only Client only Full Custom
2.2 Authentication Method
Strong Password
2.3 root密码设置
2.4 自启动设置
windows service name: MySQL8, 可以修改,设置多个服务
Start the MySQL at System Startup 勾选
3. 配置环境变量
复制bin目录的路径(默认路径):C:\Program Files\MySQL\MySQL Server 8.0\bin
编辑系统环境变量 - 环境变量... - 用户环境变量 - path(双击) - 新建
4. mysql 确认安装成功
cmd查看mysql版本和进行数据库操作
mysql -V
mysql -u root -p
5. 常用操作
5.1 数据库日常操作
show databases;
use database;
设置字符集和校对规则(排序规则):
create database mypen default charset utf8mb4 default collate utf8mb4_general_ci;
gbk_chinese_ci 简体中文,不区分大小写
utf8_general_ci Unicode(多语言),不区分大小写
Unicode 万国码 4个字节
Utf8 Unicode的可变长度的字符编码 1-6字节
5.2 表的日常操作
create table tbname (id int primary key, name varchar(16), pwd char(32));
如何建表时,指定innodb, charset, primary key, index, fulltext, unique, auto_increment
desc table;
修改表名
alter table tbname rename user;
删除表
drop table tbname;
5.3 表结构的操作
添加字段
alter table tbname add (id int primary key, age int);
修改字段类型
alter table tbname modify name varchar(64)
修改字段名称
alter table tbname change tel phone varchar(11);
删除字段
alter table tbname drop f;
6. 字段类型
6.1 整型
数据类型 | 字节 | 取值范围 |
---|---|---|
tinyint | 1 | -128-127 |
smallint | 2 | -32768-32767 |
mediumint | 3 | -8388608-8388607 |
int | 4 | -2147483648-2147483647 |
bigint | 8 | -+9.22*10的18次方 |
6.2 小数
数据类型 | 字节 | 取值范围 |
float(m, d) | 4 | 单精度 |
double(m, d) | 8 | 双精度浮点,m总个数, d小数位 |
decimal(m, d) | 定点类型,适用精度要求高的数据 |
6.3 字符
数据类型 | 字节 | 取值范围 |
char | 0-255 | 定长字符串,不足空格补,超出截掉 |
varchar | 0-65535 | 变长字符串,超出截掉 |
text | 0-65535 | 长文本数据 |
6.4 时间
数据类型 | 字节 | 取值范围 |
date | 4 | 2014-09-09 |
time | 3 | 08:08:30 |
datetime | 8 | 2014-09-09 08:08:30 |
timestamp | 4 | |
year | 1 | 1901-2155 |
6.5 复合类型
数据类型 | 字节 | 取值范围 |
set | 集合类型 | set("aihao1", "aihao2") |
enum | 枚举类型 | enum("male", "female") 多选一 |
7. 索引
唯一索引,不允许有重复的值
主键索引,常用于id,类似于书的页码
全文索引, 适用全局搜索的, 常借助第三方组件,去实现
普通索引
索引越多,写入修改数据的速度就越慢。
create table user (id int, name varchar(32), tel char(11), email varchar(64), pwd varchar(32));
show index from user;
alter table user add index(name); KEY = MUL Index_type = BTREE
alter table user add unique(tel); KEY = UNI Index_type = BTREE
alter table user add fulltext(email); KEY = MUL Index_type = FULLTEXT
alter table user add primary key(id); KEY = PRI Index_type = BTREE
创建表时,同时指定索引,指定字符集。
create table user6 (id int auto_increment, name char(32), tel varchar(11), primary key(id), index(name), unique(tel))engine = innodb default charset utf8mb4;