A business user requested that I change a dropdown list to a textbox for creating records. The dropdown list is what was created by default for relating to tables in my database. As requested I made the change and replaced the dropdown list with an #Html.EditorFor element. I now get this error message when I try to create records. Is there a possible solution that can fix this error? There aren't any syntax errors that stop me from compiling my code. This error occurs at runtime.
This means that your forward thinking database designer placed a constraint in your database to stop people entering invalid values. Your application allows the user to enter invalid values, but luckily your database is stopping it.
A foreign key is how you ensure that a table only gets it's 'lookup' values from a valid list.
If you remove the constraint you will risk putting garbage into your database that is a great effort to fix.
If you start putting invalid values in this table it means things like inner joins stop working and data starts going missing in reports.
Need a bit more information such as tables being inserted and the foreign keys on it. Likely the text entered doesn't match that of the drop down. You would likely have to remove the foreign key. However if the design was that a numeric value was being inserted and not just a text one, The users now have a text area... where's the numeric value (I'm assuming some design here which is why I asked for tables and keys)? I'd likely do the following:
Change the table structure so that the value being inserted is character based (if not already)
Remove key constraint
Update the existing records so the text of the lookup value is replacing the numeric values (assuming numeric design of foreign key.
Drop original lookup table.
Alter design so code uses distinct on the column in the remaining table for it's list of values.
The alternative is that you'd have to insert the new text value first into the lookup table, then get it's new key value then do the insert statement. I suppose this could be handled with triggers and a before update statement but the question; but unless there's a reason to keep them separate, the above numbered list seems simpler in the long run
Previously with the dropdown, you were allowing the user to chose from the predefined values, i.e. values that were present in your lookup table to which you have put a foreign key constraint. But now as user can enter any value(which might not be present in the foreign table) thus resulting in this error.
To remove this error you can drop the constraint
ALTER TABLE <TABLE_NAME> DROP CONSTRAINT <FOREIGN_KEY_NAME>
or check the value(against the foreign key table) entered by user before inserting it.
UPDATE
If you have removed the foreign key constraint, then you'll have to insert any new(you'll have to check if data is new or old) data in the lookup table, assign it an ID and then insert this new ID in the main table to create the link between the two. NOTE: You can maintain the foreign key constraint in this case.
Related
I am trying to create a table serving as a log of calculations. Upon entering data, I would like to check if the data entered in some columns exists in another table. In this other table, the data is in fact the primary key, so I could create a FOREIGN KEY constraint - however, I only want the consistency with this "foreign key" to be checked once for each row after newly inserting it, but never again. I do not want to create an actual parent-child relationship of these tables, as the 'child' should keep records regardless of changes to the 'parent'.
I have attempted to implement this using a CHECK constraint, e.g.:
CONSTRAINT CSTR_CALCLOG1 CHECK (USERID in(select USERID from USER_TABLE))
Resulting in the error:
ORA-02251: subquery not allowed here
It seems that check constraints are fairly limited as described here, and therefore not the right tool for the job:
http://www.dba-oracle.com/t_oracle_check_constraint.htm
How can this be achieved?
I want Write ALTER TABLE SQL statement to add a column to the table. The column is classified as NUMBER datatype, NOT NULL attribute, and primary key.
But it shows ORA-01758.
ALTER TABLE INSURANCE
ADD (INS_ID NUMBER PRIMARY KEY NOT NULL);
If I select DEFAULT 0, it really solves the problem, but I cannot set up a primary key and INS_ID shows 0, not (null)
Because this table's data is from a excel document, what should I solve it without delete data?
If I must delete data how restore it easily?
Typically you can either:
provide a default value so oracle can fill the column as it creates, satisfying the constraint or
create the column as nullable, fill it with relevant data, then enable the not null restriction/make it the primary key after it has data or
empty the table
1 is not an option for you, because the values will have to be unique if they are to be a primary key. You could consider associating the column with a sequence or making it an identity column though
2 is a likely option for you if an auto generated incrementing number is no good as a PK (for example the key data is already known or calculated)
3 is something you've already said is not an option
Give some thought to the ongoing maintenance requirements - every front end app that writes data into this table will need to be upgraded to understand it has a primary key unless you're using a sequence/identity or similar that provides a unique value for the row. If there will be a lot to update and you dont care to have a PK in a particular form or from some existing value/relationship elsewhere, having an auto number PK can be helpful. If this data needs to relate to existing data that has a key, you need to upgrade front end apps so they can respect the new PK
I'm working in a dev SQL database within SSMP.
We've got a junction table, jnc_roles_users, which pulls values from two lookup tables: lu_roles and lu_users.
Initially, all but one column in the junction table had the Allow Nulls checkbox checked. I then went into the design editor and checked all the boxes - got the alert that this will affect the two lookup tables, and saved the changes successfully.
Now, I want to switch back to the original table design of nothing allowing nulls except one column. But SSMS is no longer allowing me to uncheck the Allow Nulls box for one of the fields. Ironically, no problem unchecking our 'ID' primary key. It's our 'pseudo' primary key 'roleUserID' that I can't change back. When I try to uncheck 'Allow Nulls' for it and save, I get the alert:
'jnc_roles_users' table
- Unable to modify table.
Cannot insert the value NULL into column 'roleUserID', table 'jnc_roles_users'; column does not allow nulls. INSERT fails.
The statement has been terminated.
This seems counterintuitive because the column is currently stuck at 'Allow Nulls', yet this popup is telling me it can't accept nulls.
lu_roles and lu_users was saved successfully, but not jnc_roles_users which is the only table which contains 'roleUserID'.
The roleUserID column isn't technically a primary key - but we're considering it 'like' one as it does take unique values that we set. For certain reasons we're considering this column as a sort of primary key - something to do w/ database backups in different locations and how the regular ID pk might get duplicated incorrectly in different environments, so we needed a second pseudo pk of roleUserID.
Any ideas?
It's most likely that some new data with a NULL value was added to the table between the time you changed Allow NULL in the designer and the time you went to change it back.
You can try using ALTER TABLE -
ALTER TABLE jnc_role_users
ALTER COLUMN roleUserID NVARCHAR(50) NOT NULL
Change NVARCHAR(50) to the data type the roleUserID column uses
Assume that I know that updating a primary key is bad.
There are other questions which imply that the inserted and updated table records match by position (the first of one matches the first of the other.) Is this a fact or coincidence?
Is there anything that could join the two tables together when the primary key changes on an update?
There is no match of inserted+deleted virtual table row positions.
And no, you can't match rows
Some options:
there is another unique unchanging (for that update) key to link rows
limit to single row actions.
use a stored procedure with the OUTPUT clause to capture before and after keys
INSTEAD OF trigger with OUTPUT clause (TBH not sure if you can do this)
disallow primary key updates (added after comment)
Each table is allowed to have one identity column. Identity columns are not updateable; they are assigned a value when the records are inserted (or when the column is added), and they can never change. If the primary key is updateable, it must not be an identity column. So, either the table has another column which is an identity column, or you can add one to it. There is no rule that says the identity column has to be the primary key. Then in the trigger, rows in inserted and updated that have the same identity value are the same row, and you can support updating the primary key on multiple rows at a time.
Yes -- create an "old_primary_key" field in the table you're updating, and populate it first.
Nothing you can do to match-up the inserted and deleted psuedo table record keys -- even if you store their data in a log table somewhere.
I guess alternatively, you could create a separate log table that tracked changes to primary keys (old and new). This might be more useful than adding a field to the table you're updating as I suggested right at first, as it would allow you to track more than one change for a given record. Just depends on your situation, I guess.
But that said -- before you do anything, please go find a chalk board and write this 100 times:
I know that updating a primary key is bad.
I know that updating a primary key is bad.
I know that updating a primary key is bad.
I know that updating a primary key is bad.
I know that updating a primary key is bad.
...
:-) (just kidding)
This is a 2 part question.
Question 1: I am trying to create a foreign key on a table where I need to turn off the "Check Existing Data on Creation or Re-Enabling". I know theres an option visually but I'm looking for a way to do it programmatically. Is there anyway to do this?
Question 2: I have a code table and two tables A and B that need to reference that code table. I want to have these both referenced from a relationship table but I want to able to use the same column. Can I have 2 foreign keys pointing to the same column?
Yes you can have the same column inthe parent table refer to differnt columns in multiple tables.
I do not recommend turning off checking FK on creation. If you have bad data now, you need to fix it now. Otherwise the first time someone edits one of those records it will fail the FK check then.
From Books online as to why it is a bad idea to use nocheck:
If you do not want to verify new CHECK
or FOREIGN KEY constraints against
existing data, use WITH NOCHECK. We do
not recommend doing this, except in
rare cases. The new constraint will be
evaluated in all later data updates.
Any constraint violations that are
suppressed by WITH NOCHECK when the
constraint is added may cause future
updates to fail if they update rows
with data that does not comply with
the constraint.