Question

In: Computer Science

I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint....

I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'test_ibfk_5' in the referenced table 'appointment', can you please tell me what is wrong with my code:

-- Table III: Appointment = (site_name [fk7], date, time)
-- fk7: site_name -> Site.site_name
DROP TABLE IF EXISTS appointment;
CREATE TABLE appointment (
   appt_site VARCHAR(100) NOT NULL,
appt_date DATE NOT NULL,
appt_time TIME NOT NULL,
PRIMARY KEY (appt_date, appt_time),
FOREIGN KEY (appt_site) REFERENCES site(site_name)
) ENGINE=innodb;

-- Table IV: Test = (test_id, status, appointment[fk3])
-- fk3: (site_name, date, time) -> Site.site_name, Appointment.date, Appointment.time, appointment is non-null
DROP TABLE IF EXISTS test;
CREATE TABLE test (
   test_id INT NOT NULL,
test_status VARCHAR(100) NOT NULL,
appt_site VARCHAR(100) NOT NULL,
appt_date DATE NOT NULL,
appt_time TIME NOT NULL,
PRIMARY KEY (test_id),
FOREIGN KEY (appt_site) REFERENCES site(site_name),
   CONSTRAINT test_ibfk_4 FOREIGN KEY (appt_date) REFERENCES appointment(appt_date), -- CHECK!!!
   CONSTRAINT test_ibfk_5 FOREIGN KEY (appt_time) REFERENCES appointment(appt_time) -- CHECK!!!
) ENGINE=innodb;


Solutions

Expert Solution

  • Definitin of foreign key :

A FOREIGN KEY is a key used to link two tables together.

A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table.

The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table.

  • When creating foreign keys be sure the columns you are using have the same:
  1.    Data Type
  2. Collation
  3. Zero Fill
  4. Not Null
  5. Unsigned
  6. Binary

As shown in above example :

As you are using foreign key as app_site in test and appoinment table but you give reference of site_name attribute which is primary key in sitr table which not satisfy condition for foreign key.

The attributr name must be same for foreign key in other table and primary key in parent table.

the correct code is as below:

DROP TABLE IF EXISTS appointment;
CREATE TABLE appointment (
   appt_site VARCHAR(100) NOT NULL,
appt_date DATE NOT NULL,
appt_time TIME NOT NULL,
PRIMARY KEY (appt_date, appt_time),
FOREIGN KEY (site_name) REFERENCES site(site_name)
) ENGINE=innodb;

DROP TABLE IF EXISTS test;
CREATE TABLE test (
   test_id INT NOT NULL,
test_status VARCHAR(100) NOT NULL,
appt_site VARCHAR(100) NOT NULL,
appt_date DATE NOT NULL,
appt_time TIME NOT NULL,
PRIMARY KEY (test_id),
FOREIGN KEY (site_name) REFERENCES site(site_name),


Related Solutions

My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at Main.main(Main.java:34) Your output Welcome to the food festival! Would you like to place an order? Expected output This test case should produce no output in java import java.util.Scanner; public class Main {    public static void display(String menu[])    {        for(int i=0; i<menu.length; i++)        {            System.out.println (i + " - " + menu[i]);...
HI. I have been trying to run my code but I keep getting the following error....
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method. Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at project2.areacalculation.main(areacalculation.java:26) My code is below package project2; import java.util.Scanner; public class areacalculation { private static...
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
Keep getting error where the code cannot read the text file and create an arraylist of...
Keep getting error where the code cannot read the text file and create an arraylist of objects from it. HouseListTester: import java.util.*; //Hard codes the criteria public class HouseListTester { static HouseList availableHouses; public static void main(String []args) { availableHouses = new HouseList("C:\\Users\\jvs34\\Downloads\\houses.txt"); Criteria c1 = new Criteria(1000, 500000, 100, 5000, 0, 10); Criteria c2 = new Criteria(1000, 100000, 500, 1200, 0, 3); Criteria c3 = new Criteria(100000, 200000, 1000, 2000, 2, 3); Criteria c4 = new Criteria(200000, 300000, 1500,...
I working on this program in C++ and I keep getting 20 errors of the same...
I working on this program in C++ and I keep getting 20 errors of the same type again.cpp:36:11: error: use of undeclared identifier 'Polynomial' int main() { // create a list of polinomials vector<Polynomial> polynomials; // welcome message cout << "Welcome to Polynomial Calculator" << endl; int option = 0; while (option != 6) { // display menu displayMenu(); // get user input; cin >> option; if (option == 1) { cout << "Enter a polynomial :" << endl; string...
I keep getting an error that I cannot figure out with the below VS2019 windows forms...
I keep getting an error that I cannot figure out with the below VS2019 windows forms .net framework windows forms error CS0029 C# Cannot implicitly convert type 'bool' to 'string' It appears to be this that is causing the issue string id; while (id = sr.ReadLine() != null) using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; namespace Dropbox13 { public partial class SearchForm : Form { private List allStudent = new List(); public SearchForm() { InitializeComponent(); } private void SearchForm_Load(object...
I am making a html game with phaser 3 I keep getting an error at line...
I am making a html game with phaser 3 I keep getting an error at line 53 of this code (expected ;) I have marked the line that needs to be fixed. whenever I add ; my whole code crashes const { Phaser } = require("./phaser.min"); var game; var gameOptions = {     tileSize: 200,     tileSpacing: 20,     boardSize: {     rows: 4,     cols: 4     }    }    window.onload = function() {     var gameConfig = {         width: gameOptions.boardSize.cols * (gameOptions.tileSize +             gameOptions.tileSpacing) + gameOptions.tileSpacing,...
Need C++ code to be able to run, keep getting a constant value error #include #include...
Need C++ code to be able to run, keep getting a constant value error #include #include #include #include #include #include using namespace std; using namespace std::chrono; int c; void insertionSort(int* arr, int n) { for (int i = 1;i < n;i++) { int v = arr[i]; int j; for (j = i - 1;j > -1;j--) { c++; if (arr[j] > v) { arr[j + 1] = arr[j]; } else { break; } } arr[j + 1] = v; }...
I keep getting this error "LetterDemo.cs(21,14): error CS1519: Unexpected symbol `string' in class, struct, or interface...
I keep getting this error "LetterDemo.cs(21,14): error CS1519: Unexpected symbol `string' in class, struct, or interface member declaration" Can someone please help me. Here is my code: using static System.Console; class LetterDemo {    static void Main()    {      Letter letter1 = new Letter();      CertifiedLetter letter2 = new CertifiedLetter();      letter1.Name = "Electric Company";      letter1.Date = "02/14/18";      letter2.Name = "Howe and Morris, LLC";      letter2.Date = "04/01/2019";      letter2.TrackingNumber = "i2YD45";      WriteLine(letter1.ToString());      WriteLine(letter2.ToString() +       " Tracking number: " + letter2.TrackingNumber);    } } class Letter {...
In java, I keep getting the error below and I can't figure out what i'm doing...
In java, I keep getting the error below and I can't figure out what i'm doing wrong. Any help would be appreciated. 207: error: not a statement allocationMatrix[i][j];
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT