3.1 插入

3.1.1 语法

  1. /* 插入 */
  2. # 1 方式1
  3. insert into 表名(列1,列2, ...)
  4. values(值1,值2, ...);
  5. # 2 方式2
  6. insert into 表名
  7. set 1=值1, 2=值2, ...;
  • 列和值一一对应,数据类型一致或兼容
  • Nullable 的列可以插入 null 值或不插入值
  • 列的顺序可以调换
  • 插入方式1
    • 可以省略列名,值的顺序与表中列顺序一致
    • 支持插入多行
    • 支持子查询

      3.1.2 使用

      ```sql

      1 【插入1】

      1.1 常规插入

      insert into female(id, name, sex) values(1, ‘张三’, ‘女’);

1.2 省略列名

insert into female values(1, ‘张三’, ‘女’);

1.3 插入多行

insert into female values(1, ‘张三’, ‘女’),(2, ‘李四’, ‘女’);

1.4 子查询

insert into female select 3, ‘王五’, ‘女’ union select 4, ‘赵六’, ‘女’;

2 【插入2】

insert into female set id=1, name=’张三’, sex=’女’;


---

<a name="YkTvl"></a>
## 3.2 修改
<a name="3tShs"></a>
### 3.2.1 语法
```sql
/* 修改 */
# 1 单表修改
update 表名 set 列1=值1, 列2=值2, ...
where 筛选条件;

# 2 多表修改
# 2.1 sql92
update 表1 别名1, 表2 别名2
set 列1=值1, 列2=值2, ...
where 连接条件
and 筛选条件;

# 2.2 sql99
update 表1 别名1
inner/left/right join 表2 别名2
on 连接条件
set 列1=值1, 列2=值2, ...
where 筛选条件;

3.2.2 使用

# 1 【单表修改】
update female set sex='男'
where name like '王%';

# 2 【多表修改】
update male m
inner join female f
on m.gril_id = f.id
set f.sex = '男'
where m.name = '王二麻子';

3.3 删除

3.3.1 语法

/* 删除 */
# 1 单表删除
delete from 表名 where 筛选条件;

# 2 多表删除
# 2.1 sql92
delete 别名1, 别名2
from 表1 别名1, 表2 别名2
where 连接条件
and 筛选条件;

# 2.2 sql99
delete 别名1, 别名2
from 表1 别名1
inner/left/right join 表2 别名2
on 连接条件
where 筛选条件;

# 3 清空表
truncate table 表名;
  • 使用 delete 删除
    • 自增长列的值不会重置
    • 返回受影响的行数
    • 可以回滚
  • 使用 truncate 删除
    • 自增长列的值重置为 1
    • 无返回值
    • 不能回滚

      3.3.2 使用

      ```sql

      1 【单表删除】

      delete from female where name like ‘%五’;

2 【多表删除】

delete m from male m inner join fmale f on m.gril_id = f.id where f.name = ‘张三’;

3 【清空表】不能加 where

truncate table male; ```