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.

뷰란 무엇입니까? MySQL?
VIEWS 자신의 데이터를 저장하지 않고 다른 테이블에 저장된 데이터를 표시하는 가상 테이블입니다. 즉, VIEWS는 SQL 쿼리일 뿐입니다. 뷰에는 테이블의 행 전체 또는 일부가 포함될 수 있습니다. ㅏ MySQL 뷰는 하나의 테이블 또는 여러 테이블의 데이터를 표시할 수 있습니다.
MySQL 뷰 구문
이제 뷰를 생성하는 데 사용되는 기본 구문을 살펴보겠습니다. MySQL.
CREATE VIEW `view_name` AS SELECT statement;
Neocity
- “CREATE VIEW `view_name`" 말하다 MySQL 'view_name'이라는 데이터베이스에 뷰 객체를 생성하는 서버
- "AS SELECT 문" 은(는) 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를 사용하는 단일 쿼리 줄로 변환됩니다. 이러한 압축된 코드는 애플리케이션에 통합하기가 더 쉽습니다. 이렇게 하면 오타가 발생할 가능성이 없어지고 코드가 더 읽기 쉬워집니다.
- 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에 대해 워크벤치를 사용하고 데이터베이스 탐색기에서 뷰 노드를 확장하면 다음과 같은 결과를 얻을 수 있습니다.
이제 account_v_members 개체가 데이터베이스 보기 개체에 표시됩니다.
단계 3) SELECT 문 실행
이제 다음을 실행해 보겠습니다. SELECT 문 아래 표시된 대로 뷰에서 모든 필드를 선택합니다. MySQL 보기 예제를 만듭니다.
SELECT * FROM `accounts_v_members`;
단계 4) 스크립트 실행
위 스크립트를 실행하면 MySQL myflixdb에 대한 Workbench를 실행한 결과는 아래와 같습니다.
| 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.
이제 여러 테이블과 사용이 포함된 상당히 복잡한 예를 살펴보겠습니다. 조인.
멤버, 영화, 영화 대여라는 3개의 테이블에서 정보를 가져오는 생성된 JOIN을 패키지화합니다. 다음은 이를 달성하는 데 도움이 되는 스크립트입니다.
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 위에서 실행되는 애플리케이션 시스템의 어느 곳에서나 호출할 수 있습니다.
드롭ping 의 조회수 MySQL
DROP 명령을 사용하여 뷰에서 뷰를 삭제할 수 있습니다. 데이터베이스 그것은 더 이상 필요하지 않습니다. 뷰를 삭제하는 기본 구문은 다음과 같습니다.
DROP VIEW `general_v_movie_rentals`;
드롭ping a view removes only the stored query; the underlying tables and their data are untouched.

