Câu hỏi phỏng vấn MySQL
Câu hỏi

What is a VIEW in MySQL. How can you create and query a view?

Câu trả lời

In MySQL, a VIEW is essentially a virtual table that is based on the result-set of an SQL statement. It contains rows and columns, just like a real table, but does not store data of its own. Instead, it displays data that is stored in other tables. This means that a VIEW allows you to create a selectable interface to the data that your SQL statement represents. It is particularly useful for encapsulating complex SQL queries, simplifying data access, and managing data security by restricting access to a predetermined set of rows and columns[1][2][6][11].

Creating a VIEW

To create a VIEW in MySQL, you use the CREATE VIEW statement followed by the view name and the SQL statement that defines the view. The basic syntax is as follows:

sql Copy
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Here, view_name is the name you want to give to your view, column1, column2, etc., are the columns you want to include in your view, table_name is the name of the table where these columns are located, and condition is any condition you want to apply to the rows included in your view[1][2][6][11].

Example

Suppose you have a table named employees with columns for id, name, department, and salary. You want to create a view that shows the name and department of all employees in the 'IT' department. The SQL statement to create this view would look like this:

sql Copy
CREATE VIEW it_employees AS
SELECT name, department
FROM employees
WHERE department = 'IT';

Querying a VIEW

Once a VIEW is created, you can query it in the same way you would query a r...

junior

junior

Gợi ý câu hỏi phỏng vấn

middle

What is the difference between commands create database and create schema in MySQL?

middle

How can VIEW be used to provide security layer for your app?

middle

What is MySQL Workbench?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào