zoukankan      html  css  js  c++  java
  • day05--sql之DML数据操作语言

    DML数据操作语言(insert、delete、update)

    1.insert 命令

    1)查看表结构

    mysql> desc student;
    +----------+---------------------+------+-----+-------------------+----------------+
    | Field    | Type                | Null | Key | Default           | Extra          |
    +----------+---------------------+------+-----+-------------------+----------------+
    | id       | int(11)             | NO   | PRI | NULL              | auto_increment |
    | name     | varchar(12)         | NO   |     | NULL              |                |
    | age      | tinyint(3) unsigned | NO   |     | NULL              |                |
    | gender   | enum('m','f')       | YES  |     | f                 |                |
    | cometime | datetime            | YES  |     | CURRENT_TIMESTAMP |                |
    | birthday | datetime            | YES  |     | NULL              |                |
    +----------+---------------------+------+-----+-------------------+----------------+
    6 rows in set (0.00 sec)
    

    2)插入数据(不规范写法)

    mysql> insert into student values(1,'邱导',78,'f',now(),'1942-07-14');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from student;
    +----+--------+-----+--------+---------------------+---------------------+
    | id | name   | age | gender | cometime            | birthday            |
    +----+--------+-----+--------+---------------------+---------------------+
    |  1 | 邱导   |  78 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
    +----+--------+-----+--------+---------------------+---------------------+
    1 row in set (0.00 sec)
    

    3)插入数据(规范写法)

    #1.插入指定列数据
    mysql> insert into student(name,age) values('曾导','84');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from student;
    +----+--------+-----+--------+---------------------+---------------------+
    | id | name   | age | gender | cometime            | birthday            |
    +----+--------+-----+--------+---------------------+---------------------+
    |  1 | 邱导   |  78 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
    |  2 | 邱导   |  78 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
    |  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                |
    +----+--------+-----+--------+---------------------+---------------------+
    3 rows in set (0.00 sec)
    
    #2.插入指定列数据
    mysql> insert into student(name,age,birthday) values('曾导','84','1936-02-20');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from student;
    +----+--------+-----+--------+---------------------+---------------------+
    | id | name   | age | gender | cometime            | birthday            |
    +----+--------+-----+--------+---------------------+---------------------+
    |  1 | 邱导   |  78 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
    |  2 | 邱导   |  78 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
    |  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                |
    |  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
    +----+--------+-----+--------+---------------------+---------------------+
    4 rows in set (0.00 sec)
    

    4)插入多条数据

    mysql> insert into student(name,age,birthday) values('好大','18',1936-02-21),('好小','28','1992-01-01');
    Query OK, 2 rows affected, 1 warning (0.00 sec)
    Records: 2  Duplicates: 0  Warnings: 1
    
    mysql> select * from student;
    +----+--------+-----+--------+---------------------+---------------------+
    | id | name   | age | gender | cometime            | birthday            |
    +----+--------+-----+--------+---------------------+---------------------+
    |  1 | 邱导   |  78 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
    |  2 | 邱导   |  78 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
    |  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                |
    |  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
    |  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
    |  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
    |  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
    |  8 | 好小   |  28 | f      | 2020-07-15 09:31:07 | 1992-01-01 00:00:00 |
    +----+--------+-----+--------+---------------------+---------------------+
    8 rows in set (0.00 sec)
    

    2.update命令

    1)查看数据

    mysql> select * from student;
    +----+--------+-----+--------+---------------------+---------------------+
    | id | name   | age | gender | cometime            | birthday            |
    +----+--------+-----+--------+---------------------+---------------------+
    |  1 | 邱导   |  78 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
    |  2 | 邱导   |  78 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
    |  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                |
    |  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
    |  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
    |  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
    |  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
    |  8 | 好小   |  28 | f      | 2020-07-15 09:31:07 | 1992-01-01 00:00:00 |
    +----+--------+-----+--------+---------------------+---------------------+
    8 rows in set (0.00 sec)
    

    2)修改数据

    #使用update语句必须要加where条件
    mysql> update student set age=18 where name='邱导';
    Query OK, 2 rows affected (0.01 sec)
    Rows matched: 2  Changed: 2  Warnings: 0
    
    mysql> select * from student;
    +----+--------+-----+--------+---------------------+---------------------+
    | id | name   | age | gender | cometime            | birthday            |
    +----+--------+-----+--------+---------------------+---------------------+
    |  1 | 邱导   |  18 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
    |  2 | 邱导   |  18 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
    |  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                |
    |  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
    |  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
    |  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
    |  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
    |  8 | 好小   |  28 | f      | 2020-07-15 09:31:07 | 1992-01-01 00:00:00 |
    +----+--------+-----+--------+---------------------+---------------------+
    8 rows in set (0.00 sec)
    

    3)指定修改一条数据

    #如果数据库有主键,一定使用主键来作为where判断
    mysql> update student set age=88 where name='邱导' and cometime='2020-07-15 09:21:12';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> update student set age=88 where id=2;
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> select * from student;
    +----+--------+-----+--------+---------------------+---------------------+
    | id | name   | age | gender | cometime            | birthday            |
    +----+--------+-----+--------+---------------------+---------------------+
    |  1 | 邱导   |  88 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
    |  2 | 邱导   |  88 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
    |  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                |
    |  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
    |  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
    |  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
    |  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
    |  8 | 好小   |  28 | f      | 2020-07-15 09:31:07 | 1992-01-01 00:00:00 |
    +----+--------+-----+--------+---------------------+---------------------+
    8 rows in set (0.00 sec)
    

    3.delete语句

    1)删除数据

    #1.先查看数据,确认要删除的数据,怎么确定唯一
    
    #2.使用delete语句也一定要加where条件
    mysql> delete from student where id=8;
    Query OK, 1 row affected (0.01 sec)
    
    mysql> select * from student;
    +----+--------+-----+--------+---------------------+---------------------+
    | id | name   | age | gender | cometime            | birthday            |
    +----+--------+-----+--------+---------------------+---------------------+
    |  1 | 邱导   |  88 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 |
    |  2 | 邱导   |  88 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 |
    |  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                |
    |  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 |
    |  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 |
    |  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 |
    |  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 |
    +----+--------+-----+--------+---------------------+---------------------+
    7 rows in set (0.00 sec)
    
    #3.如果就是要清空表
    mysql> delete from student where 1=1;
    Query OK, 1 row affected (0.01 sec)
    #(危险)
    truncate table student;
    drop table student
    

    4.使用update代替delete

    1)添加状态字段

    mysql> alter table student add status enum('1','0') default 1;
    Query OK, 0 rows affected (0.08 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> select * from student;
    +----+--------+-----+--------+---------------------+---------------------+--------+
    | id | name   | age | gender | cometime            | birthday            | status |
    +----+--------+-----+--------+---------------------+---------------------+--------+
    |  1 | 邱导   |  88 | f      | 2020-07-15 09:21:12 | 1942-07-14 00:00:00 | 1      |
    |  2 | 邱导   |  88 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 | 1      |
    |  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                | 1      |
    |  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 | 1      |
    |  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 | 1      |
    |  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 | 1      |
    |  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 | 1      |
    +----+--------+-----+--------+---------------------+---------------------+--------+
    7 rows in set (0.00 sec)
    

    2)使用update代替delete

    #相当于删除学生
    mysql> update student set status='0' where id =2;
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> select * from student;
    +----+--------+-----+--------+---------------------+---------------------+--------+
    | id | name   | age | gender | cometime            | birthday            | status |
    +----+--------+-----+--------+---------------------+---------------------+--------+
    |  2 | 邱导   |  88 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 | 0      |
    |  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                | 1      |
    |  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 | 1      |
    |  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 | 1      |
    |  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 | 1      |
    |  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 | 1      |
    +----+--------+-----+--------+---------------------+---------------------+--------+
    6 rows in set (0.00 sec)
    
    #相当于学生回来
    mysql> update student set status='1' where id =2;
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> select * from student;
    +----+--------+-----+--------+---------------------+---------------------+--------+
    | id | name   | age | gender | cometime            | birthday            | status |
    +----+--------+-----+--------+---------------------+---------------------+--------+
    |  2 | 邱导   |  88 | f      | 2020-07-15 09:22:27 | 1942-07-14 00:00:00 | 1      |
    |  3 | 曾导   |  84 | f      | 2020-07-15 09:24:17 | NULL                | 1      |
    |  4 | 曾导   |  84 | f      | 2020-07-15 09:25:22 | 1936-02-20 00:00:00 | 1      |
    |  5 | 曾导   |  84 | f      | 2020-07-15 09:28:11 | 1936-02-21 00:00:00 | 1      |
    |  6 | 好大   |  18 | f      | 2020-07-15 09:29:18 | 0000-00-00 00:00:00 | 1      |
    |  7 | 好大   |  18 | f      | 2020-07-15 09:31:07 | 0000-00-00 00:00:00 | 1      |
    +----+--------+-----+--------+---------------------+---------------------+--------+
    6 rows in set (0.00 sec)
    

    5. drop,truncate,delete区别

    1、drop (删除表):删除内容和定义,释放空间。简单来说就是把整个表去掉.以后要新增数据是不可能的,除非新增一个表。
      drop语句将删除表的结构被依赖的约束(constrain),触发器(trigger)索引(index);依赖于该表的存储过程/函数将被保留,但其状态会变为:invalid。
    
    2、truncate (清空表中的数据):删除内容、释放空间但不删除定义(保留表的数据结构)。与drop不同的是,只是清空表数据而已。
       truncate 不能删除行数据,要删就要把表清空。
       
    3、delete (删除表中的数据):delete 语句用于删除表中的行。delete语句执行删除的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存以便进行进行回滚操作。
       truncate与不带where的delete :只删除数据,而不删除表的结构(定义)
    
    4、truncate table 删除表中的所有行,但表结构及其列、约束、索引等保持不变。新行标识所用的计数值重置为该列的种子。如果想保留标识计数值,请改用delete。
       如果要删除表定义及其数据,请使用 drop table 语句。
      
    5、对于由foreign key约束引用的表,不能使用truncate table ,而应使用不带where子句的delete语句。由于truncate table 记录在日志中,所以它不能激活触发器。
        
    6、执行速度,一般来说: drop> truncate > delete。
        
    7、delete语句是数据库操作语言(dml),这个操作会放到 rollback segement 中,事务提交之后才生效;如果有相应的 trigger,执行的时候将被触发。
       truncate、drop 是数据库定义语言(ddl),操作立即生效,原数据不放到 rollback segment 中,不能回滚,操作不触发 trigger。
    
  • 相关阅读:
    Atitit 图片验证码功能设计文档总结目录1.1. 使用图片验证码img src标签设置图片。。验证码图片有png,jpg,svg等格式。。 11.2. Php png图像 11.3. P
    Atitt php script lan debug bp 最佳实践调试php目录1.1. Error_log 11.2. Echo vs log法 11.3. 输出与debug信息昏药问题
    Atitit 项目分析与统计目录1. 静态分析+动态分析 。其中, 12. 模块分析,与模块位置idx 13. 编程语言类型与版本 13.1. 类库统记表 类型与版本 23.2. 中间
    Atitit 增强代码健壮性 出错继续执行恢复模式,就像vbs那样我以为我可以使用Try/Catch,但是我找不到异常后是否可以继续执行代码,并且找不到如何在最后显示错误消息。目录PHP
    Atitit bootsAtitit bootstrap布局 栅格.docx目录1. 简述container与container-fluid的区别 11.1.1. 在bootstrap中的布局
    Atitit php读取数据库记录集合并循环修改展示//------------------------ini db sys$dbstr = “mysql:host=“ . $mysql_con
    Atitit 验证码功能修复总结文档原有的tp5里面的验证码不知怎么有问题了,试图在tp5框架内修复无果。。使用了新的验证码组件 “lifei6671/php-captcha“: “0.
    Atitt 支付业务 银行国际代码(SWIFT Code银行国际代码(SWIFT Code)是由SWIFT协会提出并被ISO通过的银行识别代码,凡该协会的成员银行都有自己特定的SWIFT代码
    Atitit img hot click link 图像背景拉伸100%Map area trouble..So body backgrd img mode...is easy...No
    Atitit doc mng 文档管理总结目录1. 主要几大内容 12. 存储管理 22.1. 一般来说 ,文档存储在IM网盘note邮箱blog wiki等地 22.2. 文档格式与体
  • 原文地址:https://www.cnblogs.com/tcy1/p/13305993.html
Copyright © 2011-2022 走看看