Update primary key - primary-key

I have a table as A (code,id,num,address)
Here code, id, and num are primary keys and no Foreign key dependency is present on any other table.
I need to update num using id… can I do that?
num was telephone number.i figurd this out.but i have another question
can we update the same column num using num only.example
UPDATE A
SET num = ''
WHERE num = '';

You could try something like this:
update A
set num = $someValue
where id = $someOtherValue;
You say there are no foreign keys that rely on this key so why not?

UPDATE A
SET num = '<value>'
WHERE id = '<identifier>';
Should suffice, if I understand your question correctly.

Given that the primary key will still be unique after the update and given that you did not define something like "alway generated" for this column: Yes, you can.

Related

Updating a column in a table based on its value from another table on id match

I have two tables:
activities (
org_id bigint,
user_id bigint,
lang text,
timestamp bigint,
...
PRIMARY KEY ((org_id, user_id), timestamp)
and
metadata (
user_id text,
org_id text,
lang text,
date_str text,
...
PRIMARY KEY ((user_id, org_id), date_str)
Given:
activities.rows >> metadata.rows
I need to
update metadata and set metadata.lang = activities.lang for all rows in metadata,
where metadata.user_id = activities.user_id and metadata.org_id = activities.org_id
Q: What would be an elegant short cql query to achieve the same?
I tried:
update metadata set metadata.lang = (select activities.lang from
activities where activities.user_id = metadata.user_id and activities.org_id = metadata.org_id)
which obviously fails for cardinality violations.
CQL doesn't support this kind of update statements (see the docs). You need to provide actual values in the where condition.
So you'll need to do everything in your code (fetch data, generate update statements, etc.), or you can use something like Spark, with something like this (not tested):
metadata = spark.read\
.format("org.apache.spark.sql.cassandra")\
.options(table="metadata", keyspace="test")\
.load()\
.select("user_id", "org_id")
activities = spark.read\
.format("org.apache.spark.sql.cassandra")\
.options(table="activities", keyspace="test")\
.load()\
.select("user_id", "org_id", "lang")
joined = activities.join(metadata,
(metadata["user_id"] == activitites["user_id"]) &
(metadata["org_id"] == activities["org_id"]), "inner")\
.select(metadata["user_id"], metadata["org_id"], "lang")
joined.write\
.format("org.apache.spark.sql.cassandra")\
.mode('append')\
.options(table="metadata", keyspace="test")\
.save()

Query to delete first added element by a topic when it exceeds provided limit

I have a table with 5 columns, one of the columns is topic. I need a query that will delete the row I added first (oldest) when I group it by topic.
I have an id column which is set as the primary key, however the increment(although it's all unique) is not reliable because of the way the data is getting filled with my python code.
This is for a SQLite database
DELETE FROM news where topic = "world"
The obvious issue with the above query is that it will delete everything when the topic = world, I only want to delete my oldest addition to the table
You can use a correlated subquery. You have specified the "oldest", so I assume there is some type of date/time column with this information:
DELETE FROM news n
WHERE n.topic = 'world' AND
n.datetimecol = (SELECT MIN(n2.datetimecol)
FROM news n2
WHERE n2.topic = n.topic
);
If not, you can use an auto-incremented primary key.
Same as other answerer said, there must be some date_time column. You can use not exists based on it as follows:
DELETE FROM news n
WHERE n.topic = 'world'
AND not exists
(SELECT 1 FROM news nn
WHERE nn.topic = n.topic
AND nn.datetimecol < n.datetimecol);

Change row in PostgreSQL

I would like to change the key value and also tried to create a test in SQLfiddle, but it did not work. The below output I have got from pgAdmin3:
Select * from api_keys;
id, create_time, user_id, key
1;"2015-11-17 01:23:08.872941";1;"d0dff17e3753a88d298ae775bb32eee6"
How is it possible to change the key value in api_keys?
You mean UPDATE?
To change all rows with key = 'd0dff17e3753a88d298ae775bb32eee6':
UPDATE api_keys
SET key = 'd0dff17e3753a88d298ae775bb32eee7'
WHERE key = 'd0dff17e3753a88d298ae775bb32eee6';

Cannot insert duplicate key row in object with unique index 'SAXXIRPT while updating

I am writing a SQL statement to update a table in SQL from a temp table. I keep getting this error message: Cannot insert duplicate key row in object with unique index 'SAXXIRPT'.
Here is my update statement:
Update dbo.sat_ser_rpt_itm
SET itm_key_cd = n.itm_key_cd,
itm_typ_cd = n.itm_typ_cd,
ser_id = n.ser_id ,
as_of_dt = n.as_of_dt,
ocrn_nr = n.ocrn_nr ,
id_rssd = n.id_rssd,
ocrn_day_txt = n.ocrn_day_txt ,
ocrn_dt = n.ocrn_dt ,
hol_flg = n.hol_flg ,
ocrn_val_nr = n.ocrn_val_nr
from #LookupTable n
on sat_ser_rpt_itm.id_rssd = n.id_rssd
AND sat_ser_rpt_itm.as_of_dt = n.as_of_dt
AND sat_ser_rpt_itm.ser_id = n.ser_id
and sat_ser_rpt_itm.itm_typ_cd = n.itm_typ_cd
and sat_ser_rpt_itm.ocrn_nr = n.ocrn_nr
where t.id_rssd is not null and t.as_of_dt is not null and t.ser_id is not null and t.itm_typ_cd is not null and t.ocrn_nr is not null
These are my indexes (clustered):
id_rssd, as_of_dt, ser_id, itm_key_cd and ocrn_nr
What's causing this error message?
There isn't much ambiguity in the error message: you are setting a duplicate somewhere
The combination already exists and you are trying to insert it again OR
It doesn't exist and you are updating multiple rows with the same
combination Or
the overlap: The combination already exists and you
are updating multiple rows with the same combination.
I think the problem causing you this Updating multiple rows with same combination
I am not sure about the what is primary key for the table dbo.sat_ser_rpt_itm
Try like this by joining two tables (dbo.sat_ser_rpt_itm,#lookup_table)
Update itm
SET
//itm_key_cd = n.itm_key_cd,
//ser_id = n.ser_id
itm_typ_cd = n.itm_typ_cd,
//as_of_dt = n.as_of_dt,
//ocrn_nr = n.ocrn_nr ,
//id_rssd = n.id_rssd,
ocrn_day_txt = n.ocrn_day_txt ,
ocrn_dt = n.ocrn_dt ,
hol_flg = n.hol_flg ,
ocrn_val_nr = n.ocrn_val_nr
FROM dbo.sat_ser_rpt_itm itm
INNER JOIN #LookupTable n
ON .................. ( it could be itm.id_rssd = n.id_rssd OR itm.as_of_dt = n.as_of_dt OR
OR itm.ser_id = n.ser_id OR itm.itm_key_cd = n.itm_key_cd OR
itm.ocrn_nr = n.ocrn_nr )
WHERE t.id_rssd is not null AND t.as_of_dt is not null
AND t.ser_id is not null AND t.itm_typ_cd is not null AND t.ocrn_nr is not null
Quite late to answer but your problem is probably simple and your key constraint (you don't show that but that's likely your issue) is doing exactly what it should be doing.
When you combine your update statement data into the constrained key, were you to do a select by that key you would find that you return a single result. The solution is to select by the key first and see if it exists and either update that if desired, xor delete the duplicate, or do something else.
so look at the key constraint which is being violated--the message should say 'duplicate key is (something,etc...) Select by that and see if you are not trying to change an existing item into another existing item.
Cheers,
Daniel Westcott

How do you do a update if you don't know the id, and just you know a information with another table relationed

I am trying to do this query:
UPDATE asignaturasemestre
SET asignatura11 = 'cambiado'
WHERE asignaturasemestre.iddatosgenerales = datosgenerales.iddatosgenerales
AND datosgenerales.curp = 'CURP'
I know this is bad, but this is the idea:
As you can see, I don't know the iddatosgenerales, but I do know it has a foreign key (iddatosgenerales). The users will write the curp only, so with that curp is not in the another table, so I need to update the another table but I don't know the id of this row.
As I have told you, I just know the CURP column, but this is in the another table (this is unique). But it is not the primary key - it doesn't mind, the id is iddatosgenerales which is a foreign key in the another table where I want to update.
This is for MySQL:
UPDATE asignaturasemestre AS a
, datosgenerales AS d
SET a.asignatura11='cambiado'
WHERE a.iddatosgenerales=d.iddatosgenerales
AND d.curp='CURP'
And this for SQL-Server:
UPDATE a
SET a.asignatura11='cambiado'
FROM asignaturasemestre AS a
JOIN datosgenerales AS d
ON a.iddatosgenerales=d.iddatosgenerales
WHERE d.curp='CURP'
update asignaturasemestre,datosgenerales
set asignatura11='cambiado'
where asignaturasemestre.iddatosgenerales=datosgenerales.iddatosgenerales
and datosgenerales.curp='CURP'
Now i have found the answer it is
update asignaturasemestre set asignatura11='cambiado'
where iddatosgenerales=(select datosgenerales.iddatosgenerales from datosgenerales inner join asignaturasemestre on
datosgenerales.iddatosgenerales=asignaturasemestre.iddatosgenerales where curp='123ABC');
I did the query normal, but when i put the iddatosgenerales i do a query getting the iddatosgenerales with curp user gave me