SQLBolt:https://sqlbolt.com/lesson/introduction
資料表(Table):每一行為唯一資料,每一列為不同屬性。
這次我們要來修改 & 刪除資料。
這兩個語法都要需要加上WHERE
的條件。
-- Update statement with values
UPDATE mytable
SET column = value_or_expr,
other_column = another_value_or_expr,
…
WHERE condition;
-- 1. The director for A Bug's Life is incorrect, it was actually directed by John Lasseter
UPDATE movies
SET director = "John Lasseter"
WHERE id = 2;
-- 2. The year that Toy Story 2 was released is incorrect, it was actually released in 1999
UPDATE movies
SET year = 1999
WHERE id = 3;
-- 3. Both the title and director for Toy Story 8 is incorrect! The title should be "Toy Story 3" and it was directed by Lee Unkrich
UPDATE movies
SET title = "Toy Story 3", director = "Lee Unkrich"
WHERE id = 11;
-- Delete statement with condition
DELETE FROM mytable
WHERE condition;
-- 1. This database is getting too big, lets remove all movies that were released before 2005.
DELETE FROM movies
where year < 2005;
-- 2. Andrew Stanton has also left the studio, so please remove all movies directed by him.
DELETE FROM movies
where director = "Andrew Stanton";
今天講解完資料操作的部分,明天就要開始介紹如何對資料表進行 新增
、修改
、刪除
啦。
參考資料: