MS Access - A relationships already exists - ms-access

I am using MS Office 365 Access application and facing relationships already exists error When creating a Foreign Key constraint between two tables.
Table 1: sc_user where student_id is primary key
Table 2: sc_user_course where same student_id to be the foreign key
In the table 2 already one more FK is there (course_id) between sc_course and sc_user_course table.
Schema image
I tried adding two ids as a foreign key but getting error. Also I tried using Lookup method even getting same error.

Related

Should all FOREIGN KEYS have a drop down option using INSERT GUI

I have a table(APPOINTMENT) with 3 foreign keys.
These are Staff_ID, Dentist_ID, and Patient_ID.
When using the GUI INSERT function I noted only the patient_ID field has a drop down for the patients with the patient table.
However the Staff_ID and Dentist_ID the user can enter any value.
Should all foreign key fields not have a drop down for existing entries in respective tables?
EDIT- adding additional images
When I tried to add the FK again I noted the following error which for some reason I had not noted previously:
MySQL error 1452 - Cannot add or update a child row: a foreign key
constraint fails
The issue in my case was the field I was converting to a FK already had a value in it that had no corresponding value in the primary key column it referencing. There is a good explanation of the error here

MySQL error: Failed to add the foreign key constraint. Missing index for constraint

I have searched questions about this problem:
Similar question 01
Similar question 02
but I find they are not similar to my case.
Here is my tables:
Table 1 history:
create table if not exists history(
worker_num int(11),
cust_num int(11),
primary key (cust_num,worker_num)
)engine=innodb, default charset=utf8;
Table 2 customer:
drop table if exists customer;
create table if not exists customer(
cust_number int(11) not null,
foreign key (cust_number) references history(cust_num)
)engine=innodb, default charset utf8;
Table 3 worker:
drop table if exists worker;
create table if not exists worker(
worker_number int(11) not null,
foreign key (worker_number) references history(worker_num)
)engine=innodb, default charset=utf8;
I can create Table 1 and Table 2 successfully. However, when I try to create Table 3, It throws me an error like below:
Failed to add the foreign key constraint. Missing index for constraint 'fk_customer' in the referenced table 'history
Error code 1822.'
Question 01:
I know this is the problem of index. Because I found that if I put the Table3 creating code before Table2, I cannot create Table 2 successfully.
Thus, I know it must be caused by when the Table3 calls
foreign key (worker_number) references history(worker_num)
it will try to find the first primary key, primary key (cust_num,worker_num), in Table1 as its corresponding primary.
However, the first primary key in table 1 is cust_num and this cust_num is not the correct corresponding primary key to foreign key worker_num. Thus, it throws an error.
However, I search official Mysql document and find the description of index. It says:
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.
Here, you can see that Such an index is created on the referencing table automatically.
So, I'm very curious about if the referencing table is created automatically, why we cannot create child tables in any orders?(in my case, why should I create table 2 first or I will fail creating tables?)
Question 2:
In my case, I want define a foreign key in each child tables(table 2 and table 3) references to the primary keys in parent table(table 1). How to solve this index problem?
Appreciate any helps!!!
Thanks!
You have to read the documentation carefully:
(...) Such an index is created on the referencing table automatically if it does not exist. (...)
Emphasis: me
That is, you get the error because a suitable index on the referenced table is missing, not on the referencing table. The latter maybe crated automatically but not the former.
It also looks like you reversed the foreign key logic. Assuming that customer should list all the customers, worker all the workers and history some relationship between customers and workers, probably who has worked for who, then cust_number and worker_number should be primary key in the respective tables and there should be no foreign key in customer nor worker. In history there should be two (separate) foreign keys, cust_num pointing to customer and worker_num pointing to worker. The primary key definition of history is OK.

PHP MyAdmin Altering an existing table to create foreign keys

first post so very sorry if this seems like an obvious answer. Here's the premise of my problem. I have a database I am managing using PhpMyAdmin. I have a table called "Schedules" and want the id of the drivers on the schedule to be a foreign key that references a larger table "Users" where the ID column in this table is a primary key. Here is what i tried:
ALTER TABLE `Schedules`
ADD CONSTRAINT `FK_DriverID`
FOREIGN KEY (`Driver_ID`)
REFERENCES `users`(`ID`);
However, i get this error:
1005 - Can't create table 'Scheduler.#sql-3b7_3b9d' (errno: 150)
(Details…)
I am really at a loss with this error because im not trying to create any tables just alter an existing one. Thanks again and sorry if I butchered the formatting.
This mainly is due to a wrong primary key reference in your table.
Usually this means the key you are referencing does not exist / there might be difference in datatype between the FOREIGN KEY and the REFERENCED column / or there might be difference in column name.
Make sure that
you have a column named ID in users table
Driver_ID column have the same data type as ID column in Users table. Make sure both are exactly of the same data type.
Refer this for more information

Using a Primary key as the foreign key for multiple tables -PHPMyAdmin

I have a Table A (with a primary Key) ID (A.ID)
A.ID needs to be the foreign key for 2 other tables, B and C.
how can I do this in PHPMyAdmin DB: i seem to only have the option to set it as the foreign key to only one other table using INNODB
If I guess right than you mixed the direction. I build a database where I could add multiple foreign key for one table. Do you use for creating the foreign key sql statments or do you add them with some kind of wizzard of phpmyadmin?
As far I remember I also had some trouble to setup that foreign keys so I used only SQL to enfoce that the keys also get those names I would like to have.

Setting up foreign key in Mysql Workbench

I am trying to set up a foreign key in Mysql workbench. I used the same name for the foreign key as the primary key of the table I am trying to set a relationship with. I already have one relation set up this way in another table, but When I try and apply the alterations to this table, the script gives me an error:
Error 1005: Can't create table 'X.#sql-718_a' (errno: 121)
SQL Statement:
ALTER TABLE `X`.`X_use`
ADD CONSTRAINT `XyzID`
FOREIGN KEY (`XyzID` ) REFERENCES `X`.`Xyz` (`XyzID` )
ON DELETE NO ACTION O
N UPDATE NO ACTION ,
ADD INDEX `XyzID` (`XyzID` ASC) ,
However, if I change the foreign key name to "AbcID" I have no problem setting up the foreign key relation. Why is that and why can't I have the primary key name from one table be the same for the foreign key for this table? I have set up relations like that previously but for this table I cannot.
Constraint names have to be unique within the database.
That (errno: 121) in the error message means that MySQL encountered a duplicate key exception. The usual cause of this is that there is already a constraint of the same name in the database.
This "unique name" requirement is one reason why the normative pattern is to include the table name when constructing the name of the foreign key constraint. e.g. FK_table_cols e.g. FK_X_use_XyzID.
Why must the constraint name be unique within the database? That's a question for the dbms designers.
But consider this: when the database encounters a constraint violation, it throws an error that contains the name of the constraint. When that constraint name references only one constraint in the database, that makes locating the problem a bit easier.
You can not use same constrain name through out the database as described in ACID properties of DB.