INSERT INTO TABLE(col1, col2)VALUES(val1, val2)
案例1:单行插入
INSERT INTO users(username, age, sex)
VALUES('张三', 23, NULL)
-- 或者 不指定列(列顺序需要和数据一致)
INSERT INTO users
VALUES(1, '张三', 23, NULL)
案例2:多行插入
-- 方式1
INSERT INTO users(username, age, sex)
VALUES('张三1', 23, NULL),
('张三2', 22, NULL),
('张三3', 25, NULL)
-- 方式2
INSERT INTO users(username, age, sex)
SELECT '张三1', 23, NULL UNION
SELECT '张三2', 23, NULL UNION
SELECT '张三3', 23, NULL
INSERT INTO users SET username='李四', age=20
案例1:子查询 插入(可批量操作)
INSERT INTO users(username, age, sex)
SELECT username, age, sex FROM users_2 WHERE id IN (12,3,4,5,3,6)
案例1:修改多行或者单行的值
UPDATE users SET username='赵六', age=40
WHERE username='赵六1'
案例1:修改id=1的员工的部门名称
UPDATE users
JOIN deps ON deps.id = users.dep_id
SET deps.name='IT部门'
delete 删除后,自增列的值,从断点开始增加
案例1:单表删除
DELETE FROM users WHERE id IN (1,2)
案例2:多表删除
-- 删除it部门下的所有员工
DELETE FROM users
JOIN deps ON deps.id=users.dep_id
WHERE deps.name='it部'
-- 需要删除 it部门下所有员工和it部门
DELETE FROM users, deps
JOIN deps ON deps.id=users.dep_id
WHERE deps.name='it部'
TRUNCATE 删除后,自增列的值,从起始值开始
TRUNCATE TABLE users