数据库基础
1.数据库概念
1…数据库
数据存储的仓库,数据进行有组织的存储
2.数据库管理系统
操纵和管理文件的大型软件
3.SQL
操作数据库的语言,定义了一套操作关系型数据库统一标准
2.停止
net stop mysql80
3.客户端连接
mysql -u root -p
1.关系型数据库
概念:建立在关系模型基础上,由多张相互连接的二维表组成的数据库
特点:1.使用表存储数据,格式统一,便于维护2.使用sql语言操作,标准统一,使用方便
2.SQL语法
1,数据库的操作
1、显示当前数据库
show databases;
2.创建数据库
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,
create_specification] ...]
演示:创建名为 demo1 的数据库
create database demo1;
如果系统没有 demo2的数据库,则创建一个名叫 demo2 的数据库,如果有则不创建
create database if not db_test1;
3.数据库的使用
语法:
use 数据库名;
4.删除数据库
说明: 数据库删除以后,内部看不到对应的数据库,里边的表和数据全部被删除
DROP DATABASE [IF EXISTS] db_name;
2.常用数据类型
1.基本型
2.字符串型
3.日期型
3.表操作
综合案例
select distinct name as '姓名' from emp;
-- 查询年龄为18的
select name from emp where age=18;
-- 查询年龄大于10的
select name from emp where age>10;
-- 年龄在15到20之间
select * from emp where age>15 and age<20;
select *from emp where age between 15 and 20;
-- between后面跟的是小的数and后面跟的是大的数
-- 查询性别为女并且年龄小于20的员工
select *from emp where age<20 and gender='女';
-- 查询年龄为18 或16或 20
select * from emp where age=18 or age=16 or age=20;
select * from emp where age in(16,18,20);
-- 查询姓名为两个数字的员工
select * from emp where name like '__';
-- 查询身份证最后一位是x的员工
select * from emp where code like '%x';
-- 分组查询
-- 根据性别分组,统计男性员工和女性员工的数量
select gender, count(*) from emp group by gender;
-- 根据性别分组,统计男性员工和女性员工的平均年龄
select gender,avg(age) from emp group by gender;
-- 查询年龄小于45的员工,根据工作地址分组,获取员工换数量大于3的地址
select myadress,count(*) from emp where age<45 group by myadress having count(*)>0;
-- 排序查询
-- 升序排序
select name,age from emp order by age asc ;
select name,age from emp order by age;
-- 降序排序
select name,age from emp order by age desc;
-- 根据年龄,如果年龄相同,再根据time
select name,age,mytime from emp order by age asc ,mytime desc ;
函数
指的是能直接被其他函数调用的代码或或程序
字符串函数
-- 字符串函数
-- 1.字符串拼接
select concat('hello','world');
-- 2.大小写转换
select upper('hello');
select lower('WORLD');
-- 3.字符串填充
select lpad('ss',10,'*');
select rpad('aa',10,'%');
-- 4.字符串截取
select substr('avnvasnvo',1,3);
-- 5.去除空格
select trim('savvs a ');
数值函数
-- 数值函数
-- 1.向上取整
select ceil(3.2);
-- 2.向下取整
select floor(3.2);
-- 取模
select mod(4,2);
-- 随机数
select rand(10);
-- 四舍五入
select round(3.4);
聚合函数
-- 聚合函数
-- 1.统计该企业员工的数量
select count(name) from emp;
-- 统计员工的平均成绩
select avg(age) from emp;
-- 统计员工的最大年龄
select max(age) from emp;
-- 统计新乡员工最小年龄
select min(age) from emp where myadress='新乡医学院';
-- 统计除年龄和
select sum(age) from emp;
日期函数
流程函数
本周小结
个人感觉这一次安排的内容有点多,一直在学新的东西,还是有好多没有学完,而且学着后面的忘前面的,这一次算法测试也是,好几天没有 写有点忘了,好多都超时了
下周计划
``
日期函数
[外链图片转存中…(img-sNZTn0cJ-1679797793983)]
流程函数
[外链图片转存中…(img-qh9GuOch-1679797793984)]
本周小结
个人感觉这一次安排的内容有点多,一直在学新的东西,还是有好多没有学完,而且学着后面的忘前面的,这一次算法测试也是,好几天没有 写有点忘了,好多都超时了
下周计划
抓紧点自己的时间,完成学习计划,还有多练习练习算法,争取把任务完成