zoukankan      html  css  js  c++  java
  • mysql数据库基本操作

    数据库

    创建数据库
    create database 数据库名;
    
    选择数据库
    use 数据库名;
    
    查看数据库
    show databases ;
    
    删除数据库
    drop database 数据库表名;
    

    数据表

    创建数据表
    create table table_name(列名1 属性,列名2 属性...);
    
    查看表结构
    show columns from 数据库名.数据表明;
    或者
    describe 数据表名;
    describe 数据表名 列名;
    
    修改表结构
    alert table 数据表名 alert_spec;
    
    重命名数据表
    rename table 数据表名1 to 数据表名2;
    
    删除数据表
    drop table 数据表名;
    或者
    drop table if exists 数据表名;
    
    数据表记录的添加
    insert into 数据表名(column_name,column_name2,...) value (value1,value2,...);
    insert into 数据表名(value1,value2,...);
    
    数据表记录的查询
    select select_list from 数据表名 where condition;
    
    数据表记录的修改
    update 数据表名 set column_name1 = new_value1,column_name2 =new_value2 where condition;
    
    数据表记录的删除
    delete from 数据表名 where condition;
    
    数据表的查询操作
    select select_list             -- 要查询的内容,选择那些列
    from 数据表名                   -- 指定数据表
    where primary_constraint       -- 查询时需要满足的条件
    group by grouping_columns      -- 如何对结果进行分组
    order by sorting_cloumns       -- 如何对结果进行排序
    having secondary_constraint    -- 查询时满足的第二条件
    
    1. select_list
    select*from tb_mrbook;             //查询数据表中所有数据
    select id,bookname from tb_mrbook; //查询数据表中id和bookname列的数据
    
    1. table_list
    select tb_mrbook.id,tb_mrbook.bookname,auther,price from tb_mrbook,tb_bookinfo where tb_mrbook.bookname=tb_bookinfo.bookname and tb_bookinfo.bookname = '水浒';
    
    1. order by 对结果排序
    2. like 模糊查询
    select * from tb_mrbook where bookname like "%php%";
    
    1. concat 联合多列
    select id,concat(bookname,":",price) as info from tb_mrbook;
    
    1. limit 限定结果行数
    select * from tb_mrbook limit 2,6;
    
    1. 使用函数和表达式
    avg 平均值 count 统计 min 获取最小 max 获取最大  sum 获取所有总和
    select sum(price) as totalprice from tb_mrbook;
    
    1. group by 对结果分组 (对指定字段分组,除指定字段外其他字段通过聚合函数合并)
    select type1,avg(price) as type2 from tb_mrbook group by type1;
    
    1. 使用having子句设定第二查询条件
      与where的区别是where是在查询前,having是在之后,并且可以使用函数筛选
    select type1,avg(price) as type2 from tb_mrbook group by type1 having avg(price)>60;
    
    多表关联查询
    left join 
    select 表名.字段名,(表名.字段名...) from 左表名  left join 右表名  on  condition(条件语句);
    

    关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹配,则结果为 NULL。(首先取出a表中所有数据,然后再加上与a,b匹配的的数据,select 后面限定显示的字段,右表中没有匹配的就显示null)

    innner join
    

    关键字在表中存在至少一个匹配时返回行。

  • 相关阅读:
    如何使用反射技术获取泛型类的真实类型?
    applicationContext.xml文件如何调用外部properties等配置文件
    applicationContext.xml中的使用${}是代表什么意思?
    net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
    MySql中LongText字段对应Hibernate映射文件的设置(转)
    ckeditor的详细配置(转)
    XML-学习
    WSDL-学习总结
    ONVIF-WSDL
    sourceinsight相关配置
  • 原文地址:https://www.cnblogs.com/heanwanfeng/p/13294039.html
Copyright © 2011-2022 走看看