MySQL 基础一(B站黑马程序员MySQL教程笔记)
目录
一、MySQL概述
二、SQL
1.SQL通用语法
2.SQL分类
DDL
(1)数据库操作
(2)表操作
DML
DQL
语法
执行顺序
DCL
管理用户
权限控制
三、函数
一、MySQL概述
1.基本概念
- 数据库:数据存储的仓库
- 数据库管理系统:操纵和管理数据库的大型软件
- SQL:操作关系型数据库的编程语言,是一套标准
2.主流关系型数据库管理系统(操作语言均是SQL)
- ORACLE:收费
- MYSQL:开源免费的中小型数据库,社区版本不收费
- SQLServer:微软研发的中型数据库,收费
- PostgreSQL:开源免费的中小型数据库
3.启动与连接
- 启动,停止:windows 搜索services.msc--mysql80
- 客户端连接:菜单-MySQL-输入密码123456
4.
- 关系型数据库:建立在关系模型基础上,由多张相互连接的二维表组成的数据库。
- 一个数据库服务器中可以创建多个数据库,一个数据库中可以创建多张表
二、SQL
1.SQL通用语法
- 可以单行或多行书写,以分号(英文)结尾
- 可以使用空格、缩进来增强语句的可读性
- MySQL数据库的SQL语句不区分大小写,关键字建议使用大写。
- 单行注释:--注释内容或#注释内容;多行注释/*注释内容*/
2.SQL分类
- DDL:数据定义语言,用来定义数据库对象(数据库,表,字段)
- DML:数据操作语言,用来对数据库表中数据进行增删改
- DQL:数据查询语言,用来查询数据库中表的记录
- DCL:数据控制语言,用来创建数据库用户,控制数据库的访问权限
DDL
(1)数据库操作
- 查询:查询所有数据库:show databases;查询当前数据库:select database();
- 创建:create database[if not exists]数据库名[default charset字符集][collate排序规则]#方括号表可选
- 删除:drop database[if exists]数据库名;
- 使用:use 数据库名
(2)表操作
- 查询当前数据库所有表:show tables;
- 查询表结构:desc表名;
- 查询指定表的建表语句:show create table 表名;
- 创建:
create table 表名(字段1 字段1类型[comment 字段1注释],字段2 字段2类型[comment 字段1注释],字段3 字段3类型[comment 字段1注释],……字段n 字段n类型[comment 字段1注释],)[comment 表注释];
#最后一个字段后面没有逗号mysql> create table tb_user(-> id int comment '编号',-> name varchar(50) comment '姓名',-> age int comment '年龄',-> gender varchar(1) comment '性别'-> ) comment '用户表';
Query OK, 0 rows affected (0.08 sec)mysql> desc tb_user;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int | YES | | NULL | |
| gender | varchar(1) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)mysql> show create table tb_user;
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_user | CREATE TABLE `tb_user` (`id` int DEFAULT NULL COMMENT '编号',`name` varchar(50) DEFAULT NULL COMMENT '姓名',`age` int DEFAULT NULL COMMENT '年龄',`gender` varchar(1) DEFAULT NULL COMMENT '性别'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户表' |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.02 sec)mysql>
数据类型
举例:age tinyint unsigned(年龄一般不会超过四位,不会为负数)
score double(4,1)(分数一般取一位小数,个数不会超过4位数)
举例:char(10)-----性能好 性别 gender char(1)一直占10个位
varchar(10)-----性能较差 用户名 username varchar(50) 有几个字符占几个位
举例:birthday data
create table emp(id int,workno varchar(10),name varchar(10),gender char(1),age tinyint unsigned,idcard char(18),entrydate date
) comment '员工表';
修改
添加字段 | alter table 表名 add 字段名 类型(长度)[comment 注释][约束]; |
修改数据类型 | alter table 表名 modify 字段名 新数据类型(长度); |
修改字段名和字段类型 | alter table 表名change 旧字段名 新字段名 类型(长度)[comment 注释][约束]; |
删除字段 | alter table 表名 drop 字段名; |
修改表名 | alter table 表名 rename to 新表名; |
alter table emp add nickname varchar(20);
alter table emp change nickname username varchar(30);
mysql> alter table emp drop nickname;
mysql> alter table emp rename to employee;
删除
删除表 | drop table[if exist]表名; |
删除指定表,并重新创建该表 | truncate table 表名;#删除了数据,留下了框架 |
DML
添加数据
给指定字段添加数据 | insert into 表名(字段名1,字段名2,……) values(值1,值2,……); |
给全部字段添加数据 | insert into 表名 values (值1,值2,……); |
批量添加数据 |
insert into 表名(字段名1,字段名2,……)values(值1,值2,……),(值1,值2,……); |
insert into 表名 values(值1,值2,……),(值1,值2,……); |
修改数据:update 表名 set 字段名1=值1,字段名2=值2,……[where 条件];#没有条件就是全部修改
update user set name ='ittest' where id=1;
删除数据:delect from 表名[where 条件]
delete from user where age=18;
DQL
语法
- 基本查询
查询多个字段 | select 字段1,字段2,字段3……from 表名; |
查询所有字段 | select*from 表名; |
设置别名 | select 字段1[as 别名1],字段2[as 别名2]……from 表名; |
去除重复记录 | select distinct 字段列表 from 表名; |
create table emp(id int,workno varchar(10),name varchar(10),gender char(1),age tinyint unsigned,idcard char(18),entrydate date,worksddress varchar(50)
) comment '员工表';insert into emp (id, workno, name, gender, age, idcard, entrydate, worksddress) values (1,'1','刘艳','女',20,'12345678909876','2000-01-01','北京'),(2,'2','刘小艳','女',21,'12345678809876','2006-01-01','北京'),(3,'3','刘近','男',24,'12333678909876','2001-05-01','河南'),(4,'4','丽丽','女',25,'44345678909876','2005-01-01','上海'),(5,'5','黄丽','女',23,'22775678909876','2004-05-01','南昌'),(6,'6','金小','女',27,'32348878909876','2009-05-01','北京'),(7,'7','张艳','女',20,'42345678909876','2010-01-01','北京'),(8,'8','小力','男',20,'52345678909876','2022-07-01','北京'),(9,'9','宏光','男',20,'62345678909876','2013-01-01','江苏'),(10,'10','勇士','男',20,'72345678909876','2014-08-01','北京'),(11,'11','故里','女',20,'82345678909876','2011-01-01','北京'),(12,'12','范嘉','女',23,'92345678909876','2017-07-01','沈阳'),(13,'13','王丹','女',24,'23345678909876','2006-06-01','北京'),(14,'14','诚实','男',26,'45345678909876','2000-07-01','大连'),(15,'15','黄亮','男',28,'78345678909876','2009-07-01','北京'),(16,'16','周芷若','女',20,null,'2009-01-01','北京');
select name,workno,age from emp;
select*from emp;
select worksddress from emp;
select worksddress as '工作地址' from emp;
select distinct worksddress as '工作地址' from emp;
- 条件查询
语法 | select 字段列表 from 表名 where 条件列表; |
<>或!= | 不等于 |
between and | 某个范围之内(含最小值,最大值) |
in() | 在in之后的列表中的值,多选一 |
like 占位符 | 模糊匹配(_匹配单个字符,%匹配任意个字符) |
is null | 是null |
and或&& | 并且 |
or 或|| | 或者 |
not或 ! | 非,不是 |
select *from emp where age=20;
select * from emp where age<26;
select*from emp where idcard is null;
select *from emp where idcard is not null;
select *from emp where age!=20;
select *from emp where age>=23 and age<=28;
select *from emp where age between 23 and 28;
select *from emp where gender='女' and age<23;
select *from emp where age=20 or age=21 or age=23;
select *from emp where age in (20,21,23);
select *from emp where name like '___';
select *from emp where idcard like '%6';
- 聚合函数:将一列数据作为一个整体,进行纵向计算
语法 | select 聚合函数(字段列表)from 表名; |
count | 统计数量 |
max | 最大值 |
min | 最小值 |
avg | 平均值 |
sum | 求和 |
select count(*) from emp;#null不参与聚合函数计算
select avg(age) from emp;
select max(age) from emp;
select sum(age) from emp where worksddress='北京';
- 分组查询
语法 | select 字段列表 from 表名[where 条件] group by 分组字段[having 分组后过滤条件]; | |
where和having区别 |
执行时机不同:where是分组前进行过滤,不满足where条件,不参与分组,having是分组后对结果进行过滤 判断条件不同:where不能对聚合函数进行判断,而having可以 |
|
注意 | ||
执行顺序:where>聚合函数>having | ||
分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无意义 |
select gender,count(*) from emp group by gender;
select gender,avg(age) from emp group by gender;
select worksddress,count(*) from emp where (age<45) group by worksddress having count(*)>=4;
- 排序查询
语法 select 字段列表 from 表名 order by 字段1 排序方式1,字段2 排序方式2; 排序方式 asc:升序(默认)desc:降序
select * from emp order by age asc;
select * from emp order by entrydate desc;
select * from emp order by age asc,entrydate desc;
- 分页查询
语法 | select 字段列表 from 表名 limit 起始索引,查询记录数; |
注意 |
起始索引从0开始,起始索引=(查询页码-1)*每页显示记录数 分页查询是数据库的方言,不同数据库有不同的实现,MySQL中是limit 如果查询的是第一页数据,起始索引可以省略,直接简写为limit 查询记录数 |
select * from emp limit 0,10;#查询第一页员工信息,每页展示10条记录
select * from emp limit 10,10;#查询第二页员工信息,每页展示10条记录#练习
select * from emp where gender = '女' and age in(20,21,22,23);
select * from emp where gender='女' and (age between 20 and 40) and name like '___';
select gender,count(*) from emp where age<60 group by gender;
select name,age from emp where age<=35 order by age asc,entrydate desc;
select * from emp where gender='男' and (age between 20 and 40) order by age asc,entrydate desc limit 5;
执行顺序
from 表名列表 --where 条件列表-- group by 分组字段列表--having 分组后条件列表--select 字段列表--order by 排序字段列表--limit 分页字段列表
DCL
管理用户
查询用户 |
use mysql; select * from user; |
创建用户 | creat user '用户名'@‘主机名’ identified by '密码'; |
修改用户密码 | alter user ‘用户名’@‘主机名’ identified with mysql_native_password by '密码'; |
删除用户 | drop user '用户名'@‘主机名’; |
#创建用户itcast,只能够在当前主机localhost访问,密码123456;
create user 'itcast'@'localhost' identified by '123456';
#创建用户heima,可以在任意主机访问该数据库,密码123456;
create user 'heima'@'%' identified by '123456';
#修改用户heima 的访问密码为1234;
alter user 'heima'@'%' identified with mysql_native_password by '1234';
#删除itcast@localhost用户
drop user 'itcast'@'localhost';
权限控制
查询权限 | show grants for '用户名'@‘主机名’; |
授予权限 | grant 权限列表 on 数据库名.表名 '用户名'@'主机名'; |
撤销权限 | revoke 权限列表 on 数据库名.表名 from ‘用户名’@‘主机名’ |
权限(多个权限间用逗号分隔) | 说明 |
all,all privileges | 所有权限 |
select | 查询数据 |
insert | 插入数据 |
update | 修改数据 |
delete | 删除数据 |
alter | 修改表 |
drop | 删除数据库/表/视图 |
create | 创建数据库/表 |
show grants for 'heima'@'%';
grant all on test.* to 'heima'@'%';
#windows搜索cmd 输入MySQL -u heima -p回车再输入密码1234回车用户连接成功可以查询相关权限
revoke all on test.* from 'heima'@'%';
三、函数
(1)字符串函数
函数 | 功能 |
concat(s1,s2,...,sn) | 字符串拼接,将s1,s2,...sn拼接成一个字符串 |
lower(str) | 将字符串str全部转为小写 |
upper(str) | 将字符串str全部转为大写 |
lpad(str,n,pad) | 左填充,用字符串pad对str的左边进行填充,达到n个字符串长度 |
rpad(str,n,pad) | 右填充,用字符串pad对str的右边进行填充,达到n个字符串长度 |
trim(str) | 去掉字符串头部和尾部的空格 |
substring(str,start,len) | 返回从字符串str从start位置起的len个长度的字符串 |
select concat('hello','mysql');#hellomysql
select lower('Hello');#hello
select lpad('01','5','-');#---01
select trim(' hello mysql ');#hello mysql
select substring('hello mysql',1,5);#hello
update emp set workno =lpad(workno,5,'0');
(2)数值函数
函数 | 功能 |
ceil(x) | 向上取整 |
floor(x) | 向下取整 |
mod(x/y) | 返回x/y的模 |
rand() | 返回0-1内的随机数 |
round(x,y) | 求参数x的四舍五入的值,保留y位小数 |
select ceil(1.5);#2
select floor(1.1);#1
select mod(7,4);#3
select rand();
select round(2.345,2)#2.35
select lpad(round(rand()*1000000,0),6,'0');#随机生成6位数字的密码
(3)日期函数
函数 | 功能 |
curdate() | 返回当前日期 |
curtime() | 返回当前时间 |
now() | 返回当前日期和时间 |
year(date) | 获取date的年份 |
month(date) | 获取date的月份 |
day(date) | 获取date的日期 |
date_add(date,interval exper type) | 返回日期/时间值加上一个时间间隔exper后的时间值 |
datediff(date1,date2) | 返回起始时间date1和结束时间date2之间的天数 |
select curdate();
select curtime();
select now();
select year(now());
select month(now());
select day(now());
select date_add(now(),interval 70 day);
select datediff('2021-12-1',curdate());#-168
select name,datediff(curdate(),entrydate) as'entrydays' from emp order by entrydays desc;
(4)流程函数
函数 | 功能 |
if(value,t,f) | 如果value为true,则返回t,否则返回f |
ifnull(value1,value2) | 如果value1不为空,返回value1,否则返回value2 |
case when [val1] then [res1]... else[defult] end | 如果val为true,返回res1,..否则返回default默认值 |
case [exper] when [val1] then [res1]...else[default] end | 如果exper 的值等于val1,返回res1,...否则返回default 默认值 |
select if(true,'ok','error');
select ifnull(null,'default');
select name,(case worksddress when '北京' then '一线城市' when '上海' then '一线城市' else '二线城市'end) as '工作地址' from emp;
MySQL 基础一(B站黑马程序员MySQL教程笔记)相关推荐
- C++学习笔记(B站黑马程序员C++教程)
C++学习笔记(B站黑马程序员C++教程) 简介 视频链接https://www.bilibili.com/video/BV1kV411U7Ub?spm_id_from=333.999.0.0 建议用 ...
- b站黑马程序员python教程飞机大战源码
plane_main.py import pygame.timefrom plane_sprites import * pygame.init()class PlaneGame:"" ...
- Python学习笔记(1)---B站黑马程序员
Python学习笔记(1)-B站黑马程序员 Python学习笔记(2)-B站黑马程序员 Python学习笔记(3)-B站黑马程序员 文章目录 Linux基础 Python基础 一.Python介绍 0 ...
- Python学习笔记(3)---B站黑马程序员
Python学习笔记(1)-B站黑马程序员 Python学习笔记(2)-B站黑马程序员 Python学习笔记(3)-B站黑马程序员 文章目录 五.项目实战--飞机大战 001-Pycharm中安装py ...
- Visual Studio 2022版本 B站黑马程序员C++自学分享-第一阶段(主要包括:自己敲的代码、通过注释来备注上自己对代码的理解)
Visual Studio 2022版本 B站黑马程序员C++自学分享-第一阶段(主要包括:自己敲的代码.通过注释来备注上自己对代码的理解) 前言 一.第一阶段 C++基础语法入门 对C++有初步了解 ...
- Visual Studio 2022版本 B站黑马程序员C++自学分享-第三阶段(1)(主要包括:自己敲的代码、通过注释来备注上自己对代码的理解)
Visual Studio 2022版本 B站黑马程序员C++自学分享-第三阶段(1)(主要包括:自己敲的代码.通过注释来备注上自己对代码的理解) 前言 三.第三阶段 C++提高编程 介绍C++泛型编 ...
- B站黑马程序员pink老师讲的JavaScript代码
B站黑马程序员pink讲课的JavaScript代码(部分代码) <!DOCTYPE html> <html lang="en"> <head> ...
- [学习笔记]黑马程序员python教程
文章目录 思维导图 Python基础知识图谱 面向对象 SQL入门和实战 Python高阶技巧 第一阶段 第九章:Python异常.模块与包 1.9.1异常的捕获 1.9.1.1 为什么要捕获异常 1 ...
- python视频教程黑马-python视频教程免费:求黑马程序员python教程?
python后端开发需要学什么? 第一阶段:Python语言基础 主习Python最基识,如Python3.数据类型.字符串.函数.类件操作等.阶段课程结束后,学员需要完成Pygame实战飞机大战.2 ...
- python从入门到高阶(基于黑马程序员python教程)
文章目录 0.文章来源 1."学it就来%s%s员"% (name,"程序员") 2.%s:占位字符串.%d:占位整型.%f:占位浮点型 3.字符串格式化的精度 ...
最新文章
- jQuery中 :first 和 :last 选择器诡异问题
- 深度解读 OpenYurt:从边缘自治看 YurtHub 的扩展能力
- automatic asynchronous creation if no note exists
- python 天气预报地图_在树莓派上用 python 做一个炫酷的天气预报
- python中list是什么类型_Python中的list数据类型
- 基础知识—条件判断语句-switch语句
- win10安装python3.7及配置环境变量
- 计算机应用技术教程的答案,大学计算机应用技术教程答案
- idea svn 不见的问题
- NLP --- 文本分类(基于LDA的隐语意分析训练算法详解)
- Python画一个国旗
- php中的冒泡排序和选择排序d
- [算法题] 安排会议室——贪心算法的应用
- 2021-4月最全停词表(1893个),速取
- oracle j2ee数据库,J2EE连接远程Oracle数据库表
- c语言不满秩矩阵方程组的解,【线代】矩阵的秩与方程组的解[坑]
- 算法训练 - 黑色星期五 有些西方人比较迷信,如果某个月的13号正好是星期五,他们就会觉得不太吉利,用古人的说法,就是“诸事不宜”。请你编写一个程序,统计出在某个特定的年份中,出现了多少次既是13号又
- 如何在CentOS系统服务器下搭建代理IP
- 网络摄像机·监控摄像机用 镜头驱动芯片MS41909 功能对标BU24036MW
- win10怎么取消登录密码
热门文章
- VMware许可证过期解决
- Spark SVN + Eclipse 3.3 + Subversive Installation Guide
- 一个OEIS数列的通式
- mac matlab破解
- WinForm界面控件Telerik UI for WinForm入门指南教程
- asp.net中使用mschart控件
- 有哪些值得推荐的 Java 练手项目?
- php搭建h5商城,关于PHP的H5商城网站开发方案
- 怎么将计算机的网络恢复连接,怎么修复互联网连接 修复网络连接方法【介绍】...
- 加固机箱设计资料第289篇:基于3U VPX的 5槽加固机箱