02.mysql的CURD1

结构化查询语句SQL

  • SQL是结构化查询语言(Structure Query Language),它是关系型数据库的通用语言。
  • SQL主要可以划分为以下 3 个类别:
    • DDL(Data Definition Languages)语句
      数据定义语言,这些语句定义了不同的数据库、表、列、索引等数据库对象的定义。常用的语句关键字主要包括 create、drop、alter等。
    • DML(Data Manipulation Language)语句
      数据操纵语句,用于添加、删除、更新和查询数据库记录,并检查数据完整性,常用的语句关键字主要包括 insert、delete、update 和select 等。
    • DCL(Data Control Language)语句
      数据控制语句,用于控制不同的许可和访问级别的语句。这些语句定义了数据库、表、字段、用户的访问权限和安全级别。主要的语句关键字包括 grant、revoke 等。

数据库操作

查看数据库

显示所有数据库
show databases;
显示创建数据库的语句信息
show create database mydb2;

创建数据库

  1. 创建一个名称为mydb1的数据库。
    create database mydb1;
  2. 创建一个使用utf-8字符集的mydb2数据库。
    create database mydb2 character set utf8;
  3. 创建一个使用utf-8字符集,并带校对规则的mydb3数据库。会对存入的数据进行检查。
    create database mydb3 character set utf8 collate utf8_general_ci;

删除数据库

drop database Demo;

选择数据库

use Demo;

表的CRUD

在mysql中对表操作前,必须先选择所使用的数据库。

use mydb;

mysql表名称区分大小写, 对列名不区分大小写

查看表

show tables;

创建表

create table user(id int unsigned primary key not null auto_increment,
name varchar(50) not null,
age tinyint not null,
sex enum('M','W') not null)engine=INNODB default charset=utf8;

显示指定表的结构

desc user;

查看建表sql

在Mysql中显示多行数据应该在查询语句结尾处添加 \G来替换结束标记“;”

show create table user\G

删除表

drop table user;

CRUD

insert增加

insert into user(name, age, sex) values('zhang san', 22,'M');
insert into user(name, age, sex) values'li si', 21, 'W'),
( 'gao yang', 20, 'M');
  • 注意,第二种效率高,推荐使用,可以调高性能
    1.client和server要进行TCP的三次握手
    2.client发送sql到server上,接受并处理,然后返回处理结果
    3.server和client断开连接,TCP四次挥手

update修改

update user set age=23 where name='zhang san';
update user set age=age+1 where id=3;

delete删除

delete from user where age=20;
delete from user where age between 21 and 22;
delete from user;

select查询

select * from user;
select id,name,age,sex from user;
select id,name from user;
select id,name,age,sex from user where sex='M' and age>=20 and age<=25;
select id,name,age,sex from user where sex='M' and age between 20 and 25;
select id,name,age,sex from user where sex='W' or age>=22;

去重distinct

select distinct name from user;

空值查询

select * from user where name is null;

union合并查询

SELECT expression1, expression2, … expression_n
FROM tables[WHERE conditions]
UNION [ALL | DISTINCT] # 注意:union默认去重,不用修饰distinct,all表示显示所有重复值
SELECT expression1, expression2, … expression_n
FROM tables[WHERE conditions];

select id,name,age,sex from user where sex='M' and age>=20 and age<=25 union all select id,name,age,sex from user where sex='M' and age between 20 and 25;

综合案例

创建一个学生表

create table student(
id int,
name varchar(20),
chinese int,
english int,
math int
);

常见的SQL操作

  • 查询表中所有学生的信息。
select * from student;
  • 查询表中所有学生的姓名和对应的英语成绩。
select name,english from student;
  • 过滤表中重复数据。
select english from student;
select DISTINCT english,name from student;
select english+chinese+math as 总分 from student;
  • 在所有学生英语分数上加10分特长分。
select name,english+10 from student;
  • 统计每个学生的总分。
select english+chinese+math from student;
  • 使用别名表示学生分数
select name,english+chinese+math as 总分 from student;
select name,english+chinese+math 总分 from student;
  • 查询姓名为demo的学生成绩
select * from student where name='demo';
  • 查询英语成绩大于90分的同学
select * from student where english>90;
  • 查询总分大于250分的所有同学
select * from student where english+chinese+math>250;
  • 查询英语分数在 85-95之间的同学。
select * from student where english>=85 and english<=95;
select * from student where english between 85 and 95;
  • 查询数学分数为84,90,91的同学。
select * from student where math=84 or math=90 or math=91;
select * from student where math in(84,90,91);
  • 查询所有姓何的学生成绩。
select * from student where name like '何%';
  • 查询数学分>85,语文分>90的同学。
select * from student where math>85 and chinese>90;
  • 对数学成绩排序后输出。
select * from student order by math;
  • 对总分排序后输出,然后再按从高到低的顺序输出
select * from student order by math+chinese+english desc;
  • 对姓何的学生成绩排序输出
select * from student where name like '李%' order by math+chinese+english desc;
select name, math+chinese+english from student where name like '何%' order by math+chinese+english desc;
  • 统计一个班级共有多少学生?
select count(*) from student;
  • 统计数学成绩大于90的学生有多少个?
select count(*) from student where math>90;
  • 统计总分大于250的人数有多少?
select count(*) from student where math+chinese+english>250;
  • 统计一个班级数学总成绩?
select sum(math) from student;
  • 统计一个班级语文、英语、数学各科的总成绩
select sum(math), sum(chinese), sum(english) from student;
  • 统计一个班级语文、英语、数学的成绩总和
select sum(math+chinese+english)from student;
select sum(math)+sum(chinese)+sum(english) from student;
  • 求一个班级数学平均分?
select avg(math) from student;
  • 求一个班级总分平均分
select avg(math+chinese+english)from student;
select avg(math)+avg(chinese)+avg(english) from student;
  • 求班级最高分和最低分
select max(math+chinese+english),min(math+chinese+english) from student;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值