2021-07-27 SQL Day10

一.增删改

image.png
image.png

原表

image.png

1.数据插入——INSERT INTO

(1)在表中插入自己写的行
SELECT * FROM prod_info2;

INSERT INTO prod_info2
VALUES('T00001', '测试商品', 'test', 'test', 'test', 16, 20, 'NJ0001');

SELECT * FROM prod_info2 ORDER BY prod_id DESC;

INSERT INTO prod_info2 (prod_id, prod_name, brand, type)
VALUES('T00002', '测试商品', 'test', 'test');

INSERT INTO prod_info2 (prod_id, prod_name, type, brand)
VALUES('T00003', '测试商品', 'type', 'brand');

SELECT * FROM prod_info2 ORDER BY prod_id DESC;
image.png
(2)在表中插入另一个表中的一行
插入整行
INSERT INTO prod_info2
SELECT CONCAT('T', p.prod_id), p.prod_name, p.brand, p.type, p.class, p.cost, p.sale_price, p.supplier_id
FROM prod_info AS p
WHERE p.prod_id = '10001';
image.png
插入部分行
INSERT INTO prod_info2(prod_id, prod_name, brand, type)
SELECT CONCAT('T', p.prod_id), p.prod_name, p.brand, p.type
FROM prod_info AS p
WHERE p.prod_id = '10002';
image.png
(3)在表中插入了一个表中的多行
SELECT CONCAT('T', p.prod_id), p.prod_name, p.brand, p.type, p.class, p.cost, p.sale_price, p.supplier_id
FROM prod_info AS p
WHERE p.prod_id LIKE '2000_';

INSERT INTO prod_info2
SELECT CONCAT('T', p.prod_id), p.prod_name, p.brand, p.type, p.class, p.cost, p.sale_price, p.supplier_id
FROM prod_info AS p
WHERE p.prod_id LIKE '2000_';
image.png

2.数据更新——UPDATE

image.png
(1)无过滤修改列
UPDATE prod_info2
SET class = '日用品';
image.png
(2)有过滤条件修改列
UPDATE prod_info2
SET class = '零食'
WHERE prod_id LIKE 'T2%';
image.png
UPDATE prod_info2
SET class = '饮料'
WHERE prod_id LIKE '3%';
image.png
WHERE prod_info2
SET sale_price = sale_price * 0.9
WHERE prod_name = '抽纸' OR class = '饮料';
UPDAT prod_info2
SET sale_price = sale_price * 0.9, cost = cost * 0.9;
(3)通过关联方式对表进行更新
SELECT p2.*, p.*
FROM prod_info2 AS p2 INNER JOIN prod_info AS p
WHERE p2.prod_name = p.prod_name
  AND p2.brand = p.brand
    AND p2.type = p.type;
    

UPDATE prod_info2 AS p2
INNER JOIN prod_info AS p
SET p2.cost = p.sale_price
WHERE p2.prod_name = p.prod_name
  AND p2.brand = p.brand
    AND p2.type = p.type;
    
SELECT * FROM prod_info2 ORDER BY prod_id DESC;
image.png

3.数据删除——DELETE

image.png
(1)删除
SELECT * FROM prod_info2 AS p2 WHERE class = '测试商品';

DELETE FROM prod_info2 WHERE prod_name = '测试商品';
image.png

二.今日代码

SELECT * FROM prod_info2;

INSERT INTO prod_info2
VALUES('T00001', '测试商品', 'test', 'test', 'test', 16, 20, 'NJ0001');

SELECT * FROM prod_info2 ORDER BY prod_id DESC;

INSERT INTO prod_info2 (prod_id, prod_name, brand, type)
VALUES('T00002', '测试商品', 'test', 'test');

INSERT INTO prod_info2 (prod_id, prod_name, type, brand)
VALUES('T00003', '测试商品', 'type', 'brand');

SELECT * FROM prod_info2 ORDER BY prod_id DESC;



SELECT * FROM prod_info AS p WHERE p.prod_id = '10001';



INSERT INTO prod_info2
SELECT CONCAT('T', p.prod_id), p.prod_name, p.brand, p.type, p.class, p.cost, p.sale_price, p.supplier_id
FROM prod_info AS p
WHERE p.prod_id = '10001';

INSERT INTO prod_info2(prod_id, prod_name, brand, type)
SELECT CONCAT('T', p.prod_id), p.prod_name, p.brand, p.type
FROM prod_info AS p
WHERE p.prod_id = '10002';



SELECT CONCAT('T', p.prod_id), p.prod_name, p.brand, p.type, p.class, p.cost, p.sale_price, p.supplier_id
FROM prod_info AS p
WHERE p.prod_id LIKE '2000_';

INSERT INTO prod_info2
SELECT CONCAT('T', p.prod_id), p.prod_name, p.brand, p.type, p.class, p.cost, p.sale_price, p.supplier_id
FROM prod_info AS p
WHERE p.prod_id LIKE '2000_';



SELECT * FROM prod_info2 ORDER BY prod_id DESC;

UPDATE prod_info2
SET class = '日用品';

UPDATE prod_info2
SET class = '零食'
WHERE prod_id LIKE 'T2%';

UPDATE prod_info2
SET class = '饮料'
WHERE prod_id LIKE '3%';

WHERE prod_info2
SET sale_price = sale_price / 0.9
WHERE prod_name = '抽纸' OR class = '饮料';

UPDATE prod_info2
SET sale_price = sale_price * 0.9, cost = cost * 0.9;



SELECT p2.*, p.*
FROM prod_info2 AS p2 INNER JOIN prod_info AS p
WHERE p2.prod_name = p.prod_name
  AND p2.brand = p.brand
    AND p2.type = p.type;
    

UPDATE prod_info2 AS p2
INNER JOIN prod_info AS p
SET p2.cost = p.sale_price
WHERE p2.prod_name = p.prod_name
  AND p2.brand = p.brand
    AND p2.type = p.type;
    
SELECT * FROM prod_info2 ORDER BY prod_id DESC;




SELECT * FROM prod_info2 AS p2 WHERE class = '测试商品';

DELETE FROM prod_info2 WHERE prod_name = '测试商品';

三.课后作业

Q1:写出插入语句的两种语句结构,并分析其使用场景。
答:INSERET INTO<表名>VALUES(<插入值1>,<插入值2>,......)
第一种方法较为简单,仅适用于整行插入,且要求表结构固定。
INSERT INTO<表名>(<插入字段1>,<插入字段2>,......)VALUES(<插入值>,<插入值>,......)
第二种方法更为具体,既可以用于整行插入也可以用于部分行插入,且插入顺序可以进行自定义。并且,即使表后续结构发生修改,只要插入字段依然存在,则该语句依旧有效。

Q2:请写出更新表的语句结构(多列),以及更新操作的注意事项。
答:
UPDATE<表名>
SET<列名1> = 值1, <列名2> = 值2
WHERE<过滤条件>;
一定要注意过滤条件的检查。

Q3:分别写出以下语句。
1、向 milk_tea 表中插入一个完整的行:(9,测试商品 1,100g,2019-01-01,12,13,20)

INSERT INTO milk_tea
VALUES(9, '测试商品1', '100g', 20190101, 12, 13, 20);

2、向 milk_tea 表中插入以下数据:prod_id = 10,pro_date = “2018-01-01”,prod_name = “测试商品 2”

INSERT INTO milk_tea (prod_id, pro_date, prod_name)
VALUES (10, 20180101, '测试商品2');

3、从 milk_tea 表中找到‘棒棒糖’的行,修改 prod_id 为 11 后重新插入 milk_tea 表

UPDATE milk_tea
SET prod_id = 11
WHERE prod_name = '棒棒糖';

4、修改 prod_id 为 11 的行数据,将 prod_name 修改为“测试商品 3”

UPDATE milk_tea
SET prod_name = '测试商品3'
WHERE prod_id = 11;

5、删除 prod_id 为 9、10、11 的行。

DELETE
FROM milk_tea
WHERE prod_id IN (9, 10, 11);

你可能感兴趣的:(2021-07-27 SQL Day10)