数据库
概念:DataBase 简称DB 用于存储和管理数据的仓库
特点 : 其实数据库就是一个文件系统 方便存储和管理数据 使用了统一的方式操作数据库sql
常见的数据库软件 Oracle MySQL SQLServer ……….
MySQL
安装,去百度
卸载
- 找到安装目录 找到my.ini文件 复制C:/ProgramData/MySQL/MySQL Server 8.0/Data
- 卸载mySQL
- 删除C:/ProgramData/MySQL文件
登陆
cmd下services.nsc打开服务
打开cmd 输入 mysql -uroot -p密码
远程登陆 mysql –hIP -uroot -p密码
mysql --hostIP --user = root --password = 密码
退出exit quit
sql通用语法
- 以单行或多行书写,以分号结尾
- 以空格和缩进来增强可读性Mysql数据库的sql语句不区分大小写 ,关键字建议使用大写来书写
- 注释
单行注释 – 注释内容或 # 注释内容(mysql特有)
多行注释 /* 注释 */
- SQL(Structurd Query Language) 结构化查询语言
其实就是定义了操作所有关系型数据库的规则
每一种数据库操作的方式存在不一样的地方,称为方言
sql语句
- DDL(Data Definition Language) 定义数据库对象 create drop alter
- DML(Data Manioulation Language)数据库操作语言 增删改 insert delete update
- DQL(Data Query Language) 数据库查询语句 select where
- DCL(Data Control Language) 数据库控制语言 定义数据库的访问权限和安全级别的,及创建用户 grant revoke
数据类型
- int 整数类型 age int;
- double:小数类型 double( 最多有几位小数 , 保留几位小数 );
- date : 日期 只包含年月日 , yyyy-MM-dd
- datetime : 日期,包含年月日时分秒 yyyy-MM-dd HH:mm:ss
- timestamp : 时间戳,包含年月日时分秒 yyyy-MM-dd HH:mm:ss 如果为空默认用当前的系统时间
- varchar :字符串 name varchar(20); 指定长度
DDL : 操作数据库
操作数据库:CRUD
C(create) : 创建
创建一个数据库 create database 名字;
判断一下存不存在,存在就不会创建
创建时判断是否存在,设置字符集
创建名为first的数据库,判断是否存在,并指定字符集编码为gbK
create database if not exists first character set gbk;
R(Retrieve): 查询
show databases; 查询所有数据库的名称
show create database; 数据库名称 查看某个数据库对应的字符集
U(Update) : 修改
修改数据库的字符集 alter database 数据库名称 character s;唠嗑节能环保GV从 et 字符集名称
D(Delete) : 删除数据库
drop database 数据库名称;
drop database if exists two; 删除前判断一下 如果存在就删除
使用数据库
查询当前正在使用的数据库名称 select database();
使用数据库 use 数据库名称;
操作表CRUD
C(create) : 创建表
create table 表名( create table student(
列名1 数据类型, id int,
列名1 数据类型, name varchar(10),
列名1 数据类型, age int,
); score double(4,1),
birthday date,
insert_time timestamp
);
复制表 create table 表名 like 要复制的表名;
R(Retrieve): 查询表
查询表名称: show tables;
查询表结构: desc 表名;( desc students;)
U(Update) : 修改
- 修改表名
alter table 表名 rename to 新的表名
- 修改表的字符集
查看表的字符集 show create table 表名;
修改表的字符集 alter table 表名 character set 要改为的啥编码;
- 修改字段 类型 alter table 表名 change 字段名 改为的字段名 新字段的数据类型;
只改数据类型 alter table 表名 modify 字段名 新的数据类型
- 添加字段
alter table 表名 add 要添加的字段名 数据类型;
- 删除字段
alter table 表名 drop 要删除的字段名
D(Delete) : 删除表
drop table 表名;
drop table if exists 表名; 判断如果有就删除
DML增删改表中数据
- 添加数据 :
insert into 表名(字段名1 , 字段名2,…..字段名n) values( 值 , 值 , …… ,值); 列名和值一一对应
insert into 表名 values( 值 , 值 , …… ,值); 默认给所有的字段添加值
除了数字类型,其他类型需要引号(单双都可以)
- 删除数据 : delete from 表名 where 条件; 如果不加条件,删除表中所有记录(DELETE FROM students WHERE id = 1;)
truncate table 表名; 删除表中所有记录删除表然后在创建一个一模一样的表(TRUNCATE table students;)
- 修改数据 : update 表名 set 列名1 = 值1,列名2 = 值2 where id = ??;
DQL 查询表中的数据
create table student(
id int,
name varchar(20),
age int,
sex varchar(5),
address varchar(100),
math int,
english int
);
insert into student(id,name,age,sex,address,math,english) values
(1,'马云',55,'男','杭州',66,78),
(2,'马化腾',45,'女','深圳',98,87),
(3,'亚索',55,'男','香港',56,77),
(4,'剑圣',15,'男','北京',83,0),
(5,'剑魔',15,'男','杭州',90,66),
(6,'鳄鱼',15,'女','上海',70,98),
(7,'德玛',35,'女','上海',88,62),
(8,'寒冰',52,'男','上海',46,76);
select * from 表名;( SELECT * FROM student;)
- 语法
select
字段列表
from
表名列表 ()以逗号分割)
where
条件列表
group by (分组查询)
分组字段
having
分组之后的条件
order by
排序
limit
分页限定
- 基础条件
- 多个字段查询: select 字段名,… from 表名;
select name,age,address from student; 查询name , age ,address 字段
- 去除重复: select distinct 字段 from student; 多个字段需要这几个字段值都重复(完全相同)才去掉
select distinct age from student; 去除年龄的重复
- 计算列 : 可以使用加减乘除运算来计算一些列的值
select name,math,english,math+english from student; 计算math 和 english的和
ifnull( 可能为空的字段名 , 为空时当做几) select name,math,ifnull(english,0),math+english from student; 如果为空当做0
- 起别名 as 也可以省略 select name 姓名,math 数学,ifnull(english,0) as 英语,math+english as 总分 from student;
- 条件查询
- > < >= <= = <>(不等于) SELECT NAME,age FROM student WHERE age > 20;
- like 模糊查询
占位符 : _ :单个任意字符 select * from student where name like '_化_';
% :多个任意字符 SELECT * FROM student WHERE NAME LIKE '%马%';
-
- is null select * from student where english is null;(is not null) 查询英语为null的 ( null只能用 is null)
- and 或 && 并且 select name,age from student where age > 20 && age <46;
- between …. and select * from student where age BETWEEN 20 and 46;
- or 或 || 或 select * from student where age = 20 or age = 35 or age = 55;
- in(集合) select * from student where age in(20,35,55);
- not 或 ! 非 select * from student where english is not null;
- 排序查询
语法: order by 子句
order by 排序字段1 排序方式1 , 排序字段2 排序方式2 …
排序: 升序 asc (可省略,默认的) select * from student order by math asc; -- 按照数学升序排序
降序 desc SELECT * FROM student ORDER BY math DESC; -- 按照数学降序排序
相等按第二排序字段排序 SELECT * FROM student ORDER BY math DESC , english DESC; -- 按照数学升序排序,相等就按第二条排
- 聚合函数 : 将一列数据作为一个整体,进行纵向计算的;
- count : 计算个数 select count(name) from student; -- 计算有多少人
- 会排除null值 解决: ifnull(English,0) 如果是空就换为0 count(*)
- max : 计算最大值 select max(math) from student; -- 计算数学最高分
- min : 计算最小值 select min(math) from student; -- 计算数学最低分
- avg : 计算平均值 SELECT AVG(math) FROM student; -- 计算数学平均分
- sum : 计算列的和 select sum(math) from student; -- 计算数学总分
6. 组查询