zoukankan      html  css  js  c++  java
  • MYSQL之数据库初窥

    mysql数据库
    1、数据库简单介绍
       数据库概念:是依照数据结构来组织、存储和管理数据的仓库。
    2、经常使用术语
       数据库:是一些关联表的集合
       数据表:表是数据的矩阵,在数据库中看起来像一个简单的电子表格
       列:一列数据元素包括了同样的数据
       行:一行是一组相关的数据
       主键:主键是唯一的,一个数据表中仅仅能包括一个主键
       外键:外键用于关联两个表
       索引:使用索引能够高速訪问数据库表中的特定信息
    3、经常使用命令
       1、链接数据库:mysql -u root -p 回车输入password就可以
       2、退出数据库:mysql>exit 或者 mysql>quit
       3、数据库帮助信息:mysql --help
       4、创建数据库:mysql>create database test     //创建了一个名为test的数据库
       5、删除数据库:mysql>drop database test
       6、选择数据库:mysql>use test
    4、数据库类型
       类型大致能够分为三类:数值、日期/时间和字符串类型
     
          类型             大小        用途
       1、数值
          INT/INTEGER      4字节       整数型
          MEDIUMINT        3字节       整数型
          SMALLINT         2字节       整数型
          TINYINT          1字节       整数型
          BIGINT           8字节       整数型
          FLOAT            4字节       单精度浮点型
          DOUBLE           8字节       双精度浮点型
       2、日期/时间
          DATE             3字节       YYYY-MM-DD
          TIME             3字节       HH:MM:SS
          YEAR             1字节       YYYY
          DATETIME         8字节       YYYY-MM-DD HH:MM:SS
          TIMESTAMP        8字节       YYYYMMDD HHMMSS
       3、字符串
          CHAR             1字节       字符串型
          TEXT             4字节       文本型
    5、基本的语法命令
       1、创建数据表
       mysql>create table student(
           ->id int not null auto_increment,
           ->name char(20) not null,
           ->age int,
           ->primary key (id)
           -> );
       注:mysql命令终止符为分号(;)。
       2、删除数据表
       mysql>drop table student;
       3、插入数据
       mysql>insert into student(id,name) values (20028,"王刚");
       4、查询数据
       mysql>select * from student;
       mysql>select name from student;
       5、更新数据
       mysql>update student set name="王强" where id=20028;
       6、删除数据
       mysql>delete from student where id=20028;
    6、高级语法命令
       1、like子句
       mysql>select * from student where id like '%00%' and name="王强";
       注:"%"指匹配多个,"?

    "指匹配一个
       2、排序
       mysql>select * from student order by id asc/desc;
       注:asc为升序。desc为降序
       3、join子句
       mysql>select a.id,a.age,b.sex from student a, test b
           ->where a.id=b.id;
       mysql>select a.id,a.age,b.sex from student a left join test b
           ->on a.id=b.id;
       注:使用left join,该语句会读取左边的数据表(student)的全部选取的字段数据,
           即使右边表(test)中没有相应的字段值。
       4、删除、加入或改动表字段
          1、改动表名
          mysql>alter table test rename to new_test;
          2、删除表中字段
          mysql>alter table student drop age;
          注:假设数据表中仅仅剩余一个字段则无法使用drop来删除字段
          3、加入表中字段   
          mysql>alter table student add age int;
          注:运行以上命令后,age字段会自己主动加入到数据表字段的末尾
          4、改动字段类型
          mysql>alter table test modify name char(30);
          5、改动字段名称
          mysql>alter table test chang name new_name char(40);
          6、改动字段默认值
          mysql>alter table test alter age set default 24;
    7、正則表達式
       
          模式                  描写叙述
           $                匹配输入字符串的结束位置
           ^                匹配输入字符串的開始位置
           .                匹配除" "之外的不论什么单个字符。要匹配" "可使用"[. ]"
          [...]             字符集合,匹配包括的任一个字符,列如[abc]能够匹配"apple"中的a
          [^...]            字符集合。匹配未被包括的随意字符。列如[^abc]能够匹配"apple"中的p
           *                匹配前面的子表达式零次或多次
           +                匹配前面的子表达式一次或多次
          {n}               n是一个非负整数,匹配确定的n次,比如"o{2}"不能匹配"Bob"中的"o"可是能匹配"food"中的两个o
          {n,m}             m和n均为非负整数,当中n<=m,最少匹配n次且最多匹配m次
       1、查找name字段中以'st'开头的全部数据
       mysql>select name from student where name regexp '^st';
       2、查找name字段中包括'mar'字符的全部数据
       mysql>select name from student where name regexp '*mar*';

  • 相关阅读:
    liunx挂载Ios镜像文件
    liunx下删除多个目录下的相同文件
    tomcat启动报错:Unable to complete the scan for annotations for web application [] due to a StackOverflow
    lr中controller 中scenario-> rendezvous显示灰色不可用
    liunx 下在指定文件夹下搜索字符
    python运行时提示:dot not in the path
    1.4 python 类型转换函数
    js字符串拼接
    自己写的时间轴空控件
    ios禁止页面下拉
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/7229876.html
Copyright © 2011-2022 走看看