MySQL View Creation | Data Manipulation | Database Management
Introduction
In this project, you will learn how to create a view based on the teaches table and delete the related data from the view.
? Preview
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)
? Tasks
In this project, you will learn:
- How to create a view with specific fields from the teaches table
- How to delete data from the created view based on a certain condition
? Achievements
After completing this project, you will be able to:
- Understand the concept of a view and how to create one
- Manipulate data in a view by deleting records that match a specific condition
- Apply these skills to manage data in a database more efficiently
Create a View
In this step, you will learn how to create a view based on the teaches table. Follow the steps below to complete this step:
- Open a terminal and start the MySQL service:
sudo /etc/init.d/mysql start
- Log into the MySQL terminal.
mysql -uroot
- Import the edusys.sql data into the MySQL database.
SOURCE ~/project/edusys.sql;
- Create a new file named teachesDump.sql in the ~/project directory.
- Add code to the file to create a view named teaches_view that contains the following fields:
| 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;
Delete View Data
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:
- Open the teachesDump.sql file and add code to the file to delete the data from the teaches_view view where the year is 2018.
DELETE FROM teaches_view WHERE year = 2018;
- The final code is as follows:
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)
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.
? Practice Now: Delete Course Schedule View Data
Want to Learn More?
- ? Learn the latest MySQL Skill Trees
- ? Read More MySQL Tutorials
- ? Join our Discord or tweet us @WeAreLabEx
以上是MySQL View Creation | Data Manipulation | Database Management的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

MySQL在Web应用中的主要作用是存储和管理数据。1.MySQL高效处理用户信息、产品目录和交易记录等数据。2.通过SQL查询,开发者能从数据库提取信息生成动态内容。3.MySQL基于客户端-服务器模型工作,确保查询速度可接受。

InnoDB使用redologs和undologs确保数据一致性和可靠性。1.redologs记录数据页修改,确保崩溃恢复和事务持久性。2.undologs记录数据原始值,支持事务回滚和MVCC。

MySQL在数据库和编程中的地位非常重要,它是一个开源的关系型数据库管理系统,广泛应用于各种应用场景。1)MySQL提供高效的数据存储、组织和检索功能,支持Web、移动和企业级系统。2)它使用客户端-服务器架构,支持多种存储引擎和索引优化。3)基本用法包括创建表和插入数据,高级用法涉及多表JOIN和复杂查询。4)常见问题如SQL语法错误和性能问题可以通过EXPLAIN命令和慢查询日志调试。5)性能优化方法包括合理使用索引、优化查询和使用缓存,最佳实践包括使用事务和PreparedStatemen

MySQL与其他编程语言相比,主要用于存储和管理数据,而其他语言如Python、Java、C 则用于逻辑处理和应用开发。 MySQL以其高性能、可扩展性和跨平台支持着称,适合数据管理需求,而其他语言在各自领域如数据分析、企业应用和系统编程中各有优势。

MySQL适合小型和大型企业。1)小型企业可使用MySQL进行基本数据管理,如存储客户信息。2)大型企业可利用MySQL处理海量数据和复杂业务逻辑,优化查询性能和事务处理。

MySQL索引基数对查询性能有显着影响:1.高基数索引能更有效地缩小数据范围,提高查询效率;2.低基数索引可能导致全表扫描,降低查询性能;3.在联合索引中,应将高基数列放在前面以优化查询。

MySQL的基本操作包括创建数据库、表格,及使用SQL进行数据的CRUD操作。1.创建数据库:CREATEDATABASEmy_first_db;2.创建表格:CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY,titleVARCHAR(100)NOTNULL,authorVARCHAR(100)NOTNULL,published_yearINT);3.插入数据:INSERTINTObooks(title,author,published_year)VA

MySQL适合Web应用和内容管理系统,因其开源、高性能和易用性而受欢迎。1)与PostgreSQL相比,MySQL在简单查询和高并发读操作上表现更好。2)相较Oracle,MySQL因开源和低成本更受中小企业青睐。3)对比MicrosoftSQLServer,MySQL更适合跨平台应用。4)与MongoDB不同,MySQL更适用于结构化数据和事务处理。
