学习目标:
- 学习mysql入门知识
学习内容:
- 搭建 mysql 环境
- 了解mysql的理论知识
- 掌握 mysql基本语法
- 掌握增加语句
- 掌握删除语句
- 掌握修改语句
- 掌握查询语句
学习资料:
原文链接:https://blog.youkuaiyun.com/qq_45036591/article/details/105078355
学习产出:
一、MYSQL环境
官网下载地址:mysql安装包下载
mysql图形化工具:Navicat Premium
二、三大范式
- 第一范式(1NF):字段或属性是不可分割的最小单元,不会有重复的列,体现原子性
- 第二范式(2NF):在满足1NF前提下,存一个候选码,非主属性全部依赖改候选码,即存在主键,体现唯一性
- 第三范式:在满足2NF前提下,非主属性必须互不依赖,消除传递依赖
三、语法
- 数据操作语言:select、delete、insert、into、update
- 数据定义语言:create、drop、alter
- 数据控制语言:grant、remove、commit、rollback
- 聚合函数:sum(求和)、max(最大值)、min(最小值)、avg(平均值)、frist(第一条记录)、last(最后一条记录)、count(统计记录数)
四、数据类型
- 整数类型
| 类型 | 占几字节 |
| TINYINT | 1 |
| SMALLINT | 2 |
| MEDIUMINT | 3 |
| INT | 4 |
| BIGINT | 8 |
整数类型可以被指定长度,不限制值得合法范围,只会影响显示字符的个数,需要和UNSIGNED、ZEROFILL属性配合使用才有意义
- 实数类型
| 类型 | 占几字节 |
| FLOAT | 4 |
| DOUBLE | 8 |
| DECIMAL | 128bit |
DECIMAL可以用于储存比BIGINT还要大的整型,能储存精确的小数
而FLOAT和DOUBLE是有取值范围的,支持标准浮点进行近似计算
- 字符串类型
| 类型 | 占几字节 |
| VARCHAR | 最大长度65535 |
| CHAR | 0-255 |
| TEXT | 最大长度65535个字元(2^16-1) |
| BLOB | 最大长度65535个字元(2^16-1) |
VARCHAR:用于储蓄可变长度字符串,比定长类型更节省空间,在列长度小于255字节时使用1字节表示。否则2字节,当超过设置长度时,内容会被截断
CHAR:储存定长字符串,根据定义的字符串长度分配足够的空间,超过设置长度时,内容会被截断。
BLOB,二进制大对象(字节流)。可以用来存储图片,声音和视频等二进制文件。没有字符集的说法。
TEXT,文本大对象(字符流)。可以用来存储大量的字符串,可以理解为超大的char或者varchar类型。由于是存储字符,所以有字符集的说法。
TEXT、BLOB:查询时会使用临时表,导致严重的性能开销
- 日期和时间类型
| 类型 | 占几字节 |
| TIMESTAMP | 4 |
| DATETIME | 8 |
timestamp空间效率高于datetime,如果需要储存微秒,可以使用bigint
五、练习
1、创建数据库my_db2
create database my_db2;
2、查询数据库
show databases;
3、删除数据库mydb_one;
drop databases mydb_one;

1、使用数据库my_db2;(在进行数据库操作时,要先指明在哪个数据库)
use my_db2;
2、创建数据表student
create table student(属性,数据类型等);
3、查询数据库my_db2中的表格
show tables;
4、查询数据库my_da2中的student表格结构语法
desc student;

1、向数据表student中插入数据
insert into student values(1000,'小明',18);
2、将数据表student的字符集修改成UTF8
alter table student default character set utf8;
3、将数据表student中name列的数据格式;
alter table student change name name varchar(100) character set utf8;

1、查询数据表student的全部信息
select * from student;

1、查询数据表student中id为10001的学生信息;
select * from student where id = 10001;

1、查询数据表student 中 姓名为小明 且 id为10000 的学生信息;
select * from student where name=’小明‘ and id = 10000;

1、查询数据表student中 姓名为小明 或 年龄为18岁的学生信息;
select * from student where name='小明' or age = 18;

1、 查询数据表student中 年龄在18到19的学生信息;
select * from student where age between 18 and 19;

1、查询数据表student中 年龄大于等于18且小于等于19的学生信息;
select * from student where age>=18 and age <=19;

1、查询数据表student中年龄 在 18到19间的学生信息;
select * from student where age in (18,19);

1、查询数据表student中年龄 不在 18到19间的学生信息;
select * from student where age not in (18,19);

1、查询数据表student中 姓名 包含明字 的学生姓名、年龄信息;
select * from student where name like ’%明%‘;

1、将数据表student按照年龄来分组;
select * from student group by age;

1、查询数据表student中学生的最小年龄;
select min(age) as '年龄最小' from student;

1、查询数据表student中学生的最大年龄;
select max(age) as '年龄最大' from student;

1、查询数据表student中学生的平均年龄;
select max(age) as '年龄最大' from student;

1、查询数据表student中一共有多少学生;
select count(id) as '总人数' from student;

1、查询数据表student中全部学生的总年龄 ;
select sum(age) as '总年龄' from student;

559

被折叠的 条评论
为什么被折叠?



