下载地址
Select Operating System:Microsoft Windows
快捷下载:mysql-8.0.22-winx64.zip
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
# 设置mysql客户端连接服务端时默认使用的字符集
default-character-set=utf8mb4
注意:复制保存 MySQL初始化密码 fVdpg:bM9pAk
使用上面方式无法登录的解决方案
1、停止 mysql8
net stop mysql82、无密码启动
mysqld --console --skip-grant-tables --shared-memory3、前面窗口不能关闭,再开启一个新的窗口进行无密码登录
mysql -u root -p4、清空密码
update mysql.user set authentication_string='' where user='root' and host='localhost;'5、刷新权限
plush privileges;6、重新启动 mysql 服务,再以无密码登录 mysql
GRANT ALL ON *.* TO ‘root’ @’%’;
# alter user ‘root’@’%’ identified with mysql_native_password by ‘123456’;
FLUSH privileges;
下载地址
Select Operating System:Source Code
Select OS Version:Generic Linux (Architecture Independent)
快捷下载:mysql-8.0.22.tar.gz
注意:复制保存 MySQL初始化密码 fVdpg:bM9pAk
my.cnf
[mysql]
default-character-set=utf8mb4
[client]
port=3306
default-character-set=utf8mb4
GRANT ALL ON *.* TO ‘root’ @’%’;
FLUSH privileges;
CREATE TABLE tb_name (建表的字段、类型、长度、约束、默认、注释)
NOT NULLUNSIGNEDPRIMARY KEYAUTO_INCREMENTDEFAULTCOMMENTTIYINT 1个字节,无符号最大值 256 (2^8 -1),正负 -128 ~ 127 (-2^7 -1 ~ 2^7 -1)SMALLINT 2个字节,无符号最大值 65535 (2^16 – 1),正负 -32768 ~ 32767 (-2^15 – 1 ~ 2^15 – 1)MEDIUMINT 3个字节,无符号最大值 16777215 (2^24 – 1),正负 (-2^23-1 ~ 2^23-1)INT 4个字节,无符号最大值 2^32 -1,正负 (-2^31-1 ~ 2^31-1)BIGINT 8个字节,无符号最大值 2^64 – 1, 正负 (-2^63-1 ~ 2^63-1)FLOAT 4个字节 Float [(M,D)] -3.4E+38~3.4E+38( 约 )DOUBLE 8个字节 Double [(M,D)] -1.79E+308~1.79E+308( 约 )DECIMAL M>D ? M+2 : D+2 个字节 Decimal [(M,D)] 注:M 为长度, D 为小数CHAR 最大保存255个字节,如果值没有达到给定的长度,使用空格补充VARCHAR 最大保存255个字节,用多大长度占多大长度TINYTEXT 最大长度255个字节(2^8-1)MEDIUMTEXT 最大长度 16777215 个字节(2^24-1)TEXT 最大长度65535个字节(2^16-1)LONGTEXT 最大长度4294967295个字节 (2^32-1)DATE 日期(yyyy-mm-dd)TIME 时间(hh:mm:ss)DATETIME 日期与时间组合(yyyy-mm-dd hh:mm:ss)TIMESTAMP yyyymmddhhmmssYEAR 年份yyyytable_name ADD PRIMARY KEY (column),用于唯一标识一条记录table_name ADD UNIQUE (column) 往往不是为了提高访问速度,而是为了避免数据出现重复table_name ADD INDEX index_name (column),唯一任务是加快对数据的访问速度table_name ADD FULLTEXT index_name (column1, column2) ,仅可用于 MyISAM 表,针对较大的数据,生成全文索引很耗时好空间table_name ADD INDEX index_name (column1, column2, column3) ,为了更多的提高mysql效率# 删除唯一索引
ALTER TABLE `table_name` DROP INDEX unique_index_name;
ALTER TABLE `table_name` DROP INDEX cloumn;
# 删除普通索引
ALTER TABLE `table_name` DROP INDEX index_name;
# 删除全文索引
ALTER TABLE `table_name` DROP INDEX fulltext_index_name;
ALTER TABLE `table_name` DROP INDEX cloumn;
# 全表插入
INSERT INTO `tb_user`(`user_name`, `user_birthday`, `user_gender`, `user_status`, `user_height`, `user_desc`) VALUES (‘曾小贤’, ‘2020-11-22’, ‘男’, 1, 174.5, ‘好男人就是我,我就是好男人曾小贤’);
# 指定列插入,前提是其他列没有非空的约束
INSERT INTO `tb_user`(`user_name`, `user_birthday`, `user_gender`, `user_status`, `user_height`) VALUES (‘胡小梅’, ‘2020-11-22’, ‘女’, 1, 174.5);
UPDATE `tb_user` SET user_status = 1 where user_id > 1;
UPDATE `tb_user` SET user_status = 1;
# 不带条件查询
select * from tb_user;
select user_id,user_name from tb_user;
# 带条件查询 (比较运算 >, <, >=, <=, !=, <>, =)
select * from tb_user where user_id > 1;
# 带逻辑条件查询 (and,or)
select * from tb_user where user_status = 1 and user_id > 1;
select * from tb_user where user_id = 1 or user_name = ‘胡小梅’;
# 模糊查询 (like %%)
select * from tb_user where user_name like ‘曾%’;
select * from tb_user where user_name like ‘%闲’;
select * from tb_user where user_name like ‘%小%’;
# 范围查询
select * from tb_user where tb_status in (0,1,2);
# 聚合函数
— count(field)
select count(user_id) 用户数量 from tb_user;
— sum(field)
select sum(user_height) 总身高 from tb_user;
— avg(field)
select avg(user_height) 平均身高 from tb_user;
…
# 分组查询
— group by 统计男女的平均身高: group by 查询中出现的字段必须是 group by 后面的字段
select user_gender as 性别,avg(user_height) 平均身高 from tb_user group by user_gender;
select user_status,user_gender as 性别,avg(user_height) 平均身高 from tb_user group by user_gender,user_status;
select user_gender as 性别,avg(user_height) 平均身高,sum(user_height),count(user_id) 用户数量 from tb_user group by user_gender;
# 排序查询
— order by 默认是 asc 升序, desc 降序; order by 是放在 group by 之后的
select * from tb_user order by user_id asc;
select * from tb_user order by user_id desc;
select * from tb_user where user_id < 10 order by user_id desc;
select * from tb_user where user_id < 10 order by user_id,user_status desc;
select user_gender as 性别,avg(user_height) 平均身高,sum(user_height),count(user_id) 用户数量 from tb_user group by user_gender order by 用户数量;
— 插入测试数据
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(1,1,89.0);
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(1,2,78.0);
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(1,3,94.0);
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(1,4,77.0);
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(1,5,99.0);
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(3,1,90.0);
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(3,2,88.0);
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(3,3,69.0);
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(3,4,83.0);
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(3,5,92.0);
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(2,1,77.0);
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(2,2,84.0);
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(2,3,91.0);
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(2,4,80.0);
INSERT INTO tb_score (`stu_id`, `cou_id`, `score`) VALUES(2,5,99.0);
# 分页查询
— 查询科目id为1的最高成绩
select max(score) from tb_score where course_id = 1;
select * from tb_score where course_id = 1 limit 1;
— 查询课程id为4的前五名成绩信息
select * from tb_score where course_id = 4 order by score limit 5;
— limit 分页, 起始值是 0: (pageIndex – 1) * pageSize, pageSize
select * from tb_score limit 0,10
select * from tb_score limit 10,10
select * from tb_score limit 20,10
到此这篇关于MySQL8数据库安装及SQL语句详解的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。