Lệnh nào sau đây có thể được sử dụng để xóa chế độ xem khỏi mysql?

Chế độ xem là một đối tượng dữ liệu không chứa bất kỳ dữ liệu nào. Nội dung của dạng xem là kết quả của một bảng cơ sở. Chúng được vận hành giống như bảng cơ sở nhưng chúng không chứa bất kỳ dữ liệu nào của riêng chúng. Sự khác biệt giữa dạng xem và bảng là dạng xem là các định nghĩa được xây dựng trên các bảng [hoặc dạng xem] khác. Nếu dữ liệu được thay đổi trong bảng bên dưới, thay đổi tương tự sẽ được phản ánh trong dạng xem. Một khung nhìn có thể được xây dựng trên một hoặc nhiều bảng

Phiên bản. mysql 5. 6

nội dung

  • Tại sao Lượt xem?
  • Làm cách nào để tạo chế độ xem MySQL?
  • Hạn chế về định nghĩa Chế độ xem
  • Công cụ để tạo Chế độ xem MySQL
  • Thay đổi chế độ xem MySQL
  • DROP chế độ xem MySQL
  • MySQL TẠO XEM với WHERE
  • MySQL TẠO VIEW với AND và OR
  • MySQL TẠO XEM với NHÓM THEO
  • MySQL TẠO VIEW với ORDER BY
  • MySQL CREATE VIEW với BETWEEN và IN
  • MySQL TẠO XEM với THÍCH
  • MySQL TẠO XEM bằng truy vấn con
  • MySQL TẠO XEM với THAM GIA
  • MySQL TẠO XEM với UNION

Tại sao lượt xem?

  • Lượt xem có thể là bản sao hiệu quả của các bảng cơ sở
  • Chế độ xem có thể có tên cột và biểu thức
  • Bạn có thể sử dụng bất kỳ mệnh đề nào trong chế độ xem
  • Chế độ xem có thể được sử dụng trong INSERT/UPDATE/DELETE
  • Chế độ xem có thể chứa các biểu thức trong danh sách chọn
  • Lượt xem có thể là lượt xem của lượt xem

Chế độ xem MySQL cần Phiên bản 5. 0 hoặc cao hơn

Để chế độ xem hoạt động, bạn cần nâng cấp lên MySQL phiên bản 5. 0 [hoặc cao hơn]. Bạn có thể kiểm tra phiên bản MySQL của mình theo cách sau

mysql>SELECT VERSION[];
+-----------+
| VERSION[] |
+-----------+
| 5.6.12    |
+-----------+
1 row in set [0.00 sec]

Kiểm tra các đặc quyền của người dùng hiện tại

Câu lệnh CREATE VIEW yêu cầu đặc quyền CREATE VIEW cho dạng xem và một số đặc quyền cho mỗi cột được chọn bởi câu lệnh SELECT. Lệnh sau hiển thị các đặc quyền của người dùng

mysql> SHOW PRIVILEGES;
+-----------------+----------------------------+-------------------------------------------------------+
| Privilege       | Context                    | Comment                                               |
+-----------------+----------------------------+-------------------------------------------------------+
| Alter           | Tables                     | To alter the table                                    |
| Alter routine   | Functions,Procedures       | To alter or drop stored functions/procedures          |
| Create          | Databases,Tables,Indexes   | To create new databases and tables                    |
| Create routine  | Databases                  | To use CREATE FUNCTION/PROCEDURE                      |
| Create temporary| Databases                  | To use CREATE TEMPORARY TABLE                         |
| tables          |                            |                                                       |
| Create view     | Tables                     | To create new views                                   |
| Create user     | Server Admin               | To create new users                                   |
| Delete          | Tables                     | To delete existing rows                               |
| Drop            | Databases,Tables           | To drop databases, tables, and views                  |
| Event           | Server Admin               | To create, alter, drop and execute events             |
| Execute         | Functions,Procedures       | To execute stored routines                            |
| File            | File access on server      | To read and write files on the server                 |
| Grant option    | Databases,Tables,          | To give to other users those privileges you possess   |
|                 | Functions,Procedures       |                                                       |
| Index           | Tables                     | To create or drop indexes                             |
| Insert          | Tables                     | To insert data into tables                            |
| Lock tables     | Databases                  | To use LOCK TABLES [together with SELECT privilege]   |
| Process         | Server Admin               | To view the plain text of currently executing queries |
| Proxy           | Server Admin               | To make proxy user possible                           |
| References      | Databases,Tables           | To have references on tables                          |
| Reload          | Server Admin               | To reload or refresh tables, logs, and privileges     |
| Replication     | Server Admin               | To ask where the slave or master servers are          |
| client          |                            |                                                       |
| Replication     | Server Admin               | To read binary log events from the master             |
| slave           |                            |                                                       |
| Select          | Tables                     | To retrieve rows from table                           |
| Show databases  | Server Admin               | To see all databases with SHOW DATABASES              |
| Show view       | Tables                     | To see views with SHOW CREATE VIEW                    |
| Shutdown        | Server Admin               | To shut down the server                               |
| Super           | Server Admin               | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc.   |
| Trigger         | Tables                     | To use triggers                                       |
| Create          | Server Admin               | To create/alter/drop tablespaces                      |
| tablespace      |                            |                                                       |
| Update          | Tables                     | To update existing rows                               |
| Usage           | Server Admin               | No privileges - allow connecting only                 |
+-------------------------+--------------------+-------------------------------------------------------+
31 rows in set [0.00 sec]

Chọn một cơ sở dữ liệu

Trước khi tạo chế độ xem, chúng ta phải chọn cơ sở dữ liệu. Lệnh sau hiển thị danh sách cơ sở dữ liệu

mysql> SHOW DATBASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hr                 |
| mysql              |
| performance_schema |
| sakila             |
| test               |
| world              |
+--------------------+
7 rows in set [0.06 sec]

Bây giờ chọn cơ sở dữ liệu 'hr' và liệt kê các bảng

mysql> USE hr;
Database changed
mysql> SHOW TABLES;
+--------------+
| Tables_in_hr |
+--------------+
| alluser      |
| departments  |
| emp_details  |
| job_history  |
| jobs         |
| locations    |
| regions      |
| user         |
| user_details |
+--------------+
9 rows in set [0.00 sec]

Tạo Chế độ xem

Các câu lệnh sau đây tạo một khung nhìn. Theo mặc định, một dạng xem được liên kết với cơ sở dữ liệu mặc định [cơ sở dữ liệu hiện được sử dụng]. Để liên kết chế độ xem với một cơ sở dữ liệu nhất định, hãy chỉ định tên là database_name. view_name khi bạn tạo nó. Đây là cú pháp hoàn chỉnh

cú pháp

CREATE
    [OR REPLACE]
    [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
    [DEFINER = { user | CURRENT_USER }]
    [SQL SECURITY { DEFINER | INVOKER }]
    VIEW view_name [[column_list]]
    AS select_statement
    [WITH [CASCADED | LOCAL] CHECK OPTION]

Giải trình

CREATE
    [OR REPLACE]
    [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
    [DEFINER = { user | CURRENT_USER }]
    [SQL SECURITY { DEFINER | INVOKER }]
    VIEW view_name [[column_list]]
    AS select_statement
    [WITH [CASCADED | LOCAL] CHECK OPTION]

Câu lệnh CREATE VIEW tạo một khung nhìn mới

view_name. view_name là tên của chế độ xem. Chế độ xem luôn thuộc về cơ sở dữ liệu. Theo mặc định, một dạng xem mới được tạo trong cơ sở dữ liệu hiện được sử dụng. Tên khung nhìn cũng có thể được sử dụng với tên cơ sở dữ liệu, như tên_cơ sở dữ liệu. view_name, nhưng không cần thiết nếu database_name là cơ sở dữ liệu mặc định

select_statement. select_statement là một câu lệnh SELECT và cung cấp định nghĩa của dạng xem. select_statement có thể chọn dữ liệu từ các bảng cơ sở hoặc các dạng xem khác

Thí dụ

mysql> USE hr;
Database changed
mysql> CREATE VIEW my_v1 AS SELECT * FROM user_details;
Query OK, 0 rows affected [0.13 sec]

cột_list. Phần column_list là tùy chọn. Nó cung cấp danh sách tên cho các cột của chế độ xem ngay sau tên chế độ xem trong đó tên phải là duy nhất. Số lượng tên trong column_list phải giống với số lượng cột được truy xuất bởi câu lệnh SELECT. Nếu bạn muốn đặt tên khác cho các cột dạng xem của mình, bạn có thể làm như vậy bằng cách thêm mệnh đề [AS name] vào danh sách chọn

Thí dụ. Xem không có column_list

mysql> SELECT * FROM user;
+----------+-----------+--------+
| userid   | password  | name   |
+----------+-----------+--------+
| scott123 | [email protected]   | Scott  |
| ferp6734 | [email protected]&3 | Palash |
| diana094 | [email protected]   | Diana  |
+----------+-----------+--------+
3 rows in set [0.04 sec]
____9
mysql> SHOW PRIVILEGES;
+-----------------+----------------------------+-------------------------------------------------------+
| Privilege       | Context                    | Comment                                               |
+-----------------+----------------------------+-------------------------------------------------------+
| Alter           | Tables                     | To alter the table                                    |
| Alter routine   | Functions,Procedures       | To alter or drop stored functions/procedures          |
| Create          | Databases,Tables,Indexes   | To create new databases and tables                    |
| Create routine  | Databases                  | To use CREATE FUNCTION/PROCEDURE                      |
| Create temporary| Databases                  | To use CREATE TEMPORARY TABLE                         |
| tables          |                            |                                                       |
| Create view     | Tables                     | To create new views                                   |
| Create user     | Server Admin               | To create new users                                   |
| Delete          | Tables                     | To delete existing rows                               |
| Drop            | Databases,Tables           | To drop databases, tables, and views                  |
| Event           | Server Admin               | To create, alter, drop and execute events             |
| Execute         | Functions,Procedures       | To execute stored routines                            |
| File            | File access on server      | To read and write files on the server                 |
| Grant option    | Databases,Tables,          | To give to other users those privileges you possess   |
|                 | Functions,Procedures       |                                                       |
| Index           | Tables                     | To create or drop indexes                             |
| Insert          | Tables                     | To insert data into tables                            |
| Lock tables     | Databases                  | To use LOCK TABLES [together with SELECT privilege]   |
| Process         | Server Admin               | To view the plain text of currently executing queries |
| Proxy           | Server Admin               | To make proxy user possible                           |
| References      | Databases,Tables           | To have references on tables                          |
| Reload          | Server Admin               | To reload or refresh tables, logs, and privileges     |
| Replication     | Server Admin               | To ask where the slave or master servers are          |
| client          |                            |                                                       |
| Replication     | Server Admin               | To read binary log events from the master             |
| slave           |                            |                                                       |
| Select          | Tables                     | To retrieve rows from table                           |
| Show databases  | Server Admin               | To see all databases with SHOW DATABASES              |
| Show view       | Tables                     | To see views with SHOW CREATE VIEW                    |
| Shutdown        | Server Admin               | To shut down the server                               |
| Super           | Server Admin               | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc.   |
| Trigger         | Tables                     | To use triggers                                       |
| Create          | Server Admin               | To create/alter/drop tablespaces                      |
| tablespace      |                            |                                                       |
| Update          | Tables                     | To update existing rows                               |
| Usage           | Server Admin               | No privileges - allow connecting only                 |
+-------------------------+--------------------+-------------------------------------------------------+
31 rows in set [0.00 sec]
0

Bây giờ hãy chỉ định tên cột trong chế độ xem trên

mysql> SHOW PRIVILEGES;
+-----------------+----------------------------+-------------------------------------------------------+
| Privilege       | Context                    | Comment                                               |
+-----------------+----------------------------+-------------------------------------------------------+
| Alter           | Tables                     | To alter the table                                    |
| Alter routine   | Functions,Procedures       | To alter or drop stored functions/procedures          |
| Create          | Databases,Tables,Indexes   | To create new databases and tables                    |
| Create routine  | Databases                  | To use CREATE FUNCTION/PROCEDURE                      |
| Create temporary| Databases                  | To use CREATE TEMPORARY TABLE                         |
| tables          |                            |                                                       |
| Create view     | Tables                     | To create new views                                   |
| Create user     | Server Admin               | To create new users                                   |
| Delete          | Tables                     | To delete existing rows                               |
| Drop            | Databases,Tables           | To drop databases, tables, and views                  |
| Event           | Server Admin               | To create, alter, drop and execute events             |
| Execute         | Functions,Procedures       | To execute stored routines                            |
| File            | File access on server      | To read and write files on the server                 |
| Grant option    | Databases,Tables,          | To give to other users those privileges you possess   |
|                 | Functions,Procedures       |                                                       |
| Index           | Tables                     | To create or drop indexes                             |
| Insert          | Tables                     | To insert data into tables                            |
| Lock tables     | Databases                  | To use LOCK TABLES [together with SELECT privilege]   |
| Process         | Server Admin               | To view the plain text of currently executing queries |
| Proxy           | Server Admin               | To make proxy user possible                           |
| References      | Databases,Tables           | To have references on tables                          |
| Reload          | Server Admin               | To reload or refresh tables, logs, and privileges     |
| Replication     | Server Admin               | To ask where the slave or master servers are          |
| client          |                            |                                                       |
| Replication     | Server Admin               | To read binary log events from the master             |
| slave           |                            |                                                       |
| Select          | Tables                     | To retrieve rows from table                           |
| Show databases  | Server Admin               | To see all databases with SHOW DATABASES              |
| Show view       | Tables                     | To see views with SHOW CREATE VIEW                    |
| Shutdown        | Server Admin               | To shut down the server                               |
| Super           | Server Admin               | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc.   |
| Trigger         | Tables                     | To use triggers                                       |
| Create          | Server Admin               | To create/alter/drop tablespaces                      |
| tablespace      |                            |                                                       |
| Update          | Tables                     | To update existing rows                               |
| Usage           | Server Admin               | No privileges - allow connecting only                 |
+-------------------------+--------------------+-------------------------------------------------------+
31 rows in set [0.00 sec]
1
mysql> SHOW PRIVILEGES;
+-----------------+----------------------------+-------------------------------------------------------+
| Privilege       | Context                    | Comment                                               |
+-----------------+----------------------------+-------------------------------------------------------+
| Alter           | Tables                     | To alter the table                                    |
| Alter routine   | Functions,Procedures       | To alter or drop stored functions/procedures          |
| Create          | Databases,Tables,Indexes   | To create new databases and tables                    |
| Create routine  | Databases                  | To use CREATE FUNCTION/PROCEDURE                      |
| Create temporary| Databases                  | To use CREATE TEMPORARY TABLE                         |
| tables          |                            |                                                       |
| Create view     | Tables                     | To create new views                                   |
| Create user     | Server Admin               | To create new users                                   |
| Delete          | Tables                     | To delete existing rows                               |
| Drop            | Databases,Tables           | To drop databases, tables, and views                  |
| Event           | Server Admin               | To create, alter, drop and execute events             |
| Execute         | Functions,Procedures       | To execute stored routines                            |
| File            | File access on server      | To read and write files on the server                 |
| Grant option    | Databases,Tables,          | To give to other users those privileges you possess   |
|                 | Functions,Procedures       |                                                       |
| Index           | Tables                     | To create or drop indexes                             |
| Insert          | Tables                     | To insert data into tables                            |
| Lock tables     | Databases                  | To use LOCK TABLES [together with SELECT privilege]   |
| Process         | Server Admin               | To view the plain text of currently executing queries |
| Proxy           | Server Admin               | To make proxy user possible                           |
| References      | Databases,Tables           | To have references on tables                          |
| Reload          | Server Admin               | To reload or refresh tables, logs, and privileges     |
| Replication     | Server Admin               | To ask where the slave or master servers are          |
| client          |                            |                                                       |
| Replication     | Server Admin               | To read binary log events from the master             |
| slave           |                            |                                                       |
| Select          | Tables                     | To retrieve rows from table                           |
| Show databases  | Server Admin               | To see all databases with SHOW DATABASES              |
| Show view       | Tables                     | To see views with SHOW CREATE VIEW                    |
| Shutdown        | Server Admin               | To shut down the server                               |
| Super           | Server Admin               | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc.   |
| Trigger         | Tables                     | To use triggers                                       |
| Create          | Server Admin               | To create/alter/drop tablespaces                      |
| tablespace      |                            |                                                       |
| Update          | Tables                     | To update existing rows                               |
| Usage           | Server Admin               | No privileges - allow connecting only                 |
+-------------------------+--------------------+-------------------------------------------------------+
31 rows in set [0.00 sec]
2

HOẶC THAY THẾ. Nếu mệnh đề OR REPLACE tùy chọn được thêm vào với câu lệnh CREATE VIEW, thì câu lệnh CREATE VIEW sẽ thay thế một dạng xem hiện có và tạo một dạng xem mới. Nếu chế độ xem không tồn tại, CREATE VIEW cũng giống như CREATE OR REPLACE VIEW

mysql> SHOW PRIVILEGES;
+-----------------+----------------------------+-------------------------------------------------------+
| Privilege       | Context                    | Comment                                               |
+-----------------+----------------------------+-------------------------------------------------------+
| Alter           | Tables                     | To alter the table                                    |
| Alter routine   | Functions,Procedures       | To alter or drop stored functions/procedures          |
| Create          | Databases,Tables,Indexes   | To create new databases and tables                    |
| Create routine  | Databases                  | To use CREATE FUNCTION/PROCEDURE                      |
| Create temporary| Databases                  | To use CREATE TEMPORARY TABLE                         |
| tables          |                            |                                                       |
| Create view     | Tables                     | To create new views                                   |
| Create user     | Server Admin               | To create new users                                   |
| Delete          | Tables                     | To delete existing rows                               |
| Drop            | Databases,Tables           | To drop databases, tables, and views                  |
| Event           | Server Admin               | To create, alter, drop and execute events             |
| Execute         | Functions,Procedures       | To execute stored routines                            |
| File            | File access on server      | To read and write files on the server                 |
| Grant option    | Databases,Tables,          | To give to other users those privileges you possess   |
|                 | Functions,Procedures       |                                                       |
| Index           | Tables                     | To create or drop indexes                             |
| Insert          | Tables                     | To insert data into tables                            |
| Lock tables     | Databases                  | To use LOCK TABLES [together with SELECT privilege]   |
| Process         | Server Admin               | To view the plain text of currently executing queries |
| Proxy           | Server Admin               | To make proxy user possible                           |
| References      | Databases,Tables           | To have references on tables                          |
| Reload          | Server Admin               | To reload or refresh tables, logs, and privileges     |
| Replication     | Server Admin               | To ask where the slave or master servers are          |
| client          |                            |                                                       |
| Replication     | Server Admin               | To read binary log events from the master             |
| slave           |                            |                                                       |
| Select          | Tables                     | To retrieve rows from table                           |
| Show databases  | Server Admin               | To see all databases with SHOW DATABASES              |
| Show view       | Tables                     | To see views with SHOW CREATE VIEW                    |
| Shutdown        | Server Admin               | To shut down the server                               |
| Super           | Server Admin               | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc.   |
| Trigger         | Tables                     | To use triggers                                       |
| Create          | Server Admin               | To create/alter/drop tablespaces                      |
| tablespace      |                            |                                                       |
| Update          | Tables                     | To update existing rows                               |
| Usage           | Server Admin               | No privileges - allow connecting only                 |
+-------------------------+--------------------+-------------------------------------------------------+
31 rows in set [0.00 sec]
3

- THUẬT TOÁN. Mệnh đề ALGORITHM là tùy chọn, nó ảnh hưởng đến cách MySQL xử lý chế độ xem. THUẬT TOÁN nhận ba giá trị. MERGE, TEMPTABLE, hoặc UNDEFINED. Thuật toán mặc định là UNDEFINED

[DEFINER = { người dùng. CURRENT_USER }]
[BẢO MẬT SQL { DEFINER. NGƯỜI MỜI }]. Các mệnh đề DEFINER và SQL SECURITY chỉ định ngữ cảnh bảo mật sẽ được sử dụng khi kiểm tra các đặc quyền truy cập tại thời điểm gọi chế độ xem.

Nếu bạn chỉ định mệnh đề DEFINER, các quy tắc sau sẽ xác định giá trị người dùng DEFINER hợp pháp

  • Nếu bạn không có đặc quyền SUPER, giá trị người dùng hợp pháp duy nhất là tài khoản của chính bạn và bạn không thể đặt định nghĩa cho một số tài khoản khác
  • Nếu bạn có đặc quyền SUPER, bạn có thể chỉ định bất kỳ tên tài khoản hợp pháp nào về mặt cú pháp

Trong một quy trình được lưu trữ được xác định bằng đặc tính SQL SECURITY DEFINER, CURRENT_USER trả về giá trị DEFINER của quy trình. Điều này cũng ảnh hưởng đến chế độ xem được xác định trong một thói quen như vậy, nếu định nghĩa chế độ xem chứa giá trị DEFINER của CURRENT_USER

[VỚI [XÁC NHẬN. ĐỊA PHƯƠNG] KIỂM TRA TÙY CHỌN]. Mệnh đề VỚI TÙY CHỌN KIỂM TRA có thể được cung cấp cho chế độ xem có thể cập nhật để ngăn việc chèn hoặc cập nhật các hàng ngoại trừ những hàng mà mệnh đề WHERE trong câu lệnh select_là đúng. Trong mệnh đề VỚI KIỂM TRA TÙY CHỌN cho chế độ xem có thể cập nhật, từ khóa ĐỊA PHƯƠNG và CASCADED xác định phạm vi kiểm tra kiểm tra khi chế độ xem được xác định theo chế độ xem khác. Từ khóa ĐỊA PHƯƠNG chỉ giới hạn TÙY CHỌN KIỂM TRA đối với chế độ xem được xác định. CASCADED làm cho việc kiểm tra các chế độ xem cơ bản cũng được đánh giá. Khi không có từ khóa nào được đưa ra, mặc định là CASCADED

Hạn chế về định nghĩa Chế độ xem

  • Câu lệnh SELECT không được chứa truy vấn con trong mệnh đề FROM
  • Câu lệnh SELECT không thể tham chiếu đến biến hệ thống hoặc người dùng
  • Trong một chương trình được lưu trữ, định nghĩa không thể đề cập đến các tham số chương trình hoặc biến cục bộ
  • Câu lệnh SELECT không thể tham chiếu đến các tham số câu lệnh đã chuẩn bị
  • Mọi bảng hoặc dạng xem được đề cập trong định nghĩa phải tồn tại
  • Định nghĩa không thể tham chiếu đến bảng TẠM THỜI và bạn không thể tạo chế độ xem TẠM THỜI
  • Bất kỳ bảng nào có tên trong định nghĩa dạng xem phải tồn tại tại thời điểm định nghĩa
  • Bạn không thể liên kết trình kích hoạt với chế độ xem
  • Bí danh cho tên cột trong câu lệnh CHỌN được kiểm tra đối với độ dài cột tối đa là 64 ký tự [không phải độ dài bí danh tối đa là 256 ký tự]

Công cụ để tạo Chế độ xem MySQL

Bạn có thể viết một thủ tục trong công cụ dòng lệnh của MySQL hoặc bạn có thể sử dụng MySQL workbench, một công cụ giao diện người dùng tuyệt vời [ở đây chúng tôi đã sử dụng phiên bản 5. 3 CN]

Công cụ dòng lệnh MySQL

Chọn Máy khách lệnh MySQL từ menu Bắt đầu


Chọn dấu nhắc lệnh MySQL màn hình sau sẽ đến


Sau khi đăng nhập thành công, bạn có thể truy cập dấu nhắc lệnh của MySQL


Bây giờ bạn có thể tạo và chạy chế độ xem của riêng mình, hãy xem ví dụ sau


Bàn làm việc MySQL [5. 3 CN]

Chọn bàn làm việc MySQL từ menu Bắt đầu


Sau khi chọn bàn làm việc của MySQL, màn hình đăng nhập sau sẽ đến


Bây giờ hãy nhập chi tiết đăng nhập


Sau khi đăng nhập thành công, một màn hình mới sẽ xuất hiện và từ bảng trình duyệt đối tượng, chọn cơ sở dữ liệu


Sau khi chọn cơ sở dữ liệu, nhấp chuột phải vào Lượt xem, một cửa sổ bật lên mới sẽ xuất hiện


Sau khi chọn "Tạo chế độ xem", màn hình sau sẽ đến nơi bạn có thể viết chế độ xem của riêng mình


Sau khi viết chế độ xem, nhấp vào nút Áp dụng và màn hình sau sẽ xuất hiện


Màn hình tiếp theo sẽ là xem lại tập lệnh và áp dụng trên cơ sở dữ liệu


Bây giờ hãy nhấp vào nút Kết thúc và chạy chế độ xem


Thay đổi chế độ xem

Câu lệnh ALTER VIEW thay đổi định nghĩa của một dạng xem hiện có. Cú pháp của câu lệnh tương tự như CREATE VIEW

cú pháp

mysql> SHOW PRIVILEGES;
+-----------------+----------------------------+-------------------------------------------------------+
| Privilege       | Context                    | Comment                                               |
+-----------------+----------------------------+-------------------------------------------------------+
| Alter           | Tables                     | To alter the table                                    |
| Alter routine   | Functions,Procedures       | To alter or drop stored functions/procedures          |
| Create          | Databases,Tables,Indexes   | To create new databases and tables                    |
| Create routine  | Databases                  | To use CREATE FUNCTION/PROCEDURE                      |
| Create temporary| Databases                  | To use CREATE TEMPORARY TABLE                         |
| tables          |                            |                                                       |
| Create view     | Tables                     | To create new views                                   |
| Create user     | Server Admin               | To create new users                                   |
| Delete          | Tables                     | To delete existing rows                               |
| Drop            | Databases,Tables           | To drop databases, tables, and views                  |
| Event           | Server Admin               | To create, alter, drop and execute events             |
| Execute         | Functions,Procedures       | To execute stored routines                            |
| File            | File access on server      | To read and write files on the server                 |
| Grant option    | Databases,Tables,          | To give to other users those privileges you possess   |
|                 | Functions,Procedures       |                                                       |
| Index           | Tables                     | To create or drop indexes                             |
| Insert          | Tables                     | To insert data into tables                            |
| Lock tables     | Databases                  | To use LOCK TABLES [together with SELECT privilege]   |
| Process         | Server Admin               | To view the plain text of currently executing queries |
| Proxy           | Server Admin               | To make proxy user possible                           |
| References      | Databases,Tables           | To have references on tables                          |
| Reload          | Server Admin               | To reload or refresh tables, logs, and privileges     |
| Replication     | Server Admin               | To ask where the slave or master servers are          |
| client          |                            |                                                       |
| Replication     | Server Admin               | To read binary log events from the master             |
| slave           |                            |                                                       |
| Select          | Tables                     | To retrieve rows from table                           |
| Show databases  | Server Admin               | To see all databases with SHOW DATABASES              |
| Show view       | Tables                     | To see views with SHOW CREATE VIEW                    |
| Shutdown        | Server Admin               | To shut down the server                               |
| Super           | Server Admin               | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc.   |
| Trigger         | Tables                     | To use triggers                                       |
| Create          | Server Admin               | To create/alter/drop tablespaces                      |
| tablespace      |                            |                                                       |
| Update          | Tables                     | To update existing rows                               |
| Usage           | Server Admin               | No privileges - allow connecting only                 |
+-------------------------+--------------------+-------------------------------------------------------+
31 rows in set [0.00 sec]
4

Câu lệnh này yêu cầu các đặc quyền CREATE VIEW và DROP cho dạng xem và một số đặc quyền cho mỗi cột được tham chiếu trong câu lệnh SELECT

Thả một cái nhìn

Câu lệnh DROP VIEW được sử dụng để xóa một hoặc nhiều chế độ xem. Để bỏ chế độ xem, bạn phải có đặc quyền DROP cho từng chế độ xem. Đây là cú pháp

cú pháp

mysql> SHOW PRIVILEGES;
+-----------------+----------------------------+-------------------------------------------------------+
| Privilege       | Context                    | Comment                                               |
+-----------------+----------------------------+-------------------------------------------------------+
| Alter           | Tables                     | To alter the table                                    |
| Alter routine   | Functions,Procedures       | To alter or drop stored functions/procedures          |
| Create          | Databases,Tables,Indexes   | To create new databases and tables                    |
| Create routine  | Databases                  | To use CREATE FUNCTION/PROCEDURE                      |
| Create temporary| Databases                  | To use CREATE TEMPORARY TABLE                         |
| tables          |                            |                                                       |
| Create view     | Tables                     | To create new views                                   |
| Create user     | Server Admin               | To create new users                                   |
| Delete          | Tables                     | To delete existing rows                               |
| Drop            | Databases,Tables           | To drop databases, tables, and views                  |
| Event           | Server Admin               | To create, alter, drop and execute events             |
| Execute         | Functions,Procedures       | To execute stored routines                            |
| File            | File access on server      | To read and write files on the server                 |
| Grant option    | Databases,Tables,          | To give to other users those privileges you possess   |
|                 | Functions,Procedures       |                                                       |
| Index           | Tables                     | To create or drop indexes                             |
| Insert          | Tables                     | To insert data into tables                            |
| Lock tables     | Databases                  | To use LOCK TABLES [together with SELECT privilege]   |
| Process         | Server Admin               | To view the plain text of currently executing queries |
| Proxy           | Server Admin               | To make proxy user possible                           |
| References      | Databases,Tables           | To have references on tables                          |
| Reload          | Server Admin               | To reload or refresh tables, logs, and privileges     |
| Replication     | Server Admin               | To ask where the slave or master servers are          |
| client          |                            |                                                       |
| Replication     | Server Admin               | To read binary log events from the master             |
| slave           |                            |                                                       |
| Select          | Tables                     | To retrieve rows from table                           |
| Show databases  | Server Admin               | To see all databases with SHOW DATABASES              |
| Show view       | Tables                     | To see views with SHOW CREATE VIEW                    |
| Shutdown        | Server Admin               | To shut down the server                               |
| Super           | Server Admin               | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc.   |
| Trigger         | Tables                     | To use triggers                                       |
| Create          | Server Admin               | To create/alter/drop tablespaces                      |
| tablespace      |                            |                                                       |
| Update          | Tables                     | To update existing rows                               |
| Usage           | Server Admin               | No privileges - allow connecting only                 |
+-------------------------+--------------------+-------------------------------------------------------+
31 rows in set [0.00 sec]
5

Mệnh đề IF EXISTS ngăn lỗi xảy ra đối với các chế độ xem không tồn tại

MySQL TẠO XEM với WHERE

Lệnh CREATE VIEW có thể được sử dụng với mệnh đề WHERE

Thí dụ

bảng mẫu. tác giả


mysql> SHOW PRIVILEGES;
+-----------------+----------------------------+-------------------------------------------------------+
| Privilege       | Context                    | Comment                                               |
+-----------------+----------------------------+-------------------------------------------------------+
| Alter           | Tables                     | To alter the table                                    |
| Alter routine   | Functions,Procedures       | To alter or drop stored functions/procedures          |
| Create          | Databases,Tables,Indexes   | To create new databases and tables                    |
| Create routine  | Databases                  | To use CREATE FUNCTION/PROCEDURE                      |
| Create temporary| Databases                  | To use CREATE TEMPORARY TABLE                         |
| tables          |                            |                                                       |
| Create view     | Tables                     | To create new views                                   |
| Create user     | Server Admin               | To create new users                                   |
| Delete          | Tables                     | To delete existing rows                               |
| Drop            | Databases,Tables           | To drop databases, tables, and views                  |
| Event           | Server Admin               | To create, alter, drop and execute events             |
| Execute         | Functions,Procedures       | To execute stored routines                            |
| File            | File access on server      | To read and write files on the server                 |
| Grant option    | Databases,Tables,          | To give to other users those privileges you possess   |
|                 | Functions,Procedures       |                                                       |
| Index           | Tables                     | To create or drop indexes                             |
| Insert          | Tables                     | To insert data into tables                            |
| Lock tables     | Databases                  | To use LOCK TABLES [together with SELECT privilege]   |
| Process         | Server Admin               | To view the plain text of currently executing queries |
| Proxy           | Server Admin               | To make proxy user possible                           |
| References      | Databases,Tables           | To have references on tables                          |
| Reload          | Server Admin               | To reload or refresh tables, logs, and privileges     |
| Replication     | Server Admin               | To ask where the slave or master servers are          |
| client          |                            |                                                       |
| Replication     | Server Admin               | To read binary log events from the master             |
| slave           |                            |                                                       |
| Select          | Tables                     | To retrieve rows from table                           |
| Show databases  | Server Admin               | To see all databases with SHOW DATABASES              |
| Show view       | Tables                     | To see views with SHOW CREATE VIEW                    |
| Shutdown        | Server Admin               | To shut down the server                               |
| Super           | Server Admin               | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc.   |
| Trigger         | Tables                     | To use triggers                                       |
| Create          | Server Admin               | To create/alter/drop tablespaces                      |
| tablespace      |                            |                                                       |
| Update          | Tables                     | To update existing rows                               |
| Usage           | Server Admin               | No privileges - allow connecting only                 |
+-------------------------+--------------------+-------------------------------------------------------+
31 rows in set [0.00 sec]
6

Câu lệnh MySQL ở trên sẽ tạo một chế độ xem 'view_author' lấy các bản ghi [cho tất cả các cột] của bảng tác giả nếu các bản ghi đó chứa giá trị 'USA' cho cột quốc gia

MySQL TẠO VIEW với AND và OR

Lệnh CREATE VIEW có thể được sử dụng với các toán tử AND và OR

Thí dụ

bảng mẫu. nhà xuất bản


mysql> SHOW PRIVILEGES;
+-----------------+----------------------------+-------------------------------------------------------+
| Privilege       | Context                    | Comment                                               |
+-----------------+----------------------------+-------------------------------------------------------+
| Alter           | Tables                     | To alter the table                                    |
| Alter routine   | Functions,Procedures       | To alter or drop stored functions/procedures          |
| Create          | Databases,Tables,Indexes   | To create new databases and tables                    |
| Create routine  | Databases                  | To use CREATE FUNCTION/PROCEDURE                      |
| Create temporary| Databases                  | To use CREATE TEMPORARY TABLE                         |
| tables          |                            |                                                       |
| Create view     | Tables                     | To create new views                                   |
| Create user     | Server Admin               | To create new users                                   |
| Delete          | Tables                     | To delete existing rows                               |
| Drop            | Databases,Tables           | To drop databases, tables, and views                  |
| Event           | Server Admin               | To create, alter, drop and execute events             |
| Execute         | Functions,Procedures       | To execute stored routines                            |
| File            | File access on server      | To read and write files on the server                 |
| Grant option    | Databases,Tables,          | To give to other users those privileges you possess   |
|                 | Functions,Procedures       |                                                       |
| Index           | Tables                     | To create or drop indexes                             |
| Insert          | Tables                     | To insert data into tables                            |
| Lock tables     | Databases                  | To use LOCK TABLES [together with SELECT privilege]   |
| Process         | Server Admin               | To view the plain text of currently executing queries |
| Proxy           | Server Admin               | To make proxy user possible                           |
| References      | Databases,Tables           | To have references on tables                          |
| Reload          | Server Admin               | To reload or refresh tables, logs, and privileges     |
| Replication     | Server Admin               | To ask where the slave or master servers are          |
| client          |                            |                                                       |
| Replication     | Server Admin               | To read binary log events from the master             |
| slave           |                            |                                                       |
| Select          | Tables                     | To retrieve rows from table                           |
| Show databases  | Server Admin               | To see all databases with SHOW DATABASES              |
| Show view       | Tables                     | To see views with SHOW CREATE VIEW                    |
| Shutdown        | Server Admin               | To shut down the server                               |
| Super           | Server Admin               | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc.   |
| Trigger         | Tables                     | To use triggers                                       |
| Create          | Server Admin               | To create/alter/drop tablespaces                      |
| tablespace      |                            |                                                       |
| Update          | Tables                     | To update existing rows                               |
| Usage           | Server Admin               | No privileges - allow connecting only                 |
+-------------------------+--------------------+-------------------------------------------------------+
31 rows in set [0.00 sec]
7

Câu lệnh MySQL ở trên sẽ tạo một chế độ xem 'view_publisher' lấy các bản ghi cho các cột pub_name, pub_city và quốc gia của bảng nhà xuất bản, nếu [A][i]giá trị của cột quốc gia là Hoa Kỳ và [ii]giá trị của pub_city là Mới

MySQL TẠO XEM với NHÓM THEO

Lệnh CREATE VIEW có thể được sử dụng với mệnh đề GROUP BY

Thí dụ

bảng mẫu. book_mast


mysql> SHOW PRIVILEGES;
+-----------------+----------------------------+-------------------------------------------------------+
| Privilege       | Context                    | Comment                                               |
+-----------------+----------------------------+-------------------------------------------------------+
| Alter           | Tables                     | To alter the table                                    |
| Alter routine   | Functions,Procedures       | To alter or drop stored functions/procedures          |
| Create          | Databases,Tables,Indexes   | To create new databases and tables                    |
| Create routine  | Databases                  | To use CREATE FUNCTION/PROCEDURE                      |
| Create temporary| Databases                  | To use CREATE TEMPORARY TABLE                         |
| tables          |                            |                                                       |
| Create view     | Tables                     | To create new views                                   |
| Create user     | Server Admin               | To create new users                                   |
| Delete          | Tables                     | To delete existing rows                               |
| Drop            | Databases,Tables           | To drop databases, tables, and views                  |
| Event           | Server Admin               | To create, alter, drop and execute events             |
| Execute         | Functions,Procedures       | To execute stored routines                            |
| File            | File access on server      | To read and write files on the server                 |
| Grant option    | Databases,Tables,          | To give to other users those privileges you possess   |
|                 | Functions,Procedures       |                                                       |
| Index           | Tables                     | To create or drop indexes                             |
| Insert          | Tables                     | To insert data into tables                            |
| Lock tables     | Databases                  | To use LOCK TABLES [together with SELECT privilege]   |
| Process         | Server Admin               | To view the plain text of currently executing queries |
| Proxy           | Server Admin               | To make proxy user possible                           |
| References      | Databases,Tables           | To have references on tables                          |
| Reload          | Server Admin               | To reload or refresh tables, logs, and privileges     |
| Replication     | Server Admin               | To ask where the slave or master servers are          |
| client          |                            |                                                       |
| Replication     | Server Admin               | To read binary log events from the master             |
| slave           |                            |                                                       |
| Select          | Tables                     | To retrieve rows from table                           |
| Show databases  | Server Admin               | To see all databases with SHOW DATABASES              |
| Show view       | Tables                     | To see views with SHOW CREATE VIEW                    |
| Shutdown        | Server Admin               | To shut down the server                               |
| Super           | Server Admin               | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc.   |
| Trigger         | Tables                     | To use triggers                                       |
| Create          | Server Admin               | To create/alter/drop tablespaces                      |
| tablespace      |                            |                                                       |
| Update          | Tables                     | To update existing rows                               |
| Usage           | Server Admin               | No privileges - allow connecting only                 |
+-------------------------+--------------------+-------------------------------------------------------+
31 rows in set [0.00 sec]
8

Câu lệnh trên sẽ tạo một dạng xem 'view_bookmast' lấy tất cả các bản ghi được nhóm w. r. t. pub_lang, từ pub_lang và số sách cho mỗi ngôn ngữ [pub_lang]

MySQL TẠO VIEW với ORDER BY

Lệnh CREATE VIEW có thể được sử dụng với mệnh đề ORDER BY

Thí dụ

bảng mẫu. book_mast


mysql> SHOW PRIVILEGES;
+-----------------+----------------------------+-------------------------------------------------------+
| Privilege       | Context                    | Comment                                               |
+-----------------+----------------------------+-------------------------------------------------------+
| Alter           | Tables                     | To alter the table                                    |
| Alter routine   | Functions,Procedures       | To alter or drop stored functions/procedures          |
| Create          | Databases,Tables,Indexes   | To create new databases and tables                    |
| Create routine  | Databases                  | To use CREATE FUNCTION/PROCEDURE                      |
| Create temporary| Databases                  | To use CREATE TEMPORARY TABLE                         |
| tables          |                            |                                                       |
| Create view     | Tables                     | To create new views                                   |
| Create user     | Server Admin               | To create new users                                   |
| Delete          | Tables                     | To delete existing rows                               |
| Drop            | Databases,Tables           | To drop databases, tables, and views                  |
| Event           | Server Admin               | To create, alter, drop and execute events             |
| Execute         | Functions,Procedures       | To execute stored routines                            |
| File            | File access on server      | To read and write files on the server                 |
| Grant option    | Databases,Tables,          | To give to other users those privileges you possess   |
|                 | Functions,Procedures       |                                                       |
| Index           | Tables                     | To create or drop indexes                             |
| Insert          | Tables                     | To insert data into tables                            |
| Lock tables     | Databases                  | To use LOCK TABLES [together with SELECT privilege]   |
| Process         | Server Admin               | To view the plain text of currently executing queries |
| Proxy           | Server Admin               | To make proxy user possible                           |
| References      | Databases,Tables           | To have references on tables                          |
| Reload          | Server Admin               | To reload or refresh tables, logs, and privileges     |
| Replication     | Server Admin               | To ask where the slave or master servers are          |
| client          |                            |                                                       |
| Replication     | Server Admin               | To read binary log events from the master             |
| slave           |                            |                                                       |
| Select          | Tables                     | To retrieve rows from table                           |
| Show databases  | Server Admin               | To see all databases with SHOW DATABASES              |
| Show view       | Tables                     | To see views with SHOW CREATE VIEW                    |
| Shutdown        | Server Admin               | To shut down the server                               |
| Super           | Server Admin               | To use KILL thread, SET GLOBAL, CHANGE MASTER, etc.   |
| Trigger         | Tables                     | To use triggers                                       |
| Create          | Server Admin               | To create/alter/drop tablespaces                      |
| tablespace      |                            |                                                       |
| Update          | Tables                     | To update existing rows                               |
| Usage           | Server Admin               | No privileges - allow connecting only                 |
+-------------------------+--------------------+-------------------------------------------------------+
31 rows in set [0.00 sec]
9

Câu lệnh MySQL ở trên sẽ tạo một dạng xem 'view_bookmast' lấy tất cả các bản ghi được nhóm w. r. t. pub_lang và được sắp xếp theo pub_lang, từ pub_lang và số lượng sách cho mỗi ngôn ngữ [pub_lang] của bảng book_mast

MySQL CREATE VIEW với BETWEEN và IN

Lệnh CREATE VIEW có thể được sử dụng với toán tử BETWEEN và IN

Thí dụ

bảng mẫu. book_mast


mysql> SHOW DATBASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hr                 |
| mysql              |
| performance_schema |
| sakila             |
| test               |
| world              |
+--------------------+
7 rows in set [0.06 sec]
0

Câu lệnh trên sẽ tạo chế độ xem 'view_bookmast' lấy tất cả các bản ghi của bảng book_mast, nếu [A]tên sách [book_name] bắt đầu bằng bất kỳ ký tự nào từ 'A' đến 'G' và [B] số trang

MySQL TẠO XEM với THÍCH

Lệnh CREATE VIEW có thể được sử dụng với toán tử LIKE

Thí dụ

bảng mẫu. tác giả


mysql> SHOW DATBASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hr                 |
| mysql              |
| performance_schema |
| sakila             |
| test               |
| world              |
+--------------------+
7 rows in set [0.06 sec]
1

Câu lệnh MySQL trên sẽ tạo một view 'view_author' lấy tất cả các bản ghi của bảng tác giả nếu [A]tên tác giả [aut_name] không bắt đầu bằng 'T' và [B] tên tác giả [aut_name] không bắt đầu

MySQL TẠO XEM bằng truy vấn con

Lệnh CREATE VIEW có thể được sử dụng với các truy vấn con

Thí dụ

bảng mẫu. mua, tựa vào, bám vào


bảng mẫu. book_mast

mysql> SHOW DATBASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hr                 |
| mysql              |
| performance_schema |
| sakila             |
| test               |
| world              |
+--------------------+
7 rows in set [0.06 sec]
2

Câu lệnh MySQL ở trên sẽ tạo một chế độ xem 'view_purchase' lấy tất cả các bản ghi của các cột invoice_no, book_name và cate_id của bảng mua hàng, nếu id danh mục [cate_id] thỏa mãn điều kiện được xác định trong truy vấn phụ [theo sau là cate_id=]

Truy vấn con chỉ truy xuất cate_id từ bảng book_mast chứa sách có 201 trang

MySQL TẠO XEM với THAM GIA

Lệnh CREATE VIEW có thể được sử dụng cùng với câu lệnh THAM GIA

Thí dụ

bảng mẫu. thể loại


bảng mẫu. mua, tựa vào, bám vào


mysql> SHOW DATBASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hr                 |
| mysql              |
| performance_schema |
| sakila             |
| test               |
| world              |
+--------------------+
7 rows in set [0.06 sec]
3

Câu lệnh MySQL ở trên sẽ tạo chế độ xem 'view_purchase' cùng với câu lệnh THAM GIA

Câu lệnh THAM GIA ở đây truy xuất cate_id, cate_descrip từ bảng danh mục và invoice_no, bill_dt và book_name từ bảng mua hàng nếu cate_id của bảng danh mục và danh mục mua giống nhau

MySQL TẠO XEM với UNION

Lệnh CREATE VIEW có thể được sử dụng với UNION

Thí dụ

bảng mẫu. book_mast


mysql> SHOW DATBASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hr                 |
| mysql              |
| performance_schema |
| sakila             |
| test               |
| world              |
+--------------------+
7 rows in set [0.06 sec]
4

Câu lệnh MySQL trên sẽ tạo một view 'view_bookmast' chứa các cột như trong 'book_mast'

Các bản ghi sẽ được chèn với sự kết hợp của ba truy vấn con

Truy vấn đầu tiên chèn các hàng đó vào chế độ xem 'view_bookmast' từ bảng 'book_mast' có 'pub_id' là 'P001'

Truy vấn thứ hai sẽ chèn các hàng đó vào chế độ xem 'view_bookmast' từ bảng 'book_mast' có các hàng có cột 'book_name' bắt đầu bằng bất kỳ chữ cái nào trong khoảng từ 'A' đến 'G'

Truy vấn thứ ba sẽ chèn các hàng đó vào chế độ xem 'view_bookmast' từ bảng 'book_mast' có các hàng có bất kỳ giá trị nào sau đây 165,250,350,400,510 trong 'no_page'

Lệnh nào được sử dụng để loại bỏ chế độ xem MySQL?

DROP VIEW xóa một hoặc nhiều chế độ xem. Bạn phải có đặc quyền DROP cho mỗi chế độ xem.

Làm cách nào bạn có thể xóa bản ghi bằng MySQL?

Nếu bạn muốn xóa bản ghi khỏi bất kỳ bảng MySQL nào, thì bạn có thể sử dụng lệnh SQL DELETE FROM . Bạn có thể sử dụng lệnh này tại dấu nhắc mysql> cũng như trong bất kỳ tập lệnh nào như PHP.

Chủ Đề