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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
云数据库 RDS PostgreSQL,高可用系列 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

     

    相关实践学习
    每个IT人都想学的“Web应用上云经典架构”实战
    本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
    MySQL数据库入门学习
    本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
    相关文章
    |
    1月前
    |
    SQL 关系型数据库 MySQL
    MySQL的查询操作语法要点
    储存过程(Stored Procedures) 和 函数(Functions) : 储存过程和函数允许用户编写 SQL 脚本执行复杂任务.
    159 14
    |
    1月前
    |
    SQL 关系型数据库 MySQL
    MySQL的查询操作语法要点
    以上概述了MySQL 中常见且重要 的几种 SQL 查询及其相关概念 这些知识点对任何希望有效利用 MySQL 进行数据库管理工作者都至关重要
    77 15
    |
    1月前
    |
    SQL Oracle 关系型数据库
    Oracle数据库创建表空间和索引的SQL语法示例
    以上SQL语法提供了一种标准方式去组织Oracle数据库内部结构,并且通过合理使用可以显著改善查询速度及整体性能。需要注意,在实际应用过程当中应该根据具体业务需求、系统资源状况以及预期目标去合理规划并调整参数设置以达到最佳效果。
    162 8
    |
    3月前
    |
    Oracle 关系型数据库 MySQL
    比较Oracle和MySQL的语法差异。
    在使用Oracle和MySQL时,数据库设计、查询优化、以及日常管理的方式会因为这些差异而有不同的考虑和应用策略。因此,开发人员和数据库管理员必须了解各自数据库的特性和语法差异,以便更有效地利用数据库资源。适应这些语法和功能上的差异对于维护跨数据库平台应用至关重要。
    206 0
    |
    5月前
    |
    SQL 关系型数据库 MySQL
    MySQL探索:详解WITH AS语法的使用。
    总的来说,MySQL的 `WITH AS`语法就如同我们路途中的导航设备,能帮助我们更好地组织和简化查询, 增强了我们和数据沟通的能力,使得复杂问题变得可控且更有趣。不论是在森林深处,还是在数据的海洋中,都能找到自己想要的路途和方向。
    746 12
    |
    4月前
    |
    存储 Oracle 关系型数据库
    oracle数据恢复—oracle数据库执行错误truncate命令的数据恢复案例
    oracle数据库误执行truncate命令导致数据丢失是一种常见情况。通常情况下,oracle数据库误操作删除数据只需要通过备份恢复数据即可。也会碰到一些特殊情况,例如数据库备份无法使用或者还原报错等。下面和大家分享一例oracle数据库误执行truncate命令导致数据丢失的数据库数据恢复过程。
    |
    6月前
    |
    SQL 数据可视化 IDE
    开发数据库不想写命令?YashanDB Developer Center 帮你轻松搞定
    YashanDB Developer Center(YDC)是一款可视化的数据库开发工具,专为提升数据库开发效率而设计。它通过图形化对象管理让数据库对象清晰可见,提供智能SQL编辑器支持语法高亮与自动补全,实现PL调试的图形化操作,帮助快速定位问题。此外,操作记录可追溯,多端灵活部署,适配多种场景。无论是中大型企业研发团队,还是不熟悉命令行的业务开发者,YDC都能显著优化开发体验,堪称YashanDB的“可视化IDE”。
    |
    10月前
    |
    SQL 存储 关系型数据库
    【MySQL基础篇】全面学习总结SQL语法、DataGrip安装教程
    本文详细介绍了MySQL中的SQL语法,包括数据定义(DDL)、数据操作(DML)、数据查询(DQL)和数据控制(DCL)四个主要部分。内容涵盖了创建、修改和删除数据库、表以及表字段的操作,以及通过图形化工具DataGrip进行数据库管理和查询。此外,还讲解了数据的增、删、改、查操作,以及查询语句的条件、聚合函数、分组、排序和分页等知识点。
    865 55
    【MySQL基础篇】全面学习总结SQL语法、DataGrip安装教程
    |
    7月前
    |
    SQL 分布式计算 数据库
    【YashanDB 知识库】Hive 命令工具 insert 崖山数据库报错
    【YashanDB 知识库】Hive 命令工具 insert 崖山数据库报错
    |
    7月前
    |
    SQL 分布式计算 数据库
    【YashanDB知识库】Hive 命令工具insert崖山数据库报错
    【YashanDB知识库】Hive 命令工具insert崖山数据库报错

    推荐镜像

    更多