Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why view with column alias is not insertable?

CREATE TABLE `projects` (
  `stars` integer, 
  `title` varchar(255)
);

CREATE VIEW projects_view AS SELECT *, title AS name FROM projects;

When I try to insert something:

INSERT INTO `projects_view` (`name`) VALUES ('Name');

I get: ERROR 1471 (HY000): The target table projects_view of the INSERT is not insertable-into

But this works in PostgreSQL.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You cannot insert because by using select * you have two references in the view to a single column.

From the documentation a view is not insertable where

Multiple references to any column of a base table (fails for INSERT, okay for UPDATE, DELETE)

It should work if you name each column:

CREATE VIEW projects_view AS 
SELECT stars, title as name FROM projects;

Fiddle example

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading