Visualizzazione dei risultati da 1 a 7 su 7

Discussione: [MySQL] Foreign key

  1. #1

    [MySQL] Foreign key

    Salve ragazzi,
    è da questa mattina che provo a inserire una chiave esterna in un tabella:

    codice:
    CREATE  TABLE IF NOT EXISTS `interviews_it` (
      `key` INT NOT NULL AUTO_INCREMENT ,
      `ID` INT(8) ZEROFILL UNSIGNED NOT NULL ,
      `title` VARCHAR(255) NULL ,
      `content` TEXT NULL ,
      `date` DATE NULL ,
      `journalist` VARCHAR(50) NULL ,
      PRIMARY KEY (`key`) )
    ENGINE = InnoDB;
    
    
    CREATE  TABLE IF NOT EXISTS `seo_it` (
      `key` INT NOT NULL AUTO_INCREMENT ,
      `ID` INT(8) ZEROFILL UNSIGNED NOT NULL ,
      `objectID` INT NOT NULL ,
      `tableName` VARCHAR(50) NULL ,
      `keywords` TEXT NULL COMMENT '		' ,
      `description` TEXT NULL ,
      `title` TEXT NULL ,
      `highlights` TEXT NULL ,
      `enable` INT(1) NULL ,
      PRIMARY KEY (`key`) ,
      INDEX `fk_seo_it_interviews_it` (`objectID` ASC) ,
      CONSTRAINT `fk_seo_it_interviews_it`
        FOREIGN KEY (`objectID` )
        REFERENCES `interviews_it` (`ID` )
        ON DELETE CASCADE
        ON UPDATE CASCADE)
    ENGINE = InnoDB;
    MySQLWorkbench mi restituisce:

    Error Code: 1005
    Can't create table 'seo_it' (errno: 150)

    Qualcuno di voi ha mai risolto questo problema? Mi sembra strano che non funzioni anche perchè ho utilizzato il software...
    Mantisworks.net - Soluzioni dal web per il web!
    SEO, SEM, Software, Siti internet, Grafica e Multimedia

  2. #2
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    http://sql-info.de/mysql/referential-integrity.html

    This means either:

    * The referenced table is not an InnoDB table
    * The referenced column in other_table is not unique
    * The referenced column in other_table has a different type to other_id

    In MySQL prior to 5.0, this error can also indicate that:

    * No index has been defined on the column other_id
    codice:
    create  table if not exists `interviews_it` (
      `key` int not null auto_increment ,
      `ID` int unsigned not null unique ,
      `title` varchar(255) null ,
      `content` text null ,
      `date` date null ,
      `journalist` varchar(50) null ,
      primary key (`key`) )
    engine = innodb;
    
    
    create  table if not exists `seo_it` (
      `key` int not null auto_increment ,
      `ID` int(8) zerofill unsigned not null ,
      `objectID` int unsigned not null ,
      `tableName` varchar(50) null ,
      `keywords` text null comment '		' ,
      `description` text null ,
      `title` text null ,
      `highlights` text null ,
      `enable` int(1) null ,
      primary key (`key`) ,
      index `fk_seo_it_interviews_it` (`objectID` asc) ,
      constraint `fk_seo_it_interviews_it`
        foreign key (`objectID` )
        references `interviews_it` (`ID` )
        on delete cascade
        on update cascade)
    engine = innodb;

  3. #3
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    Oppure

    codice:
    create  table if not exists `interviews_it` (
      `key` int not null auto_increment ,
      `ID` int(8) zerofill unsigned not null unique ,
      `title` varchar(255) null ,
      `content` text null ,
      `date` date null ,
      `journalist` varchar(50) null ,
      primary key (`key`) )
    engine = innodb;
    
    
    create  table if not exists `seo_it` (
      `key` int not null auto_increment ,
      `ID` int(8) zerofill unsigned not null ,
      `objectID` int(8) zerofill not null ,
      `tableName` varchar(50) null ,
      `keywords` text null comment '		' ,
      `description` text null ,
      `title` text null ,
      `highlights` text null ,
      `enable` int(1) null ,
      primary key (`key`) ,
      index `fk_seo_it_interviews_it` (`objectID` asc) ,
      constraint `fk_seo_it_interviews_it`
        foreign key (`objectID` )
        references `interviews_it` (`ID` )
        on delete cascade
        on update cascade)
    engine = innodb;

  4. #4
    Ho capito parecchio grazie a questo tread, ma evidentemente non tutto perché ricevo errore con questo codice:

    codice:
    $query="create  table if not exists `demoarticoli` (
        `idart` int(5) zerofill unsigned not null ,
    	 `ID` int(8) zerofill unsigned not null ,
    	`idcat` int(5) zerofill not null ,
      `subcat` int(5) zerofill not null ,
      `categoria` varchar(50) not null ,
      `subcategoria` varchar(50) not null ,
        `articolo` varchar(50) not null ,
    	`descrizione` varchar(50) not null ,
    	`dettaglio` varchar(50) not null ,
    	`prezzo` varchar(50) not null ,
    	`sconto` varchar(50) not null ,
    	`immagine` varchar(50) not null ,
    	`immagine2` varchar(50) not null ,
    	`immagine3` varchar(50) ,
    	`thumb` varchar(50),
    	`promozioni` varchar(50) ,
    	`novita` varchar(50),
    	`esclusivo` varchar(50),
        primary key (`idart`) ,
      index `fk_demoarticoli_democat` (`idcat` asc) ,
      constraint `fk_demoarticoli_democat`
        foreign key (`idcat` )
        references `democat` (`ID` )
        on delete cascade
        on update cascade)
    engine = innodb;
    cosa sbaglio?

  5. #5
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    @seokey. Hai già un tuo thread aperto. Continua in quello visto che se scrivi in più posti si perde il filo del discorso.

    Posta lì il dump delle tre tabelle.

  6. #6

  7. #7

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.