当前位置:网站首页>MySQL---数据库从入门走向大神系列(一)-基础入门
MySQL---数据库从入门走向大神系列(一)-基础入门
2022-05-14 14:06:04【51CTO】
从最开始的创建数据库,创建表,创建列开始写起,再到常用的EXISTS函数,SELECT 复杂查询,模糊查询LIKE,创建视图 等深入学习。
为了对单词加深印象,全部在DOS下演示!
创建数据库、表
<span style = "font-size:14px;" > create database hncu character set utf8 ; </span >
- 1.
创建名为hncu编码为utf-8的数据库。
<span style = "font-size:14px;" >use hncu ; </span >
- 1.
打开hncu这个数据库。(必须要打开一个数据库才能在这个数据库下面创建table哦)
创建表格stud
<span style = "font-size:14px;" > create table stud (
sno varchar ( 15 ) not null primary key ,
sname varchar ( 15 ) not null ,
age int ,
saddress varchar ( 15 )
) ; </span >
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
表格添加数据:
<span style = "font-size:14px;" > insert into stud values ( '1001' , 'Jack' , 20 , '纽约' ) ;
insert into stud values ( '1002' , 'Tom' , 30 , '纽约' ) ;
insert into stud values ( '1003' , '张三' , 24 , '湖南益阳' ) ;
insert into stud values ( '1004' , '张四' , 15 , '湖南长沙' ) ;
insert into stud values ( '1005' , '李四' , 22 , '湖南益阳' ) ;
insert into stud values ( '1006' , '张三丰' , 80 , '武侠' ) ;
insert into stud values ( '1007' , '郭襄' , 75 , '武侠' ) ;
insert into stud values ( '1008' , '灭绝师太' , 10 , '武侠' ) ; </span >
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
查看stud表的数据:
<span style = "font-size:14px;" > select * from stud ; </span >
- 1.
给列名取别名显示:
<span style = "font-size:14px;" > select sno as 编号 ,sname as 姓名 ,age as 年龄 , saddress as 地址 from stud ; </span >
- 1.
select 复杂查询:
查询stud表格中age大于等于24的:
<span style = "font-size:14px;" > select * from stud where age >= 24 ; </span >
- 1.
查询stud表格中age大于等于20且下雨等于30的数据:
<span style = "font-size:14px;" > select * from stud where age >= 20 and age <= 30 ; </span >
- 1.
还有一种方法:
<span style = "font-size:14px;" > select * from stud where age between 20 and 30 ; </span >
- 1.
查询年龄等于20或者年龄等于30的stud表中的数据:
select * from stud where age=20 or 30;
还有一种方法:用 in();
查询年龄为20,22和30的stud表中的数据:
<span style = "font-size:14px;" > select * from stud where age in ( 20 , 22 , 30 ) ; </span >
- 1.
有一个和in相对的:not in
<span style = "font-size:14px;" > select * from stud where age not in ( 20 , 22 , 30 ) ; </span >
- 1.
模糊查询LIKE '%'匹配所有 '_'匹配单字符 ---必须和LIKE共同使用:
也就是说通配符只能在有like的情况下使用,如果是和=一起使用,那就只是普通的字符了。
查询名字是张开头的:
select * from stud where sname like '张%';
查询名字张开头的,而且名字只有2个字符的:
<span style = "font-size:14px;" > select * from stud where sname like '张_' ; </span >
- 1.
查询名字张开头的,而且名字只有3个字符的:
<span style = "font-size:14px;" > select * from stud where sname like '张__' ; </span >
- 1.
查询名字中带有‘三’的:
<span style = "font-size:14px;" > select * fom stud where sname like '%三%' ; </span >
- 1.
查询名字中带有‘三’的而且年龄大于30的:
<span style = "font-size:14px;" > select * from stud where sname like '%三%' and age > 30 ; </span >
- 1.
为表格增加一列:
<span style = "font-size:14px;" > alter table stud add column sex char ( 1 ) ; </span >
- 1.
省略column 也可以添加
<span style = "font-size:14px;" > alter table stud add sex char ( 1 ) ; </span >
- 1.
从stud表格删除sex列
<span style = "font-size:14px;" > alter table stud drop sex ; </span >
- 1.
也可以用:
<span style = "font-size:14px;" > alter table stud drop column sex ; </span >
- 1.
判断NULL值时,不能用‘=’号判断,而是用is:
险插入一行数据,让他的age为null;
<span style = "font-size:14px;" > update stud set age = 20 where age = null ; </span >
- 1.
这一句是不起作用的,因为这个无法用来判断age是否为null。
应该用下面这句:
<span style = "font-size:14px;" >elect stud set age = 20 where age is null ; </span >
- 1.
作用是:如果stud表格中哪行的age为null,就设置age为20.
如果是判断哪个为空字符,就直接可以用='' 来判断。
例:
<span style = "font-size:14px;" > select * from stud where saddress = '' ; </span >
- 1.
作用是:如果stud表中有saddress为空(注意!是空,不是null),就查询显示出来。
将saddress为纽约的改为硅谷
<span style = "font-size:14px;" > update stud set saddress = '硅谷' where saddress = '纽约' ; </span >
- 1.
注意:不是:这里不能写成 update table stud set...;
同时修改多个字段的值:
<span style = "font-size:14px;" > update stud set sname = 'ROSE' , saddress = '北京' where sno = '1002' ; </span >
- 1.
删除名字是悟空的行:
<span style = "font-size:14px;" > delete from stud where sname = '悟空' ; </span >
- 1.
知识点:
select 字段 from 表名 where 条件 and 条件 or 条件
update tableName set 需要设置的值 where 条件
delete from tableName where 条件
创建视图:cerate view 视图名 as select 子句
(虚表)---只存在内存中
create view aview as select * from stud where age>20;
从视图aview中查询年龄小于40的sname,age,ano:
<span style = "font-size:14px;" > select sname ,sno ,age from aview where age < 40 ; </span >
- 1.
聚合函数:
统计非null数据的行数:(*号和1 代表只要表中一行有非null的列数据,这一行就是非null)
一般要专门给个别用: as 别名
<span style = "font-size:14px;" > select count ( * ) from stud ;
select count ( 1 ) from stud ; </span >
- 1.
- 2.
统计age不为空的行数:
也就是age为null就不会被统计进去。
<span style = "font-size:14px;" > select count (age ) from stud ; </span >
- 1.
显示出stud表中所有age的平均值:
<span style = "font-size:14px;" > select avg (age ) as averageAge from stud ; </span >
- 1.
显示所有age平均值的四舍五入。
<span style = "font-size:14px;" > select round (avg (age ) ) as averageAge2 from stud ; </span >
- 1.
还有:
Sum求和。
Max求最大值,
Min求最小值。
<span style = "font-size:14px;" > select sum (age ) as sunAge from stud ;
select max (age ) as maxAge from stud ;
select min (age ) as minAge from stud ; </span >
- 1.
- 2.
- 3.
选择年龄最小的那个人的名字和年龄:
<span style = "font-size:14px;" > select sname , age from stud where age = ( selectt min (age ) from stud ) ; </span >
- 1.
这样用in也可以:
<span style = "font-size:14px;" > select sname ,age from stud where age in ( select min (age ) from stud ) ; </span >
- 1.
再创建一个年龄等于10的行:
<span style = "font-size:14px;" > insert into stud value ( '1009' , '李白' , 10 , '湖南' ) ; </span >
- 1.
再查年龄最小的那个人的年龄:
<span style = "font-size:14px;" > select age from stud where age = ( select min (age ) from stud ) ; </span >
- 1.
我们可以看到,因为有2个数据的年龄都是最小值,所有显示了2行,但是它们是重复的,完全没必要显示2行。
这个时候我们就要用到:distinct ,把完全相同的行,合并显示!
<span style = "font-size:14px;" > select distinct age from stud where age = ( select min (age ) from stud ) ; </span >
- 1.
排序-升序和降序:
按年龄升序排:
<span style = "font-size:14px;" > select * from stud order by age asc ; </span >
- 1.
按年龄降序排:
<span style = "font-size:14px;" > select sno ,sname ,age from stud order by age desc ; </span >
- 1.
exists存在判断
<span style = "font-size:14px;" > select sname ,age from stud where exists ( select * from stud where age = 20 ) ; </span >
- 1.
exists (select * from stud where age=20) ---只要存在age=20的,就返回true、
也就是exists(...) 是判断括号内的表达式是不是null的,如果是null则返回false,否则返回true;
此句因为stud存在age=20的行,所以会输出所有的sname,age。
分组 group by
<span style = "font-size:14px;" > select saddress , avg (age ) as 平均年龄 from stud group by saddress ; </span >
- 1.
按照saddress来分组,求出每组的平均年龄。
只要saddress不同就是不同的组!
按照saddress分组后每组的年龄总和:
select saddress,sum(age) as 年龄总和 from stud group by saddress;
有2个固定搭配:
排序:
select ... from ... where ... order by ...
分组:
select ... from ... group by ... by ... having ... (条件判断在having后面,不是用where)
这里的sum(age)也可以用as 别名 取一个别用,在判断的时候直接可以用别名的。
字符串处理函数
<pre name = "code" class = "sql" ><span style = "font-size:14px;" >Length (str ) - 求字符串长度
Ltrim (str ) - 去掉左边的空格
Rtrim (str ) - 去掉右边的空格
trim (str ) - 去掉两边的空格
Left (str ,n ) ; - 从左边取出n个字符
Right (str ,n ) ; - 从右边取出n个字符
Substring (str , begin ,end ) -返回子串
Reverse (str ) –返回颠倒的字符串
Lower (str ) - 转成小写
Upper (str ) - 转成大写
Concat (Str ,str….. )串联字符串。
Instr (str ,s ) – 返回s在str中出面的位置,没有则返回0 </span >
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
这里就只选取几个来演示了:
演示left();
显示saddress开始2个字符为湖南的行
<span style = "font-size:14px;" > select * from stud where left (saddress , 2 ) = '湖南' ; </span >
- 1.
串联字符串:
<span style = "font-size:14px;" > select concat (snon ,sname ,saddress ) as 串联字符串 from stud ; </span >
- 1.
instr(str,s) 返回s在str中出面的位置,没有则返回0
其实就是返回字串自一次出现的位置(从1开始计数)
select sname,instr(sname,'三') as ind from stud;'
本篇博客适于初学SQL的朋友学习,如果想继续学习,请关注我,我会在后续继续加深SQL~
因博主也是刚刚开始学习SQL语句,让我们一起进步吧~
边栏推荐
猜你喜欢
随机推荐
- paramiko下载大文件出错问题 sftp
- CLIP学习笔记
- SAP:SWITCH用法
- AD7606/AD7616使ZYNQ在能源电力领域如虎添翼,可实现16/32/64通道AD同步采样
- AM57x 多核SoC开发板——GPMC的多通道AD采集综合案例手册(上)
- 【日常训练】面试题 01.05. 一次编辑
- 【日常训练】384. 打乱数组
- AM57x 多核SoC开发板——GPMC的多通道AD采集综合案例手册(下)
- EDA technology and market analysis
- libmodus源码解读
- 一招win7 c盘瘦身
- Day 1:轮转数组
- 美团四大名著为什么不是三或五
- 【实践篇】mmdetection修改自己的config文件
- 是能力更是文化,談談IT系統的安全發布
- 红旗H9:有一种安全感来源于“看见”
- LeetCode两句话中不常见单词
- Lamda表达式-入门篇
- 【学习笔记】seckill-秒杀项目--(10)安全优化
- Roblox剑九之剑一
- C'est la capacité, c'est la culture.
- Am57x multi-core SoC development board -- GPMC's comprehensive case manual for multi-channel AD acquisition (Part 2)
- [daily training] 384 Scramble array
- [daily training] interview question 01.05 One edit
- Am57x multi-core SoC development board -- GPMC's comprehensive case manual for multi-channel AD acquisition (Part I)
- Ad7606 / ad7616 make zynq more powerful in the field of energy and power, and can realize 16 / 32 / 64 channel ad synchronous sampling
- Sap: switch usage
- Clip learning notes
- Paramiko download large file error SFTP
- Installing the Axure plug-in from chrome
- Vscode build go development environment
- 2021-ieee paper - Application Status and performance analysis of deep neural network in document image table recognition
- How to use webpach packer
- ICDAR 2021 competition scientific literature analysis - table identification summary (the rest is document layout analysis)
- (transfer learning and fine tuning)
- 包装类的使用
- Postman loop calls the same interface
- Tensorflow learning 6 -- run through UNET image segmentation
- It is not only ability but also culture. Talk about the security release of IT system
- After you get the new iPhone, you should turn on these eight settings to make the phone safer and easier to use