zoukankan      html  css  js  c++  java
  • database

          数据库基础

    一、数据库概念

      1.数据库

        存储数据的仓库(逻辑概念,并未真实存在)

      2.数据库软件

        真实软件,用来实现数据库这个逻辑概念

      3.数据仓库

        数据量更加庞大,更加侧重数据分析和数据挖掘,供企业决策分析之用,主要是数据查询,修改和删除很少

    二、MySQL的特点 

      1.关系型数据库
      2.跨平台
      3.支持多种编程语言(python、java、php)
      4.基于磁盘存储,数据是以文件形式存放在数据库目录/var/lib/mysql下

     三 启动连接

    - 服务端启动 
    
    ```mysql
    sudo /etc/init.d/mysql start|stop|restart|status
    sudo service mysql start|stop|restart|status
    ```
    
    - 客户端连接
    
    ```mysql
    mysql -hIP地址 -u用户名 -p密码
    本地连接可省略 -h 选项

    四、基本sql命令

     

    **1.库管理**
    
    ```mysqle
        1、查看已有库; 
        show databases;
        2、创建库并指定字符集;
        create database 库名 charset utf8;
        3、查看当前所在库;
        select database();
        4、切换库;
        use 库名;
        5、查看库中已有表;
        show tables;
        6、删除库;
        drop database 库名;
    ```
    **2.表管理**
    
    ```mysql
        1、创建表并指定字符集;
        create table 表名(字段名 数据类型,xxx)charset=utf8;
        2、查看创建表的语句 (字符集、存储引擎);
        show create table 表名;
        3、查看表结构;
        desc 表名;
        4、删除表;
    ```    drop table 表名;
    1 **3.表记录管理**
    2 
    3 ```mysql
    4     1、增 :insert into 表名(字段名)values(),();
    5     2、删 :delete from 表名 where 条件;
    6     3、改 :update 表名 set 字段名=值 字段名=值 where 条件;
    7     4、查 :select * from 表名 where 条件;
    8 ```
    1 **4.表字段管理**
    2 
    3 ```mysql
    4     1、增 : alter table 表名 add 字段名 类型 first;(添加首字段)
    5             alter table 表名 add 字段名 类型 after 字段名;(添加在什么字段后)
    6             alter table 表名 add 字段名 类型(添加最后的字段)
    7     2、删 :alter table 表名 drop 字段名;
    8     3、改 :alter table 表名 modify 字段名 新类型;
    9     4、表重命名 :alter table 表名 rename 新表名;

    五、数据类型

    1 -  数值类型
    2     int smallint bigint tinyint
    3     float(m,n) double decimal
    4 - 字符类型
    5     char(固定长度) varchar(不固定长度) text logtext blob longblob
    6 - 枚举类型 
    7     enum(多个中选一个) set(多个中选多个)
    8   日期时间类型
    9     data time year datatime timestamp
    **日期时间函数** 
    ```mysql
        NOW() CURDATR() YEAR(字段名) DATE(字段名) TIME(字段名)
    ```
    **日期时间运算**
    ```mysql
        select * from 表名 where 字段名 运算符(NOW()-interval 间隔)
        间隔单位:1day| 3month|2year
        eg1:查询一年以前的用户充值信息
        select * from tab where time <(now()-interval 1 year);
    ```

    六、MySQL运算符

    - **数值比较**
    
    ```mysql
    > >= < <= = !=
    eg1 : 查询成绩不及格的学生
          select * from student where score >60;
    eg2 : 删除成绩不及格的学生
          delete from sutdents where score <60;
    eg3 : 把id为3的学生的姓名改为 周芷若
          update students set name="周芷若" where id = 3;
    ```
    - **逻辑比较** 
    
    ```mysql
    and  or
    eg1 : 查询成绩不及格的男生
         select * from students where score <60 and gender="m";
    eg2 : 查询成绩在60-70之间的学生
    ```   select * from students where score>=60 and score<=70;
    - **范围内比较** 
    
    ```mysql
    between 值1 and 值2 、in() 、not in()
    eg1 : 查询不及格的学生姓名及成绩
        select name,score from students where score between 0 and 59;
    eg2 : 查询AID1903和AID1902班的学生姓名及成绩
        select name,score from students where class in("AID1903","AID1902");
    - **模糊比较(like)**
    
    ```mysql
    where 字段名 like 表达式(%_)
    eg1 : 查询北京的姓赵的学生信息
        select *from students where address="北京" and name like "赵%"; 
    - **NULL判断**
    
    ```mysql
    is NULL 、is not NULL
    eg1 : 查询姓名字段值为NULL的学生信息
        select * from students where name is NULL;
    ```

     七、查询

    - **order by**
    给查询的结果进行排序(永远放在SQL命令的倒数第二的位置写)
    ```mysql
    order by 字段名 ASC/DESC
    eg1 : 查询成绩从高到低排列
        select * from students where score order by DESC
    ```
    - **limit**
    
    限制显示查询记录的条数(永远放在SQL命令的最后写)
    
    ```mysql
    limit n :显示前n条
    limit m,n :从第(m+1)条记录开始,显示n条
    分页:每页显示10条,显示第6页的内容
        limit (6-1)*10 ,10

    八、MySQL基础巩固

    - **创建库 :country(指定字符编码为utf8)**
      create database country; - **创建表 :sanguo 字段:id 、name、attack、defense、gender、country**
      create table sanguo(id int primary key auto_increment,
                  name varchar(16) not null,
                  attack int not null,
                  defense int not null,
                  gender enum("w","m") not null,
                  country varchar(16) not null)charset=utf8;
    **插入5条表记录(id 1-5,name-诸葛亮、司马懿、貂蝉、张飞、赵云),攻击>100,防御<100)**
       insert into sanguo values(1,"诸葛亮",150,80,"m","蜀国";
                       2,"司马懿",160,70,"m","魏国";
                       3,"貂蝉",110,50,"w","吴国";
                       4,"张飞",200,90,"w","蜀国";
                       5,"赵云",21,80,"w","蜀国";)
    - **查找所有蜀国人的信息**
      select * from sanguo where country="蜀国";
    - **将赵云的攻击力设置为360,防御力设置为68*
      update sanguo set attack=360 ,defense=68 where name="赵云";
    - **将吴国英雄中攻击值为110的英雄的攻击值改为100,防御力改为60**
      update sanguo set attack = 100 ,defense=60 where country="吴国"
    - **找出攻击值高于200的蜀国英雄的名字、攻击力**
      select name,attack from sanguo where attack>200 and country="蜀国";
    - **将蜀国英雄按攻击值从高到低排序**
      select * from sanguo where country = "蜀国" order by attark DESC;
    - **魏蜀两国英雄中名字为三个字的按防御值升序排列**
      select * from sanguo where country in("魏国","蜀国") and name like"___" order by defense ASC;
    - **在蜀国英雄中,查找攻击值前3名且名字不为 NULL 的英雄的姓名、攻击值和国家**
      select name,attack,country from sanguo where country = "蜀国" and name is not NULL order by attack DESC limit 3;
                      

     

     

      

      

  • 相关阅读:
    eclipse安装svn插件,在输入url后,一直卡在in progress界面不懂。
    android——背景颜色渐变(梯度变化)
    android——ObjectAnimator动画(一)
    android——ObjectAnimator动画
    android 无线调试
    android——字体颜色跟随状态改变
    爬虫基本原理
    java基础 数组
    java基础 方法
    java基础 流程控制和条件语句,循环语句
  • 原文地址:https://www.cnblogs.com/Acekr/p/11025270.html
Copyright © 2011-2022 走看看