DDL、DML、DQL、DCL基础语法

1、DDL

查询

查询所有数据库:show databases;

show databases;

查询当前数据库:select database();

select database();

数据库创建

创建数据库:create database [if not exist(若存在重名数据库,则不创建)] 数据库名 [default charset 指定字符集] [collate 排序规则]

creat database MYDATA 

数据库删除

删除指定数据库:drop database [if exist(若不存在指定数据库,则不操作)] 数据库名

数据库切换

使用指定数据库:use 数据库名;

use MYDATA

表创建

在当前数据库中创建表格:show tables;

creat table 表名(

                                字段1 字段1类型 [comment 注释1],

                                字段2 字段2类型 [comment 注释2],

                                ……

                                字段n 字段n类型 [comment 注释n]

                           )[comment =表注释];

create table my_table (id int comment '序号',name varchar(50) comment '名字',number varchar(30) comment '学号',score int comment '分数'
) comment='成绩单';

表查询

查询当前数据库所有表格:show tables;

show tables;

查询指定的表结构:desc 表名;

desc my_table;

查询指定表的建表语序:show create table 表名;

show create table my_table;

表修改

为表格添加一个字段:alter table 表名 add 字段名 类型(长度)[comment];

alter table my_table add ranks int comment '排名';

修改某个字段的类型:alter table 表名 modify 字段名 新类型(长度);

alter table my_table modify score double;

修改字段名和字段类型:alter table 表名 change 旧字段名 新字段名 新类型(长度)[comment]

alter table my_table change score fenshu int comment '得分';

删除指定字段:alter table表名 drop 字段名;

alter table my_table drop fenshu;

 重命名表格:alter table 表名 rename to 新表名

alter table my_table rename to mytable;

表删除

删除指定表格:drop table 表名;

drop table my_table;

删除指定表格,并重新创建该表:truncate table 表名;

truncate table mytable;

2、DML

添加数据

为指定的字段添加数据:insert into 表名 (字段1,字段2,……) value (值1,值2,……);

insert into mytable (id,name) value (1,'小明');

为所有字段添加数据,值的顺序与字段顺序一致: insert into 表名 values (值1,值2,……);

insert into mytable values (1,'小王','2301110','63');

批量为指定字段添加数据:insert into 表名 (字段1,字段2,……)value(值1,值2,……)(值1,值2,……);

insert  into  mytable (id,name) values (3,'小红'),(4,'小王');

批量为所有字段添加数据:insert into 表名 (值1,值2,……),(值1,值2,……);

insert into mytable values (5,'小张','234234',88),(6,'小吕','224556',35);

#注意,日期与字符串类型数据使用引号标注,插入数据的长度不超过定义的长度

修改数据

修改指定字段的指定值:update 表名 set 字段名1=值1,字段名2=值2,……[where 条件]#若不指定条件,则默认修改所有指定字段的数据

update mytable set score = 99 where name = '小吕'
update mytable set score = 78,number = '123456' where name ='小王'

删除数据

修改指定字段的该行数据(不可删除单个字段):delete from 表名 [where 条件];#若不指定条件,则清空数据表

delete from mytable where id = 1;

3、DQL

基础查询

查询表中指定字段的数据:select 字段1,字段2,字段3…… from 表名;

select id,name from mytable;

查询表中所有字段的数据(不建议在实际中用):select * from 表名;

select * from mytable;

查询表中字段时为字段起别名:select 字段1 [as] '别名1',字段2 '别名2',…… from 表名

select id as '序号',name '姓名' from mytable;

查询表中字段数据时清除重复值:select distinct 字段 from 表名;

select distinct wherefo from mytable;

条件查询

查询表中与指定的条件匹配的数据:select 字段1,字段2,字段3……from 表名 where 条件

条件包括:

>:大于               <:小于        =:等于        <=:小于等于        >=:大于等于     <>/ !=:不等于

between x and y:位于x和y之间的数,包含x和y

in(值1,值2,……):匹配in中列表的任一值

like _:模糊匹配单个字符        like %:模糊匹配任意字符

is null:为空字符 

and/&&:与

or/|| :或

not/!:非

select id,name,wherefo,score,dates from mytable where score > 560;
select id,name,wherefo,score,dates from mytable where wherefo = '北京';
select id,name,wherefo,score,dates from mytable where score between 520 and 580;
select id,name,wherefo,score,dates from mytable where wherefo != '北京'
select id,name,wherefo,score,dates from mytable where name like '___';
select id,name,wherefo,score,dates from mytable where name like '小%';
select id,name,wherefo,score,dates from mytable where id<9 and wherefo = '上海';

分组查询

聚合函数,将表中的一列作为整体纵向计算(无法统计null):select 聚合函数(字段) from 表名;

聚合函数包括:

count:统计数量

max:最大值

min:最小值

avg:平均值

sum:求和

select count(*) from mytable;
select count(number) from mytable;
select sum(score) from mytable;
select avg(score) from mytable;
select max(score) from mytable;
select min(score) from mytable;
select count(distinct wherefo) from mytable;
select count(*) from mytable where wherefo = '上海';

根据分组后的条件再进行分组过滤:select 字段 from 表名 [where 条件] group by 分组字段名 [having 过滤后条件]

where条件发生在分组前,且无法对聚合函数进行判断

having条件发生在分组后,可以对聚合函数进行判断

select wherefo as '城市',count(*) as '人数' from  mytable group by wherefo;
select wherefo as '城市',avg(score) as '平均分' from  mytable group by wherefo;
select wherefo as '城市', avg(score) as '平均分' from mytable group by wherefo having avg(score) > 560 and count( *)>3;

排序查询

根据指定的字段升序、降序排序(不写排序规则则默认升序):select 字段 from 表名 order by 字段1 排序规则1,字段2 排序规则2#在字段1值一致时看字段2

select id,name,wherefo,score,dates from mytable order by score;
select id,name,wherefo,score,dates from mytable order by score desc;
select id,name,wherefo,score,dates from mytable order by score asc,dates desc;

分页查询

根据指定的索引和每页展示的数据个数进行查询:select 字段 from 表名 limit 起始索引 每页查询数;

起始索引=(页码数-1)*每页查询数,若是第一页可以不写起始索引

select id,name,wherefo,score,dates from mytable limit 3;
select id,name,wherefo,score,dates from mytable limit 3, 3;

DQL的编写顺序:select 字段 from 表名 where 条件 group by 分组字段 having 分组后字段 order by 排序规则 limit 起始索引,每页查询数

DQL的执行顺序:from where group by having select order by limit

4、DCL

管理用户

查询用户:select * user;

创建用户:create user '用户名‘@’主机名' identified by '密码',主机名可使用通配符%代表可以在任意主机登陆

create user 'user1'@'localhost' identified by '123456';

修改用户密码:alter user '用户名‘@’主机名' identified with mysql_native_password by '新密码'

alter user 'user1'@'localhost' identified by '654321';

删除用户:drop user ’用户名‘@’主机名‘

drop user 'user1'@'localhost';

权限管理

MYSQL常用权限(多个权限用,分隔):

ALL/all privileges:所有权限

select:查询数据

insert:插入数据

update:修改数据

delete:删除数据

alter:修改表

drop:删除数据库/表/视图

create:创建数据库/表

*:通配符,代表所有

查询用户权限:show grants for '用户名'@‘主机名’;

show grants for 'user1'@'localhost';

授予用户权限:grant 权限列表 on 数据库名.表名 to '用户名'@'主机名';

grant select on my_database.mytable to 'user1'@'localhost';
grant all on my_database.* to 'user1'@'localhost';
grant all on *.* to 'user1'@'localhost';

撤销用户权限:revoke 权限列表 on 数据库名.表名 from '用户名'@'主机名'

revoke insert on my_database.mytable from 'user1'@'localhost';
revoke all on *.* from 'user1'@'localhost';

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/diannao/88452.shtml
繁体地址,请注明出处:http://hk.pswp.cn/diannao/88452.shtml
英文地址,请注明出处:http://en.pswp.cn/diannao/88452.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

VScode常用快捷键【个人总结】

注&#xff1a;快捷键以 Windows/Linux 为主&#xff0c;Mac 用户将 Ctrl 替换为 Cmd&#xff0c;Alt 替换为 Option。 1. 编辑相关 快速复制与剪切 Alt Shift ↓&#xff1a;复制当前行到下方Alt Shift ↑&#xff1a;复制当前行到上方Ctrl X&#xff1a;剪切整行&…

数据结构与算法:线性表-顺序表(顺序存储)

一、线性表的定义&#xff08;逻辑结构&#xff09; 线性表是由 n (n > 0) 个相同数据类型的数据元素组成的有限序列&#xff0c;其中 n 为线性表的表长&#xff0c;当 n 0 时&#xff0c;线性表为空表。如果用 L 命名线性表&#xff0c;那么一般表示为&#xff1a;L (a1…

从源码到实践:Java集合框架面试核心知识点全解析

在Java开发中&#xff0c;集合框架&#xff08;Java Collections Framework&#xff09;是最基础也最常用的工具集。无论是处理业务逻辑时的数据暂存&#xff0c;还是高性能场景下的算法优化&#xff0c;集合的使用都贯穿始终。因此&#xff0c;Java集合相关的面试题几乎是所有…

【深度学习新浪潮】空间计算的医疗应用技术分析(简要版)

空间计算是一种通过融合计算机视觉、传感器技术与三维渲染,将虚拟内容精准锚定到物理空间,实现数字世界与现实世界无缝交互的技术体系。其核心在于让计算机理解真实环境的结构、位置和动态,从而支持自然交互(如手势、语音、眼动)和沉浸式体验。例如,苹果Vision Pro通过实…

win电脑没有xcode怎么上传ipa

在上架IOS项目的时候&#xff0c;遇到一个问题&#xff0c;如下图&#xff0c;在app store connect上架的时候&#xff0c;需要选择一个构建版本&#xff0c;然后它在下方提示&#xff0c;点击查看上传工具后&#xff0c;会发现需要下载xcode或mac命令行等工具来上传编译后的文…

相机标定与3D重建技术通俗讲解

一、什么是相机标定&#xff1f;能解决什么问题&#xff1f; 相机标定是计算机视觉中的基础技术&#xff0c;简单来说&#xff0c;就是确定相机从3D世界拍摄到2D图像时的"转换规则"。具体解决两个核心问题&#xff1a; 相机内部属性&#xff1a;如焦距&#xff08;…

DeepSeek-Reasoner推理模型示例

《DEEPSEEK原生应用与智能体开发实践 王晓华 书籍 图书》【摘要 书评 试读】- 京东图书 在之前讲解的示例中&#xff08;指这个示例&#xff1a;通过Prompt提示构建思维链-CSDN博客&#xff09;&#xff0c;无论是进行日常对话还是调用特定工具&#xff0c;我们所依赖的底层技…

常说的电源芯片到底指什么?

电源芯片是电子系统中用于管理、转换和分配电能的集成电路&#xff0c;根据功能和应用场景的不同&#xff0c;主要分为以下几类&#xff1a; 一、线性稳压器&#xff08;LDO, Low Dropout Regulator&#xff09; LDO内部的基本电路情况如下&#xff1a; LDO内部主要分为四大部…

【大模型学习】项目练习:套壳DeepSeek

这里是阿川的博客&#xff0c;祝您变得更强 ✨ 个人主页&#xff1a;在线OJ的阿川 &#x1f496;文章专栏&#xff1a;AI入门到进阶 &#x1f30f;代码仓库&#xff1a; 写在开头 现在您看到的是我的结论或想法&#xff0c;但在这背后凝结了大量的思考、经验和讨论 &#x1f4…

笔记03:布线-过孔的调用与添加

布线-过孔的调用与添加 &#xff08;1&#xff09;在进行PCB设计时&#xff0c;都必须使用到过孔&#xff0c;对走线进行换层处理。在走线进行打过孔之前&#xff0c;必须先要添加过孔&#xff0c;这样在PCB布线时才可以使用过孔。 &#xff08;2&#xff09;需要使用pad des…

在vscode中,Python程序的内置对象、关键字、自定义函数名/类名、字符串进行着色,说明分别是什么颜色?

在 VS Code 中&#xff0c;Python 代码的着色完全取决于你当前使用的主题。不同主题&#xff08;如 Dark, Monokai, Solarized Dark, Light, Quiet Light 等&#xff09;对不同类型的代码元素会使用不同的颜色。 一、Default Dark&#xff08;默认的深色主题&#xff09; impo…

Visual Studio 中使用 AddressSanitizer 指南

Visual Studio 中使用 AddressSanitizer 指南 基于 Microsoft Visual Studio 2022&#xff0c;支持 MSVC 和 Clang 编译器链&#xff0c;本文详细说明如何在 VS 中配置和使用 AddressSanitizer&#xff0c;用于检测内存误用&#xff0c;如消息释放后访问、超界读写等类型错误。…

Flink Sink函数深度解析:从原理到实践的全流程探索

在Flink的数据流处理体系中&#xff0c;Sink函数作为数据处理的最终出口&#xff0c;肩负着将处理后的数据写入外部存储引擎的关键使命。它如同数据旅程的终点站&#xff0c;决定着数据的最终归宿与应用价值。深入理解Sink函数的工作原理、核心概念及实现方式&#xff0c;对构建…

Codex+ 自建中转 API 部署教程(Windows 版)

&#x1f4cc; 一、前置环境准备 安装 Node.js 和 Codex CLI&#xff1a; npm install -g openai/codex准备 OpenAI API Key 确保你已有的中转接口兼容 OpenAI 格式&#xff0c; &#x1f4cc; 二、设置 PowerShell 环境变量 # 设置你的 API Key&#xff08;使用哪家的看你的…

Centos 7离线部署Nginx 高效省时

给脚本执行权限&#xff1a;chmod x install_nginx.sh以root用户运行&#xff1a;sudo ./install_nginx.sh 脚本如下&#xff1a; #!/bin/bash # Nginx一键化部署脚本&#xff08;修复版本开机自启&#xff09; # 需要以root权限运行set -e # 任何命令失败时立即退出脚本# 定…

P7915 [CSP-S 2021] 回文

题目描述 给定正整数 n n n 和整数序列 a 1 , a 2 , … , a 2 n a_1, a_2, \ldots, a_{2 n} a1​,a2​,…,a2n​&#xff0c;在这 2 n 2 n 2n 个数中&#xff0c; 1 , 2 , … , n 1, 2, \ldots, n 1,2,…,n 分别各出现恰好 2 2 2 次。现在进行 2 n 2 n 2n 次操作&#xf…

小智AI -- ESP32-S3 DIY面包板WIFI-LCD彩屏

DIY 所需硬件 开发板&#xff1a;ESP32-S3-DevKitC-1&#xff08;选择 WROOM N16R8 模组&#xff09; Goouuu ESP32-S3-N16R8开发板数字麦克风&#xff1a;INMP441 INMP441全向麦克风模块功放&#xff1a;MAX98357A MAX98357 I2S 音频放大器模块腔体喇叭&#xff1a;8Ω 2~3W 或…

家用网络进行DNS优选

家用网络进行DNS优选的好处主要体现在以下几个方面&#xff1a; 提升网络访问速度&#xff1a; DNS优选通过选择响应时间更快的DNS服务器&#xff0c;减少域名解析的延迟&#xff0c;从而加快网页加载和应用访问速度。尤其在访问国内外网站时&#xff0c;选择合适的DNS服务器可…

刷题 | 牛客 - js中等题-下 (更ing)45/54知识点解答

JS45 数组去重 描述 为 Array 对象添加一个去除重复项的方法 示例1 输入&#xff1a; [false, true, undefined, null, NaN, 0, 1, {}, {}, a, a, NaN] 复制输出&#xff1a; [false, true, undefined, null, NaN, 0, 1, {}, {}, a] Array.prototype.uniq function () …

vue3使用krpano1.22

官方文档链接 https://krpano.com/docu/js/#top 例子 https://krpano.com/releases/1.22/viewer/examples/javascript-interface/js-api-examples.html https://krpano.com/viewsource.html?releases/1.22/viewer/examples/javascript-interface/js-api-examples.html 注…