We use SQL Compare 11.6.11.2463 to move our database changes to different environments. When we doing that we notices that Table definitions were changes and Foreign Key Constraint was altered where the data in the table was not correct. I have a question here that why the constraint were modified without giving an error that data is bad for that constraint and it should have error while deploying that Foreign Key Constraint.
Second Question is, When we are seeing the dependencies of a table, it does not show all the procedures that table is referenced and used. We are missing the dependencies when we right click on a table and see View Dependencies. Is something changed by the use of SQL Compare as we are not seeing Dependencies.
Related
Violating a simple foreign key contraints in MS-SQL generates following useless error-message:
Error: The INSERT statement conflicted with the FOREIGN KEY constraint
"FK_employee". The conflict occurred in database "AgentAndAgency", table "dbo.employee", column 'id'.
SQLState: 23000
ErrorCode: 547
Missing is the detail, which key is causing the foreign key contraint, so, e.g. PostgreSQL would say in the same situation:
Error: inserting into table employement violates foreign key contraint- „FK_employee“
Details: key (employee_id)=(958980) does not exist in table „employee“.
MS-SQL does not provide this information, which makes it completely useseless (I'm bulk-inserting thoundsands of records).
Question: how can I make MS-SQL tell me at least one missing key?
Because the data may contain multiple errors, the "identify one error, fix that and iterate" technique tends not to work out well.
No server, so far as I'm aware, will identify all missing keys - they terminate the work as soon as they've identified that an error has occurred1 since the presence of just a single error may mean that the entire task needs to be aborted, rather than the data needing fixing.
To identify all of the errors, a better approach is to perform your bulk-insert into a staging table that doesn't have any constraints. Then write a query that left joins to the employee table and identifies all missing keys.
1Rather than potentially wasting resources by attempting to identify the complete set. But from a "relational-purist" perspective, that's what they ought to do - since we try to keep everything set-based, the errors ought to be sets too.
Instead of the update, do a SELECT from your update data WHERE employee_id NOT IN (SELECT employee_id FROM employee)
Should give you the list of missing employee_ids
I was trying to create a table via SSMS 11.0.6020.0 using SQL Server, via the table designer. I have defined the primary key, the columns and foreign keys. All was well, but when I have hit Ctrl+s to save the script, I received an error message stating that On Delete Cascade may cause cycles and change the remove behavior. I have followed the instructions given and changed the on delete and on update to do nothing for the nullable column used as foreign key, but I wonder what is the problem with on delete cascade and on update cascade for nullable foreign key.
My question is related to this one, but as far as I know it is not a duplicate, since I have a single reference to a given table and the problem was that it was nullable and the other question is about multiple references. The error message is similar though.
I believe that if that column is null for a record, then it does not have a reference in the referenced table, therefore there is no reference which could trigger delete/update on that record. However, if there is a reference, that could act as if the column was not nullable in that case. Is there an objective reason for this behavior (like performance, indexing, etc.)? I can simply not see the possible cycles when a nullable column is used as a foreign key, as the null values should be immune to changes at the reference table according to my opinion.
I am trying to remove all data from a table and re-add it, but I get the below exception:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code Additional information: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
At first I though it was pretty obvious, until I looked at my database (below). To check if I was missing something I deleted all relevant rows in SSMS successfully. If I can delete the rows without foreign key constraints in SQL, why cant I do it in EF?
EDIT:
I am trying to delete from the rooms table
Based on the error message and that you are attempting to delete from the Rooms table, it seems likely that you have an enforced foreign key constraint on the Images table tied to RoomID.
You can determine what relationship is causing the issue by right-clicking the Rooms table in SSMS and clicking "Show Dependencies". This will show you all of the schema-bound dependencies which will be based on foreign keys.
Assuming that is the case, deleting those rows from Rooms would orphan the records in Images or potentially tie them to the records you are inserting (if doing an identity insert), not certain what your intention is. You have two options if you wish to proceed:
Delete the records constrained by the foreign key.
Disable the enforcement of the foreign key in SSMS by right-clicking the foreign key, click Modify, and set "Enforce Foreign Key Contraint" to "No". You may also need to set the Insert and Update specifications to No Action.
Ideally you are just deleting some records and removing the constraint is not necessary, since that compromises the referential integrity of your database.
I'm having problems adding a cascade delete onto a foreign key in SQL Server. Table A has three columns. Column 1 and 2 in Table A are foreign key look ups to the same column in Table B. I want a delete of a row in Table B to cascade a delete on a row on Table A based on these foreign keys.
The other column in Table A has a foreign key lookup to table C. If a row in table C is deleted then I want the corresponding cell to be set to null in Table A.
When I add in these constraints I am thrown the error:
Introducing FOREIGN KEY constraint 'FK_RDU_TODELIVERABLEUNITREF' on table 'RelatedDeliverableUnit' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
I am a little stuck with this, Oracle seems perfectly happy with this logic. I am adding in these constraints using Liquibase. I think the error is down to my logic and not syntax but for completeness here is the liquidbase script that manages the foreign keys:
<addForeignKeyConstraint constraintName="FK_RDU_FROMDELIVERABLEUNITREF" baseTableName="relatedDeliverableUnit"
baseColumnNames="FROMDELIVERABLEUNITREF" referencedTableName="DELIVERABLEUNIT" referencedColumnNames="DELIVERABLEUNITREF" onDelete="CASCADE"/>
<addForeignKeyConstraint constraintName="FK_RDU_TODELIVERABLEUNITREF" baseTableName="relatedDeliverableUnit"
baseColumnNames="TODELIVERABLEUNITREF" referencedTableName="DELIVERABLEUNIT" referencedColumnNames="DELIVERABLEUNITREF" onDelete="CASCADE"/>
<addForeignKeyConstraint constraintName="FK_RDU_RELATIONSHIPREF"
baseTableName="relatedDeliverableUnit" baseColumnNames="RELATIONSHIPREF" referencedTableName="RELATIONSHIPTYPES" referencedColumnNames="RELATIONSHIPREF" onDelete="SET NULL"/>
Thanks in advance for any help
I can't find corresponding documentation for later versions, but the SQL Server 2000 BOL addresses this issue:
The series of cascading referential actions triggered by a single DELETE or UPDATE must form a tree containing no circular references. No table can appear more than once in the list of all cascading referential actions that result from the DELETE or UPDATE. The tree of cascading referential actions must not have more than one path to any given table. Any branch of the tree is terminated when it encounters a table for which NO ACTION has been specified or is the default.
And later versions haven't changed this. You're falling foul of this:
The tree of cascading referential actions must not have more than one path to any given table
The only way I know of to accomplish this is to implement one of the cascades between B and A using an INSTEAD OF trigger, rather than using ON DELETE....
The relation between tables A and C shouldn't be impacted by any of this.
(2008 BOL)
I have created tables in sql server. And i have also inserted data/rows in that tables.
Now i want to make relationship among them means i want to create foreign key constraints among them, is it possible ?
Whenever i try to create relationship among table a problem is occured. "Saving changes is permitted, The changes you made required table to re-created and dropped"
Please suggest me what should i do to make relationship(foreign key) among them ?
My Child table design is this
this is my parent table:-
please now right what alter query i should write..?
You can try this link
"Error message when you try to save a table in SQL Server 2008: "Saving changes is not permitted"
Another solution is below.
I think the problem is because of a feature when using the GUI. If you have a look at this link it shows you how to work round it. It is a feature which prevents you from dropping and recreating the table which is what SSMS does in the background when you click ok.
The code provided by the previous posted is the best way to do this.
You could do this with a script like this:
alter table ChildTable
add constraint FK_ChildTable_ColumnName foreign key (ColumnName) references ParentTable(PrimaryKeyColumnName)
[Edit] If I read your description correctly the script would be:
alter table emp
add constraint FK_emp_salary foreign key(salary) references testing(roll)
You can only add foreign constraints that aren't violated by existing data. You may also have to add suitable indices first, although this depends on the DBMS. In any case, first make sure your existing data is compatible with the foreign keys you want to introduce. In other words, if you were to create the foreign key first, and then insert the data, you should not produce any foreign key violations.