The error you're having is pretty clear:
ERROR: duplicate key violates uniqueness restriction «matricula_pkey»
DETAIL: The key already exists (cod_est, cod_asig) = (1085, 1080). SQL
state: 23505
In the CREATE TABLE
of the table matricula
you can see that you have declared a first compound key (a primary key can have one or several columns).
The primary key is a restriction , which prevents duplicate rows in the table whose values are equal in the columns that make up that key.
Therefore, if in the table matricula
you declare:
... primary key (cod_est, cod_asig),
you're saying: never accept me two rows with the same values in the columns cod_est
and cod_asig
Suppose there is already a row in the table:
cod_est cod_asig
1085 1080
If you try to insert another row with those two values in those two columns, it will give you the error you are having.
However, you can insert rows like this:
cod_est cod_asig
1080 1085
1080 1080
1080 1081
1080 1082
1085 1083
That is, you are entitled to only one row for each combination of cod_est/cod_asig
.
What do I do then?
You have to decide, in the design of your table, the records that should be unique and based on that, declare it to be a primary key, or an index of uniqueness.
For example:
- If for example what should not be repeated is only
cod_est
, you declare the restriction like this: ... primary key (cod_est), ...
,
In that case, you can have rows like this:
cod_est cod_asig
1080 1085
1080 1080 --ERROR ya existe un cod_est 1080
1080 1081 --ERROR ya existe un cod_est 1080
1080 1082 --ERROR ya existe un cod_est 1080
1085 1083
1085 1084 --ERROR ya existe un cod_est 1085
1086 1085
- On the other hand, if it were
cod_asig
, you declare the restriction like this: ... primary key (cod_asig), ...
.
In that case, you can have rows like this:
cod_est cod_asig
1080 1085
1080 1080
1080 1081
1080 1082
1085 1083
1085 1084
1086 1085 --ERROR ya existe un cod_asig 1085
In the case of needing a combination of columns, then you declare the restriction as you already have it, but knowing that you will not be able to repeat rows with equal values in the columns that are part of the primary key.
Also , you can use a uniqueness index ( UNIQUE
) that indicates that the table should not have repeated values according to the criteria indicated in the index.
Here is another problem (let's say better dilemma) that may be the subject of an interesting question to ask here if it does not already exist: should I use a combined primary key or a combined single index to restrict duplication? of data?
Everything will depend on the reality of your program and your data. Restrictions are a fundamental element for the operation of the database and if they are not well established, it can cause problems such as data redundancy, slow queries, etc.
I hope at least I have clarified the problem you are having now, that is the intention of the answer ... and to open your appetite to this wonderful world of databases and to the important decision of how to set the restrictions.