MySQL-sql增删改(insert、delete、update)

概述

本文讲解增删改的sql语句

增加

语法1

insert into 表(列1,列2,...) values (值1,值2,...)

列的 数量、顺序 要和值的 数量、顺序 一致

例子1
insert into dept (deptno,dname,loc) values ('1','市场部','北京')
例子2
insert into dept (deptno,dname) values ('2','市场部')

语法2

当 值 的 数量、顺序 与表中 字段 的 数量、顺序 一致时,列名可以省略(当表中字段几十个时,可以大大节省工作量)

insert into 表 values (值1,值2,...)
例子
insert into dept  values (3,'市场部','北京')

插入多条数据

insert into 表名 (列名1 ,列名2 ,列名3 ) values
(值1,值2,值3),
(值1,值2,值3),
(值1,值2,值3),

或者

insert into 表名 values
(值1,值2,值3),
(值1,值2,值3),
(值1,值2,值3),

例子:

insert into t_student_mt values 
(1,'lucy',22,170,'2022-01-02 09:30:00'),
(4,'lili',22,171,'2022-01-02 09:50:00'),
(4,'张三',21,171,'2022-01-02 09:50:00'),
(2,'韩梅梅',19,165,'2022-01-01 09:30:00'),
(2,'李雷',20,182,'2022-01-01 09:30:00');

根据 select 查询结果插入数据

将select查询结果插入到表中

insert into 表1  ( select * from 表2 )

删除

语法

delete from 表 [where 条件]
例子
  1. 删除该表所有记录,慎用 慎用 慎用
    delete from dept
    
  2. 删除deptno为4的记录
    delete from dept where deptno=4
    
  3. 删除deptno为4、5的记录
    delete from dept where deptno in (4,5)
    

修改

语法

update 表 set 列=值,列2=值2,...  [where 条件]
例子
  1. 修改所有记录的值
    update emp set sal=sal*1.2
    
  2. 将job是MANAGER的奖金增加300
    update emp set comm=nvl(comm,0)+300 where job='MANAGER'