I have a Customer table, an Order table, an Orderline table and a Product table. All of them have an Autonumber field as their primary key, and Orderline has a foreign key reference to Order ID on Order table:
ORDER
-----
Order ID - Autonumber
Customer ID - Number
...
ORDERLINE
---------
OrderLine ID - Autonumber
Order ID - FK to Order
Product ID - FK to Product
Quantity
PRODUCT
-------
Product ID - Autonumber
Product details...
I have a form where I can choose a customer, and then a list of records from the Orderline table, and a query which I reference from this sub-form which lists the Order ID, Orderline ID, Product ID, Product details...
I have 2 problems.
All the orders appear, and I only want the ones associated with this order, (which should be none when the form first loads).
When I enter a Product ID that I want to add to a new order, I am expecting a new Order ID to appear, (Autoincremented) AND a new Orderline ID, (Autoincremented) and the details of the product that I have selected, corresponding to theProduct ID` I have entered, but instead I get this error message:
The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again
The thing is, the tables should create me unique keys when I try to create the new record, and when I go into each table directly and enter a new record, the autonumber does work and does create a unique key - it is just when it is trying to create both the Order ID and the Orderline ID at the same time that it seems to be failing.
I should say, I have spent days on this, searched countless search engines, watched whole series of YouTube videos on creating Order forms but to no avail. Anyone who understands Access I am sure would be able to help me, as I would be able to help anyone in a similar circumstance in a matter of minutes if this was a problem in SQL.
When you create a sub-form you have to specify the relationship between the parent and sub-form. the same relationship you have created for your tables. Then only Access will filter the records for you.
Regarding your question. You should create a new [order] record in [order] table where you will be entering/selecting [customer_id, staff_id, order details ect]
one order can have more than one items so your [order_items] table (i assume orderline is the term you use for this table) exists of
order_id
product_id (order_id, product_id composite key)
quantity
price
etc..
Now when you want to start taking order you need to create a new form that is bound to tbl_order. In the frm_order you will have a sub-from which is bound to tbl_oder_items (in your case orderline)
The frm_order and frm_oder_items should be have a relationship. usually when you drag the table to create the subform ACCESS will ask to set the relationship. if you create the subform manually:
Select the sub-form
go to property sheet
select the link master field : order_id
select the link child field : order_id
Now when you open the frm_order it will show all the records (in other words all the products the order has in its list) from the tbl_order_items table.
Your tbl_order_item /orderline table also referencing to the product table via the product_id field.
Insert a combobox in the frm_order_items and bound it to the product_id. The combobox's rowsource would be
select product_id, product_name from tbl_product
This error message will occur:
'The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again'
when you try to add a product twice for the same order. instead you should increase the quantity for the product.
Try this and let us know how it went.
Related
I have two tables defined as:
dealership_inventory(vin, dealer_id, price, purchase_date)
where vin is the PK and dealer_id is the FK
transactions(transaction_id, dealer_id, customer_id, vin, cpurchase_date, price)
where transaction_id is PK and dealer_id,customer_id, and vin are FKs
Whenever I add a new transaction to the transactions table with an insert statement, I would like to remove that tuple with matching vin from the dealership_inventory table. Is this possible with some type of constraint?
You don't really need to do the removal of the VIN number from the inventory table. Instead, if you want to find out whether a vehicle be still available, just use an exists query, e.g.
SELECT di.*
FROM dealership_inventory di
WHERE NOT EXISTS (
SELECT 1
FROM transactions t
WHERE t.vin = di.vin
);
If, at some later point, the inventory table gets bogged down with items no longer available, you can run a batch job which moves these sold items to another table.
My database is called 'shopping' and it contains the following tables:
products (product_id, product_name, price, category_id)
product_category (category_id, cagtegory_name)
customers (customer_id, username, pass, fname, lname, email)
orders (order_id, order_date, product_id, customer_id)
Now I want to consider keeping records of each products stock levels.
i.e
product_id = 1
product_name = MICROSOFT Limited Edition Gears 5 Xbox One X, Tekken 7 & Project Cars 2 Bundle
price = 449.99
category_id = 2
stock _id = 1 ( references stock_id in product_stock table ) :-/ ?
I have considered THREE approaches to storing 'stock info' on products.
Approach a) Add a new Column in products table called 'stock_level' simple, and valid since stock level is entirely dependent on its primary key in this situation.
Approach b) Create a new table called 'product_stock' , with the columns 'stock_id, and stock_level'.
The confusion is arising in the logic here.
For some reason, having gone with approach b, I had made the primary key of product_stock table (stock_id) ALSO a foreign key, referencing back to the products table as:
product_stock.stock_id = products.product_id
i.e
product_stock.stock_id 1 REFERENCES BACK TO products.product_id 1
product_stock.stock_id 2 REFERENCES BACK TO products.product_id 2
etc etc
THE THIRD approach I thought about was keep the stock_id key as a Primary Key only, and sever the link back to Products table. Then in products, create a column called stock_id with is a FK referencing stock_id in product_stock table.
Which of the three approaches seems the best ? I for some reason, regret deleting the columns and relations set up which was giving me behavior as discussed in approach b.
The reason I liked approach b was because, if you hover over each stock_id key in product_stock, you can see instantly what product it relates to in the products table ( phpmyadmin ).
thoughts on this folks ?
Refer to question and read.
no need, its all on phpmyadmin and not required in this case.
refer to question.
As far as you explained, each product has only and only one stock value. The simplest way to proceed is to to store that information directly in a products table: option a) in your question.
Using a separated table would be useful if, for example, you were in a situation where you have several stores and you need to keep track of the stock level of each product in each store. You would then create a separated table, with a column to store the id of the product, another to store the id of the store, and a third column to store the level of this particular product in this particular stock.
In mySQL if I created two tables and on table contains a foreign key referenced to the other table. I have entered data in the other table and for the defined foreign key in that as well. Is there any way for the foreign key to automatically update in the second table without having to type in the entire data?
For example I have a customer table which has 2 fields- customerID and customerName. Another table is say a invoice table which has 3 fields- invoiceID, cost and customerID, where customerID is foreign key. So if I enter data in customer table and invoice table as the number of customers are very large I do not want to keep on entering the customerID in the invoice table. As customerID is a foreign key in the other table, how do I make it automatically reference it from customer table?
You can automatically add a predefined number, like 1, but that will assign all invoices to customer#1.
On the other hand, you can add customers automatically while adding invoices. For that, you need to control the customerID and if a customer with the given ID is not present, insert a new customer to the customer table with empty name. After that, you still need to enter names for those customers, but not by one and that would get things (maybe) easier.
I think you have a misleading idea here. I strongly suggest reading a book or seeing a tutorial online or examining an example project's database tables.
If you have the customer name in invoice data and customer data already populated, you could do
INSERT INTO tbl_invoice (invoiceData, invoiceData2, customerID)
SELECT 'value1', 'value2', customer.customerID
FROM tbl_customer customer
WHERE customer.customerName = 'customer name'
So you wouldn't need to deal with customer ids directly.
There is no automatic feature in MySQL to do exactly this. A trigger could be used for it if needed.
I am developing online shopping cart system, I have items and item_options tables.
Each Item can have 1 or more options, see below.
items table
item_id (PK)
item_name
item_description
Note: item prices are in the item_options tables
item_options table
option_id (PK)
item_id (FK)
option_name
option_price
When the customer placed an order, the data will be added into the order and order_items table.
orders table
orders_id (PK)
customer_id (FK)
address_address_id (FK)
date_purchased
orders_status
orders_date_finished
order_items table
orders_items_id (PK)
orders_id (FK)
item_id (FK)
item_option_id (FK)
There is a problem, if the staffs change the price, item name or delete a item.. the customer invoices will be affected because the FK from the order_items will no longer be existed in the items table. What is the solution to this?
How about this solution: add active field in the items and item_options tables. When the staffs want to change the price or name of a item - just turn the current active record to 0 (items.active) and then insert a new record with the new information and active become 1. The old orders still point the correct id of the entry that is not active. Is this good way doing it?
When the new information has been inserted in the items table, does that mean I have to update/change the new PK ID in the item_id.item_options table?
In something like this, I am definitely against the use of hard deletes. Every update or delete should be adding a new row into the item_options table and then marking the previous one as inactive (that way there is no chance that a user can select one of the previous values).
Each new item inserted should consist of two steps:
Add the item to the items table
Using the item_id from the items table, add the item into the item_options table
The typical solution to this that I've seen is to store the snapshot of the data that you want a history of in a separate table. That way you can store only the data you want a snapshot of at the time of the invoice, instead of all data in a particular table.
It will also help to store history data away from live data when it comes to simplifying reporting and queries in the code.. generally these two things don't need to be accessed at the same time, so you avoid the more complex queries (and heavier load on a single table) just to access one or the other.
If you are going to do things the way you describe, you are going to have to add a new rows to the item_options table to handle the new items.
So I have to InnoDB tables (Products) and (Categories). I wanted to setup a relationship between the "Category" field on the Products table, and the "CategoryId" of the Categories table.
But when attempting to create a FK it will only let me select the Primary Key "ProductsId" on the Products table and map to the PK "CategoryId" on the Categories table.
Maybe I am missing the way/reason to setup a Foreign key. My thoughts and tell me if I am wrong:
1) Was to require a product be added to a category when added, the category must exist or you must create it first. You can't remove a category unless you perform some task (programmically or on the DB backend) to remove the products from a category you are wanting to remove.
2) I was expecting the CategoryId value to be stored in the "Category" field of the Products table. Then when displaying in my view, would need to look up the Categories.Name field by the CategoriesId value.
EDIT:
So I understand that two fields involved in the Foreign keys must be the same, size, types...etc. However, how does linking up the ProductId and CategoryId work in context to what I mentioned above I am wanting to do. When I did create a FK between ProductId and CategoryId, I won't let me add a product record.
Also, the Category Name field and the Product Category field are the same type, size..etc, yet I don't get the option to select those in the foreign key tab?
How should I be setting it up so that the categories table will know what products are part of each category.
Ok, unfortunately, I must answer my own questions. The reason for most of my technical problems is because the field you are trying to make "must be indexed".
The comprehesive issue I was having was that I needed to get rid of the actual "Category" varchar field on the Products table, and create a CategoryId field that would have only a value that exists in the Category table CategoryId field.
Now I will just have to reference the Categories.Name field through the Products.CategoryId value.
At least this is what I have come to understand.
If you're on the Workbench, verify that the types, lengths and attributes of both columns involved in the FK are the same.