带有TimeStamp的外键错误地形成

时间:2016-07-07 13:42:28

标签: mysql database phpmyadmin

我在XAMPP for OS X中使用MariaDB时遇到了一些问题。

我正在开发一个DataBase,我必须在其他表中插入一些引用TIMESTAMP属性的外键。

当系统尝试处理"创建外键"声明它给了我这个错误:

"错误:150"外键错误形成"

以下是我的SQL命令

CREATE DATABASE IF NOT EXISTS cinguettio;
USE cinguettio;


CREATE TABLE IF NOT EXISTS utente(
    email VARCHAR(50) PRIMARY KEY,
    città VARCHAR(30),
    stato VARCHAR(30),
    provincia VARCHAR(30),
    datanascita DATE,
    luogonascita VARCHAR(30),
    nome VARCHAR(30),
    cognome VARCHAR(30),
    vip VARCHAR(1),
    sesso VARCHAR(1),
    password VARCHAR(20)
);

CREATE TABLE IF NOT EXISTS apprezzamento(
    dataora TIMESTAMP,
    email VARCHAR(50),
    testo VARCHAR(50),
    dataoraimm TIMESTAMP,
    id INT(7),
    PRIMARY KEY (dataora, email),
    FOREIGN KEY (email) REFERENCES utente(email)
);

CREATE TABLE IF NOT EXISTS hobby(
    nome VARCHAR(20) PRIMARY KEY
);


CREATE TABLE IF NOT EXISTS immagine(
    id INT(7),
    dataora TIMESTAMP,
    email VARCHAR(50),
    nomefile VARCHAR(30),
    percorso VARCHAR(200),
    PRIMARY KEY (id, dataora, email),
    FOREIGN KEY (email) REFERENCES utente(email)
);

CREATE TABLE IF NOT EXISTS luogo(
    id INT(7),
    dataora TIMESTAMP,
    email VARCHAR(50),
    latitudine FLOAT(2,2), 
    longitudine FLOAT(2,2),
    PRIMARY KEY (id, dataora, email),
    FOREIGN KEY (email) REFERENCES utente(email)
);

CREATE TABLE IF NOT EXISTS pratica(
    nome VARCHAR(20),
    email VARCHAR(50),
    PRIMARY KEY (nome, email),
    FOREIGN KEY (email) REFERENCES utente(email),
    FOREIGN KEY (nome) REFERENCES hobby(nome)
);



CREATE TABLE IF NOT EXISTS scrivea(
    dataora TIMESTAMP,
    email VARCHAR(50),
    PRIMARY KEY (dataora, email),
    FOREIGN KEY (email) REFERENCES utente(email),
    FOREIGN KEY (dataora) REFERENCES apprezzamento(dataora)

);

CREATE TABLE IF NOT EXISTS testo(
    id INT(7),
    dataora TIMESTAMP,
    email VARCHAR(50),
    contenuto VARCHAR(100),
    PRIMARY KEY (id, dataora, email),
    FOREIGN KEY (email) REFERENCES utente(email)
);

CREATE TABLE IF NOT EXISTS segue(
    segue VARCHAR(50),
    seguito VARCHAR(50),
    PRIMARY KEY (segue, seguito),
    FOREIGN KEY (segue) REFERENCES utente(email),
    FOREIGN KEY (seguito) REFERENCES utente(email)
);

CREATE TABLE IF NOT EXISTS scrivet(
    id INT(7),
    dataora TIMESTAMP,
    email VARCHAR(50),
    PRIMARY KEY (id, dataora, email),
    FOREIGN KEY (email) REFERENCES utente(email),

    FOREIGN KEY (id) REFERENCES testo(id)
);

CREATE TABLE IF NOT EXISTS segnala(
    id INT(7),
    dataora TIMESTAMP,
    email VARCHAR(50),
    PRIMARY KEY (id, dataora, email),
    FOREIGN KEY (email) REFERENCES utente(email),
    FOREIGN KEY (dataora) REFERENCES testo(dataora),
    FOREIGN KEY (id) REFERENCES testo(id)
);

CREATE TABLE IF NOT EXISTS preferisce(
    id INT(7),
    dataora TIMESTAMP,
    email VARCHAR(50),
    PRIMARY KEY (id, dataora, email),
    FOREIGN KEY (email) REFERENCES utente(email),
    FOREIGN KEY (dataora) REFERENCES luogo(dataora),
    FOREIGN KEY (id) REFERENCES luogo(id)
);

CREATE TABLE IF NOT EXISTS pubblica(
    id INT(7),
    dataora TIMESTAMP,
    email VARCHAR(50),
    PRIMARY KEY (id, dataora, email),
    FOREIGN KEY (email) REFERENCES utente(email),
    FOREIGN KEY (dataora) REFERENCES luogo(dataora),
    FOREIGN KEY (id) REFERENCES luogo(id)
);

直到表格" scrivet"一切顺利,然后停止。我确定问题出在属性" dataora"因为我试图删除它并且工作正常。

所以...我该怎么办?

1 个答案:

答案 0 :(得分:0)

您不应在复合键或任何键中使用时间戳。

Use timestamp(or datetime) as part of primary key (or part of clustered index)