2017-12-13 4 views
0

Die ersten 4 Tabelle sind in Ordnung, die Transaktionstabellen laufen in Problem. Ich bekomme den 1215-Fehler: kann keinen Fremdschlüssel hinzufügen. Ich habe überprüft, die Datentypen überprüft und sichergestellt, dass alle FK PK ihrer eigenen Tabellen sind. Was ist hier falsch?MySQL-Fehler: 1251 kann keinen Fremdschlüssel hinzufügen

CREATE SCHEMA FinalDB; 

CREATE TABLE `User` (
    userId int not null auto_increment primary key, 
    first_name varchar(255) not null, 
    last_name varchar(255) not null, 
    address varchar(255) null, 
    DOB date not null, 
    availableBalance int not null default 0, 
    currency varchar(20) 
); 

CREATE TABLE Verifications(
    userId int not null primary key, 
    passport int null, 
    ssn int null, 
    license int null, 
    constraint 
    foreign key (userId) 
    references User(userId) 
); 

CREATE TABLE Linked_Account(
    account_Id int not null, 
    userId int not null, 
    routing int null, 
    swift int null, 
    primary key (userId, account_Id), 
    constraint 
    foreign key (userId) 
    references User(userId) 
); 

CREATE TABLE Wallet (
    userId int not null, 
    walletId varchar(5) not null, 
    coinAmount int not null default 0, 
    netWorth int not null default 0, 
    primary key(userId, walletId), 
    constraint 
    foreign key (userId) 
    references `User`(userId) 
); 

CREATE TABLE Transactions (
    transactionId int not null primary key auto_increment, 
    userId int not null, 
    type varchar(30) not null, 
    walletId varchar(5) not null, 
    payment_method int null, #optional 
    total int null, #optional 
    quantity int not null, 
    fee int null, #optional 
    `date` date not null, 
    sender varchar(50) null, #optional 
    reciever varchar(50) null, #optional 
    status varchar(20) not null, 
    notes varchar(200) null, #optional 
    constraint 
    foreign key (userId) 
    references `User`(userId) 
    ON DELETE CASCADE ON UPDATE CASCADE, 
    constraint 
    foreign key (walletId) 
    references Wallet(walletId) 
    ON DELETE CASCADE ON UPDATE CASCADE, 
    constraint 
    foreign key (payment_method) 
    references Linked_Account(account_id) 

); 

CREATE TABLE TransactionsExchange(
    transactionId int not null auto_increment primary key, 
    userId int not null, 
    currencyFrom int not null, 
    currencyFromAmount int not null, 
    currencyInto int not null, 
    currencyIntoEquivalent int not null, 
    notes varchar(200) null, 
    `date` date not null, 
    constraint 
    foreign key (userId) 
    references User(userId), 
    constraint 
    foreign key (currencyFrom) 
    references Wallet(walletId), 
    constraint 
    foreign key (currencyInto) 
    references Wallet(walletId) 
); 

Ich habe schauen Sie online für mögliche Antwort, aber es ist in der Regel mit inkonsistenten Datentypen oder nicht angemeldetem PK zu tun. Ich versuche im Grunde eine Transaktionstabelle zu erstellen, um verschiedene Daten in verschiedenen Kompositionen zu protokollieren. Verwenden von Back-End-Logik, um zu behandeln, was erforderlich ist und was nicht, abgesehen von ein paar Standardeinstellungen.

Antwort

1

To use a compound Primary Key as Foreign Key, you'll have to add the same number of columns (that compose the PK) with same datatypes to the child table and then use the combination of these columns in the FOREIGN KEY definition.

sehen Sie diese 'Transaktionen' Tabelle erstellen Abfrage im Zusammenhang hier Beitrag https://stackoverflow.com/a/10566463/4904726

Versuchen:

CREATE TABLE Transactions (
      transactionId int not null primary key auto_increment, 
      userId int not null, 
      type varchar(30) not null, 
      walletId varchar(5) not null, 
      payment_method int null, #optional 
      total int null, #optional 
      quantity int not null, 
      fee int null, #optional 
      `date` date not null, 
      sender varchar(50) null, #optional 
      reciever varchar(50) null, #optional 
      status varchar(20) not null, 
      notes varchar(200) null, #optional 
      constraint 
      foreign key (userId) 
      references `User`(userId) 
      ON DELETE CASCADE ON UPDATE CASCADE, 
      constraint 
      foreign key (userId, walletId) 
      references Wallet(userId, walletId) 
      ON DELETE CASCADE ON UPDATE CASCADE, 
      constraint 
      foreign key (userId, payment_method) 
      references Linked_Account(userId, account_id) 

     ); 
+0

, die gut funktionierten! Ich musste bisher keine zusammengesetzten Schlüssel als Fremdschlüssel importieren. Ich habe einfach angenommen, dass jeder Schlüssel einzeln deklariert werden muss. Vielen Dank! –

Verwandte Themen