开始MySQL之路—— DDL语法、DML语法、DQL语法基本操作详解

news2024/9/25 1:22:46

DDL语法

DDL(Data Definition Language) 数据定义语言,该语言部分包括以下内容。

  • 对数据库的常用操作

  • 对表结构的常用操作

  • 修改表结构

对数据库的常用操作

1: 查看当前所有的数据库

show databases;

2:创建数据库

create database if not exists 数据库名称;

create database 数据库名称;

3:选择使用哪一个数据库

use 数据库名称;

4:删除数据库

drop database 数据库名称;

drop database if exists 数据库名称;

5:修改数据库编码

alter database school character set utf8;

对表结构的常用操作-创建表

1:创建表格式

create table [if not exists] 表名(
  字段1 类型[(宽度)] [约束条件] [comment '字段说明'],
  字段2 类型[(宽度)] [约束条件] [comment '字段说明'],
  字段3 类型[(宽度)] [约束条件] [comment '字段说明'],
)[表的一些设置];

创建表是构建一张空表,指定这个表的名字,这个表有几列,每一列叫什么名字,以及每一列存储的数据类型。

2:数据类型

数据类型是指在创建表的时候为表中字段指定数据类型的,只有数据符合类型要求才能存储起来,使用数据类型的原则是:够用就行,尽量使用取值范围小的,而不用大的,这样可以更多的节省存储空间。

  • 数值类型

  • 日期和时间类型

  • 字符串类型

3:数值类型

create table if not exists student(
   -- 无符号 没有负数
   sid int unsigned,
   name varchar(20),
   gender varchar(20),
   age int,
   birthday date,
   address varchar(20)
);

  • 字符串类型

  • 日期类型

对表结构的其它操作

1:查看当前数据库的所有表

show tables ;

2:查看数据表的创建语句

show create table 数据表名;

3:查看表结构

desc 数据表名;

4:删除表

drop table 数据表表名;

5:修改表结构格式

语法格式

alter  table 表名 add  列名  类型(长度) 【约束】;

例子:

为student表添加一个字段为:系别dept类型为department
alter table student add column dept varchar(20);

6:修改列名和类型

语法格式

修改列名和表名,alter table 表名 change 旧列名 新列名 类型(长度) [约束]

为student表中的dept字段更名为department varchar(30)

alter table student change dept department varchar(30);

7:修改表删除列

语法格式:

alter table 表名 drop 列名;

例如:

# 删除student表中department这列
alter table student drop department;

8:修改表名

语法格式:

rename table 表名 to 新表名;

例如:

# 将表student改名为stu
rename table student to stu

DML语法基本介绍

DML是指数据操作语言,英文全称是Data Manipulation Language,用来对数据库中标的数据几列进行更新。

关键字:

  • 插入Insert

  • 删除delete

  • 更新update

数据插入

  • 语法格式

insert into 表(列名1,列表2,列表3...) values(值1,值2,值3...);// 向表中插入某些
insert into 表 values(值1,值2,值3...);// 向表中插入所有列

例子:

向表中插入所有列

insert into student(sid, name, gender, age, birthday, address,department)
values (1,'tom','男',18,'2000-01-01','郑州','销售部');
​insert into student values (2,'jerry','女',28,'2001-01-01','北京','研发部');

一次性插入多条数据

insert into student(sid, name, gender, age, birthday, address,department)
values (3,'tom1','男',18,'2000-01-01','郑州','销售部'),

           (4,'corky1','女',18,'2001-01-01','北京','法务部');
     
insert into student values (5,'jerry','女',28,'2001-01-01','北京','研发部'),

                                         (6,'yi','男',39,'2002-01-01','上海','研发部');

数据修改

  • 语法格式

update 表名 set 字段名=值,字段名=值...;
update 表名 set 字段名=值,字段名=值... where 条件;

例子:

将所有学生的地址修改为河南
update student  set address='河南';

将id为1的学生的地址修改为河南
update student set address='河南' where id=1;

将id为2的学生的地址修改为北京,成绩修成绩修改为100
update student set address='北京',score=100 where id=2

数据删除

  • 语法格式:

delete from 表名[where 条件];
truncate table 表名 或者truncate 表名

例子:

1:删除sid为3的学生数据
delete from student where sid=3;
2: 删除表所有数据
delete from student;
3:清空表数据
truncate table student;
truncate student;

注意:delete和truncate原理不同,delete只删除内容,而truncate类似于drop table,可以理解为是将整个表删除,然后再创建该表。

实例代码

 

DQL语法概述

  • 概念

    1. 数据库管理系统一个重要功能就是数据查询,数据查询不应只是简单返回数据库中存储的数据,还应该根据需要对数据库进行筛选以及确定数据以什么样的格式显示。

    2. MySQL提供了功能强大,灵活的语句来实现这些操作。

    3. MySQL数据库使用select语句来查询数据。

  • 应用 

基本查询

  • 语法格式

select
[all|distinct]
<目标列的表达式1> [别名],
<目标列的表达式2> [别名]...
from <表名或视图名> [列名],<表名或视图名> [别名]...
[where<条件表达式>]
[group by<列名>
[having<条件表达式>]]
[order by<列名>[asc|desc]]
[limit<数字或者列表>];

  • 简化版语法

select *| 列名 from 表 where 条件

  • 数据准备

创建数据库和表

-- 创建数据库
create database if not exist mydb2;
use mydb2;
-- 创建商品表:
create table product(
pid int primary key auto_increment, -- 商品编号
pname varchar(20) not null , -- 商品名字
price double,  -- 商品价格
category_id varchar(20) -- 商品所属分类
);

  • 添加数据

insert into product values(null,'海尔洗衣机',5000,'c001');
insert into product values(null,'美的冰箱',3000,'c001');
insert into product values(null,'格力空调',5000,'c001');
insert into product values(null,'九阳电饭煲',200,'c001');
insert into product values(null,'啄木鸟衬衣',300,'c002');
insert into product values(null,'恒源祥西裤',800,'c002');
insert into product values(null,'花花公子夹克',440,'c002');
insert into product values(null,'劲霸休闲裤',266,'c002');
insert into product values(null,'海澜之家卫衣',180,'c002');
insert into product values(null,'杰克琼斯运动裤',430,'c002');
insert into product values(null,'兰蔻面霜',300,'c003');
insert into product values(null,'雅诗兰黛精华水',200,'c003');
insert into product values(null,'香奈儿香水',350,'c003');
insert into product values(null,'SK-II神仙水',350,'c003');
insert into product values(null,'资生堂粉底液',180,'c003');
insert into product values(null,'老北京方便面',56,'c004');
insert into product values(null,'良品铺子海带丝',17,'c004');
insert into product values(null,'三只松鼠坚果',88,null);

数据准备

1:创建数据库

create database if not exists mydb2;
use mydb2;

2:创建商品表

create table if not exists product(
   pid int primary key auto_increment,-- 商品编号
   pname varchar(20) ,-- 商品名称
   price double, -- 商品价格
   category_id int -- 商品所属分类
);

​alter table product modify category_id varchar(20);

3:添加数据

insert into product values(null,'海尔洗衣机',5000,'c001');
insert into product values(null,'美的冰箱',3000,'c001');
insert into product values(null,'格力空调',5000,'c001');
insert into product values(null,'九阳电饭煲',200,'c001');
insert into product values(null,'啄木鸟衬衣',300,'c002');
insert into product values(null,'恒源祥西裤',800,'c002');
insert into product values(null,'花花公子夹克',440,'c002');
insert into product values(null,'劲霸休闲裤',266,'c002');
insert into product values(null,'海澜之家卫衣',180,'c002');
insert into product values(null,'杰克琼斯运动裤',430,'c002');
insert into product values(null,'兰蔻面霜',300,'c003');
insert into product values(null,'雅诗兰黛精华水',200,'c003');
insert into product values(null,'香奈儿香水',350,'c003');
insert into product values(null,'SK-II神仙水',350,'c003');
insert into product values(null,'资生堂粉底液',180,'c003');
insert into product values(null,'老北京方便面',56,'c004');
insert into product values(null,'良品铺子海带丝',17,'c004');
insert into product values(null,'三只松鼠坚果',88,null);

  • 简单查询操作

查询所有的商品

select * from product;
select pid,pname,price,category_id from product;

查询商品名和商品价格

select  price,pname from product;

别名查询,使用的关键字是as,(as可以省略的)

表别名
   select * from product as p;
   select * from product p;
   select p.id,u.id from product p,user u;

列表名
   select pname as 商品名,price as 商品价格 from product;

去掉重复值

select distinct price from product;

每一行和每一行都不一样

select distinct * from product;

查询结果是表达式(运算查询):将所有商品加价10元进行展示

select pname,price+10 as 新价格,price from product;

运算符

  • 算数运算符

  • 比较运算符

  • 逻辑运算符

  • 位运算符

位运算符是在二进制数上进行计算的运算符。位运算会先将操作数变成二进制数,进行位运算。然后再将计算结果从二进制数变回十进制数。

①:算数运算符

算数运算符

select 6+2;
select 6-2;
select 6*2;
select 6/2;
select 6 div 2;
select 6 mod 2;

将所有商品价格+10元
select price+10 as 价格 from product;

将所有商品的价格上调10%
select price*(1+0.1) as 价格 from product;

条件查询

算数运算符

 

代码实现:

查询商品名称为“海尔洗衣机”的商品所有信息
select * from product where pname='海尔洗衣机';

查询价格为800商品
select * from product where price=800;

查询价格不是800商品
select * from product where price <>800;
select * from product where price!=800;
select * from product where not (price=800);

查询商品价格大于等于60元的所有商品信息
select * from product where price >=60;

查询商品价格在200到1000之间所有商品
select * from product where price between 200 and 1000;
select * from product where price >= 200 and price<= 1000;
select * from product where price >= 200 && price<= 1000;

查询商品价格在200或800的所有商品
select * from product where price=200 or price=800;
select * from product where price=200 || price=800;
select * from product where price in(200,800);

查询含有'裤'字的所有商品
select * from product where pname like '%裤%'; -- %任意字符

查询以‘海’开头的所有商品
select * from product where pname like '海%';

查询第二个字为'蔻'的所有商品
select * from product where pname like '_蔻%';

查询category_id为null的商品
select * from product where category_id is null;

查询category_id不为null的商品
select * from product where category_id is not null;

使用Least求取最小值
select LEAST(10,20,30) as small_number;
select least(10,null,30); -- 如果求最小值有一个null值,不会比较直接null

使用greatest求最大值
select greatest(10,20,30) as big_number ;
select greatest(10,null,30) as big_number ;
如果求最大值有一个null值,不会比较直接null

排序查询

聚合查询

 

 

 

聚合函数--null值得处理

分组查询

 

分页查询

Insert into Select

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Linux下的Shell基础——流程控制(三)

前言&#xff1a; 每门编程都有它独特的语法&#xff0c;比如C语言&#xff0c;Java等编程语言&#xff0c;有相同的地方也有自己独特的地方&#xff0c;但都离不开变量、运算符&#xff0c;条件判断、循环和函数这几个地方的学习&#xff0c;下面就让我们学习一下shell编程里…

CRM软件的功能与报价如何制定?

CRM软件很贵吗&#xff1f;CRM价格与系统功能、部署方式、用户数量、附加服务等有关。当然&#xff0c;不同的CRM厂商&#xff0c;也会有不同的定价模式。下面我们就来说说决定CRM系统报价的几个因素。 系统功能&#xff1a; CRM软件的功能越全面、越强大&#xff0c;其定价也…

携手共进:OpenAI与ScaleAI开展合作,为企业增强GPT模型微调功能

8 月 26 日消息&#xff0c;OpenAI 近日发布新闻&#xff0c;除了与Scale AI 展开深度合作外&#xff0c;OpenAI 还宣布他们计划扩展GPT系列的大语言模型。通过与Scale AI 的合作&#xff0c;OpenAI 能够在企业环境中定制GPT-3.5 Turbo和即将发布的GPT-4&#xff0c;以满足不同…

Typora上使用Mermaid语法展示流程图、时序图、甘特图

你已经安装Typora并打开了一个新文档后,可以按照以下详细步骤在Typora上使用Mermaid语法展示流程图、时序图、甘特图 流程图 使用graph LR声明开始,并使用箭头和连接符号定义节点之间的关系。例如,A --> B表示从节点A指向节点B的箭头连接。graph TB A[界面布局图] -->…

npm 卸载 vuecli后还是存在

运行了npm uninstall vue-cli -g&#xff0c;之后是up to date in&#xff0c;然后vue -V&#xff0c;版本号一直都在&#xff0c;说明没有卸载掉 1、执行全局卸载命令 npm uninstall vue-cli -g 2、删除vue原始文件 查看文件位置&#xff0c;找到文件删掉 where vue 3、再…

魏副业而战:闲鱼卖货做什么类目好?

我是魏哥&#xff0c;与其躺平&#xff0c;不如魏副业而战&#xff01; 做闲鱼&#xff0c;有人做高客单价的&#xff0c;也有人做低客单价的。 之前魏哥有做宠物产品&#xff0c;利润低&#xff0c;每单赚几元&#xff0c;做的很累&#xff0c;但做过一段时间后发现有很多复…

左耳朵耗子:TCP 的那些事儿(上)

原文地址&#xff1a;https://coolshell.cn/articles/11564.html TCP是一个巨复杂的协议&#xff0c;因为他要解决很多问题&#xff0c;而这些问题又带出了很多子问题和阴暗面。所以学习TCP本身是个比较痛苦的过程&#xff0c;但对于学习的过程却能让人有很多收获。关于TCP这个…

nrm管理源仓库及发布私人npm包

使用nrm管理源及切换源仓库 1.安装nrm源管理器 npm install nrm -g2.查看目前现有的源仓库 通过 nrm ls 查看现有的源 nrm ls 输出&#xff1a;这是目前现有的源 3.切换不同的源 可以通过 nrm use xxx&#xff08;源仓库名&#xff09;来切换不同的源地址 nrm use taobao…

激活函数总结(二十):激活函数补充(SQNL、PLU)

激活函数总结&#xff08;二十&#xff09;&#xff1a;激活函数补充 1 引言2 激活函数2.1 Square nonlinearity (SQNL)激活函数2.2 Piecewise Linear Unit (PLU)激活函数 3. 总结 1 引言 在前面的文章中已经介绍了介绍了一系列激活函数 (Sigmoid、Tanh、ReLU、Leaky ReLU、PR…

【Android】相对布局(RelativeLayout)最全解析

【Android】相对布局&#xff08;RelativeLayout&#xff09;最全解析 一、相对布局&#xff08;RelativeLayout&#xff09;概述二、根据父容器定位三、根据兄弟控件定位 一、相对布局&#xff08;RelativeLayout&#xff09;概述 相对布局&#xff08;RelativeLayout&#x…

Xtrfy M42W鼠标说明书

下载:https://wwtf.lanzoul.com/ikXQh16gnmpe 密码:8h5t

OpenCV实战(基础知识三)

简介 OpenCV是一个流行的开源计算机视觉库&#xff0c;由英特尔公司发起发展。它提供了超过2500个优化算法和许多工具包&#xff0c;可用于灰度、彩色、深度、基于特征和运动跟踪等的图像处理和计算机视觉应用。OpenCV主要使用C语言编写&#xff0c;同时也支持Python、Java、C…

RocketMQ零拷贝原理

1 PageCache ●由内存中的物理page组成&#xff0c;其内容对应磁盘上的block。 ●page cache的大小是动态变化的。 ●backing store&#xff1a;cache缓存的存储设备。 ●一个page通常包含多个block,而block不一定是连续的。 1.1读Cache ●当内核发起一个读请求时&#x…

Meta发布AI编码工具,或可实现智能生成代码和调试代码

据悉&#xff0c;日前Meta推出名为 Code Llama 的AI编码工具&#xff0c;号称“最先进的大型编码语言模型”。 该模型基于Llama 2大型语言模型打造&#xff0c;可以理解为“Llama 2 的写代码专用版”&#xff0c;能够“生成新的代码并调试人类编写的工作”&#xff0c;目前已上…

Mycat事务补偿设计

1.概述 在使用数据分片场景下&#xff0c;单库下的事务处理无法满足系统的需求&#xff0c;因而需要进行分布式事务处理设计。 2.主要方案对比分析 处理分布式场景下的事务有很多种方案&#xff0c;主要方案如下表所示: 主要技术 优点 缺点 适用场景 XA with 2PC (JTA)、…

左耳朵耗子:TCP 的那些事儿(下)

原文地址&#xff1a;https://coolshell.cn/articles/11609.html 这篇文章是下篇&#xff0c;所以如果你对TCP不熟悉的话&#xff0c;还请你先看看上篇《TCP的那些事儿&#xff08;上&#xff09;》 上篇中&#xff0c;我们介绍了TCP的协议头、状态机、数据重传中的东西。但是T…

Spark 7:Spark SQL 函数定义

SparkSQL 定义UDF函数 方式1语法&#xff1a; udf对象 sparksession.udf.register(参数1&#xff0c;参数2&#xff0c;参数3&#xff09; 参数1&#xff1a;UDF名称&#xff0c;可用于SQL风格 参数2&#xff1a;被注册成UDF的方法名 参数3&#xff1a;声明UDF的返回值类型 ud…

部分调试记录

Ubuntu16.04纯命令行安装VMwareTools hudahuahudahua-virtual-machine:~$ sudo apt-get install open-vm-tools -yhudahuahudahua-virtual-machine:~$ sudo apt-get install open-vm-tools-desktop无法加载so文件&#xff0c;版本问题 [rootdragonboard /]# ./Qserial -qws .…

工厂生产作业流程合规检测

工厂生产作业流程合规检测系统通过yolov7网络模型算法&#xff0c;工厂生产作业流程合规检测对作业人员的操作行为进行全面监测&#xff0c;通过图像识别算法和数据分析&#xff0c;对人员的操作动作、工具使用、安全防护等方面进行检测和评估&#xff0c;能够实时监测工人的操…

10行Python代码能做出哪些酷炫的事情?

Python凭借其简洁的代码&#xff0c;赢得了许多开发者的喜爱。因此也就促使了更多开发者用Python开发新的模块&#xff0c;从而形成良性循环&#xff0c;Python可以凭借更加简短的代码实现许多有趣的操作。下面我们来看看&#xff0c;我们用不超过10行代码能实现些什么有趣的功…