zoukankan      html  css  js  c++  java
  • Mysql 使用命令及 sql 语句示例

    Mysql 是数据库开发使用的主要平台之一。sql 的学习掌握与使用是数据库开发的基础,此处展示详细sql 语句的写法,及各种功能下的 sql 语句。

        在此处有 sql 语句使用示例:在这里 

    此处插入两张图更有说服力:

    sql1.PNG

    sql2.PNG

    说明:

        第一张图片是进入该环境,输入自己设定的密码即可进入数据库并进行相关操作;

        第二张图片是演示显示所有数据库,设置当前数据库,并对当前数据库操作,显示当前数据库的所有表,查询表中部分记录的命令操作。

        

        基本命令使用是这样的。当然sql 语句也可在其他平台使用。此处不多说明;

        SQL语句详细如下:

    一、数据库操作

    1.  
      <span style="font-size:14px;">创建一个名称为mydb1的数据库
    2.  
      create database mydb1;
    3.  
      show databases;
    4.  
       
    5.  
      创建一个使用utf-8字符集的mydb2数据库。
    6.  
      create database mydb2 character set utf8;
    7.  
       
    8.  
      创建一个使用utf-8字符集,并带校对规则的mydb3数据库。
    9.  
      create database mydb3 character set utf8 collate utf8_general_ci;
    10.  
       
    11.  
      查看前面创建的mydb2数据库的定义信息
    12.  
      show create database mydb2;
    13.  
       
    14.  
      删除前面创建的mydb1数据库
    15.  
      drop database mydb1;
    16.  
       
    17.  
      查看服务器中的数据库,并把其中某一个库的字符集修改为gb2312;
    18.  
      alter database mydb2 character set gb2312;
    19.  
      show create database mydb2;
    20.  
       
    21.  
      使用当前数据库 mydb1,即想对当前数据库进行操作之前使用的命令
    22.  
      use mydb1;</span>

    二、表的操作

        1>表的创建演示

    1.  
      <span style="font-size:14px;">创建一个员工表
    2.  
      use mydb2;
    3.  
      create table employee
    4.  
      (
    5.  
      id int,
    6.  
      name varchar(40),
    7.  
      sex varchar(4),
    8.  
      birthday date,
    9.  
      entry_date date,
    10.  
      job varchar(40),
    11.  
      salary decimal(8,2),
    12.  
      resume text
    13.  
      );
    14.  
       
    15.  
      show tables; 查看库的所有表(查看库里的表要先打开库)
    16.  
      show create table employee; 查看表的创建细节
    17.  
      desc employee; 看表结构</span>


        2>对表的基本操作:增、删、改、查

    1.  
      <span style="font-size:14px;">在上面员工表的基本上增加一个image列。
    2.  
      alter table employee add image varchar(20);
    3.  
       
    4.  
      修改job列,使其长度为60。
    5.  
      alter table employee modify job varchar(60);
    6.  
       
    7.  
      删除sex列
    8.  
      alter table employee drop sex;
    9.  
       
    10.  
      表名改为user。
    11.  
      rename table employee to user;
    12.  
       
    13.  
      修改表的字符集为utf-8
    14.  
      alter table user character set utf8;
    15.  
       
    16.  
      列名name修改为username
    17.  
      alter table test change column address address1 varchar(30)
    18.  
       
    19.  
      删除表
    20.  
      drop table user;</span>


        3>增加、插入记录的 sql 语句详细

    1.  
      <span style="font-size:14px;">使用insert语句向表中插入三个员工的信息。
    2.  
      rename table user to employee;
    3.  
      insert into employee(id,username,birthday,entry_date,job,salary,resume) values(1,'aaa','1980-09-09','1980-09-09','bbb',90,'aaaaa');
    4.  
      select * from employee;
    5.  
       
    6.  
      插入数据的细节1
    7.  
      insert into employee values(1,'aaa','1980-09-09','1980-09-09','bbb',90,'aaaaa');
    8.  
       
    9.  
      插入数据的细节2
    10.  
      insert into employee values('1','aaa','1980-09-09','1980-09-09','bbb','90','aaaaa');
    11.  
       
    12.  
      插入数据的细节3(插入中文)
    13.  
      要告诉mysql客户采用gb2312编码
    14.  
      show variables like 'chara%';
    15.  
      set character_set_client=gb2312;
    16.  
      insert into employee(id,username) values('3','张三');
    17.  
       
    18.  
      要想查看时不乱码
    19.  
      show variables like 'chara%';
    20.  
      set character_set_results=gb2312;
    21.  
      select * from employee;</span>


        4>删除记录的 sql 语句详细

    1.  
      <span style="font-size:14px;">删除表中名称为’zs’的记录。
    2.  
      delete from employee where username='bbb';
    3.  
       
    4.  
      删除表中所有记录。
    5.  
      delete from employee;
    6.  
       
    7.  
      使用truncate删除表中记录。
    8.  
      truncate table employee;</span>


        5>修改记录的 sql 语句详细

    1.  
      <span style="font-size:14px;">将所有员工薪水修改为5000元。
    2.  
      update employee set salary=5000;
    3.  
       
    4.  
      将姓名为’bbb’的员工薪水修改为3000元。
    5.  
      update employee set salary=3000 where username='bbb';
    6.  
       
    7.  
      将姓名为’bbb的员工薪水修改为4000元,job改为ccc。
    8.  
      update employee set salary=4000,job='ccc' where username='bbb';
    9.  
       
    10.  
      将bbb的薪水在原有基础上增加1000元。
    11.  
      update employee set salary=salary+1000 where username='bbb';
    12.  
       
    13.  
      更新要注意的问题
    14.  
      update employee set username='ccc',salary=9000,birthday='1980-09-09',.....................
    15.  
      update where id=1;</span>

        6>查询记录的 sql 语句详细

    1.  
      <span style="font-size:14px;">查询表中所有学生的信息。
    2.  
      select * from student;
    3.  
       
    4.  
      查询表中所有学生的姓名和对应的英语成绩。
    5.  
      select name,english from student;
    6.  
       
    7.  
      过滤表中重复的英语数据。
    8.  
      select distinct english from student;
    9.  
       
    10.  
      在所有学生总分上加10分特长分。
    11.  
      select name,(chinese+english+math)+10 from student;
    12.  
       
    13.  
      统计每个学生的总分。
    14.  
      select name,(chinese+english+math) from student;
    15.  
       
    16.  
      关于排序
    17.  
      将对象成绩过去
    18.  
      统计大于该成绩的人数即可
    19.  
      统计数学成绩大于90的学生有多少个?
    20.  
      select count(*) from student where math>80;
    21.  
       
    22.  
       
    23.  
      使用别名表示学生分数。
    24.  
      select name as 姓名,(chinese+english+math)+10 as 总分 from student;
    25.  
      select name 姓名,(chinese+english+math)+10 总分 from student;
    26.  
       
    27.  
      查询姓名为wu的学生成绩
    28.  
      select * from student where name='王五';
    29.  
       
    30.  
      查询英语成绩大于90分的同学
    31.  
      select * from student where english>'90';
    32.  
       
    33.  
      查询总分大于200分的所有同学
    34.  
      select name from student where (chinese+english+math)>200;
    35.  
       
    36.  
      查询英语分数在 80-90之间的同学。
    37.  
      select name from student where english>80 and english<90;
    38.  
      select name from student where english between 80 and 90; == select name from student where english>=80 and english<=90;
    39.  
       
    40.  
      查询数学分数为89,90,91的同学。
    41.  
      select * from student where math in(89,90,91);
    42.  
       
    43.  
      查询所有姓李的学生成绩。
    44.  
      select * from student where name like '李%';
    45.  
      select * from student where name like '李_';
    46.  
       
    47.  
       
    48.  
      查询数学分>80,语文分>80的同学。
    49.  
      select * from student where math>80 and chinese>80;
    50.  
       
    51.  
      分页查询,查询从第 8 条记录开始的 3 条记录;即:第8 、9 、10 三条记录
    52.  
      int from = 2;
    53.  
      int end = 10;
    54.  
      String sql = "select * from student limit "+from+","+end; //字符串类型的语句
    55.  
      select * from student limit 8,3;</span>

    三、对数据记录的操作

        查询统计排序等相关处理的 sql 语句

      1.  
        <span style="font-size:14px;">对数学成绩排序后输出。
      2.  
        select name,math from student order by math;
      3.  
         
      4.  
        对总分排序后输出,然后再按从高到低的顺序输出
      5.  
        select name 姓名,(chinese+english+math) 总分 from student order by (chinese+english+math) desc;
      6.  
        select name 姓名,(chinese+english+math) 总分 from student order by 总分 desc;
      7.  
         
      8.  
        对姓李的学生成绩排序输出
      9.  
        select * from student where name like '李%' order by (chinese+english+math) desc;
      10.  
         
      11.  
        统计一个班级共有多少学生?
      12.  
        select count(name) from student;
      13.  
        select count(*) from student;
      14.  
         
      15.  
        统计数学成绩大于90的学生有多少个?
      16.  
        select count(*) from student where math>80;
      17.  
         
      18.  
        统计总分大于250的人数有多少?
      19.  
        select count(*) from student where (chinese+english+math)>250;
      20.  
         
      21.  
        关于 count的函数的细节 (count只统有值的行)
      22.  
         
      23.  
         
      24.  
        统计一个班级数学总成绩?
      25.  
        select sum(math) from student;
      26.  
         
      27.  
        统计一个班级语文、英语、数学各科的总成绩
      28.  
        select sum(chinese),sum(english),sum(math) from student;
      29.  
         
      30.  
        统计一个班级语文、英语、数学的成绩总和
      31.  
        select sum(chinese+english+math) from student;
      32.  
         
      33.  
        统计一个班级语文成绩平均分
      34.  
        select sum(chinese)/count(*) from student;
      35.  
         
      36.  
        统计一个班级语文成绩平均分
      37.  
        select avg(chinese) from student;
      38.  
         
      39.  
        求一个班级总分平均分
      40.  
        select avg(chinese+math+english) from student;
      41.  
         
      42.  
        求班级最高分和最低分
      43.  
        select max(chinese+math+english),min(chinese+math+english) from student;
      44.  
         
      45.  
        对订单表中商品归类后,显示每一类商品的总价
      46.  
        select product,sum(price) from orders group by product;
      47.  
         
      48.  
        查询购买了几类商品,并且每类总价大于100的商品
      49.  
        select product from orders group by product having sum(price)>100;
      50.  
         
      51.  
        按某一属性对记录进行排序
      52.  
        select * from student order by grade desc;</span>
    你应该了解真相,真相使你自由!
  • 相关阅读:
    git
    centos7安装python3和ipython
    centos7 安装mysql5.7
    ceph-文件存储
    ceph-对象存储
    ceph-块存储客户端
    ceph-简介及安装(luminous)版
    centos7 kvm安装使用
    webpack多页面应用打包问题-新增页面打包JS影响旧有JS资源
    webpack4.0 babel配置遇到的问题
  • 原文地址:https://www.cnblogs.com/Hooper_he/p/9636933.html
Copyright © 2011-2022 走看看