In this project, you will learn how to create a view based on the teaches table and delete the related data from the view.
MySQL [edusys]> SELECT * FROM teaches_view; +-------+----------+----------+------+ | id | courseId | semester | year | +-------+----------+----------+------+ | 76766 | BIO-101 | Summer | 2017 | | 10101 | CS-101 | Fall | 2017 | | 83821 | CS-190 | Spring | 2017 | | 83821 | CS-190 | Spring | 2017 | | 10101 | CS-347 | Fall | 2017 | | 98345 | EE-181 | Spring | 2017 | | 22222 | PHY-101 | Fall | 2017 | +-------+----------+----------+------+ 7 rows in set (0.000 sec)
In this project, you will learn:
After completing this project, you will be able to:
In this step, you will learn how to create a view based on the teaches table. Follow the steps below to complete this step:
sudo /etc/init.d/mysql start
mysql -uroot
SOURCE ~/project/edusys.sql;
| teaches | teaches_view |
| --------- | ------------ |
| ID | id |
| course_id | courseId |
| semester | semester |
| year | year |
The code should look like the following:
CREATE VIEW teaches_view AS SELECT ID AS id, course_id AS courseId, semester, year FROM teaches;
In this step, you will learn how to delete the data from the teaches_view view where the year is 2018. Follow the steps below to complete this step:
DELETE FROM teaches_view WHERE year = 2018;
CREATE VIEW teaches_view AS SELECT ID AS id, course_id AS courseId, semester, year FROM teaches; DELETE FROM teaches_view WHERE year = 2018;
Now, you have completed the project of creating a view and deleting the related data in the view. You can run the teachesDump.sql script in the MySQL terminal to execute the SQL statements.
SOURCE ~/project/teachesDump.sql;
After executing the SQL statements, you can view the data in the teaches_view view as follows:
MySQL [edusys]> SELECT * FROM teaches_view; +-------+----------+----------+------+ | id | courseId | semester | year | +-------+----------+----------+------+ | 76766 | BIO-101 | Summer | 2017 | | 10101 | CS-101 | Fall | 2017 | | 83821 | CS-190 | Spring | 2017 | | 83821 | CS-190 | Spring | 2017 | | 10101 | CS-347 | Fall | 2017 | | 98345 | EE-181 | Spring | 2017 | | 22222 | PHY-101 | Fall | 2017 | +-------+----------+----------+------+ 7 rows in set (0.000 sec)
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.
? Practice Now: Delete Course Schedule View Data
以上是MySQL View Creation | Data Manipulation | Database Management的详细内容。更多信息请关注PHP中文网其他相关文章!