Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create view for insert query

i have created a table name "viewex"

create table viewex(
    sno int,
    name varchar(30),
    email varchar(30),
    address varchar(50),
    contact varchar(30)
);

Inserted data to the table.

Now I am intrested to insert data only for 3 columns (name, address, contact):

insert into viewex(name, address, contact) values('celcabs', 'good', 'bad');

Now the issue is.......... Is it possible to create the view for the query

insert into viewex(name, address, contact) values('celcabs', 'good', 'bad');
like image 788
Laxminarayan Alas Avatar asked Sep 06 '11 07:09

Laxminarayan Alas


People also ask

Can insert be done on a view?

You can insert rows into a view only if the view is modifiable and contains no derived columns. The reason for the second restriction is that an inserted row must provide values for all columns, but the database server cannot tell how to distribute an inserted value through an expression.

Can we use a view with a join query to insert data?

you cannot insert record into complex view(means select query with joins). But you can insert records by creating INSTEAD of trigger on the view. You can use below code for your issue. But be sure on primary keys that your going to insert, if any on the table.

Can a view be created from any SQL query?

A view is actually a composition of a table in the form of a predefined SQL query. A view can contain all rows of a table or select rows from a table. A view can be created from one or many tables which depends on the written SQL query to create a view.

Can we insert data into view in MySQL?

In MySQL, views are not only query-able but also updatable. It means that you can use the INSERT or UPDATE statement to insert or update rows of the base table through the updatable view. In addition, you can use DELETE statement to remove rows of the underlying table through the view.


2 Answers

As of PostgreSQL 9.3 you can insert into and update "simple views": http://www.postgresql.org/docs/9.3/static/sql-createview.html

like image 200
Trygve Laugstøl Avatar answered Sep 28 '22 12:09

Trygve Laugstøl


What you're looking for is an updatable view and postgresql doesn't have direct support for them.

You can get the effect using CREATE RULE - that page has info on how to get the effect of an updatable view.

like image 23
Brian Roach Avatar answered Sep 28 '22 12:09

Brian Roach