MySQL基础

简介: MySQL基础

MySQL基础

文章目录

1.数据库的操作

数据库安装

1.1 显示当前数据库

show databases;

数据库中的命令没有大小写之分

1.3创建数据库

create database db_name ;

示例

#创建名为db_test的数据库
create database db_test;
#创建名为db_test2的数据库,如果有则不创建
create database if not exists db_test2;
#创建一个使用utf8mb4字符集的 db_test 数据库
create database if not exists db_test character set utf8mb4; 

1.4使用数据库

use db_name;

1.5删除数据库

drop database [if exists] db_name;  

2.表的操作

需要使用数据库中的表时,首先得先使用该数据库

use db_test;  

2.1查看表结构

desc 表;

9364c0cf0a2fc6fa245cced86e235b18.png

2.2 创建表

create  table table_name(
  field1 datatype,
  field2 datatype,
  field3 datatype,
);

示例

创建一个学生个人信息表

可使用comment增加字段说明

create table student(
  id int,
    name varchar(20) comment '姓名',
    age int,
    gender varchar(2) comment '性别',
    birthday timestamp,
);

206556152f10199d78cc8f472f1bea2b.png

2.3 删除表

示例

-- 删除student表
drop table student;
# 如果存在student表,则删除student表
drop table if exists student;

3.新增 Create

案例

-- 创建一张学生表
drop table if extists student;
create table student(
  id int,
    sn int comment '学号',
    name varchar(20) comment '姓名',
    mail varchar(20) comment '邮箱'
);

3.1 单行数据+全列插入

-- 插入两年数据,value_list数量和定义表的列的数量及顺序一致
insert into studene values(1,101,'张三',NULL);
insert into student values(2,102,'李四','123456');

3.2多行数据+指定列插入

-- 插入两条数据,value_list数量和定义表的列的数量及顺序一致
insert into student(id,sn,name)values
  (3,103,'王五'),
  (4,104,'孙悟空');

4.查询 Select

4.1全列查询

select id,sn,name from student;

通常情况下不建议使用*进行全列查询

  1. 查询的越多,对网络、磁盘的开销非常大
  2. 对服务器资源的使用也非常大

4.2指定列查询

select id,sn,name from student;

4.3查询字段为表达式

-- 表达式不含字段
select id,name,10 from exam_result;
-- 表达式含一个字段
select id,name,english+10 from exam_result;
-- 表达式中含多个字段
select id,name,chinese+math+english from exam_result;

4.4别名

select id, name, chinese+math+english 总分 from exam_result;
select id, name, chinese+math+english as 总分 from exam_result;

4.5 去重

select distinct math from exam_result;

4.6 排序

-- 降序
select name,qq_mail from student order by desc;
-- 升序
select name,qq_mail from student order by asc;

注意:

NULL数据排序,视为比任何值都小,升序时出现在最上面,降序时出现在最下面

-- 使用表达式排序
select name,chinese+math+english from exam_result order by chinese+math+english order by desc;
-- 使用别名
select name,chinese+math+english as totle from exam_result order by totle order by desc;

MySQL中的NULL

  1. 不论什么值和他运算,返回值都是NULL
  2. NULL 始终会判定为FALSE - 0

4.7条件查询 WHERE

比较运算符

image.png

逻辑运算符

运算符 说明
and 类似于编程中的&&多个条件同时为true,结果才为true
or `
not !条件为true,结果为false

  1. where条件可以使用表达式,但不能使用别名
  2. and的优先级高于or

4.8分页查询 LIMIT

-- 起始下标为0
-- 从0开始,查询n条结果
select ... from table_name [where..] [order by..] limit n;
-- 从s开始,查询n条结果
select ... from table_name [where..] [order by..] limit s,n;
-- 从s开始,查询n条结果
select ... from table_name [where..] [order by..] limit n offser s;

5.修改 Update

-- 将孙悟空同学的数学成绩变更为80分
update exam_result set math=80 where name='孙悟空';
-- 将曹孟德同学的数学成绩更改为90分,语文更改为93分
update exam_result set math=90,chinese=93 where name='曹孟德';
-- 将总成绩倒数前三的3位同学的数学成绩加上30分
update exam_result set math=math+30 order by chinese+math+english limit 3;
-- 将所有同学的语文成绩更新为原来的2倍
update exam_result set chinese=chinese*2;

6.删除 Delete

delete from 表 where 条件

chinese=93 where name=‘曹孟德’;

– 将总成绩倒数前三的3位同学的数学成绩加上30分

update exam_result set math=math+30 order by chinese+math+english limit 3;

– 将所有同学的语文成绩更新为原来的2倍

update exam_result set chinese=chinese*2;

## 6.删除 Delete
```sql
delete from 表 where 条件




相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
3天前
|
数据采集 人工智能 安全
|
12天前
|
云安全 监控 安全
|
4天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
1056 151
|
4天前
|
编解码 人工智能 机器人
通义万相2.6,模型使用指南
智能分镜 | 多镜头叙事 | 支持15秒视频生成 | 高品质声音生成 | 多人稳定对话
|
17天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1738 9
|
9天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
684 152
|
11天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
652 12
|
6天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
412 4