zoukankan      html  css  js  c++  java
  • MySQL的增、删、改、查

    数据库的常用命令以及作用
    用法 作用
    CREATE database 数据库名称。 创建新的数据库
    DESCRIBE 表单名称; 描述表单
    UPDATE 表单名称 SET attribute=新值 WHERE attribute > 原始值; 更新表单中的数据
    USE 数据库名称; 指定使用的数据库
    SHOW databases; 显示当前已有的数据库
    SHOW tables; 显示当前数据库中的表单
    SELECT * FROM 表单名称; 从表单中选中某个记录值
    DELETE FROM 表单名 WHERE attribute=值; 从表单中删除某个记录值

    1. 创建一个名为testDB的数据库

    mysql> create Database testDB;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | testDB             |
    +--------------------+
    5 rows in set (0.00 sec)

    在testDB数据库中创建数据表mybook,并定义存储数据内容的结构。分别定义3个字段,其中,长度为15个字符的字符型字段name用来存放图书名称,整型字段price和pages分别存储图书的价格和页数。

    mysql> use testDB;
    Database changed
    mysql> create table mybook(name char(15), price int, pages int);
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> describe mybook;
    +-------+----------+------+-----+---------+-------+
    | Field | Type     | Null | Key | Default | Extra |
    +-------+----------+------+-----+---------+-------+
    | name  | char(15) | YES  |     | NULL    |       |
    | price | int(11)  | YES  |     | NULL    |       |
    | pages | int(11)  | YES  |     | NULL    |       |
    +-------+----------+------+-----+---------+-------+
    3 rows in set (0.04 sec)

    再创建一个数据表myclass,设置id字段为主键,自增,并且不能为空,sex字段默认值为0。

    mysql> create table myclass(id int(4) not null primary key auto_increment,name char(20) not null,sex int(4) not null default '0');
    Query OK, 0 rows affected (0.22 sec)
    
    mysql> describe myclass;
    +-------+----------+------+-----+---------+----------------+
    | Field | Type     | Null | Key | Default | Extra          |
    +-------+----------+------+-----+---------+----------------+
    | id    | int(4)   | NO   | PRI | NULL    | auto_increment |
    | name  | char(20) | NO   |     | NULL    |                |
    | sex   | int(4)   | NO   |     | 0       |                |
    +-------+----------+------+-----+---------+----------------+
    3 rows in set (0.01 sec)

    2. 向mybook数据表中插一条图书信息,其中书名为Linux,价格和页数分别是60元和100页。

    使用select命令查询表单内容时,需要加上想要查询的字段;如果想查看表单中的所有内容,则可以使用星号(*)通配符来显示。

    mysql> insert into mybook(name,price,pages) values('Linux','60','100');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from mybook;
    +-------+-------+-------+
    | name  | price | pages |
    +-------+-------+-------+
    | Linux |    60 |   100 |
    +-------+-------+-------+
    1 row in set (0.00 sec)

    3. 使用update命令将刚才插入的Linux图书信息的价格修改为55元

    mysql> update mybook set price=55 where name='Linux';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> select name,price from mybook;
    +-------+-------+
    | name  | price |
    +-------+-------+
    | Linux |    55 |
    +-------+-------+
    1 row in set (0.00 sec)

    4. 使用delete命令删除数据表mybook中的所有内容,然后再查看该表单中的内容,可以发现该表单内容为空了。

    mysql> delete from mybook;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from mybook;
    Empty set (0.00 sec)

    下面使用insert插入命令依次插入4条图书信息

    mysql> insert into mybook(name,price,pages) values('Linux1','30','100');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into mybook(name,price,pages) values('Linux2','40','200');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into mybook(name,price,pages) values('Linux3','50','300');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into mybook(name,price,pages) values('Linux4','60','400');
    Query OK, 1 row affected (0.00 sec)
    where命令中使用的参数以及作用
    参数 作用
    = 相等
    <>或!= 不相等
    > 大于
    < 小于
    >= 大于或等于
    <= 小于或等于
    BETWEEN 在某个范围内
    LIKE 搜索一个例子
    IN 在列中搜索多个值

    5. 分别在mybook数据表中查找出价格大于35元或价格不等于50元的图书

    mysql> select * from mybook where price>35;
    +--------+-------+-------+
    | name   | price | pages |
    +--------+-------+-------+
    | Linux2 |    40 |   200 |
    | Linux3 |    50 |   300 |
    | Linux4 |    60 |   400 |
    +--------+-------+-------+
    3 rows in set (0.00 sec)
    
    mysql> select * from mybook where price!=50;
    +--------+-------+-------+
    | name   | price | pages |
    +--------+-------+-------+
    | Linux1 |    30 |   100 |
    | Linux2 |    40 |   200 |
    | Linux4 |    60 |   400 |
    +--------+-------+-------+
    3 rows in set (0.00 sec)

    批量删除多条记录

    mysql> select * from mybook;
    +--------+-------+-------+
    | name   | price | pages |
    +--------+-------+-------+
    | Linux1 |    30 |   100 |
    | Linux2 |    40 |   200 |
    | Linux3 |    50 |   300 |
    | Linux4 |    60 |   400 |
    +--------+-------+-------+
    4 rows in set (0.00 sec)
    
    mysql> delete from mybook where name in ('Linux1','Linux3');
    Query OK, 2 rows affected (0.00 sec)
    
    mysql> select * from mybook;
    +--------+-------+-------+
    | name   | price | pages |
    +--------+-------+-------+
    | Linux2 |    40 |   200 |
    | Linux4 |    60 |   400 |
    +--------+-------+-------+
    2 rows in set (0.00 sec)

     

  • 相关阅读:
    SpringMVC从Request域中获取数据
    SpringMVC重定向
    SpringMVC的请求转发的三种方法
    SpringMVC文件上传
    SpringMVC处理请求释放静态资源的三种方式
    jackson实现json转换
    SpringMVC之请求部分
    SpringMVC的执行流程
    Java [Leetcode 39]Combination Sum
    深入解析Java对象的hashCode和hashCode在HashMap的底层数据结构的应用
  • 原文地址:https://www.cnblogs.com/opsprobe/p/11616525.html
Copyright © 2011-2022 走看看