MySQL数据库增删改查基础操作(超长详解)

news/2024/10/19 7:56:48/

目录

1库的操作

显示数据库

创建一个库

使用数据库

删除数据库的名

2表操作:

显示表

创建表

查看表

删除表名

新增

查出表的所有行和列;

实例:

别名:

去重:

排序:

限制查找的条数:(limit)

not null(指定某列不能储存null值)

unique (保证某列的每行必须有唯一的值,不能重复的)

default:默认值约束,指定插入数据,如果为空,指定默认值。

primary key :主键约束

聚合查询:

联合查询:

还有一种方法是 join  on

外连接分为左外连接和右外连接:

自连接:

子查询:

合并查询:

数据库的基本操作

1库的操作

显示数据库

show databases;

创建一个库

create database 数据库名 charset 字符集;

使用数据库

use 数据库名;

删除数据库的名

drop database 数据库名;

2表操作:

显示表

show tables;

创建表

create table 表名 (列名 类型,列名 类型);

查看表

desc 表名;

删除表名

drop table 表名;

新增

insert into 表名 values (值,值);
insert into 表名(列名,列名) values (值,值);

查出表的所有行和列;

select *from 表名;

实例:

创建一个学生表:里面有id,姓名,语文,数学,英语,成绩。

create table exam_result( id int, name varchar(20), chinese decimal(3,1), math decimal(3,1), english decimal(3,1) );

然后插入测试数据:

insert into  exam_result (id,name, chinese, math, english) VALUES (1,'唐三藏', 67, 98, 56), (2,'孙悟空', 87.5, 78, 77), (3,'猪悟能', 88, 98.5, 90), (4,'曹孟德', 82, 84, 67), (5,'刘玄德', 55.5, 85, 45), (6,'孙权', 70, 73, 78.5), (7,'宋公明', 75, 65, 30);

别名:

select id ,name chinese+math +english as total from 表;

去重:

使用distinct 关键字

比如去重数学成绩

select distinct math from 表名;

排序:

order  by(升序)

select id ,math ,chinese from 表名 order by chinese;

升序,如果降序则需在后面加上desc;

基本查询:

比如查询英语不及格的同学:

select id ,english from 表名 where english<60;

如果查完进行排序:

select id ,english from 表名 where english<60 order by english;

查找总成绩小于200的学生:

select name,chinese + math +english from exam_result where chinese +english +math <200;

查找语文大于80 和英语大于80的学生;

select name,chinese,english from exam_result where chinese>80 and english>80;

and的优先级大于or。

查找数学成绩在(58,59,98,99)的学生(in):

select name,math from exam_result where math in(58,59,98,99);

查找名字中带有孙的name(%):

select name from exam_result where name like '孙%';(孙在前)
select name from exam_result where name like '%孙%';(含孙的)

显示一个(_):

select name from exam_result where name like '孙_';

查找名字是否为空的:

 select * from exam_result where name <=> null;

限制查找的条数:(limit)

 select * from exam_result limit 3;

3之后再查找3个:

select * from exam_result limit 3 offset 3;(从最后的3开始查找);

修改:让孙悟空的成绩数学成绩加10;

update exam_result set math=math +10 where name ='孙悟空';

not null(指定某列不能储存null值)

create table student8(id int not null);

unique (保证某列的每行必须有唯一的值,不能重复的)

create table student9(id int not null,name varchar(20) unique);

default:默认值约束,指定插入数据,如果为空,指定默认值。

create table student10(id int default '1',name varchar(20) unique);

primary key :主键约束

指定id为主键

 create table student11(id int not null primary key,name varchar(20) unique);

对于整数类型的主键,常配搭自增长auto_increment来使用。插入数据对应字段不给值时,使用最大 值+1。

foreign key (外键约束)

创建班级表

create table  classes ( id int primary key auto_increment, name varchar(20), `desc` varchar(100) );

创建学生表

create table student (   id INT PRIMARY KEY auto_increment,   sn ); INT UNIQUE,   name VARCHAR(20) DEFAULT 'unkown',   qq_mail VARCHAR(20), classes_id int, FOREIGN KEY (classes_id) REFERENCES classes(id)

插入和查询结合(可以把一个表查询出的结果,插入到另一个表中):

insert into school1 select *from school;

聚合查询:

 1,select count(*) from fu;(查询行数)count遇到null会跳过2,select sum(salary) from fu;(求和)3,select avg(salary) from fu;(求平均值)4,select role ,count(id) from fu group by role;(分组进行查查询)

实例(比如分组查平均值):

select role,avg(salary) from fu group by role;

先插入一段数据:

insert into student(sn, name, qq_mail, classes_id) values->  ('09982','黑旋风李逵','xuanfeng@qq.com',1),->  ('00835','菩提老祖',null,1),->  ('00391','白素贞',null,1),->  ('00031','许仙','xuxian@qq.com',1),->  ('00054','不想毕业',null,1),->  ('51234','好好说话','say@qq.com',2),->  ('83223','tellme',null,2),->  ('09527','老外学中文','foreigner@qq.com',2);
insert into course(name) values->  ('Java'),('中国传统文化'),('计算机原理'),('语文'),('高阶数学'),('英文');
insert into score(score, student_id, course_id) values-> (70.5, 1, 1),(98.5, 1, 3),(33, 1, 5),(98, 1, 6),-> (60, 2, 1),(59.5, 2, 5),-> (33, 3, 1),(68, 3, 3),(99, 3, 5),-> (67, 4, 1),(23, 4, 3),(56, 4, 5),(72, 4, 6),-> (81, 5, 1),(37, 5, 5),-> (56, 6, 2),(43, 6, 4),(79, 6, 6),->  (80, 7, 2),(92, 7, 6);
insert into classes(name,doing) values-> ('计算机系2019级1班', '学习了计算机原理、C和Java语言、数据结构和算法'),->  ('中文系2019级3班','学习了中国传统文学'),->  ('自动化2019级5班','学习了机械自动化');

例子:查找许仙的成绩

联合查询:

1,先分析用到哪个表

select *from student,score;

2,再寻找对应条件,减少重复数据

select *from student,score where student_id =score.student_id;

3,再减少重复数据,当学生的名字为许仙时

 select *from student,score where student.classes_id =score.student_id and student.name ='许仙';

4,再减少范围,只查找范围内的

select student.name ,score.score from student,score where student.classes_id =score.student_id and student.name='许仙';

还有一种方法是 join  on

(join前后加的是两张表on后面加的是条件)

 select student.name,score.score from student join score on student.classes_id=sc
ore.student_id and student.name='许仙';

外连接分为左外连接和右外连接:

(先创建两个表)

左:

select *from student left join score on student.id =score.id;

右:

select *from student right join score on student.id =score.id;

如果修改一个表的数据,再进行查找会变成null

update score set score.id = 4 where score.id =3;

当进行右外连接的时候,id为3 的一行会变成null

自连接:

一个表的数据自己进行连接

select *from score as s1,score as s2;select *from score as s1,score as s2 where s1.id=s2.id;

子查询:

指嵌入在其他sql语句中的select语句,也叫嵌套查询

单行子查询:返回一行记录的子查询

select *from score where score.id != (select *from score where score.score);

多行子查询:返回多行记录的子查询

in关键字(查询多行数据用的in)

合并查询:

union(可以多个表进行查询)

 selct *from course where id<3 union select *from course where name='英文';


http://www.ppmy.cn/news/1540213.html

相关文章

第一百零七周周报

学习时间&#xff1a; 2024.10.12-2024.10.18 学习产出&#xff1a; 这周大部分时间都在黄山开会&#xff0c;目前cifar10还没调好&#xff0c;celebA128的fid到了13点多&#xff0c;还没有跑完&#xff0c;其他时间都在找工作。

基于Flink+Hologres搭建实时数仓

Apache Paimon是一种流批统一的数据湖存储格式&#xff0c;结合Flink及Spark构建流批处理的实时湖仓一体架构。Paimon创新地将湖格式与LSM技术结合起来&#xff0c;给数据湖带来了实时流更新以及完整的流处理能力。借助实时计算Flink版与Apache Paimon&#xff0c;可以快速地在…

矩阵相关算法

矩阵旋转90度 给定一个 n n 的二维矩阵 matrix 表示一个图像&#xff0c;请你将图像顺时针旋转 90 度。 #include <iostream> #include <vector>using namespace std;void rotate(vector<vector<int>>& matrix) {int n matrix.size();// 第一步…

侏罗纪公园不再是电影了吗?

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

每日回顾:简单用C写 选择排序、堆排序

选择排序 直接选择排序&#xff08;Selection Sort&#xff09;是一种简单的排序算法。它的基本思想是每次从未排序的部分中选择最小&#xff08;或最大&#xff09;的元素&#xff0c;将其放到已排序部分的末尾。 版本一&#xff1a; //直接选择排序1 void SelectSort_1(in…

WT2003H语音芯片MCU下载方案助力电动车智能化升级:实现多功能语音提示+报警功能

一&#xff1a;产品市场 随着科技的发展&#xff0c;电瓶车在技术革新上也在不断进步&#xff0c;如今许多厂家&#xff0c;都会加入语音提示功能&#xff0c;能在倒车、喇叭、故障时发出语音报警&#xff0c;提示骑行者电量不足、倒车请注意、故障语音提示等&#xff1b;唯创…

022 elasticsearch文档管理(添加、修改、删除、批处理)

文章目录 添加文档修改文档删除文档根据_id取文档使用批处理_bulk PortX&#xff1a; https://portx.online/zh MobaXterm&#xff1a; https://mobaxterm.mobatek.net/ FinalShell&#xff1a; http://www.hostbuf.com/ 添加文档 向索引中添加一行数据 使用json来表示 使用…

CVTE Android面试题及参考答案

Activity 的生命周期 Activity 的生命周期分为以下几个主要状态: onCreate ():在 Activity 第一次被创建的时候调用。通常在这个方法中进行一些初始化操作,如设置布局、初始化成员变量等。这是 Activity 进入可见状态的第一步。onStart ():当 Activity 即将对用户可见的时候…