Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 13

Discussione: Foreign Key

  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2013
    Messaggi
    287

    Foreign Key

    Salve a tutti.

    Sto utilizzando il mysql workbench.

    Uno strumento molto pratico e semplice se non per il fatto che la creazione delle chiavi esterne mi sta dando parecchie noie.

    In particolare, provando a creare una chiave esterna ottengo i seguenti errori:

    ERROR 1215: Cannot add foreign key constraint

    SQL Statement:

    ALTER TABLE `lastest`.`operazione`

    ADD CONSTRAINT `nomePorta`

    FOREIGN KEY (`nomePortata` )

    REFERENCES `lastest`.`portata` (`nomePortata` )

    ON DELETE NO ACTION

    ON UPDATE NO ACTION

    , ADD INDEX `nomePorta_idx` (`nomePortata` ASC)



    ERROR: Error when running failback script. Details follow.



    ERROR 1046: No database selected

    SQL Statement:

    CREATE TABLE `operazione` (

    `idOp` int(11) NOT NULL AUTO_INCREMENT,

    `nomePortata` varchar(45) NOT NULL,

    `nomeOp` varchar(45) DEFAULT NULL,

    `numeroInteressati` int(11) DEFAULT NULL,

    PRIMARY KEY (`idOp`)

    ) ENGINE=InnoDB DEFAULT CHARSET=latin1





    La tabella Portata alla quale la fk farebbe riferimento esiste e il campo e' varchar tutto corretto


  2. #2
    ERROR 1046: No database selected

    devi selezionare il database su cui eseguire la query

    Il silenzio è spesso la cosa migliore. Pensa ... è gratis.

  3. #3
    Utente di HTML.it
    Registrato dal
    Mar 2013
    Messaggi
    287
    Come lo seleziono?

    io creo tutte le tabelle allo stesso modo

  4. #4
    Utente di HTML.it
    Registrato dal
    Mar 2013
    Messaggi
    287
    Eliminando e ricreando il tutto ora ho solo questo errore :

    ERROR 1215: Cannot add foreign key constraint

    SQL Statement:

    CREATE TABLE `lastest`.`Operazione` (

    `idOp` INT NOT NULL AUTO_INCREMENT ,

    `nomeOp` VARCHAR(45) NULL ,

    `nomePortata` VARCHAR(45) NULL ,

    `numeroInteressati` INT NULL ,

    PRIMARY KEY (`idOp`) ,

    INDEX `nomePort_idx` (`nomePortata` ASC) ,

    CONSTRAINT `nomePort`

    FOREIGN KEY (`nomePortata` )

    REFERENCES `lastest`.`portata` (`nomePortata` )

    ON DELETE CASCADE

    ON UPDATE CASCADE)

  5. #5
    Utente di HTML.it
    Registrato dal
    Mar 2013
    Messaggi
    287
    Aggiungo la struttura della tabella Portata :

    idP : int (autoincrement) pk
    nomePortata : varchar(45)
    prezzoAcquisto : decimal(10,2)
    prezzoVendita : decimal(10,2)
    dataScad : date
    tipo : varchar(45)
    giacenza :int

    la colonna a cui faccio riferimento e' nomePortata.

  6. #6
    Utente di HTML.it L'avatar di comas17
    Registrato dal
    Apr 2002
    Messaggi
    6,522
    Devi fare riferimento ad una colonna che abbia una chiave definita
    Nella tua tabella "portata" hai definito una chiave per la colonna "nomeportata" ?

    vedi ad esempio: http://stackoverflow.com/questions/1...-1215-in-mysql

  7. #7
    Utente di HTML.it L'avatar di comas17
    Registrato dal
    Apr 2002
    Messaggi
    6,522
    Altre cose interessanti

    Reasons that you may get foreign key constraing error:

    You are not using InnoDB as the engine on all tables
    You are trying to reference a nonexistent key on the target table. Make sure that it is a key on the other table (it can be a primary or unique key)
    The types of the columns are not the same (exception is the column on the referecing table can be nullable).

    Check these.


    da: http://stackoverflow.com/questions/1...key-constraint

  8. #8
    Utente di HTML.it
    Registrato dal
    Mar 2013
    Messaggi
    287
    Originariamente inviato da comas17
    Devi fare riferimento ad una colonna che abbia una chiave definita
    Nella tua tabella "portata" hai definito una chiave per la colonna "nomeportata" ?

    vedi ad esempio: http://stackoverflow.com/questions/1...-1215-in-mysql
    si scusa hodimenticato di scriverlo.

    Anche la colonna nomePortata in Portata e' definita come chiave primaria.

  9. #9
    Utente di HTML.it
    Registrato dal
    Mar 2013
    Messaggi
    287
    Ho stanato l'errore.

    Se in portata ho 2 p.k.

    in Operazione non mi fa mettere la f.k.

    se in Portata metto solo nomePortata come p.k. posso mettere la f.k.

    qualcuno sa spiegarmi il perche?

    c'è un modo per tenere entrambe le p.k. in Portata e fare una sola f.k in Operazione?

    grazie

  10. #10
    Utente di HTML.it L'avatar di comas17
    Registrato dal
    Apr 2002
    Messaggi
    6,522
    Mi sfugge il senso di avere 2 separate Pk nella stessa tabella...(non dovresti nemmeno poterlo fare)
    In ogni caso, come dice anche il secondo link, puoi configurare quella che ti interessa come "unique" e dovresti poter fare la tua fk

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.