当前位置:网站首页>数据库SQL操作基础
数据库SQL操作基础
2022-06-12 17:10:00 【积分中值定理】
数据库SQL操作基础
分为四类语言:
DDL:data definition language–数据定义语言–用来定义数据库对象(数据表,列,库等)的语言,关键字如:create、drop、alter等
DML:data manipulation language–数据操作语言–增删改,关键字如insert、delete、update等
DQL:data query language–数据查询语言–查询,关键字如select、where等
DCL:data control language–数据控制语言–了解
CRUD,分别指的是:create/retrieve/update/delete
ddl:操作数据库
R: show databases; //展示库
R: show create database 库名; //查询创建库xxx时的语法,可以用来查看该库字符集
C: creata database 库名; // 创建新库
C; create database if not exists 库名; // 创建新库前先查看该库是否已存在
C: create database 库名 character set (+utf8/utf16/gbk); // 创建符合指定字符集的新库
例子:创建一个新库,船舰之前判断是否存在,,并指定字符集为gbk
create database if not exists 库名 character set gbk;
U: alter database 库名 character set 字符集名;//修改指定库的字符集
D:drop database 库名;//删库
D: drop database if exists 库名;//删库前判断是否存在,如果存在则删除
-------
use 库名;//使用某库
select database();//查询当前正在使用的库名
ddl:操作表
R: shwo tables;//查询库内所有表名字
desc 表名;// 查询表结构
show create table 表名;// 查看表的字符集类型
C: 创建语法
create table 表名(列名1 数据类型1,列名2 数据类型2,...,列名n 数据类型n);
例子:创建id+姓名+年龄+分数+生日+添加时间:
create table 表名(id int,name varchar(32),age int,
score double,birthday date,logtime timestamp);
D: drop table 表名;//删除
drop table if exists 表名;
C: create table 新表 like 旧表;
U: alter table 表名 rename 新名; // 修改表名
alter table 表名 character set 类型; // 修改表的字符集类型
alter table 表名 add 列名 数据类型;//添加一列新的类型数据
alter table 表名 change 老列名 新列名 新类型;//修改列名 类型
alter table 表名 modify 列名 新类型;//只修改类型不改名
alter table 表名 drop 列名;// 删除列
dml:增删改查表中数据
C: insert into 表名(列名1,列名2,...)
values(值1,值2,...)//添加
/*一一对应/表名可以不写,此时i默认给所有列添值/除int外,其余类型用引号引起来*/
D: delete from 表名 where 条件;//删除
//如果没有条件,则删除表中所有数据
如:delete from 表名 where id = 1;
U: update 表名 set 列名1=值1,列名2=值2,...where 条件;
如:student的表中id=3的同学,年龄修改为20
update student set age = 20 where id = 3;//如果没有条件,则修改表中所有数据
dql:查询表中记录
语法:
select 字段名 from 表名 where 条件 group by 分组字段
having 分组之后的条件 order by 排序 limit 分页
| 排序查询 | 聚合函数 |
|---|---|
| 分组查询 | 分页查询 |
1.排序查询:
语法:order by 排序字段 排序方式(默认是升序asc,也可以设置为降序desc);
ex: select * from student order by math desc;//按照数学成绩由大到小排序
注:如果有多个排序条件,则当满足第一个条件时,再去判断第二条件是否满足
如按照数学成绩从大到小对学生排序,如果数学成绩一样,就按照英语成绩由小到大排序
select * from student order by math desc,english asc;
2.聚合函数:将一列数据作为一个整体,进行纵向的计算
1. count:计算个数----一般选择非空的列
2. max:计算最大值
3. min:最小值
4. sum:求和
5. avg:平均值
注:使用聚合函数时,要排除null值。如果用null,可以用ifnull函数解决
如英语成绩是null,可以改为ifnull(english,0)//如果是null,就用0代替
3.分组查询
语法:group by 分组字段
注:分组之后查询的字段:分组字段、聚合函数
where和having的区别:
where在分组之前进行限定,如果不满足条件就不参与分组,having是在分组之后进行限定,如果不满足条件,就不能得出结果。
where后不可以跟聚合函数,having后可以进行聚合函数的判断。
分页查询
语法:limit 开始的索引,每页查询的条数;
开始的索引 = (当前的页码 - 1) * 每页显示的条数
如:select * from stu limit 0,4;//从索引位置0开始,每页查询4条数据
基础查询
- 多个字段查询:
select 字段名1,字段名2... from 表名;
- 去重复:distinct
在同学表中查询地址:
select address from stu;
在同学表中查询不重复的地址:
select distinct address from stu;
- ifnull语句
ifnull(表达式1,表达式2):
条件查询
where 子句后跟条件,条件中可有以下运算符
- > 、< 、<= 、>= 、= 、<>(不等于)
- between...and
- like:模糊查询---涉及到占位符
占位符:1.下划线_:代表单个任意字符 2.%:代表0或者多个任意字符
- is null
- and 或者 &&
- or 或者 ||
- not 或者 !
模糊查询举例:
select *from student where name like '马%';//查询同学中姓马
select *from student where name like '%马%';//查询同学名字里含有马字
select *from student where name like '_马%';//查询同学名字里第二个字是马
边栏推荐
- Gerrit+2触发Jenkins任务
- Selenium element positioning
- A variety of Qt development methods, which do you choose?
- Swintransformer network architecture
- Google browser debugging skills
- 淘宝Native研发模式的演进与思考 | DX研发模式
- Alibaba cloud image station supports IPv6!
- 使用GCC的PGO(Profile-guided Optimization)优化整个系统
- Cicada mother talks to rainbow couple: 1.3 billion goods a year, from e-commerce beginners to super goods anchor
- Li Kou today's question 926 Flip string to monotonic increment
猜你喜欢

多种Qt的开发方式,你选择哪种?

Gerrit+2 triggers Jenkins task

Google浏览器调试技巧

Qt开发高级进阶:初探qt + opengl

龙芯处理器内核中断讲解

Interesting LD_ PRELOAD

JVM memory model and local memory

I heard that distributed IDS cannot be incremented globally?

Sizepolicy policy in layout management

How to win the "Olympic Games" in retail technology for jd.com, the learning tyrant of the "regular examination"?
随机推荐
1.5 什么是架构师(连载)
C# 业务流水号规则生成组件
Tidb Hackathon 2021 - pcloud: conduct icloud pcloud team interview on the database
Feedback compilation
Pat class a 1139 first contact
Gerrit+2触发Jenkins任务
redis. clients. jedis. exceptions. JedisConnectionException: Could not get a resource from the pool
淘宝Native研发模式的演进与思考 | DX研发模式
快速入门scrapy爬虫框架
Crazy temporary products: super low price, big scuffle and new hope
Uniapp wallpaper applet source code / double ended wechat Tiktok applet source code
The R language uses the tabpct function of the epidisplay package to generate a two-dimensional contingency table, and uses the mosaic diagram to visualize the contingency table (two-dimensional conti
Use GCC's PGO (profile guided optimization) to optimize the entire system
Cloud development kunkun chicken music box wechat applet source code
[BSP video tutorial] stm32h7 video tutorial Issue 8: the last issue of the MDK theme, the new generation of debugging technologies event recorder and RTT, and using stm32cubemx to generate project tem
32-bit MCU mm32f0040 with smart micro high performance M0 kernel
Guitar Pro tutorial how to set up a MIDI keyboard
The R language uses the tablestack function of epidisplay package to generate statistical analysis tables based on grouped variables (including descriptive statistical analysis, hypothesis test, diffe
R语言使用epiDisplay包的aggregate.plot函数可视化每个子集的汇总统计信息(可视化基于单个分组下的阳性指标的概率值及其95%置信区间、基于折线图、仅仅适用于目标类别为二分类)
Add static route