文章目录
一、windows数据库启动指令
mysql -h localhost -P 3306 -u root -p123456
二、数据库的相关概念
DBMS
数据库管理系统(Database Management System)。数据库是通过DBMS创建和操作的容器
常见的数据库管理系统:MySQL、Oracle、DB2、SqlServer
DB
数据库(database):存储数据的“仓库”。它保存了一系列有组织的数据
SQL
结构化查询语言(Structure Query Language):专门用来与数据库通信的语言
优点:
1、支持范围广
2、简单易学
3、强有力
三、数据库存储数据的特点
1、将数据放到表中,表再放到数据库中
2、一个数据库可以有多张表,每一个表都有唯一的名字做标示
3、表具有的特性定义了数据在表中如何存储,类似java中的类
4、表由列组成,也称为字段。所有的表都是由一个或多个列组成的,每一列类似java中的属性
5、表中的数据是按行存储的,每一行类似于java中的“对象”
四、初始MySQL
常见命令和语法规范
数据库常见命令
1.查看当前所有库
show databases;
2.打开指定的库
use 库名
3.查看当前库的所有表
show tables;
4.查看其他库的所有表
show tables from 库名;
5.创建一个表
create table 表名(
列名 列类型,
列名 列类型,
...
)
6.查看表的结构
desc 表名;
7.查看服务器的版本
方式一、登录到MySQL服务器 select version();
方式二、mysql --version
语法规范
1.不区分大小写,表名、列名小写
2.每条命令最好用分号结尾
3.每条命令根据需要,可以进行缩进或者换行
4.注释
单行注释
多行注释
#文字
-- 文字(横杠和文字之间有空格)
/* 文字 */
五、DQL(Data Query Language)语言的学习
基础查询
select 查询列表 from 表名;
特点:
1.查询列表可以是表中的字段、常量值、表达式、函数。
2.查询的结果是一个虚拟的表格
#1.查询表中的单个字段
select last_name from 表名;
#2.查询表中的多个字段
select last_name , frist_name , ... from 表名;
#3.查询表中的所有字段
select * from 表名;
#4.查询常量值
select 100; 默认的字段名就是他本身
select 'jonh' 查询字符常量
#5.查询表达式
select 100*80;
#6.查询函数
select 函数();
7.起别名
方式一
select 100*98 AS 结果;
select last_name AS 姓 from table_name;
方式二
select last_name 姓 from table_name;
起别名的优点:
①便于理解
②如果有重复方便区分
8.去重
select distinct 字段名 from table_name;
9.+号的作用
mysql中,仅仅只有一个功能,运算符
select 100+90; 直接做加法运算
select '100'+90; 试图将字符型转换成数值型,如果转换成功,做加法运算。
select 'jonh'+90; 如果转换失败,将字符型转换成0
select null+10; 如果一方为null,则结果肯定为null
案例:将姓字段和名字段拼接到一起。
分析:既然加号只能做运算,那么就没有拼接功能了。使用函数CONCAT
select concat ('a'+'b'+'c') AS 结果;
条件查询
#语法
select
查询列表 ③
from
表名 ①
where
筛选条件; ②
类型:
一、按条件表达式筛
< > =(只有一个等号) != <= >=
举例:
学习数据库时使用的例表
一、查询工资数 ( x ) 在10000到20000的员工名,工资以及奖金。
select
last_name,
salary,
commission_pct (奖金)
from
employees
where
x>10000
and
x<20000;
二、查询部门编号不是在90到110之间,或者工资高于15000的员工信息
select
*
from
employees
where
department_id<=90 or department>110 or salary
二、按逻辑表达式查询
and or not
三、模糊查询
like 、 between and 、 in 、 in null
like
特点:
①一般和通配符搭配使用
通配符:
% 任意多个字符,包含零个字符
_ 任意单个字符
案例一 查询员工名中包含字符a的员工信息。
学习数据库时使用的例表
select
*
from
empolyees
where
last_name like %a%;
案例二 查询员工名中第三个字符为e , 第五个字符为a的员工名和工资
select
last_name
salary
from
employees
where
last_name like ' __e_a%'
案例三 字符为 ’ _ ’ 时要使用转义字符 ,用 escape 指定
select
last_name
salary
from
employees
where
last_name like ' __$_%' escape '$' #$ 是转义字符
between and
案例一 查询员工编号在100到120 之间的员工信息
select
*
from
employees
where
employees_id >=100 and employees <= 100 ;
#---------------------------------------------------------
select
*
from
employees
where
employees_id between 100 and 120;
in
含义
判断某字段的值是否属于in列表中的某一项
特点
①简洁
②类型要一致兼容
案例四 查询员工的工种编号是IP_PROG AD_PRES AD_VP中的一个员工名和工种编号
select
last_name
job_id
from
employees
where
job_id = 'IP_PROG' or job_id = 'AD_PRES' or job_id = 'AD_VP';
#----------------------------------------------------------
select
last_name
job_id
from
employees
where
job_id in('IP_PROG' , 'AD_PRES' , 'AD_VP');
is null
案例一查询没有奖金的员工名和奖金率
select
last_name,
commission_pct
from
employees
where
commission_pct is null ;
#只能判断null值
安全等于<=>
案例一查询奖金为12000的员工信息
select
*
from
employees
where
salary <=> 12000;
#可以判断普通数值
排序查询
语法
select 查询列表
from 表
【where 查询条件】
order by 排序列表 【asc(升)|desc(降)】#默认升序
特点
案例一
查询员工信息,要求工资从高到低排序
select * from emploees order by salary desc;
案例二
查询部门编号 >= 90 的员工信息 ,按入职时间的先后进行排序
select
*
from
employees
where
department_id>=90
order by
hiredata ASC
案例三
案例四
案例五
案例六
常见函数
概念
类似于java中的方法,将一组逻辑语句封装在方块体中,对外暴漏方法名。
好处
1.隐藏了实现细节
2.提高了代码重用性
调用
select
函数名(实参列表)
【from 表】
特点
1.函数名(叫什么)
2.函数功能(干什么)
分类
1.单行函数,分为字符、数学、日期、其他函数以及流程控制函数
2.分组函数 功能:做统计使用,又称为统计函数、聚合函数
一、 单行函数
1、 字符函数 length、concat、upper、lower、substr、substring、instr
select CONCAT(last_name , '_' , frist_name) 姓名
from employees;
select CONCAT(UPPER(last_name) , LOWER(frist_name)) 姓名
FROM employees;
结果:
陆展元
李莫愁
select
CONCAT(UPPER(last_name , 1 , 1) , '_' , LOWER(SUBSTR(last_name , 2))) out_put
from
employees;
select
INSTR('杨不殷六侠爱上了殷六侠' , '殷六侠')
AS out_put;
select
LENGTH(TRIM( '张翠山' ))
AS
out_put;
/*---------------------去掉 a --------------------------*/
select
LENGTH(TRIM('a' from 'aaaaaaaaaaaaaaaaa张aa翠aa山aaaaaaaaa' ))
AS
out_put;
2、数学函数 round、ceil、floor、truncate、mod
结果
-1
结果
-10
3. 日期函数 now、curdate、curtime、str_to_date、data_format
4. 其他函数 version() database() user()
5. 流程控制函数 if case
1.if 函数 if else效果
select if(10 > 5 , 'big' , 'litte');
/*----------------------------------------------*/
select last_name , commission_pct , if(commission_pct is null , 'no' , 'yes') 备注
from employyes;
2. case 函数 switch case 效果
case 要判断的字段或表达式
when 常量1 then 要显示的值 1 或语句 1
#when 可以有多个
else 要显示的值 n 或语句 n
end;
/*------------------------------------------------*/
#查询员工工资,部门号为30,显示员工工资 1.1 倍40-1.2 ,50-1.3,其他原工资
select salary 原始工资 , department_id
case department_id
when 30 then salary*1.1
when 40 then salary*1.2
when 50 then salary*1.3
else salary
end as 新工资
3. case 函数 多重 if 效果
case
when 条件1 then 要显示的值或语句1
when 条件2 then 要显示的值或语句2
else 要显示的值或语句 n
end
select salary
case
when salary > 20000 then 'A'
when salary > 15000 then 'B'
when salary > 10000 then 'C'
else 'D'
end as 工资级别
from employees;
分组函数
之后补充,还未学完
连接查询
子查询
分页查询
union联合查询
六、DML(Data Manipulation Language)语言的学习
插入语句
修改语句
删除语句
七、DDl(Data Define Language)语言的学习
库和表的管理
常见数据类型介绍
常见约束
八、TCL语言学习
事务和事务处理
九、视图讲解
十、存储过程和函数
十一、流程控制和结构
/**********************************************************************************************/
保存数据的容器
数组、集合、文件(不太适合查询)
数据库
实现数据持久化、方便查询