Introduction
In this project, you will learn how to create a student view in a MySQL database and update the data in the view.
👀 Preview
MySQL [edusys]> SELECT * FROM student WHERE ID = '70557';
+-------+---------+------+
| id | name | cred |
+-------+---------+------+
| 70557 | History | 0 |
+-------+---------+------+
1 row in set (0.001 sec)
🎯 Tasks
In this project, you will learn:
- How to create a view based on an existing table
- How to update the data in a view
🏆 Achievements
After completing this project, you will be able to:
- Understand the concept of a database view and how to create one
- Modify the data in a view using SQL update statements
- Apply these skills to manage and maintain data in a database
Creating the Student View
In this step, you will learn how to create a student view based on the student table.
Start MySQL:
sudo /etc/init.d/mysql startLog into the MySQL terminal:
mysql -urootImport the data from the
edusys.sqlscript into MySQL:SOURCE ~/project/edusys.sqlCreate a new file named
updateView.sqlin the~/projectdirectory.Add code to the file to create a view named
teaches_viewthat contains the following fields:student student_view ID id name name tot_cred cred The code should look like the following:
USE edusys CREATE VIEW IF NOT EXISTS student_view AS SELECT ID AS id, name AS name, tot_cred AS cred FROM student;This creates a view named
student_viewthat selects theID,name, andtot_credcolumns from thestudenttable and renames them toid,name, andcredrespectively.
Updating the Student View
In this step, you will learn how to update the data in the student_view view.
Open the
updateView.sqlfile and add code to the file to update thenamefield with the value'History'for the entry withid'70557'in thestudent_viewview:UPDATE student_view SET name = 'History' WHERE id = '70557';This updates the
namefield to'History'for the row in thestudent_viewview where theidis'70557'.The final code is as follows:
USE edusys -- Create a student_view table with the corresponding fields as shown in the table CREATE VIEW IF NOT EXISTS student_view AS SELECT ID AS id, name AS name, tot_cred AS cred FROM student; -- Update the name field with the value History for the entry with id 70557 in the student_view view UPDATE student_view SET name = 'History' WHERE id = '70557';Run the SQL script in the MySQL terminal.
SOURCE ~/project/updateView.sqlVerify the update by selecting the data from the
studenttable where theIDis'70557':SELECT * FROM student WHERE ID = '70557';The output should show the updated
namefield:+-------+---------+------+ | id | name | cred | +-------+---------+------+ | 70557 | History | 0 | +-------+---------+------+ 1 row in set (0.001 sec)
Congratulations! You have successfully created a student view and updated the data in the view.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



