数据库分类

  1. 关系型数据库:(SQL)
  • MySQL、Oracle、Sql Server、DB2、SQLlite
  • 通过表和表之间,行和行之间的关系进行数据的存储
  1. 非关系型数据库:(NoSQL)Not Only SQL
  • Redis、MongDB
  • 非关系型数据库,对象存储,通过对象自身的属性来决定。

DBMS(数据库管理系统)

  • 数据库的管理软件,科学有效的管理我们的数据。维护和获取数据
  • MySQL:数据库管理系统

1.7、连接数据库

命令行连接!

  1. mysql -uroot -p123456 -- 连接数据库
  2. update mysql.user set authentication_string=password('123456') where user='root' and
  3. Host = 'localhost'; -- 修改用户密码
  4. flush privileges; -- 刷新权限
  5. --------------------------------------------------
  6. -- 所有的语句都使用分号结尾
  7. show databases; -- 查看所有的数据库
  8. user school; -- 切换数据库 use + 数据库名
  9. show tables; -- 查看数据库中的所有表
  10. describe student; -- 显示数据库中所有表的信息
  11. create database jungle; -- 创建一个数据库
  12. exit; -- 退出连接
  13. /* */ -- SQL的多行注释

数据库XXX语言
DDL 数据库定义语言
DML 数据库操作语言
DQL 数据库查询语言
DCL 数据库控制语言

2、操作数据库


mysql的关键字不区分大小写

2.1、操作数据库

  1. -- 创建数据库
  2. create database [if not exists] jungle
  3. -- 删除数据库
  4. drop database [if exists] jungle
  5. -- 查看所有的数据库
  6. show databases

如果表明或者字段名是一个特殊符号,我们一般为这个名字加反引号 ,例如:name

2.2、数据库的列类型

数值

  • tinyint:十分小的数据 一个字节
  • smallint:较小的数据 两个字节
  • mediumint:中等大小的数据 三个字节
  • int:标准的正数 四个字节
  • bigint:较大的数据 八个字节
  • float :浮点数 四个字节
  • double:浮点数 八个字节
  • decimal:字符串形式的浮点数,金融计算的时候,一般使用decimal类型

字符串

  • char:字符串固定大小的 0~255
  • varchar:可变字符串 0~65535
  • tinytext:微型文本 2^8 - 1
  • text:文本串 2^16 - 1

时间日期

java.util.Date

  • date:YYYY-MM-DD,日期格式
  • time:HH:mm:ss,时间格式
  • datetime:YYYY-MM-DD HH:mm:ss(最常用)
  • timestamp:时间戳,1970.1.1到现在的毫秒数!也较为常用
  • year:年份表示

null

  • 没有值,未知
  • 注意,不要使用NULL进行计算,结果为NULL


2.3、数据库的字段属性

image.png
image.png
常用命令

  1. show create database jungle -- 查看创建jungle数据库的语句
  2. show create table jungle -- 查看jungle数据表的定义语句
  3. desc jungle -- 显示表的结构

2.5、数据表的类型

关于数据库引擎
INNODB 默认使用
MYISAM 早些年使用的

image.png

在物理空间存在的未知

所有的数据库文件都存在 data 目录下,一个文件夹对应一个数据库
本质还是文件的存储!

MySQL引擎在物理文件上的区别:

  • InnoDB:在数据库表中只有一个 *.frm 文件,以及上级目录下的 ibdata1 文件
  • MyISAM 对应的文件:
    • *.frm 表结构的定义文件
    • *.MYD 数据文件(data)
    • *.MYI 索引文件(index)

设置数据库表的字符集编码

  1. charset=utf8

不设置的话,会是mysql默认的字符集编码(不支持中文!)
MySQL的默认编码是Latin1,不支持中文
可以在数据库的核心文件my.ini中配置默认的编码,一劳永逸(一般不这样用,通用性低)

  1. character-set-server=utf8

2.6、修改删除表

  1. -- 修改表 alter table 旧表名 rename as 新表名
  2. alter table teacher rename as teacher1
  3. -- 增加表的字段 alter table 表名 add 字段名 列属性
  4. alter table teacher1 add age int(11)
  5. -- 修改表的字段(两种方式:1.重命名,2.修改约束)
  6. alter table teacher1 modify age varchar(11) -- 修改约束
  7. alter table teacher1 change age age1 int(10) -- 字段重命名
  8. -- 删除表的字段 alter table 表明 drop 字段名
  9. alter table teacher1 drop age1
  10. -- 删除表(如果表存在才删除)
  11. drop table if exists teacher1

注意:change用来字段重命名,不能修改字段类型和约束;
modify不能用来字段重命名,只能修改字段类型和约束;

所有的创建和删除操作应该尽量加上判断,以免报错

3、MySQL数据管理

3.1、外键

image.png

删除有外键关系的表的时候,必须要先删除引用别人的表(从表),再删除被引用的表(主表)

方式二:创建表成功后,添加外键约束

image.png

注意:以上的操作都是物理外键,数据库级别的外键,我们不建议使用!(避免数据库过多造成困扰)

最佳实践

  • 数据库就是单纯的表,只用来存数据,只有行(数据)和列(字段)
  • 我们想使用多张表的数据,想使用外键,我们往往在程序中实现

3.2、DML语言(全部记住)

3.3、添加

insert

image.png
语法:insert into 表明 ( [ 字段名1, 字段名2, 字段名3 ] ) values ( ‘值1’ ), ( ‘值2’ ), ( ‘值3’ )

3.4、修改

image.png
条件:where 子句 运算符

image.png

image.png

3.5、删除

image.png
image.png
delete 和 truncate 的区别

  • 相同点:都能删除数据,都不会删除表结构
  • 不同:
    • truncate:重新设置自增序列,计数器会归零
    • truncate不会影响事务

image.png
DELETE删除的问题:重启数据库,现象

  • InnoDB:自增列会重1开始,(存在内存当中,断电即消失)
  • MyISAM:继续从上一个自增量开始(存在文件中的,不会丢失)

4、DQL查询数据(最重点)


4.1、DQL

(Data Query Language:数据库查询语言)

4.2、查询所有字段

  1. -- 查询全部的学生 select * from student
  2. select * from student
  3. -- 查询指定字段
  4. select `StudentNo`, `StudentName` from student
  5. -- 别名, As 可以给字段和表起别名
  6. select `StudentNo` as 学号, `StudentName` as 学生姓名 from student
  7. -- 函数 Concat(a, b) 拼接字符串
  8. select concat('姓名:',StudentName) as 新名字 from student

去重 distinct

作用:取出Select查询出来的结果中重复的数据,重复的数据只显示一条

  1. -- 查询一下有哪些同学参加了考试,成绩
  2. select * from result -- 查询全部的考试成绩
  3. -- 查询有哪些同学参加了考试
  4. select `StudentNo` from result
  5. -- 发现重复数据,需要去重
  6. select distinct `StudentNo` from result
  7. select version() -- 查询系统版本(函数)
  8. select 100 * 3 - 1 as 计算结果 -- 用来计算(表达式)
  9. select @@auto_increment_increment -- 查询自增的步长(变量)
  10. -- 学院成绩 +1 分查看
  11. select `StudentNo`, `StudentResult` + 1 as `加分后` from result

数据库中的表达式:文本值,列,null,函数,计算表达式,系统变量
select 表达式 from 表

4.3、where条件子句

作用:检索数据中符合条件的值

逻辑运算符

image.png

  1. -- 查询学生成绩
  2. select studentNo, studentResult from result
  3. -- 查询考试成绩在95-100之间
  4. select studentNo, studentResult from result where studentResult between 95 and 100
  5. -- 或者用and
  6. select studentNo, studentResult from result where studentResult >= 95 and studentResult <= 100
  7. -- 除了1000号学生之外的同学的成绩
  8. -- !=
  9. select studentNo, studentResult from result where studentNo != 1000
  10. -- not
  11. select studentNo, studentResult from result where not studentNo = 1000

模糊查询:比较运算符

image.png

模糊查询

  1. -- 查询姓刘的同学
  2. -- like结合 %(代表0到任意个字符), _(代表一个字符)
  3. select studentNo, studentName from student
  4. where studentName like '刘%'
  5. -- 查询姓刘的同学,名字后面只有一个字的
  6. select studentNo, studentName from student
  7. where studentName like '刘_'