MySQL的背景
- 前身属于瑞典的一家公司,MySQL AB
- 2008年被sun公司收购
- 2009年被oracle公司收购
MySQL的优点
- 开源免费,成本低
- 性能高,移植性好
- 体积小,便于安装
数据库相关概念
- DB : 数据库 DataBase 保存了一些列有组织的数据。
- DBMS : 数据库管理系统 Database Management System , 用于管理数据库中的数据
- SQL : 结构化查询语言 :专门用来与数据库进行通讯的语言
SQL的优点:
- 不是某个特定数据库供应商专有的语言,几乎所有的DBMS都支持SQL
- 简单易学
数据库特点
- 持久化数据到本地
- 可以实现结构化查询,方便管理
数据库存储数据的特点
- 将数据放到表中,表在放到库中
- 一个数据库中可以有多张表,每个表都有一个名字,用来标识自己,具有唯一性
- 表具有一些特性,这些特性定义了数据在表中如何存储,类似java中“类”的设计
- 表由列组成,我们也称为字段。所有表都是有一·个列组成的,每一列类似java中的“属性”
常见的数据库管理系统
- mysql
- oracle
- db2
- sqlserver
MySQL服务的启动和停止
方式(1):通过命令行
- net start 服务名
- net stop 服务名
方式(2):设置
- 计算机--右击--管理--服务
MySQL服务的登录和退出
- 登录:mysql -h主机名 -p端口号 -u用户名 -p密码
- 退出:exit
MySQL可视化工具
- SQLyong
- Navicat
MySQL语法规范
- 不区分大小写,蛋建议关键字大写,表名、列名小写
- 每条命令用分号结尾
- 每条命令根据需要,可以进行缩进或者换行
- 关键字单独一行,字段单独一行,字段建议用 `` 标记,如 select `name` from student ;
- 注释
注释
- 单行注释 #注释文字
- 单行注释 -- 注释文字
- 多行注释 /* 注释文字 */
MySQL常用命令
- show databases ; -- 查看当前所有的数据库
- use test ; -- 打开指定数据库
- show tables ; -- 查看当前库的所有表
- show tables from mysql ; -- 查看其他库的所有表
- select database() ; -- 查询当前使用的数据库
- create table student(int id , name varchar(20)); --创建表
- desc student ; -- 显示表结构
- select * from student ; -- 查询表数据
- insert into student(id . name ) values(1,'掘金宝宝_'); --增加字段
- update student set name ='狗哥' where id = 1 ; --修改字段id为1的学生名字为狗哥
- delete from student where id = 1 ; --删除id为1的学生信息
- select version(); -- 查看数据库的版本
SQL语言
- DQL语言 : select
- DML语言 : 增删改
- DDL语言 : 库和表的定义
- TCL语言 : 事物控制语言
查询语句
说明:以下sql语句或关键字,为了查看方便,均为小写,或者没有加``符号。
基础查询
规则:select 查询列表 from 表名 ;
特点
- 查询列表可以是:表中的字段、常量、表达式 、函数
- 查询的结果是一个虚拟的表格
查询表中的单个字段
select laset_name from student ;
查询表中的多个字段
select last_name ,email ,from student ;
查询表中的所有字段
select * from student ;
查询常量值
select 100 ;
select 'tom';
注意:字符型和日期型的常量值必须用单引号引起来,数值型不需要
查询表达式
select 100 * 100 ;
+-----------+
| 100 * 100 |
+-----------+
| 10000 |
+-----------+
查询函数
select version();
起别名
优点
- 便于理解
- 如果要查询的字段有重名的情况,使用别名可以区分开来
方式(1) as
select 100 * 100 as 结果;
+--------+
| 结果 |
+--------+
| 10000 |
+--------+
select last_name as 姓,first_name as 名 from student ;
+---------+-----------+
| 姓 | 名 |
+---------+-----------+
| 张 | 三 |
| 赵 | 四 |
| 王 | 五 |
+---------+-----------+
空格
select last_name 姓 ,first_name 名 from student;
提示:假如别名中有特殊符号,比如#、空格等,建议把别名加上双引号(或者单引号)按照规范应该加双引号。
去重复 distinct
select distinct student_id from student ;
+号的作用
java中的+号作用:
- 充当运算符:两个操作数都为数值型
- 充当连接符:只要有一个操作数位字符串
MySQL中+号的作用:
- 只是充当运算符
比如:select 100 + 99 ;
两个操作数都为数值型,则做加法运算
select '124' +90 ;
其中一方为字符型,试图将字符数值转换为数值型,如果转换成功,则继续做加法运算
select 'abc' +90 ;
如果转换失败,则将字符型数值转换为0
select null + 90 ;
只要其中一方为null,则结果肯定为null
concat拼接字符:
select concat(`a`,`b`,`c`,...) as 结果 ;
例如:select concat(`last_name`,`first_name`) as 姓名 from student ;
IFNULL 关键字:
select IFNULL(可能为null的字段,如果这个字段真的为null则他返回的内容) as 别名 from 表名 ;
条件查询
语法:select 查询列表 from 表名 where 筛选条件;
按照条件表达式筛选
条件运算符:> < = != <> >= <=
案例1:查询年龄>20的学生信息
select * from student where age > 20 ;
案例2:查询年龄不等于20的学生名和学生年级
select student_name,student_grade from student where age != 20 ;
按照逻辑表达式筛选
作用:用于连接条件表达式
逻辑运算符: && || ! and or not
&& 和 and : 如果两个条件都为true,则结果为true,反之为false;
|| 和 or : 只要有一个条件为true,结果为true ,反之为false;
! 和 not : 如果连接的条件本身为false ,结果为true ,反之为false;
案例1:查询年龄在10-20之间的学生名以及所在年级
select student_name,student_grade from student where age>=10 and age<=20;
案例2:查询学生年龄不是在10-20之间,或者身高大于170的学生信息
select * from student where age<10 or age>20 or student_height>170;
select * from student where not(age<10 and age>20) or student_height>170;
模糊查询
关键字:like 、between and、in 、 is null
like
案例1:查询学生中包含a的学生信息
select * from student where last_name like '%a%';
特点: 一般和通配符搭配使用
% :任意多个字符,包含0个字符
_ : 任意单个字符
注意:假如要查询的某个值中有和通配符一样的下划线 _ ,可以自定义普通符号使用,通过转义的方式,escape
between and
案例1:查询学生编号在1-100 之间的学生信息
方式1:
select * from student where student_id >=1 and student_id<=100 ;
方式2:
select * from student where student_id between 1 and 100 ;
注意事项:1、使用between and 可以提高语句的简洁度;
2、包含临界值
3、两个临界值不能颠倒
in
含义:判断某字段的值是否属于in 列表中的某一项
特点:
- 使用in提高语句的简洁度、
- in 列表的值类型必须一致或兼容
案例1: 查询学生的专业编号是 it , computer , accounting中的一个学生名和专业编号
方式1:
select student_name ,professional_id from student where professional_id = 'it' or professional_id= 'computer' or professional_id ='accounting' ;
方式2:
select student_name ,professional_id from where professional_id in('it','computer','accounting');
is null
注意事项:
- = 或者 <> 不能用于判断null 值
- is null 或者 is not null 可以判断null 值
案例1:查询没有/有考试分数的学生名和及格率
没有:select student_name ,pass from student where scores is null ;
有:select student_name ,pass from student where scores is not null ;
补充:# 安全等于 <=>
案例1:查询没有考试分数的学生名和及格率
select student_name ,pass from student where scores <=> null ;
比较:is null 和 <=>
- is null :仅仅可以判断null 值 ,可读性较高
- <=> : 既可以判断null值,也可以判断普通的数值,可读性较低
排序查询
语法:select 查询列表 from 表 【where 筛选条件】 order by 排序列表 【asc|desc】
特点:
-
asc: 升序,从低到高
-
desc:降序,从高到低 注意:如果都不写,默认是升序
-
order by 子句中可以支持单个字段、多个字段、表达式、函数、别名
-
order by 子句一般放在查询语句的最后面,但是 limit 子句除外。
案例1:查询学生的年龄>=22的信息,按从小到大排序
select * from student where age>=22 order by asc ;
案例2:查询学生信息,先按照年龄排序,再按照学生编号排序(按照多个字段排序)
select * from student order by age asc , student_id desc ;
(假如有年龄一样的学生,则学生编号按照升序排序)
MySQL从基础到高级!
后续持续更新~
如有错误欢迎指出~
感谢支持