0

I have 3 tables. and I want to create a view. There is no problem about generating SQL query and creating a view.

But when I want to use this view in my web app, it says me (LIKE THIS):

Your view's column doesn't have a primary key

So I want to set a PK to a column in my view.

Please help. Thanks.

marc_s
9,0626 gold badges46 silver badges52 bronze badges
asked Jul 13, 2014 at 16:12
2
  • I'm guessing your web app framework is trying to do an update on the view? Commented Jul 13, 2014 at 20:06
  • yes. my web app framework requires a pk for any column in view. Commented Jul 14, 2014 at 8:47

2 Answers 2

0

You can't have a PRIMARY KEY on a VIEW. See here, here . You have to put PRIMARY KEYs and INDEXes on the underlying tables. A VIEW is a stored query - you can't have KEYs on a query.

answered Jul 13, 2014 at 16:19
6
  • first thanks for replying. and the second: okay I understand. and the view has store the column which it has a primary key in its own table. Commented Jul 13, 2014 at 16:25
  • I believe that you've understood - the view is over the columns of the base tables and it is they that would have the INDEXes and PRIMARY KEYs. Commented Jul 13, 2014 at 16:29
  • I understood your answer like this: "there is no need to set a primary key for the if any column of this view has a PK." my view has a column named as 'iduser'. this column is also in USER table & it has primary key in this table. So should I set a primary in view for this or another column? If your answer "No", but I have a problem about this. My system is not working properly. It says me error like this: "Your view doesn't have a primary key". Commented Jul 13, 2014 at 16:52
  • Edit your question and put in your table definitions and your view defintion. I'm puzzled! p.s. English not an issue :-) Commented Jul 13, 2014 at 17:05
  • and sorry for bad english Commented Jul 13, 2014 at 17:05
0

Note that Vérace's answer is specific to the MySQL product, and not general for SQL Views. For instance, SQL Server allows the definition of Indexed Views which can have both clustered and non-clustered indices.

These are of course just aggregate tables such as one could create and maintain oneself, but in this case the DBMS performs all that bookkeeping automatically. All the normal benefits of using declarative structures in place of functional programming naturally ensue.

answered Jul 13, 2014 at 17:16
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.