I have to insert a person's information into a database that is running through SQL Server, and I keep getting a foreign key constraint violation.
I am trying to insert the information into a table called Person.Person, but it says the problem occurred in a table called Person.BusinessEntityID. The BusinessEntityID column is in both tables, so do I have to update both tables at the same time for it to work?
Also I noticed the BusinessEntityID value corresponds with the row number in the database, so would it be possible to set a value equal to the row number in the table?
Thank you!
Related
I have two tables, there is a foreign key relationship defined between the two tables. If I attempt to update the second table, I receive the following error:
The UPDATE statement conflicted with the REFERENCE constraint
"FK__T2__owner__48CFD27E". The conflict occurred in database "MY_DB",
table "dbo.T2", column 'T2.colomn1'.
How can I update the column in the second table that references the first table?
It probably isn't necessary to update both tables, in fact there's a good chance you don't want to. Based on your comment, I'm assuming you have a postal_code table? It seems less likely that you would have a foreign key on address but I suppose it's possible. The reason you don't want to update both is that other rows in your post_office table may use the same postal_code.
Simply add your new postal code to the postal_code table. (I've guessed at the name.)
INSERT INTO postal_code
VALUES ('1234567891')
Then do your update.
UPDATE post_office
SET address = 'Tehran p 190', postal_code = '1234567891'
WHERE social_id = '0020366760'
If you don't think any other post offices use the old postal_code you can always clean up after yourself.
DELETE FROM postal_code
WHERE id = 'someid'
When you are trying to update the entry in table T1, make sure that the value with which we are updating in a column that has foreign key reference, you update with values that are there in the referenced foreign keys table(Unless the column is nullable).
The value with which you are trying to update T2,column1 should be already present in T1.column2.
Hello i have a question i assume mainly about SQL server functionality. Im building a test database and i have stumbled a problem when i try to insert data into my tables.
picture 1 shows the error message i get when trying to add rows.
https://i.stack.imgur.com/GW3C2.png
Picture 2 shows all relations in the database
//i.stack.imgur.com/7BhHa.png
picture 3 shows the table i am currently trying to update
//i.stack.imgur.com/3JqtA.png
In the table i have a combined primary key ("SDat" and "Kurs") The error message i get implyes that primary key must be uniqe, but what dont understand is since i have a third column "Elev" which makes the row uniqe, why wont SQL server me insert this row to table? I have tried making the same database in Acess and it works so i assume problem is something in SQL server
Regards Robert
A Primary Key by definition means the value must be unique. So if you have a combined primary key on 2 fields, then that value on those 2 fields needs to be unique meaning it can only have 1 row. If you need to enforce unique values on the combination of 3 fields (SDat, Kurs, and Elev) then your PK needs to include all 3 fields.
If you really need to enforce a unique constraint across alot of fields in a table, I wouldn't use a PK to enforce that, but instead use a UNIQUE constraint.
ALTER TABLE tablename ADD CONSTRAINT constraintname UNIQUE (column1, ..., columnn)
Then you can create a different column for your Primary Key so that as you add columns and need to require those additional columns be unique, you don't have to edit your PK and rebuild your table.
I just started taking a SQL class, and for a problem the professsor wants us to "Write the SQL that will add 1 to every employee ID (intEmployeeID, it's the PK of the table) in the TEmployees table". There is a foreign key constraint to the table TEmployeeParkingSpots, and she explicitly stated that we shouldn't need to use UPDATE or DELETE, and I am at a loss.
This question have been asked by several people but my problem seems to be different.
Actually I have to merge same structured tables from different databases in postgresql into a new DB. What I am doing is that I connect to remote db using dblink, reads that table in that db and insert it into the table in the current DB like below
INSERT INTO t_types_of_dementia SELECT * FROM dblink('host=localhost port=5432 dbname=snap-cadence password=xxxxxx', 'SELECT dementia_type, snapid FROM t_types_of_dementia') as x(dementia_type VARCHAR(256),snapid integer);
First time this query runs fine, but when I run it again or try to run it with some other remote database's table: it gives me this error
ERROR: duplicate key value violates unique constraint
"t_types_of_dementia_pkey"
I want that this new tables gets populated by entries of others tables from other dbs.
Some of the solutions proposed talks about sequence, but i am not using any
The structure of the table in current db is
CREATE TABLE t_types_of_dementia(
dementia_type VARCHAR(256),
snapid integer NOT NULL,
PRIMARY KEY (dementia_type,snapid)
);
P.S. There is a specific reason why both the columns are used as a primary key which may not be relevant in this discussion, because same issue happens in other tables where this is not the case.
As the error message tells you - you can not have two rows with the same value in the columns dementia_type, snapid since they need to be unique.
You have to make sure that the two databases has the same values for dementia_type, snapid.
A workaround would be to add a column to your table alter table t_types_of_dementia add column id serial generated always and use that as primary key instead of your current.
I am newbie to MSSQL Server and don't have any knowledge about it.
i have below question.
I have added nine records with same value as show per below image in SQL Server 2005.
i Have not given any primary key to Table.
Now when i selecting one record or multiple record and hit the delete key it does not delete the records from table instead it gives me error.
You need to add a primary key to uniquely identify each record, otherwise the SQL server has no way of distinguishing the records, and therefore no way of knowing which one to delete, causing an error.
That's because you don't have any primary key and server doesn't know which row to remove. Clear the table ( DELETE * FROM dbo.Patient ) and create new Id column as a primary key.
In MSSQL you need to have a primary key for the table. This will uniquely identify each row of that particular table.
For example in Oracle you don't need this as there you can use ROWID (meaning every row from every table has a unique ID in the database). Once you know this ID you Oracle knows for sure from which table it is.
So now you can add a primary key to the table and you can make it be auto-increment - ensuring uniqueness.