Mysql数据库基本操作语法命令归纳

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,高可用系列 2核4GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: Mysql数据库基本操作语法命令归纳

 

一、数据库相关概念:

    •    DB          Database                                     数据库
    •    DBA        Database Administrator               数据库管理员
    •    DBS        Database System                        数据库系统
    •    DBMS     Database Managerment System 数据库管理系统

       DBS=DBMS+DBA+DB+软件+硬件

       三种模型:层次模型  网状模型  关系模型

       关系型数据库:

          设计数据库的三个步骤:

           1.概念结构设计阶段:E-R 图: 实体(集)  属性  联系

                   一对一(1:1)  一对多(1:n) 多对多(m:n)

           2.逻辑结构设计阶段

           3.物理结构设计阶段

       常见的数据库:

           MySql

           SQLServer

           Oracle

           DB2

       约束:

           primary key    主键约束:不能为空且不能重复:唯一区分记录的标志(表级约束)

           foreign key    外键约束  

           not null    非空约束

           unique        唯一约束  null 'a'   null

           default        默认约束

           check        检查约束(在mysql中无效)   set enum

           auto_increment    自增  (主键)  

           主表  从表

    二、数据库操作:

    sql命令

    status;        查看当前状态
        show databases; 显示所有数据库
        use 数据库名;    使用/打开数据库

    image.gif

    create database bochy;
      create database [if not exists] bochy;(如果不存在)创建数据库
      drop database [if exists] bochy;  (如果存在)删除数据库(谨慎使用)

    image.gif

    三、数据库中表的操作:

    use bochy;    先打开数据库

    image.gif

       创建表:

    create table [if not  exists]  student(
        id int,
        name  varchar(6)  not null ,
        sex set('男','女'),
        ext text
        );

    image.gif

    create table student (
         id int primary key  auto_increment,
         name varchar(8) not null,
         age smallint default 18,
         address varchar(50) default '郑州市金水区',
         ext decimal(10,2)
         );

    image.gif

    create table score (
         id int ,
         cid int ,
         grade int,
         primary key(id,cid),
         foreign key(id) references student(id),
         foreign key(cid) references course(cid)
         );

    image.gif

    显示当前数据库的所有表:show tables; 
      查看表结构:desc student;

    image.gif

    添加数据:

    insert into student  values( 2015001,'张三','男','dfdsfbhfhw'  );

    image.gif

     添加部分列数据:

    insert into student(id ,name)  value(2015005,'小明');

    image.gif

       添加多条数据:

    insert into student values(2015006,'xiao王','男',null) ,(2015007,'ssss','男',null),(2015008,'xxx','女',null);

    image.gif

       删除表中数据:

    delete from student ;(删除表中所有数据)
        delete from student where id=2015006;(删除满足条件的数据)
        delete from student where id  is null;
        delete from student where id!=2015005;
        delete from student where id <>2015005;

    image.gif

       查看表中的数据:

     

    select * | 部分列
        from  表名
        | where (筛选记录的)条件
        | group by 字段名   having 筛选分组的条件
        | order by 字段名|聚合函数  asc|desc 
        | limit [起始下标,]长度 ;

    image.gif

    select * from Student;
        select id, name from student;
        select id as 学号, name  as  姓名  from student;
        select id  学号, name  姓名  from student;

    image.gif

     查询满足条件的前10条数据

    select id,name,sex,sum from student where sex='男'  order by sum  desc  limit 10 ;

    image.gif

    从下标为2的记录开始,往后查询5条数据

    select * from student limit 2,5;

    image.gif

      模糊查询

    select *   from student where name like '%a%';
        select *   from student where name like '%a_';

    image.gif

     比较查询

    select * from student where sex='男' and sum>300 ;
     select * from student where sex='男' or  sum>300 ;

    image.gif

    范围查询

    select * from student where id='2015001' or id='2015003' or id='2015005' or id='2015021'  ;
        select * from student where id in( 2015001,2015003,2015005,2015007,2015021,2015100,2015112);

    image.gif

    统计查询

    select count(*),sum(age),avg(age),max(age),min(age) 最小年龄 from student where age between 20 and 40;

    image.gif

       +----------+----------+----------+----------+----------+

       | count(*) | sum(age) | avg(age) | max(age) | 最小年龄 |

       +----------+----------+----------+----------+----------+

       |        9 |      222 |  24.6667 |       35 |       21 |

       +----------+----------+----------+----------+----------+

       1 row in set (0.05 sec)

     

    select major,count(*) from student where major='信息工程';

    image.gif

       +----------+----------+

       | major    | count(*) |

       +----------+----------+

       | 信息工程 |        2 |

       +----------+----------+

       1 row in set (0.00 sec)

    分组查询

    select sex ,count(*) 人数 from student group by sex;

    image.gif

       +------+------+

       | sex  | 人数 |

       +------+------+

       | 男   |    7 |

       | 女   |    5 |

       +------+------+

       2 rows in set (0.00 sec)

    select major,count(*) from student group by major;

    image.gif

       +--------------+----------+

       | major        | count(*) |

       +--------------+----------+

       | java软件开发 |        2 |

       | 平面设计     |        2 |

       | 网络工程     |        3 |

       | 信息工程     |        2 |

       | 英语         |        3 |

       +--------------+----------+

       5 rows in set (0.05 sec)

    select major,count(*) 人数 from student where age between 20 and 40 
        group by major  having 人数>=2  order by 人数 desc  limit 2;

    image.gif

       +--------------+------+

       | major        | 人数 |

       +--------------+------+

       | 网络工程     |    3 |

       | java软件开发 |    2 |

       +--------------+------+

       2 rows in set (0.00 sec)

    子查询:

    查找所有年龄大于李四年龄的学生的信息

    select * from student where age> ( select age from student where name='李四'   );

    image.gif

    查找成绩大于网络工程专业最高成绩的学生的信息

    select * from student where sum> (  select max(sum) from student where major='网络工程'  );

    image.gif

    any:任意一个

    select * from student where sum > any(   select sum from student where major='网络工程');

    image.gif

    all:所有

    select * from student where sum > all(   select sum from student where major='网络工程');

    image.gif

    some:一些

    select * from student where sum > some(   select sum from student where major='网络工程');

    image.gif

    查询既选修了1001课程又选修了1002课程的学生的信息

    select id from score where cid='1002'  and id in(    select id  from score where cid='1001');

    image.gif

    +---------+

    | id      |

    +---------+

    | 2015001 |

    | 2015021 |

    +---------+

    2 rows in set (0.00 sec)

    自查询:

    select a.eno ,a.ename 员工姓名 ,b.ename 经理名称 from employee a left join employee b on a.boss=b.eno;

    image.gif

    多表查询:

    1.全链接(笛卡尔积):

    select * from 表1,表2;

    image.gif

    2.等值连接:

    select * from 表1,表2 where 表1.字段=表2.字段;

    image.gif

    3.内连接:

    select * from 表1 [inner] join 表2  on  表1.字段=表2.字段  where

    image.gif

    4.外链接

    左外链接:在保留左表所有数据的基础上进行等值连接

    select * from 表1 left[outer] join 表2  on  表1.字段=表2.字段  where

    image.gif

    右外链接:在保留右表所有数据的基础上进行等值连接

    select * from 表1 right[outer] join 表2  on  表1.字段=表2.字段 where

    image.gif

    5.联合查询

    select * from student where major='信息工程'
    union [all | distinct] 
    select * from student where id in(2015001,2015002,2015020,2015021);

    image.gif

     

    相关实践学习
    如何快速连接云数据库RDS MySQL
    本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
    全面了解阿里云能为你做什么
    阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
    目录
    打赏
    0
    1
    0
    0
    4
    分享
    相关文章
    MySQL探索:详解WITH AS语法的使用。
    总的来说,MySQL的 `WITH AS`语法就如同我们路途中的导航设备,能帮助我们更好地组织和简化查询, 增强了我们和数据沟通的能力,使得复杂问题变得可控且更有趣。不论是在森林深处,还是在数据的海洋中,都能找到自己想要的路途和方向。
    133 12
    oracle数据恢复—oracle数据库执行错误truncate命令的数据恢复案例
    oracle数据库误执行truncate命令导致数据丢失是一种常见情况。通常情况下,oracle数据库误操作删除数据只需要通过备份恢复数据即可。也会碰到一些特殊情况,例如数据库备份无法使用或者还原报错等。下面和大家分享一例oracle数据库误执行truncate命令导致数据丢失的数据库数据恢复过程。
    开发数据库不想写命令?YashanDB Developer Center 帮你轻松搞定
    YashanDB Developer Center(YDC)是一款可视化的数据库开发工具,专为提升数据库开发效率而设计。它通过图形化对象管理让数据库对象清晰可见,提供智能SQL编辑器支持语法高亮与自动补全,实现PL调试的图形化操作,帮助快速定位问题。此外,操作记录可追溯,多端灵活部署,适配多种场景。无论是中大型企业研发团队,还是不熟悉命令行的业务开发者,YDC都能显著优化开发体验,堪称YashanDB的“可视化IDE”。
    【YashanDB 知识库】Hive 命令工具 insert 崖山数据库报错
    【YashanDB 知识库】Hive 命令工具 insert 崖山数据库报错
    【YashanDB知识库】Hive 命令工具insert崖山数据库报错
    【YashanDB知识库】Hive 命令工具insert崖山数据库报错
    微服务——MongoDB常用命令1——数据库操作
    本节介绍了 MongoDB 中数据库的选择、创建与删除操作。使用 `use 数据库名称` 可选择或创建数据库,若数据库不存在则自动创建。通过 `show dbs` 或 `show databases` 查看所有可访问的数据库,用 `db` 命令查看当前数据库。注意,集合仅在插入数据后才会真正创建。数据库命名需遵循 UTF-8 格式,避免特殊字符,长度不超过 64 字节,且部分名称如 `admin`、`local` 和 `config` 为系统保留。删除数据库可通过 `db.dropDatabase()` 实现,主要用于移除已持久化的数据库。
    146 0
    【MySQL基础篇】MySQL约束语法
    文章介绍了MySQL中表的约束概念,包括非空、唯一、主键、默认和外键约束,以及如何在创建和修改表时指定这些约束。外键约束用于保持数据的一致性和完整性,文章通过示例展示了添加、删除外键的语法,并讨论了不同的删除/更新行为,如CASCADE和SETNULL。
    【MySQL基础篇】MySQL约束语法
    【MySQL基础篇】全面学习总结SQL语法、DataGrip安装教程
    本文详细介绍了MySQL中的SQL语法,包括数据定义(DDL)、数据操作(DML)、数据查询(DQL)和数据控制(DCL)四个主要部分。内容涵盖了创建、修改和删除数据库、表以及表字段的操作,以及通过图形化工具DataGrip进行数据库管理和查询。此外,还讲解了数据的增、删、改、查操作,以及查询语句的条件、聚合函数、分组、排序和分页等知识点。
    【MySQL基础篇】全面学习总结SQL语法、DataGrip安装教程
    MySQL进阶突击系列(01)一条简单SQL搞懂MySQL架构原理 | 含实用命令参数集
    本文从MySQL的架构原理出发,详细介绍其SQL查询的全过程,涵盖客户端发起SQL查询、服务端SQL接口、解析器、优化器、存储引擎及日志数据等内容。同时提供了MySQL常用的管理命令参数集,帮助读者深入了解MySQL的技术细节和优化方法。
    MongoDB是一个NoSQL数据库,有着多种不同的命令和操作。以下是一些常见的MongoDB命令:
    一些常用的MongoDB命令,如数据库和集合的管理、数据的插入、查询、更新、删除以及聚合操作等。
    98 1

    推荐镜像

    更多
    AI助理

    你好,我是AI助理

    可以解答问题、推荐解决方案等