当前位置:网站首页>The second day of learning MySQL: SQL (basic)
The second day of learning MySQL: SQL (basic)
2022-08-06 03:02:00 【like to eat beans】
目录
五,DML(数据操作语言)--Data Manipulation Language
六,DQL (数据查询语言)--Data Query Language
3)去除重复记录:(Returns the deduplicated data)
七,DCL(数据控制语言)--Data Control Language
学习大纲:

一,SQL通用语法
1. SQL语句可以单行或多行书写,以分号结尾.
2.SQL语句可以使用空格/缩进来增强语句的可读性.
3. MySQL数据库的SQL语句不区分大小写, 关键字建议使用大写.(统一规范)
4.注释:
●单行注释: -- 注释内容或 # 注释内容(MySQL特有)
●多行注释: /* 注释内容*/
二,SQL分类

三,DDL(数据定义语言)
1,数据库操作:


2,表操作:

before querying all tables,The premise is to enter this database first.(use 数据库名)

注意: [..]为可选参数,最后一个字段后面没有逗号
Try to create the following table structure using the pseudocode above.

注:Do not arbitrarily operate in the system library.

3,表操作-数据类型:
MySQL中的数据类型有很多,主要分为三类:数值类型、字符串类型、日期时间类型.
1)数据类型:

精度:所有位数
标度:小数位数
2)字符串类型:

char的性能较好,(varchar).There is a calculation process.(计算字符串的长度)
3)日期类型:

4)案例:
根据需求创建表(设计合理的数据类型、长度)
设计一张员工信息表,要求如下:
1.编号(纯数字)
2.员工工号 (字符串类型,长度不超过10位)
3. 员工姓名(字符串类型,长度不超过10位)
4.性别(男/女,Store one Chinese character)
5.年龄(正常人年龄,不可能存储负数)
6. 身份证号(二代身份证号均为18位,身份证中有X这样的字符)
7.入职时间 (取值年月日即可)
4,表操作-修改:
1)增加字段:
![]()
为emp表增加一个新的字段”昵称"为nickname,类型为varchar(20)
2)修改:

将emp表的nickname字段修改为username,类型为varchar(30)

3)删除字段:
![]()
将emp表的字段username删除

4)修改表名:
![]()
将emp表的表名修改为employee

5,表操作-删除



Delete all the original data,Recreate a new table,There is no data in the table.
6,总结:
1)数据库操作:

2)表操作:

四, 图形化界面工具DataGrip
Daily development efficiency is low,Not intuitive to use.

五,DML(数据操作语言)--Data Manipulation Language
It is used to add, delete, and modify the data records of the table in the database

1,增加数据:
1)给指定字段添加数据:
![]()
2)Add data to all fields:
![]()
3)批量添加数据:

注意:
●插入数据时,指定的字段顺序需要与值的顺序是一一对应的..
●字符串和日期型数据应该包含在引号中.
●插入的数据大小,应该在字段的规定范围内.


2,修改数据:
![]()
注意:修改语句的条件可以有,也可以没有,如果没有条件,则会修改整张表的所有数据.



如果不带where条件,Represents modifying the entire table. ![]()
3,删除数据:
![]()
注意:
DELETE语句的条件可以有,也可以没有,如果没有条件,则会删除整张表的所有数据.
DELETE语句不能删除某一个字段的值(可以使 用UPDATE).

六,DQL (数据查询语言)--Data Query Language
用来查询数据库中表的记录.

1,基本查询:
1)查询多个字段:

2)设置别名: 
3)去除重复记录:(Returns the deduplicated data)
![]()
create table emp(
id int comment '编号',
workno varchar(10) comment '工号',
name varchar(10) comment '姓名',
gender char(1) comment '性别',
age tinyint unsigned comment '年龄',
idcard char(10) comment '身份证号',
workaddress varchar(50) comment '工作地址',
entrydata date comment '入职时间'
) comment '员工表';
insert into emp(id, workno, name, gender, age, idcard, workaddress, entrydata)
values (1,'1','y11','m',29,'1234','北京','2008-01-01'),
(2,'2','y12','m',30,'2234','北京','2008-03-01'),
(3,'3','y13','w',19,'3234','北京','2008-05-01'),
(4,'4','y14','m',56,'4234','北京','2008-07-01'),
(5,'5','y15','w',25,'5234','北京','2008-08-01'),
(6,'6','y16','m',35,'6234','北京','2009-01-01'),
(7,'7','y17','w',38,'7234','北京','2002-01-01'),
(8,'8','y18','m',45,'8234','北京','2005-06-01'),
(9,'9','y19','w',29,'9234','北京','2008-01-01'),
(10,'10','y10','m',21,'0234','北京','2007-03-01');

案例:

1,

2,

尽量不写*,不直观,会影响效率.
3,

as可写可不写.
4,


You cannot use aliases to check duplicates.
2,条件查询:
![]()


案例:
1, 
2,
4,


Keywords cannot be mixed.

6,
7,


betweenfollowed by the minimum value,andfollowed by the maximum value.
9,
10,

11,The first place to look up the ID number is9的员工信息



3,聚合函数:
1)将一列数据作为一个整体,进行纵向计算.
2)常见聚合函数:

3)语法:
![]()
案例:
注意: null值不参与所有聚合函数运算.
1,


2,Four decimal places are automatically rounded
3,
5,
4,分组查询:
![]()
where与having区别
执行时机不同: where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组之后对结果进行过滤.
判断条件不同: where不能对聚合函数进行判断,而having可以.
案例:

1,

3,
注意
●执行顺序: where >聚合函数> having.
●分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无任何意义.

5,排序查询:
![]()
排序方式
ASC:升序(默认值)
DESC:降序
注意:如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序.
案例:

1,
3,

6,分页查询:
![]()
注意
●起始索引从0开始,起始索引= (查询页码-1) *每页显示记录数.
●分页查询是数据库的方言,不同的数据库有不同的实现,MySQL中是LIMIT.
●如果查询的是第一页数据,起始索引可以省略,直接简写为limit 10.
案例:

1,
2,
习题:

答案:

7,执行顺序:

验证!!!!
七,DCL(数据控制语言)--Data Control Language
用来管理数据库用户,控制数据库访问权限.
There are mainly two aspects of management.
1,Which users can access the database server.(用户管理)
2,Which databases this user can access.(权限控制)
1,用户管理:

案例:

注意:
●主机名可以使用%通配.
●这类SQLStatement developers operate less,主要是DBA ( Database Administrator 数据库管理员)使用.


2,权限控制:

还想知道更多,Just check the official documents.


无任何权限,Just be able to log into the database,Just connect to the database.


When viewing from the command line,要先退出,go in again.不然的话,Permissions remain unchanged.
注意:
●多个权限之间,使用逗号分隔
●授权时,数据库名和表名可以使用*进行通配,代表所有.
边栏推荐
猜你喜欢
随机推荐
LeetCode每日两题01:滑动窗口-无重复字符的最长样子
美国国立卫生研究院(NIH)江晓芳组诚聘生物信息学博士后
5. redux
ansible copy 模块
ansible script 模块
FTX exchange listed in Forbes 2022 fintech 50 list
KGAT recommendation system
A tester in 1995, he wouldn't dare to ask for 12K~ Looking at his resume, I have a lot of thoughts...
NRF52840-QIAA-R Nordic BLE5.0蓝牙无线收发芯片
分享好用无广告的手机浏览器,亲测值得下载
【无标题】
详解AUTOSAR:什么是AUTOSAR?(理论篇—1)
什么是内部类?
FTX交易所入榜福布斯2022金融科技50强名单
软件工程-大学体育馆管理系统用例图
华为设备配置Smart Link负载分担
ansible fetch 模块
ansible ping 模块
一个关于pm2 自动化部署项目的问题
2022/08/05 学习笔记 (day24)集合








