table of Contents
- Key words
- mysql installation
- mysql commonly used commands
- mysql client
- + Create a database table
- Data Modeling
- Project scene
- 3 database paradigm
- Data integrity
- SQL language
- Operating table records CUD
- Table 1 simple query
- Table 2 simple query
- Build tables and data initialization
- Packet --GROUP BY
- SQL execution order
- Subqueries concept
- Classification subquery
- Exercise
- Single-row subquery
- EXISTS, NOT EXISTS subqueries
Key words
- DB
- DBMS
- DBA
DB: database database
generalized: data warehouse
narrow sense: DBMS, database management system, is a server-side software. The software must have a function: to save, retrieve (query speed fast enough), the correctness and completeness of the test data.
- RelationalDatabase, there are Oracle, DB2, Microsoft SQL Server, Microsoft Access, MySQL and so on.
- Column - Fields
- Row of the table is called a record
- Non-relational database, there are redis mongodb and so on.
User table
column (column)
id | name | age |
---|---|---|
1 | lysis | 23 |
2 | wang | 24 |
mysql installation
- mysql-5.0.22-win32.rar
- MySQL installation diagram .doc
- MySQL Download: https: //dev.mysql.com/downloads/installer/
- Start Service
During installation, Notes:
1, remember the user name and password
2, the encoding format to choose utf-8
mysql commonly used commands
- Built-in client
- Mysql MySQL testers essential study guide can be found in commonly used commands: http://www.lemfix.com/topics/343
Where: mysql database and information_schema system.
mysql client
- Navicat
- SQLyog
+ Create a database table
Employees table employee
Table field | Field Type |
---|---|
id | Integer |
name | Character |
gender | Character |
birthday | Date type |
entry_date | Date type |
job | Character |
salary | Decimal type |
resume | text |
Data Modeling
-
Entity Contact(Entity Relationship Approach)
-
实体、属性和联系。
实体:学生就叫实体
属性:
联系:实体与实体之间也不是孤立存在的,是有联系的。 -
联系的三种类型
- 1:1 学生证和学生 拆表(主表,从表)主表的主键列和从表的
- 1:M 用户和订单 一个用户可能有多个订单,一个订单只可能属于一个用户,用户表的主键列在订单表里面做外键列。外键列的值可以为null。
- M:N 学生和课程 拆成两个一对多1:M
项目场景
某宾馆需开发一个客房入住系统,该系统的基本描述如下:
1、系统操作员可以登录该系统,操作员信息为:操作员编号、操作员密码、操作员姓名、操作员联系电话。
2、宾馆提供客房,如状态为“空闲”则可以入住,如状态为“客满”则不能再次被入住,客房信息为:客房编号、客房状态。
3、客房有多种类型,例如:“单人间”、“双人间”、“豪华套房”等,类型信息为:类型编号、类型名称、收费标准(价格)、拥有客房数量。
4、客户可以预定客房,客户信息为:身份证号、客户姓名、客户性别、客户联系电话。
1、一种客房类型有多间客房,一间客房只能是某一种客房类型;
2、一个客户可以预订多间客房,一间客房在不同的日期可以被不同的客户入住,入住信息包括入住日期、入住天数。
数据库3范式
https://blog.csdn.net/CharlesYooSky/article/details/89204229
- 第一范式:保证每列的原子性
- 第二范式:保证一张表只描述一件事情
- 第三范式:保证每列都和主键直接相关
数据完整性
- 主键约束
- 非空约束
- 唯一约束
- 默认值default
- 外键约束(可以没有,可以交给程序去控制)
- check约束(mysql没有)
SQL语言
SQL(Structured Query Language)结构化查询语言
- 在70年代早期,由IBM公司推出。
- SQL是关系型数据库管理系统中用的缺省语言。像Oracle、Sybase、DB2、Informix、SQL Server这些大型数据库系统支持SQL语言作为查询语言。
- DDL DCL DML
表记录的操作CUD
一、插入语句—insert
1、向employee中插入三个员工信息,要求员工姓名分别是zs,ls,wangwu
二、更新语句—update
1、将所有员工薪水改为5000元。
2、将姓名为‘zs’的员工薪水修改为3000元。
3、将姓名为‘ls’的员工薪水修改为4000元,job改为ccc。
4、将wangwu的薪水在原有基础上增加1000元。
三、删除语句—delete
1、删除表中名称为‘zs’的记录。
2、删除表中所有记录。
表简单查询1
1、查询表中所有学生的信息。
2、查询表中所有学生的姓名和对应的英语成绩。
3、过滤表中重复数据。
4、在所有学生分数上加10分特长分。
5、统计每个学生的总分。
6、使用别名表示学生分数。—使用where子句
7、查询姓名为刘备的学生成绩。
8、查询英语成绩大于90分的同学。
9、查询总分大于200分的所有同学。
10、查询英语分数在80-90之间的同学。
11、查询数学分数为89,75,91的同学。
12、查询所有姓刘的学生成绩。
13、查询所有姓刘两个字的学生成绩。
14、查询数学分>80并且语文分>80的同学。
15、查询数学分>80或者语文分>80的同学。—使用order by排序
表简单查询2
16、对数学成绩排序后输出。
17、对总分排序按从高到低的顺序推出。
18、对姓刘的学生成绩排序输出。—使用count(函数)
count统计的是结果集的行数,count()或者count(任意一列)。
count(1)效率最高,1就是第一列,count()是性能最慢的。
19、统计一个班级共有多少学生?
20、统计数学成绩大于或等于90的学生有多少个?
21、统计总分大于250的人数有多少?—使用sum函数
22、统计一个班级数学总成绩?
23、统计一个班级语文、英语、数学各科的总成绩。
24、统计一个班级语文、英语、数学的成绩总和。
25、统计一个班级语文成绩平均分。—使用avg函数
26、求一个班级数学平均分?
27、求一个班级总分平均分。—使用max,min函数
28、求班级最高分和最低分(数值范围在统计中特别有用)
建表和数据初始化
create table exam(
id int primary key auto_increment,
name varchar(20) not null,
chinese double,
math double,
english double
);
insert into exam values(null,‘关羽’,85,76,70);
insert into exam values(null,‘张飞’,70,75,70);
insert into exam values(null,‘赵云’,90,65,95);
insert into exam values(null,‘刘备’,97,50,50);
insert into exam values(null,‘曹操’,90,89,80);
insert into exam values(null,‘司马懿’,90,67,65);
分组——GROUP BY
- 根据一列或多列对结果集进行分组
- GROUP BY 字段1,字段2
- 字段1、字段2相同的为一组
- 在分组的列上可以使用聚合函数
- COUNT、SUM、AVG、MAX、MIN
- SELECT AVG(字段) FROM S GROUP BY 字段;
- SELECT中没有使用聚合函数的列,必须出现在GROUP BY后面
- COUNT、SUM、AVG、MAX、MIN
SQL执行顺序
- from
- on
- join
- where
- group by (开始使用select中的别名,后面的语句中都可以使用)
- avg, sum…
- having
- select
- distinct
- order by
- limit
可参阅:sql执行顺序是什么
https://www.php.cn/sql/421993.html
子查询概念
- 出现在其它SQL语句内的SELECT字句
- 子查询也叫做内部查询,包含子查询的语句称为外部查询或主查询
- 子查询自身可以包含一个或多个子查询,一个查询语句中可以嵌套任意数量的子查询
子查询的分类
- 非相关子查询
- 独立于外部查询,子查询只执行一次,执行完将结果传递给外部查询
- SELECT * FROM A WHERE A.id = (SELECT id FROM B);
- As a result of the subquery condition of the outer query
- As a result of the subquery table
- Correlated subquery
- Rely on external data query, the outer query Each time, the subquery is executed once
- As a result of the sub-query column
- Discover all the students in each class scored higher than average in this class
- SELECT * FROM tb_grade t1
where t1.score >
(
SELECT avg(t2.score) FROM tb_grade t2
WHERE t1.class_name = t2.class_name
);
Exercise
- Identify target users one-way information called "flowers", the condition
- Query employee multi-line information table manager, table
- Check out all the user names and objects ranked, non-related
Single-row subquery
Use subquery comparison operators
- =、>、<、>=、<=、!=、<>
- SELECT * FROM A WHERE id = (SELECT MAX(id) FROM A);
EXISTS, NOT EXISTS subqueries
- EXISTS sub-query is used to check whether the return line of data
- He stressed that returns a result set
- SELECT * FROM table WHERE EXISTS (subquery);
- EXISTS subquery does not return any type of data
- It can be understood as the return value is true or false
- If the sub-query contains the row (result set) Returns true, false otherwise