zoukankan      html  css  js  c++  java
  • mySQL学习笔记

    0. mysql特点

    1)免费  2)跨平台  3)轻  4)支持多并发

    1. select 操作

    1.1 打印出成绩最高的学生的信息

    select * from students
    order by score DESC
    limit 1;

    1.2 升序打印出成绩高于90的前两名学生的信息

    select * 
    from students
    where score > 90
    order by score;

    :当使用where和order by语句时,应该把order by 放到where之后,否则出错。

    1.3 打印出名字为'wanghan2'的学生的信息

    select id, name, score 
    from students 
    where name = 'wanghao2';

    :判断相等

    1.4 找出名字以"wang"开头的所有学生的信息

    select *
    from students
    where name LIKE 'wang%'

    :通配符

    %表示任何字符出现任意次数,注意例外:NULL.

    _只匹配单个字符

    使用注意

    • 不要过度使用通配符。通配符搜索处理一般比其他搜索花的时间长
    • 在确定使用通配符时,除非有必要,否则不要把他们用在搜索模式的开始处,否则搜索起来很慢。

    1.5 把学生信息按分数分组,并打印出考该分数的学生的人数

    select score, COUNT(*) AS NUM_STUDENTS
    from students
    group by score;

    1.6 把学生信息按分数分组,并打印出考该分数的人数大于10个的分数和对应的人数。

    select score, COUNT(*) AS NUM_STUDENTS
    from students
    group by score;
    HAVING COUNT(*) > 10;

    :where过滤行;having过滤分组。(另一理解:where在分组前过滤,having在分组后过滤)

    1.7 select 语句必须遵循的次序

    select
    from 
    where
    group by
    having 
    order by
    limit

    2.表的操作

    2.1 创建表

    create table tea
    (
    id int NOT NULL AUTO_INCREMENT,
    name char(50) NOT NULL,
    score int NULL,
    city char(50) NULL,
    PRIMARY KEY(id)
    );

    2.2 插入表(插入行)

    insert into tea values(4, 'zhangsan', 34,'beijing');
    insert into tea(id, name, score, city) values(4, 'zhangsan', 34,'beijing');

    2.3 删除行

    delete from table
    where name = "wanghao";

    2.4 删除表

    drop table tea;

    3.列操作

    3.1 增加列

    alter table tab3 
    add pro char(20);

    3.2 删除列

    alter table tab3  
    drop column pro;

    4. 更新表

    update tea
    set score = 89
    where id = 2;

    5. 子查询

    select id, name, score  
    from students 
    where score in 
    ( 
    select score 
    from tea
    );

    6. 创建连接

    select students.id, students.name, tea.name  
    from students, tea 
    where students.id = tea.id 
    order by students.name;

    7. 创建组合查询

    select vend_id, prod_id, prod_price
    from products
    where prod_price <= 5
    UNION
    select vend_id, prod_id, prod_price
    from products
    where vend_id IN (1001,1002)
    ORDER BY vend_id, prod_price

    :在使用UNION的组合查询中,只能有一条ORDER BY子句,它必须出现在最后一个查询的后边。

    8. 视图

    create VIEW info AS
    select name, socre
    where score > 80;

    :视图是虚拟的表。使用视图的原因

    • 重用SQL语句
    • 简化复杂的SQL操作
    • 使用表的组成部分而不是整个表
    • 保护数据
    • 更改数据的格式与表示,过滤不想要的数据

    9. 存储过程

    9.1 创建

    delimiter //
    create precidure pro()
    begin
    select avg(score)
    from students;
    end //
    delimiter ;

    9.2 调用

    call pro();

    9.3 删除

    drop procedure pro;

    10. 触发器

    10.1 创建表

       create table t(s1 integer);

    10.2 创建触发器

    delimiter |
    create trigger t_trigger before insert on t  for each row 
    begin set @x = "hello trigger";
           set NEW.s1 = 55;
    end; 
    |
    delimiter ;

    10.3 删除

    drop trigger t_trigger;

    10.4 当插入时,会触发触发器,把值改为55。

  • 相关阅读:
    用wamp配置的环境,想用CMD连接mysql怎么连
    Mysql删除表
    MySQL创建表
    Leetcode 130. Surrounded Regions
    Leetcode 111. Minimum Depth of Binary Tree
    Leetcode 110. Balanced Binary Tree
    Leetcode 98. Validate Binary Search Tree
    Leetcode 99. Recover Binary Search Tree
    Leetcode 108. Convert Sorted Array to Binary Search Tree
    Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
  • 原文地址:https://www.cnblogs.com/kaituorensheng/p/3798596.html
Copyright © 2011-2022 走看看