How to define composite keys in MS Access? - ms-access-2007

I am new to MS Access. Could anybody tell me how to define a composite primary key in MS Access 2007.

If memory serves correctly, you can hold SHIFT while selecting the fields in the design view. Once you've selected all of the fields that are to be part of the key, press the primary key button.

Already answered, but I wanted to chime in with additional info.
After solving my related problem, holding "ctrl" + clicking multiple columns in design view and then clicking the "primary key" icon in the upper left worked great.
I was receiving an error that stated,
The changes you requested to the table were not successful because
they would create duplicate values in the index, primary key or
relationship.
I wasn't sure if it was my data, or if I was doing something wrong. Sure enough it was my data - I had accidentally entered duplicate data without realizing it.
I used the following query that I skimmed off of a dbforums.com post to quickly identify the problem:
SELECT ColumnName1
, ColumnName2
, ColumnName3
, ColumnName4
FROM TableName
GROUP
BY ColumnName1
, ColumnName2
, ColumnName3
, ColumnName4
HAVING Count(*) > 1
After adapting the SQL to my tables the query immediately pointed out the offending entry. I eliminated it, fixed the issue in my table that allowed the duplicate to be created, and was able to create my composite primary key no problem.

Related

The INSERT statement conflicted with the FOREIGN KEY constraint__ [duplicate]

I am getting the following error. Could you please help me?
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Sup_Item_Sup_Item_Cat". The conflict occurred in database "dev_bo", table "dbo.Sup_Item_Cat". The statement has been terminated.
Code:
insert into sup_item (supplier_id, sup_item_id, name, sup_item_cat_id,
status_code, last_modified_user_id, last_modified_timestamp, client_id)
values (10162425, 10, 'jaiso', '123123',
'a', '12', '2010-12-12', '1062425')
The last column client_id is causing the error. I tried to put the value which already exists in the dbo.Sup_Item_Cat into the column, corresponding to the sup_item.. but no joy :-(
In your table dbo.Sup_Item_Cat, it has a foreign key reference to another table. The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.
If you have SQL Server Management Studio, open it up and sp_help 'dbo.Sup_Item_Cat'. See which column that FK is on, and which column of which table it references. You're inserting some bad data.
Let me know if you need anything explained better!
I had this issue myself, regarding the error message that is received trying to populate a foreign key field. I ended up on this page in hopes of finding the answer. The checked answer on this page is indeed the correct one, unfortunately I feel that the answer is a bit incomplete for people not as familiar with SQL. I am fairly apt at writing code but SQL queries are new to me as well as building database tables.
Despite the checked answer being correct:
Mike M wrote-
"The way a FK works is it cannot have a value in that column that is
not also in the primary key column of the referenced table."
What is missing from this answer is simply;
You must build the table containing the Primary Key first.
Another way to say it is;
You must Insert Data into the parent table, containing the Primary
Key, before attempting to insert data into the child table containing
the Foreign Key.
In short, many of the tutorials seem to be glazing over this fact so that if you were to try on your own and didn't realize there was an order of operations, then you would get this error. Naturally after adding the primary key data, your foreign key data in the child table must conform to the primary key field in the parent table, otherwise, you will still get this error.
If anyone read down this far. I hope this helped make the checked answer more clear. I know there are some of you who may feel that this sort of thing is pretty straight-forward and that opening a book would have answered this question before it was posted, but the truth is that not everyone learns in the same way.
You are trying to insert a record with a value in the foreign key column that doesn't exist in the foreign table.
For example: If you have Books and Authors tables where Books has a foreign key constraint on the Authors table and you try to insert a book record for which there is no author record.
You'll need to post your statement for more clarification. But...
That error means that the table you are inserting data into has a foreign key relationship with another table. Before data can be inserted, the value in the foreign key field must exist in the other table first.
The problem is not with client_id from what I can see. It looks more like the problem is with the 4th column, sup_item_cat_id
I would run
sp_helpconstraint sup_item
and pay attention to the constraint_keys column returned for the foreign key FK_Sup_Item_Sup_Item_Cat to confirm which column is the actual problem, but I am pretty sure it is not the one you are trying to fix. Besides '123123' looks suspect as well.
Something I found was that all the fields have to match EXACTLY.
For example, sending 'cat dog' is not the same as sending 'catdog'.
What I did to troubleshoot this was to script out the FK code from the table I was inserting data into, take note of the "Foreign Key" that had the constraints (in my case there were 2) and make sure those 2 fields values matched EXACTLY as they were in the table that was throwing the FK Constraint error.
Once I fixed the 2 fields giving my problems, life was good!
If you need a better explanation, let me know.
I ran into this problem when my insert value fields contained tabs and spaces that were not obvious to the naked eye. I had created my value list in Excel, copied, and pasted it to SQL, and run queries to find non-matches on my FK fields.
The match queries did not detect there were tabs and spaces in my FK field, but the INSERT did recognize them and it continued to generate the error.
I tested again by copying the content of the FK field in one record and pasting it into the insert query. When that record also failed, I looked closer at the data and finally detected the tabs/spaces.
Once I cleaned removed tabs/spaces, my issue was resolved. Hope this helps someone!
Double check the fields in the relationship the foreign key is defined for. SQL Server Management Studio may not have had the fields you wanted selected when you defined the relationship. This has burned me in the past.
run sp_helpconstraint
pay ATTENTION to the constraint_keys column returned for the foreign key
I had the same problem when I used code-first migrations to build my database for an MVC 5 application. I eventually found the seed method in my configuration.cs file to be causing the issue. My seed method was creating a table entry for the table containing the foreign key before creating the entry with the matching primary key.
Parent table data missing causes the problem.
In your problem non availability of data in "dbo.Sup_Item_Cat" causes the problem
I also got the same error in my SQL Code, This solution works for me,
Check the data in Primary Table May be you are entering a column value which is not present in the primary key column.
The problem was reproducible and intermittent for me using mybatis.
I'm sure I had correct DB configuration (PK, FK, auto increment etc)
I'm sure I had correct order of insertions (parent records first), in debug I could see parent record inserted with respective PK and just after that next statement failed with inserting child record with correct FK inside.
The problem was fixed by for reseeding identity with
DBCC CHECKIDENT ('schema.customer', RESEED, 0);
DBCC CHECKIDENT ('schema.account', RESEED, 0);
Exactly the same code that failed before started to work.
I would like somebody to explain me what was causing the issue.
In my case, I was inserting the values into the child table in the wrong order:
For the table with 2 columns: column1 and column2, I got this error when I mistakenly entered:
INSERT INTO Table VALUES('column2_value', 'column1_value');
The error was resolved when I used the below format:-
INSERT INTO Table (column2, column1) VALUES('column2_value', 'column1_value');
If your FK column table should contain that FK value as a primary key Value then data will be inserted.

Can I use one Azure Search Indexer to index multiple entities with the same Key?

Basically I got two datasources(cosmos db, azure sql), one index and two
indexers.
Both indexers are sharing the same primary key which allows me to join the data from both sources into one index. The issue right now is that the cosmos db contains multiple entries with the same key that is used in the indexers as the primary key, which then by default(I assume) just flattens all entries with the same key and only indexes the latest one found. It runs without errors, but obviously entries are missing as only the last one found is indexed.
The only solution so far is that I index the cosmos db in another indexer using the unique key. I kinda wanted to avoid having multiple search queries, but seems this is the only solution, unless anyone's got a better idea. Thank you!
No, you can not use a same key for multiple docs , key is an unique ID of each doc for looking up. If you adding multiple doc with same key to your index , sys will act multiple update operations on the doc with that key so that you can see the last record only.
Maybe my case is similar as yours which will be helpful , this is my index :
And this is the data in my cosmos db :
as you can see, the itemid is the key of my index and its value in my cosmos db all are same which is 1 .
In my case , I use the _rid value to replace the itemid value while creating data source by sql query below :
SELECT u._rid as itemid, u.FirstName , u.LastName,u.Email , u._ts FROM user u where u._ts >= #HighWaterMark ORDER BY u._ts
As you can see, index has been imported and this issue is solved :
With this way , you can import data to your original index without same key issue.
If there is any misunderstanding or unclear , pls feel free to let me know .

Get RowID on Progress Insert

I have a table which doesn't have primary key. I needed to add a primary key to the table so I added a column called 'ID'. I am attempting to use the rowid to insert unique ids into this new column. How would I go about getting the rowid when inserting a new record. This is in a Progress database.
INSERT INTO PUB.DETAILS (LASTUPDATED, FORMERVALUE, NEWVALUE, ID)
VALUES ('09/16/2015', 'NEW ITEM', 'ESISTING ITEM', '?')
Progress databases doesn't necessary have a key. At least not in the "SQL way". The keys (as well as the relations) are defined by the business logic (ie how you use the fields.
Since you seem to be working with a database that's in use it might simply be that you don't need a key - there's some kind of logic there already that does the job?
There is a thing called "sequence" in Progress databases that can be used to increase a value - how to access them using odbc or sql I really don't know.
In Progress ABL (4GL) you would say NEXT-VALUE(sequence-name)
Here's some help about SQL and Progress dbs.
Just set your ID column to autoincrement, so you won't need to know the last inserted one.

How to Troubleshoot composite key violation in MS Access

My Access database (2007) has Four tables; Customer, Supplier, Account, and AccountAgeing
AccountAgeing has a composite key made up of the foreign keys of two of the other tables, plus a date. i.e.;
AsAtDate, SupplierID, AccountNumber
I am importing data from excel via a temporary table, and my parent tables (Customers, Suppliers, Accounts) are importing well.
However importing AccountAgeing from my tempTable continually has a key violation. Of 749 possible imports, 746 violate the key. A query to test was:
SELECT DISTINCT tempTable.[SupplierID], #31/7/14#, tempTable.[AccountNumber]
FROM tempTable;
This returned 749 records (all of them). If this is the case, how do I have a key violation??
The composite key fields are all indexed, with duplicates OK. There is no data in the destination table
I have date and [Account Number] indexed as these are the fields searches will be on.
Here is a sequence of some troubleshooting steps you can try.
Remove the primary key from your target table and populate it. If you can't populate the target table, your problem may not be the key itself, and may become apparent based on error messages you receive.
If the target table does populate, try adding your desired composite key to the already populated target table.
If you are unable to add the key, re-run your "select distinct" query on the populated target table.
If you don't select 749 distinct rows, visually inspect the table contents to see what's going on.
These steps should lead you to some insight. Just a guess - but it sounds possible that you may have a data type mismatch somewhere. In cases like this, Access will sometimes convert data on the fly and insert it without giving an error. But in the process the nature of the data are changed, resulting in a key violation in the target table.
I'm curious to hear what you find. Please post a comment when you figure out what the problem is.
Hope it helps. Good luck with the troubleshooting.
Thank you Marty!! I attempted to populate manually, which errored because there was no matching record in the Customers table.
I discovered that I had incorrectly assigned AccountAgeing to be the parent of Customers, rather than of Accounts.
The business logic is that an AccountAgeing record will always have an Account, but an AccountAgeing record does not always mention Company Number (the primary key of the Customer table).
The fix was binding part of the Account Ageing composite key to the Accounts composite key.
I am unsure what will happen when I add an ATBRecord which has an account number but no Company number, but that is another question
Check the Indexed Property in table properties - make sure it is not set at Duplicates OK on any of the composite key fields

INSERT statement conflicted with the FOREIGN KEY constraint - SQL Server

I am getting the following error. Could you please help me?
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Sup_Item_Sup_Item_Cat". The conflict occurred in database "dev_bo", table "dbo.Sup_Item_Cat". The statement has been terminated.
Code:
insert into sup_item (supplier_id, sup_item_id, name, sup_item_cat_id,
status_code, last_modified_user_id, last_modified_timestamp, client_id)
values (10162425, 10, 'jaiso', '123123',
'a', '12', '2010-12-12', '1062425')
The last column client_id is causing the error. I tried to put the value which already exists in the dbo.Sup_Item_Cat into the column, corresponding to the sup_item.. but no joy :-(
In your table dbo.Sup_Item_Cat, it has a foreign key reference to another table. The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.
If you have SQL Server Management Studio, open it up and sp_help 'dbo.Sup_Item_Cat'. See which column that FK is on, and which column of which table it references. You're inserting some bad data.
Let me know if you need anything explained better!
I had this issue myself, regarding the error message that is received trying to populate a foreign key field. I ended up on this page in hopes of finding the answer. The checked answer on this page is indeed the correct one, unfortunately I feel that the answer is a bit incomplete for people not as familiar with SQL. I am fairly apt at writing code but SQL queries are new to me as well as building database tables.
Despite the checked answer being correct:
Mike M wrote-
"The way a FK works is it cannot have a value in that column that is
not also in the primary key column of the referenced table."
What is missing from this answer is simply;
You must build the table containing the Primary Key first.
Another way to say it is;
You must Insert Data into the parent table, containing the Primary
Key, before attempting to insert data into the child table containing
the Foreign Key.
In short, many of the tutorials seem to be glazing over this fact so that if you were to try on your own and didn't realize there was an order of operations, then you would get this error. Naturally after adding the primary key data, your foreign key data in the child table must conform to the primary key field in the parent table, otherwise, you will still get this error.
If anyone read down this far. I hope this helped make the checked answer more clear. I know there are some of you who may feel that this sort of thing is pretty straight-forward and that opening a book would have answered this question before it was posted, but the truth is that not everyone learns in the same way.
You are trying to insert a record with a value in the foreign key column that doesn't exist in the foreign table.
For example: If you have Books and Authors tables where Books has a foreign key constraint on the Authors table and you try to insert a book record for which there is no author record.
You'll need to post your statement for more clarification. But...
That error means that the table you are inserting data into has a foreign key relationship with another table. Before data can be inserted, the value in the foreign key field must exist in the other table first.
The problem is not with client_id from what I can see. It looks more like the problem is with the 4th column, sup_item_cat_id
I would run
sp_helpconstraint sup_item
and pay attention to the constraint_keys column returned for the foreign key FK_Sup_Item_Sup_Item_Cat to confirm which column is the actual problem, but I am pretty sure it is not the one you are trying to fix. Besides '123123' looks suspect as well.
Something I found was that all the fields have to match EXACTLY.
For example, sending 'cat dog' is not the same as sending 'catdog'.
What I did to troubleshoot this was to script out the FK code from the table I was inserting data into, take note of the "Foreign Key" that had the constraints (in my case there were 2) and make sure those 2 fields values matched EXACTLY as they were in the table that was throwing the FK Constraint error.
Once I fixed the 2 fields giving my problems, life was good!
If you need a better explanation, let me know.
I ran into this problem when my insert value fields contained tabs and spaces that were not obvious to the naked eye. I had created my value list in Excel, copied, and pasted it to SQL, and run queries to find non-matches on my FK fields.
The match queries did not detect there were tabs and spaces in my FK field, but the INSERT did recognize them and it continued to generate the error.
I tested again by copying the content of the FK field in one record and pasting it into the insert query. When that record also failed, I looked closer at the data and finally detected the tabs/spaces.
Once I cleaned removed tabs/spaces, my issue was resolved. Hope this helps someone!
Double check the fields in the relationship the foreign key is defined for. SQL Server Management Studio may not have had the fields you wanted selected when you defined the relationship. This has burned me in the past.
run sp_helpconstraint
pay ATTENTION to the constraint_keys column returned for the foreign key
I had the same problem when I used code-first migrations to build my database for an MVC 5 application. I eventually found the seed method in my configuration.cs file to be causing the issue. My seed method was creating a table entry for the table containing the foreign key before creating the entry with the matching primary key.
Parent table data missing causes the problem.
In your problem non availability of data in "dbo.Sup_Item_Cat" causes the problem
I also got the same error in my SQL Code, This solution works for me,
Check the data in Primary Table May be you are entering a column value which is not present in the primary key column.
The problem was reproducible and intermittent for me using mybatis.
I'm sure I had correct DB configuration (PK, FK, auto increment etc)
I'm sure I had correct order of insertions (parent records first), in debug I could see parent record inserted with respective PK and just after that next statement failed with inserting child record with correct FK inside.
The problem was fixed by for reseeding identity with
DBCC CHECKIDENT ('schema.customer', RESEED, 0);
DBCC CHECKIDENT ('schema.account', RESEED, 0);
Exactly the same code that failed before started to work.
I would like somebody to explain me what was causing the issue.
In my case, I was inserting the values into the child table in the wrong order:
For the table with 2 columns: column1 and column2, I got this error when I mistakenly entered:
INSERT INTO Table VALUES('column2_value', 'column1_value');
The error was resolved when I used the below format:-
INSERT INTO Table (column2, column1) VALUES('column2_value', 'column1_value');
If your FK column table should contain that FK value as a primary key Value then data will be inserted.