USE testdb;-- create a new table named itemsCREATETABLEitems ( id INT AUTO_INCREMENT PRIMARY KEY,nameVARCHAR(100) NOT NULL, price DECIMAL(11 , 2 ) NOT NULL);-- insert data into the items tableINSERT INTO items(name,price) VALUES('Laptop',700.56),('Desktop',699.99),('iPad',700.50) ;-- create a view based on items tableCREATEVIEWLuxuryItemsASSELECT*FROM itemsWHERE price >700;-- query data from the LuxuryItems viewSELECT*FROM LuxuryItems;
执行上面查询语句后,得到以下结果 -
+----+--------+--------+| id | name | price |+----+--------+--------+| 1 | Laptop | 700.56 || 3 | iPad | 700.5 |+----+--------+--------+2rowsinset
其次,使用DELETE语句来删除id为3的行。
DELETEFROM LuxuryItems WHERE id =3;
MySQL返回一条消息,表示有1行受到影响。
Query OK, 1row affected
第三步,再次通过视图检查数据。
mysql>SELECT*FROM LuxuryItems;+----+--------+--------+| id | name | price |+----+--------+--------+| 1 | Laptop | 700.56 |+----+--------+--------+1rowinset
第四步,还可以从基表items查询数据,以验证DELETE语句是否实际删除了该行。
mysql>SELECT*FROM items;+----+---------+--------+| id | name | price |+----+---------+--------+| 1 | Laptop | 700.56 || 2 | Desktop | 699.99 |+----+---------+--------+2rowsinset