MySQL Views: How to Create View from Tables

โšก Tรณm tแบฏt thรดng minh

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.

  • ๐Ÿงฑ ฤแป‹nh nghฤฉa cแป‘t lรตi: A view is a named SELECT statement that behaves like a table but holds no data.
  • ๐Ÿ” Lแปฃi รญch an ninh: 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 Cแบฃnh Quan

Lฦฐแปฃt xem trong MySQL?

LฦฏแปขT XEM lร  cรกc bแบฃng แบฃo khรดng lฦฐu trแปฏ bแบฅt kแปณ dแปฏ liแป‡u nร o cแปงa riรชng mรฌnh mร  hiแปƒn thแป‹ dแปฏ liแป‡u ฤ‘ฦฐแปฃc lฦฐu trแปฏ trong cรกc bแบฃng khรกc. Nรณi cรกch khรกc, XEM khรดng lร  gรฌ ngoร i Truy vแบฅn SQL. Mแป™t khung nhรฌn cรณ thแปƒ chแปฉa tแบฅt cแบฃ hoแบทc mแป™t vร i hร ng cแปงa mแป™t bแบฃng. Mแป˜T MySQL view cรณ thแปƒ hiแปƒn thแป‹ dแปฏ liแป‡u tแปซ mแป™t bแบฃng hoแบทc nhiแปu bแบฃng.

MySQL Cรบ phรกp lฦฐแปฃt xem

Bรขy giแป chรบng ta hรฃy xem cรบ phรกp cฦก bแบฃn ฤ‘ฦฐแปฃc sแปญ dแปฅng ฤ‘แปƒ tแบกo chแบฟ ฤ‘แป™ xem trong MySQL.

CREATE VIEW `view_name` AS SELECT statement;

แปž ฤร‚U

  • โ€œTแบ O CHแบพ ฤแป˜ XEM `view_name`โ€ nรณi MySQL mรกy chแปง ฤ‘แปƒ tแบกo mแป™t ฤ‘แป‘i tฦฐแปฃng xem trong cฦก sแปŸ dแปฏ liแป‡u cรณ tรชn `view_name`
  • โ€œNHฦฏ cรขu lแป‡nh CHแปŒNโ€ cรกc cรขu lแป‡nh SQL cรณ ฤ‘ฦฐแปฃc ฤ‘รณng gรณi trong MySQL Lฦฐแปฃt xem. Nรณ cรณ thแปƒ lร  cรขu lแป‡nh SELECT cรณ thแปƒ chแปฉa dแปฏ liแป‡u tแปซ mแป™t bแบฃng hoแบทc nhiแปu bแบฃng.

Tแบกi sao nรชn sแปญ dแปฅng lฦฐแปฃt xem?

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

Bแบกn cรณ thแปƒ muแป‘n sแปญ dแปฅng chแบฟ ฤ‘แป™ xem chแปง yแบฟu vรฌ 3 lรฝ do sau

  • 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 tฤƒng khแบฃ nฤƒng tรกi sแปญ dแปฅng. Bแบกn sแบฝ khรดng phแบฃi tแบกo cรกc truy vแบฅn phแปฉc tแบกp liรชn quan ฤ‘แบฟn cรกc phรฉp nแป‘i nhiแปu lแบงn. Tแบฅt cแบฃ sแปฑ phแปฉc tแบกp ฤ‘ฦฐแปฃc chuyแปƒn ฤ‘แป•i thร nh mแป™t dรฒng truy vแบฅn duy nhแบฅt bแบฑng cรกch sแปญ dแปฅng VIEWS. Mรฃ cรด ฤ‘แปng nhฦฐ vแบญy sแบฝ dแป… tรญch hแปฃp hฦกn vร o แปฉng dแปฅng cแปงa bแบกn. ฤiแปu nร y sแบฝ loแบกi bแป khแบฃ nฤƒng xแบฃy ra lแป—i ฤ‘รกnh mรกy vร  mรฃ cแปงa bแบกn sแบฝ dแป… ฤ‘แปc hฦกn.
  • VIEWS giรบp bแบฃo mแบญt dแปฏ liแป‡u. Bแบกn cรณ thแปƒ sแปญ dแปฅng chแบฟ ฤ‘แป™ xem ฤ‘แปƒ chแป‰ hiแปƒn thแป‹ thรดng tin ฤ‘ฦฐแปฃc แปงy quyแปn cho ngฦฐแปi dรนng vร  แบฉn dแปฏ liแป‡u nhแบกy cแบฃm nhฦฐ sแป‘ thแบป tรญn dแปฅng.

Cรกch tแบกo chแบฟ ฤ‘แป™ xem trong MySQL

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

Sau ฤ‘รขy lร  quy trรฌnh tแปซng bฦฐแป›c ฤ‘แปƒ tแบกo chแบฟ ฤ‘แป™ xem trong MySQL:

Bฦฐแป›c 1) Tแบกo chแบฟ ฤ‘แป™ xem ฤ‘แบงu tiรชn cแปงa chรบng tรดi bแบฑng cรกch sแปญ dแปฅng โ€œmyflixdbโ€

Bรขy giแป, hรฃy tแบกo chแบฟ ฤ‘แป™ xem ฤ‘แบงu tiรชn bแบฑng cรกch sแปญ dแปฅng โ€œmyflixdbโ€, chรบng tรดi sแบฝ tแบกo mแป™t chแบฟ ฤ‘แป™ xem ฤ‘ฦกn giแบฃn giแป›i hแบกn cรกc cแป™t ฤ‘ฦฐแปฃc nhรฌn thแบฅy trong bแบฃng thร nh viรชn.

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`;

Bฦฐแป›c 2) MแปŸ rแป™ng nรบt lฦฐแปฃt xem

Thแปฑc thi ฤ‘oแบกn script trรชn trong MySQL workbench so vแป›i myflixdb vร  mแปŸ rแป™ng nรบt chแบฟ ฤ‘แป™ xem trong trรฌnh khรกm phรก cฦก sแปŸ dแปฏ liแป‡u mang lแบกi cho chรบng ta kแบฟt quแบฃ sau.

Tแบกo chแบฟ ฤ‘แป™ xem trong MySQL

Lฦฐu รฝ ฤ‘แป‘i tฦฐแปฃng Account_v_members hiแป‡n hiแปƒn thแป‹ trong cรกc ฤ‘แป‘i tฦฐแปฃng khung nhรฌn cฦก sแปŸ dแปฏ liแป‡u.

Bฦฐแป›c 3) Thแปฑc thi cรขu lแป‡nh SELECT

Bรขy giแป chรบng ta hรฃy thแปฑc hiแป‡n mแป™t Cรขu lแป‡nh SELECT chแปn tแบฅt cแบฃ cรกc trฦฐแปng tแปซ chแบฟ ฤ‘แป™ xem nhฦฐ hiแปƒn thแป‹ bรชn dฦฐแป›i MySQL tแบกo vรญ dแปฅ xem.

SELECT * FROM `accounts_v_members`;

Bฦฐแป›c 4) Thแปฑc thi mแป™t kแป‹ch bแบฃn

Thแปฑc thi ฤ‘oแบกn script trรชn trong MySQL workbench so vแป›i myflixdb cung cแบฅp cho chรบng ta kแบฟt quแบฃ nhฦฐ hรฌnh dฦฐแป›i ฤ‘รขy.

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.

Nแบฟu muแป‘n xem cรกc cรขu lแป‡nh SQL tแบกo nรชn mแป™t chแบฟ ฤ‘แป™ xem cแปฅ thแปƒ, chรบng ta cรณ thแปƒ sแปญ dแปฅng tแบญp lแป‡nh hiแปƒn thแป‹ bรชn dฦฐแป›i ฤ‘แปƒ thแปฑc hiแป‡n ฤ‘iแปu ฤ‘รณ.

SHOW CREATE VIEW `accounts_v_members`;

Viแป‡c thแปฑc thi ฤ‘oแบกn mรฃ trรชn sแบฝ cung cแบฅp cho bแบกn tรชn dแบกng xem vร  cรกc cรขu lแป‡nh SQL SELECT ฤ‘ฦฐแปฃc sแปญ dแปฅng ฤ‘แปƒ tแบกo dแบกng xem.

Tham gia vร  xem trong MySQL

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

Bรขy giแป chรบng ta hรฃy xem mแป™t vรญ dแปฅ khรก phแปฉc tแบกp liรชn quan ฤ‘แบฟn nhiแปu bแบฃng vร  cรกch sแปญ dแปฅng tham gia.

Chรบng tรดi sแบฝ ฤ‘รณng gรณi THAM GIA ฤ‘ฦฐแปฃc tแบกo ฤ‘แปƒ lแบฅy thรดng tin tแปซ ba (3) bแบฃng lร  thร nh viรชn, phim vร  phim cho thuรช. Dฦฐแป›i ฤ‘รขy lร  kแป‹ch bแบฃn giรบp chรบng tรดi ฤ‘แบกt ฤ‘ฦฐแปฃc ฤ‘iแปu ฤ‘รณ.

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`;

Viแป‡c thแปฑc thi cรกc tแบญp lแป‡nh trรชn sแบฝ tแบกo ra chแบฟ ฤ‘แป™ xem cรณ tรชn General_v_movie_rentals trong myflixdb cแปงa chรบng tรดi

Bรขy giแป chรบng ta hรฃy chแปn tแบฅt cแบฃ cรกc trฦฐแปng tแปซ bแบฃng cรณ tรชn General_v_movie_rentals.

SELECT * FROM `general_v_movie_rentals`;

Thแปฑc thi ฤ‘oแบกn script trรชn trong MySQL bร n lร m viแป‡c so vแป›i myflixdb cung cแบฅp cho chรบng ta kแบฟt quแบฃ nhฦฐ bรชn dฦฐแป›i.

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

Lฦฐu รฝ rแบฑng chรบng ta khรดng cแบงn phแบฃi viแบฟt truy vแบฅn JOIN phแปฉc tแบกp ฤ‘แปƒ lแบฅy thรดng tin vแป thร nh viรชn, phim vร  chi tiแบฟt cho thuรช phim. Chรบng ta chแป‰ cแบงn sแปญ dแปฅng chแบฟ ฤ‘แป™ xem trong cรขu lแป‡nh SELECT thรดng thฦฐแปng nhฦฐ bแบฅt kแปณ bแบฃng thรดng thฦฐแปng nร o khรกc. Chแบฟ ฤ‘แป™ xem cรณ thแปƒ ฤ‘ฦฐแปฃc gแปi tแปซ bแบฅt kแปณ ฤ‘รขu trong hแป‡ thแป‘ng แปฉng dแปฅng chแบกy trรชn myflixdb.

Rฦกiping Lฦฐแปฃt xem trong MySQL

Lแป‡nh DROP cรณ thแปƒ ฤ‘ฦฐแปฃc sแปญ dแปฅng ฤ‘แปƒ xรณa mแป™t khung nhรฌn khแปi cฦก sแปŸ dแปฏ liแป‡u ฤ‘iแปu ฤ‘รณ khรดng cรฒn cแบงn thiแบฟt nแปฏa. Cรบ phรกp cฦก bแบฃn ฤ‘แปƒ loแบกi bแป chแบฟ ฤ‘แป™ xem nhฦฐ sau.

DROP VIEW `general_v_movie_rentals`;

Rฦกiping a view removes only the stored query; the underlying tables and their data are untouched.

Cรขu Hoฬ‰i Thฦฐฦกฬ€ng Gฤƒฬฃp

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 Workbench 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.

Tรณm tแบฏt bร i viแบฟt nร y vแป›i: