Relationship question

Hello all,

I am trying to create a relationship between 2 tables and am getting an error. I did a click and drag from the primary key of 1 table to the primary key of another and set the constraints to ‘Update cascade’ and ‘Delete cascade’.

Could someone translate the somewhat cryptic language that Base is ‘teasing’ me with? ha…

Here is the message…

“Primary or unique constraint on main table: “Table1” in statement [ALTER TABLE] “table2” ADD FOREIGN KEY (“table2 ID”) REFERENCES “Table1” (“Table1 ID”) ON UPDATE CASCADE ON DELETE CASCADE]”

I don’t know enough to decipher it…I would appreciate someone weighing in to help educate me…

Thanks…SC

When linking two Tables the linked Fields in each Table must be the same data type and have the SAME NAME. Looking at the SQL you posted it appears to be trying to link “table2 ID” to “Table1 ID”.

Consider this example. A Database is being used to record orders received from customers. A “Customers” Table contains all the details for a customer. It has a “CustomerID” Field, the Primary Key. This can be created automatically by making it an Auto Value Integer. This will start at 0 and will be auto entered every time a new record is created in the Table and incremented by 1 for every new Record. An “Order” Table contains details of all orders. It has an “OrderID” field, the Primary Key which can be created again using Auto Value Integer. It also contains a “CustomerID” field as integer. A link is created between the “CustomerID” Fields in both Tables.

The “CustomerID” Field in the “Order” Table should not be the Primary Key as otherwise there could be only one order for a customer and there would be no point in having the Relational Model for the Database.

If when creating the link between the Tables the Cascade option is not set then you would not be able to delete any record from the Customer table which had orders in the Order Table as this breach the referential integrity of the Database. You would get an error from the Database Engine as it would result in orphaned records in the Orders Table - records which contained a “CustomerID” which no longer existed in the Customer Table.

If you set Delete Cascade on then when an attempt is made to delete a Customer from the Customer Table the Database Engine would also delete all records from the Orders Table with that “CustomerID” and this would not generate an error.
Similarly if Update Cascade is set to on and you wanted to change the “CustomerID” field to a different number, the records in the Order Table that had the “CustomerID” that was being changed would also be updated to the new number so that they would continue to be linked to the correct Customer. With Update Cascade set to off then changing the “CustomerID” Field would result in orphaned records whose “CustomerID” Field would be linked to a “CustomerID” Field that no longer existed and would not be allowed giving an error.

You put your finger on my misconception. I was under the impression that the relationship just pointed to the column to relate to and not the same name. It appears I was trying to relate apples and oranges.

Does this mean a return cust needs a new ID for a new order or can U use the old Cust ID with a new Order ID? IOW do you need a whole and completely new record each time? I was trying to use customer ID 1 time only and just add new orders in the orders table refering back to the 1 cust ID

When a Customer, which is already in the “Customer” Table, places further orders you use that “CustomerID” in the “Order” Table. You create a new record in the “Order” Table for each order with the “CustomerID” in that new “Order” record. That is the principle of a Relational Database. You will only need to create a new entry in the “Customer” Table for a Customer who has not ordered before. All orders from the same Customer would have that Customers “CustomerID” in all the records relating to -

contd - orders from that Customer in the “Orders” Table.

Thank you…thank you…THANK YOU… I’ve been fighting this thing for a couple of days and you pinpointed my error…Now I just need to go back and edit my tables to match the concept… Good to learn new concepts and principles…sure helps to clear up confusion…You’re kind to share your knowledge and it is very much appreciated…

@Santacarl glad you have been able to sort it.