当前位置:网站首页>数据库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 '_马%';//查询同学名字里第二个字是马
边栏推荐
- Some introduction to FPC flexible circuit board design
- R语言使用epiDisplay包的aggregate.plot函数可视化每个子集的汇总统计信息(可视化基于单个分组下的阳性指标的概率值及其95%置信区间、基于折线图、仅仅适用于目标类别为二分类)
- 初识GO语言
- Tidb Hackathon 2021 - pcloud: conduct icloud pcloud team interview on the database
- Sudo of uabntu
- 邱盛昌:OPPO商业化数据体系建设实战
- Add static route
- (6) Control statement if/else switch
- Is the securities account opened by qiniu safe? Is it legal?
- 软件工程 学生信息管理系统 结构化的需求分析
猜你喜欢

Saturated! Can't future programmers work anymore?

R language arma-garch-copula model and financial time series case

Download PHP source code of leaf sharing station

How to win the "Olympic Games" in retail technology for jd.com, the learning tyrant of the "regular examination"?

JVM内存模型与本地内存

Learn the mitmproxy packet capturing tool from scratch

MySQL transaction introduction and transaction isolation level

redis. clients. jedis. exceptions. JedisConnectionException: Could not get a resource from the pool

First acquaintance with go language

Structural requirement analysis of software engineering student information management system
随机推荐
全局锁、表锁、行锁
Dongfeng Yueda Kia, Tencent advertising and hero League mobile game professional league cooperate to build a new E-sports ecology
R语言使用pdf函数将可视化图像结果保存到pdf文件中、使用pdf函数打开图像设备、使用dev.off函数关闭图像设备、自定义width参数和height参数指定图像的宽度和高度
Advanced Qt development: a preliminary study QT + OpenGL
How to view, modify, and delete SSH
多种Qt的开发方式,你选择哪种?
Go variables
Tidb Hackathon 2021 - pcloud: conduct icloud pcloud team interview on the database
"Upgrade strategy" of two new committers
Male god goddess voting source code v5.5.21 voting source code
2080 virtual machine login command
Go的变量
How to win the "Olympic Games" in retail technology for jd.com, the learning tyrant of the "regular examination"?
The safety of link 01 was questioned, and "ultra high strength" became "high strength"_ Publicity_ Steel_ problem
Memory control of node
STL——函数对象
C # final review programming question (guessed by the teacher)
How to change Golan back to the English version when it becomes the Chinese version
Fiddler抓包几种常用功能介绍(停止抓包、清空会话窗内容、过滤请求、解码、设置断点......)
How to use the official documents of pytorch and torchvision