MySQL Views: How to Create View from Tables

โšก ๆ™บ่ƒฝๆ‘˜่ฆ

MySQL Views are virtual tables that store no data of their own and instead present the result of a stored SELECT query. This resource explains how to create, query, join, and drop views, with runnable examples from the myflixdb database.

  • ๐Ÿงฑ ๆ ธๅฟƒๅฎšไน‰๏ผš A view is a named SELECT statement that behaves like a table but holds no data.
  • ๐Ÿ” ๅฎ‰ๅ…จๆ•ˆ็›Š๏ผš Views expose only the authorized columns and keep sensitive fields hidden from users.
  • โ™ป๏ธ Reusability Benefit: A complex multi-table JOIN is written once and reused as a single-line SELECT.
  • ๐Ÿ› ๏ธ Creation Syntax: CREATE VIEW `view_name` AS SELECT statement defines the view inside the database.
  • ๐Ÿ—‘๏ธ Lifecycle Commands: SHOW CREATE VIEW reveals the stored query, and DROP VIEW deletes it without touching the base tables.

MySQL ่ง‚็œ‹ๆ•ฐ

ไป€ไนˆๆ˜ฏ่ง†ๅ›พ MySQL?

ๆ™ฏ่ง‚ ๆ˜ฏ่™šๆ‹Ÿ่กจ๏ผŒไธๅญ˜ๅ‚จไปปไฝ•่‡ชๅทฑ็š„ๆ•ฐๆฎ๏ผŒไฝ†ๆ˜พ็คบๅญ˜ๅ‚จๅœจๅ…ถไป–่กจไธญ็š„ๆ•ฐๆฎใ€‚ๆขๅฅ่ฏ่ฏด๏ผŒ่ง†ๅ›พๅชไธ่ฟ‡ๆ˜ฏ SQL ๆŸฅ่ฏขใ€‚่ง†ๅ›พๅฏไปฅๅŒ…ๅซ่กจไธญ็š„ๆ‰€ๆœ‰่กŒๆˆ–ๅ‡ ่กŒใ€‚ MySQL ่ง†ๅ›พๅฏไปฅๆ˜พ็คบไธ€ไธชๆˆ–ๅคšไธช่กจ็š„ๆ•ฐๆฎใ€‚

MySQL ่ง†ๅ›พ่ฏญๆณ•

็Žฐๅœจ่ฎฉๆˆ‘ไปฌ็œ‹ไธ€ไธ‹ๅœจ MySQL.

CREATE VIEW `view_name` AS SELECT statement;

NeoCity

  • โ€œๅˆ›ๅปบ่ง†ๅ›พ`view_name`โ€ ๅ‘Š่ฏ‰ MySQL ๆœๅŠกๅ™จๅœจๆ•ฐๆฎๅบ“ไธญๅˆ›ๅปบไธ€ไธชๅไธบโ€œview_nameโ€็š„่ง†ๅ›พๅฏน่ฑก
  • โ€œAS SELECT ่ฏญๅฅโ€ ๆ˜ฏ่ฆๆ‰“ๅŒ…็š„ SQL ่ฏญๅฅ MySQL ่ง†ๅ›พใ€‚ๅฎƒๅฏไปฅๆ˜ฏไธ€ๆก SELECT ่ฏญๅฅ๏ผŒๅฏไปฅๅŒ…ๅซๆฅ่‡ชไธ€ไธชๆˆ–ๅคšไธช่กจ็š„ๆ•ฐๆฎใ€‚

ไธบไป€ไนˆ่ฆไฝฟ็”จ่ง†ๅ›พ๏ผŸ

Before building one, it helps to know what a view actually buys you.

ๆ‚จๅฏ่ƒฝๅธŒๆœ›ไฝฟ็”จ่ง†ๅ›พไธป่ฆๅ‡บไบŽไปฅไธ‹ 3 ไธชๅŽŸๅ› 

  • Ultimately, you will use your SQL knowledge, to create applications, which will use a database for data requirements. Itโ€™s recommended that you use VIEWS of the original table structure in your application instead of using the tables themselves. This ensures that when you refactor your DB, your legacy code will see the original schema via the view without breaking the application.
  • VIEWS ๆ้ซ˜ไบ†ๅฏ้‡็”จๆ€งใ€‚ๆ‚จไธๅฟ…้‡ๅคๅˆ›ๅปบๆถ‰ๅŠ่ฟžๆŽฅ็š„ๅคๆ‚ๆŸฅ่ฏขใ€‚ๆ‰€ๆœ‰ๅคๆ‚ๆ€ง้ƒฝ่ฝฌๆขไธบไฝฟ็”จ VIEWS ็š„ไธ€่กŒๆŸฅ่ฏขใ€‚่ฟ™็ง็ฒพ็ฎ€็š„ไปฃ็ ๅฐ†ๆ›ดๅฎนๆ˜“้›†ๆˆๅˆฐๆ‚จ็š„ๅบ”็”จ็จ‹ๅบไธญใ€‚่ฟ™ๅฐ†ๆถˆ้™คๆ‹ผๅ†™้”™่ฏฏ็š„ๆœบไผš๏ผŒๅนถไธ”ๆ‚จ็š„ไปฃ็ ๅฐ†ๆ›ดๅ…ทๅฏ่ฏปๆ€งใ€‚
  • ่ง†ๅ›พๆœ‰ๅŠฉไบŽๆ•ฐๆฎๅฎ‰ๅ…จใ€‚ๆ‚จๅฏไปฅไฝฟ็”จ่ง†ๅ›พไป…ๅ‘็”จๆˆทๆ˜พ็คบๆŽˆๆƒไฟกๆฏๅนถ้š่—ไฟก็”จๅกๅท็ญ‰ๆ•ๆ„Ÿๆ•ฐๆฎใ€‚

ๅฆ‚ไฝ•ๅœจ MySQL

With the syntax and the benefits established, the steps below build a working view end to end.

ไปฅไธ‹ๆ˜ฏๅˆ›ๅปบ่ง†ๅ›พ็š„ๅˆ†ๆญฅ่ฟ‡็จ‹ MySQL:

ๆญฅ้ชค1๏ผ‰ ไฝฟ็”จโ€œmyflixdbโ€ๅˆ›ๅปบๆˆ‘ไปฌ็š„็ฌฌไธ€ไธช่ง†ๅ›พ

็Žฐๅœจ่ฎฉๆˆ‘ไปฌไฝฟ็”จโ€œmyflixdbโ€ๅˆ›ๅปบๆˆ‘ไปฌ็š„็ฌฌไธ€ไธช่ง†ๅ›พ๏ผŒๆˆ‘ไปฌๅฐ†ๅˆ›ๅปบไธ€ไธช็ฎ€ๅ•็š„่ง†ๅ›พๆฅ้™ๅˆถๆˆๅ‘˜่กจไธญ็œ‹ๅˆฐ็š„ๅˆ—ใ€‚

Suppose authorization requirements state that the accounts department can only see memberโ€™s number, name and gender from the memberโ€™s table. To achieve this you can create a VIEW โ€“

CREATE VIEW `accounts_v_members` AS SELECT `membership_number`,`full_names`,`gender` FROM `members`;

ๆญฅ้ชค2๏ผ‰ ๅฑ•ๅผ€่ง†ๅ›พ่Š‚็‚น

ๅœจไปฅไธ‹ไฝ็ฝฎๆ‰ง่กŒไธŠ่ฟฐ่„šๆœฌ MySQL ้’ˆๅฏน myflixdb ็š„ๅทฅไฝœๅฐๅนถๅฑ•ๅผ€ๆ•ฐๆฎๅบ“่ต„ๆบ็ฎก็†ๅ™จไธญ็š„่ง†ๅ›พ่Š‚็‚น๏ผŒๆˆ‘ไปฌๅพ—ๅˆฐไปฅไธ‹็ป“ๆžœใ€‚

ๅœจไปฅไธ‹ไฝ็ฝฎๅˆ›ๅปบ่ง†ๅ›พ MySQL

่ฏทๆณจๆ„๏ผŒaccounts_v_members ๅฏน่ฑก็Žฐๅœจๅœจๆ•ฐๆฎๅบ“่ง†ๅ›พๅฏน่ฑกไธญๅฏ่งใ€‚

ๆญฅ้ชค3๏ผ‰ ๆ‰ง่กŒ SELECT ่ฏญๅฅ

็Žฐๅœจ่ฎฉๆˆ‘ไปฌๆ‰ง่กŒไธ€ไธช ้€‰ๆ‹ฉ่ฏญๅฅ ไปŽ่ง†ๅ›พไธญ้€‰ๆ‹ฉๆ‰€ๆœ‰ๅญ—ๆฎต๏ผŒๅฆ‚ไธ‹ๆ‰€็คบ MySQL ๅˆ›ๅปบ่ง†ๅ›พ็คบไพ‹ใ€‚

SELECT * FROM `accounts_v_members`;

ๆญฅ้ชค4๏ผ‰ ๆ‰ง่กŒ่„šๆœฌ

ๅœจไปฅไธ‹ไฝ็ฝฎๆ‰ง่กŒไธŠ่ฟฐ่„šๆœฌ MySQL ้’ˆๅฏน myflixdb ็š„ๅทฅไฝœๅฐไธบๆˆ‘ไปฌๆไพ›ไบ†ไปฅไธ‹ๆ˜พ็คบ็š„็ป“ๆžœใ€‚

membership_number full_names gender
1 Janet Jones Female
2 Janet Smith Jones Female
3 Robert Phil Male
4 Gloria Williams Female
5 Leonard Hofstadter Male
6 Sheldon Cooper Male
7 Rajesh Koothrappali Male
8 Leslie Winkle Male
9 Howard Wolowitz Male

Only the authorized columns for accounts department have been returned. Other details found in the members table have been hidden.

ๅฆ‚ๆžœๆˆ‘ไปฌๆƒณๆŸฅ็œ‹็ป„ๆˆ็‰นๅฎš่ง†ๅ›พ็š„ SQL ่ฏญๅฅ๏ผŒๆˆ‘ไปฌๅฏไปฅไฝฟ็”จไธ‹้ขๆ˜พ็คบ็š„่„šๆœฌๆฅๆ‰ง่กŒๆญคๆ“ไฝœใ€‚

SHOW CREATE VIEW `accounts_v_members`;

ๆ‰ง่กŒไธŠ่ฟฐ่„šๆœฌๅฐ†ไธบๆ‚จๆไพ›่ง†ๅ›พๅ็งฐๅ’Œ็”จไบŽๅˆ›ๅปบ่ง†ๅ›พ็š„ SQL SELECT ่ฏญๅฅใ€‚

่ฟžๆŽฅๅ’Œ่ง†ๅ›พ MySQL

A single-table view is the simplest case. Views become far more valuable when they wrap a multi-table query.

็Žฐๅœจ่ฎฉๆˆ‘ไปฌ็œ‹ไธ€ไธช็›ธๅฝ“ๅคๆ‚็š„ไพ‹ๅญ๏ผŒๅฎƒๆถ‰ๅŠๅคšไธช่กจๅ’Œ็”จ้€” ๅŠ ๅ…ฅ.

ๆˆ‘ไปฌๅฐ†ๆ‰“ๅŒ…ๅˆ›ๅปบ็š„ JOIN๏ผŒ่ฏฅ JOIN ไปŽไธ‰ (3) ไธช่กจ๏ผˆๅณไผšๅ‘˜ใ€็”ตๅฝฑๅ’Œ็”ตๅฝฑ็งŸ่ต๏ผ‰ไธญ่Žทๅ–ไฟกๆฏใ€‚ไปฅไธ‹ๆ˜ฏๅธฎๅŠฉๆˆ‘ไปฌๅฎž็Žฐๆญค็›ฎ็š„็š„่„šๆœฌใ€‚

CREATE VIEW `general_v_movie_rentals` AS SELECT mb.`membership_number`,mb.`full_names`,mo.`title`,mr.`transaction_date`,mr.`return_date`
FROM `movierentals` AS mr
INNER JOIN `members` AS mb ON mr.`membership_number` = mb.`membership_number`
INNER JOIN `movies` AS mo ON mr.`movie_id` = mo.`movie_id`;

ๆ‰ง่กŒไธŠ่ฟฐ่„šๆœฌไผšๅœจๆˆ‘ไปฌ็š„ myflixdb ไธญๅˆ›ๅปบๅไธบ general_v_movie_rentals ็š„่ง†ๅ›พ

็Žฐๅœจ่ฎฉๆˆ‘ไปฌไปŽๅไธบ general_v_movie_rentals ็š„่กจไธญ้€‰ๆ‹ฉๆ‰€ๆœ‰ๅญ—ๆฎตใ€‚

SELECT * FROM `general_v_movie_rentals`;

ๅœจไปฅไธ‹ไฝ็ฝฎๆ‰ง่กŒไธŠ่ฟฐ่„šๆœฌ MySQL ๅทฅไฝœๅฐ ้’ˆๅฏน myflixdb ๆˆ‘ไปฌๅพ—ๅˆฐไบ†ไปฅไธ‹ๆ˜พ็คบ็š„็ป“ๆžœใ€‚

membership_number full_names title transaction_date return_date
1 Janet Jones Pirates of the Caribean 4 20-06-2012 28-06-2012
1 Janet Jones Forgetting Sarah Marshal 22-06-2012 25-06-2012
3 Robert Phil Forgetting Sarah Marshal 22-06-2012 25-06-2012
2 Janet Smith Jones Forgetting Sarah Marshal 21-06-2012 24-06-2012
3 Robert Phil X-Men 23-06-2012 28-06-2012

่ฏทๆณจๆ„๏ผŒๆˆ‘ไปฌไธๅฟ…็ผ–ๅ†™ๅคๆ‚็š„ JOIN ๆŸฅ่ฏขๆฅ่Žทๅ–ๆœ‰ๅ…ณไผšๅ‘˜ใ€็”ตๅฝฑๅ’Œ็”ตๅฝฑ็งŸ่ต่ฏฆ็ป†ไฟกๆฏ็š„ไฟกๆฏใ€‚ๆˆ‘ไปฌๅช้œ€ๅœจๅธธ่ง„ SELECT ่ฏญๅฅไธญไฝฟ็”จ่ฏฅ่ง†ๅ›พ๏ผŒๅฐฑๅƒไปปไฝ•ๅ…ถไป–ๆ™ฎ้€š่กจไธ€ๆ ทใ€‚ๅฏไปฅไปŽๅœจ myflixdb ไธŠ่ฟ่กŒ็š„ๅบ”็”จ็จ‹ๅบ็ณป็ปŸไธญ็š„ไปปไฝ•ไฝ็ฝฎ่ฐƒ็”จ่ฏฅ่ง†ๅ›พใ€‚

Dropping ๆต่งˆๆฌกๆ•ฐ MySQL

DROP ๅ‘ฝไปคๅฏ็”จไบŽไปŽไธญๅˆ ้™ค่ง†ๅ›พ ๆ•ฐๆฎๅบ“ ไธๅ†้œ€่ฆใ€‚ๅˆ ้™ค่ง†ๅ›พ็š„ๅŸบๆœฌ่ฏญๆณ•ๅฆ‚ไธ‹ใ€‚

DROP VIEW `general_v_movie_rentals`;

Dropping a view removes only the stored query; the underlying tables and their data are untouched.

ๅธธ่ง้—ฎ้ข˜

Sometimes. INSERT, UPDATE and DELETE work only on simple views containing every NOT NULL column of the base table. Views built on joins or aggregate functions are not updatable.

A table physically stores rows on disk. A view stores only a SELECT statement and rebuilds its rows on every query, so it always reflects current data in the base tables.

No. A view runs its underlying SELECT on every call, so performance matches the original query. MySQL has no materialized views, and speed still depends on base-table indexes.

Yes. AI assistants in tools such as MySQL ๅทฅไฝœๅฐ can draft a CREATE VIEW statement from a description. Verify the column list and join keys before running it.

Partly. AI can flag sensitive columns, such as card numbers, and propose a safer column list. The final decision must still follow your organisationโ€™s data access policy.

ๆ€ป็ป“ไธ€ไธ‹่ฟ™็ฏ‡ๆ–‡็ซ ๏ผš