zoukankan      html  css  js  c++  java
  • mysql基础知识(1)

    一、基础

      模式定义了数据如何存储、存储什么样的数据库以及数据如何分解等信息,数据库和表都有模式。关于数据库的模式可以参考这里:https://blog.csdn.net/liaohong940908/article/details/51906697

      主键的值不允许修改,也不允许复用,也就是不能使用已经删除的主键值赋值给新数据的主键。

      SQL语句不区分大小写,但是数据库表名、列名是否区分依赖于具体的DBMS以及配置

      SQL的注释方式:

        ①#注释

        ②select * from mytable --注释

        ③/*

        注释

        */

    二、创建表

      create table mytable(

      id int not null auto_increment,

      col1 int not null default 1,

      col2 varchar(10) null,

      col3 date null,

      primary key ('id')

      );

    三、修改表

      添加列

        alter table mytable add col4 char(5);

      删除列

        alter table mytable drop column col;

      删除表

        drop table mytable;

    四、插入

      普通插入

        insert into mytable (col1,col2) values(val1,val2);

      插入搜索出来的数据

        insert into mytable (col1,col2) select col1,col2 from mytable;

      将一个表的内容插入到一个新表

        create table newtable as select * from mytable;

    五、更新

      update mytable set col=val where id=1;

    六、删除

      delete from mytable where id=1;

      truncate table 清空表,即删除了所有行

    七、查询

      select distinct col1,col2 from mytable;  distinct:相同值只会出先一次,这句就是选择相同的列

      select * from mytable limit 5;  限制返回5行

      select * from mytable limit 0,5;  限制返回5行

      select * from mytable limit 2,3  返回3~5行

    八、排序

      asc  升序,也是默认

      desc  降序

      可以按多个列进行排序,并且为每个列指定不同的排序方式:

        select * from mytable order by col1 desc,col2 asc;

    九、过滤

      select * from mytable where col is null

      下面显示了where子句可用的操作符:

        = 等于  < 小于  > 大于

        <> 不等于  != 不等于

        <= 小于等于  !> 小于等于 

        &gt;= 大于等于  !< 大于等于

        between 在两个值之间

        is null 为null的值  注意:null 与 0 、空字符串都不同

        and or 用于连接多个过滤条件,优先处理 AND,当一个过滤表达式涉及到多个 AND 和 OR 时,可以使用 () 来决定优先级,使得优先级关系更清晰。

        in 操作符用于匹配一组值,其后也可以接一个 select子句,从而匹配子查询得到的一组值。

        not 操作符用于否定一个条件。

    十、通配符

      %  匹配>=0个任意字符

          匹配1个任意字符

      []   可以匹配集合内字符,类似正则表达式;^可以否定其内容。

      eg:  select * from mytable where col like '[^AB]'  不以A和B开头的任意文本

      注意:不要滥用通配符,通配符位于开头处匹配会非常慢。

  • 相关阅读:
    MySQL "show users"
    MySQL
    A MySQL 'create table' syntax example
    MySQL backup
    MySQL show status
    Tomcat, pathinfo, and servlets
    Servlet forward example
    Servlet redirect example
    Java servlet example
    How to forward from one JSP to another JSP
  • 原文地址:https://www.cnblogs.com/ybf-yyj/p/9043461.html
Copyright © 2011-2022 走看看