CREATE VIEW
Updated at:
CREATE VIEW defines a named query stored in PolarDB-X 1.0.
Prerequisites
The PolarDB-X 1.0 instance version must be 5.4.5 or later.
Syntax
CREATE
[OR REPLACE]
VIEW view_name [(column_list)]
AS select_statementParameters
| Parameter | Description |
|---|---|
OR REPLACE | If a view with the same name already exists, replace it. Omit this clause to return an error when a duplicate name is detected. |
view_name | The name of the view. Must be unique within the database. |
column_list | An optional, comma-separated list of column names for the view. When specified, these names override the column names derived from select_statement. |
select_statement | The SELECT statement that defines the view. |
Example
The following example creates a table, defines a view over two of its columns, and queries the view.
-- Create a table partitioned by order_id
CREATE TABLE t_order (
`id` bigint(11) NOT NULL AUTO_INCREMENT,
`order_id` varchar(20) DEFAULT NULL,
`buyer_id` varchar(20) DEFAULT NULL,
`seller_id` varchar(20) DEFAULT NULL,
`order_snapshot` longtext DEFAULT NULL,
`order_detail` longtext DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `l_i_order` (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 dbpartition by hash(`order_id`);
-- Create a view that exposes order_id and order_detail
CREATE VIEW t_detail AS SELECT order_id, order_detail FROM t_order;
-- Query the view
SELECT * FROM t_detail;What's next
Is this page helpful?