创建外键时出现语法错误

huangapple go评论159阅读模式
英文:

Syntax error while trying to create a foreign key

问题

我正在创建一个带有外键的表,但是我遇到了语法错误:

drop table if exists enviada;

create table if not exists enviada 
(
    destinatario_email varchar(300),
    destinatario_nome varchar(200),
    destinatario_cpf_cnpj varchar(14),
    data_envio datetime,
    remetente varchar(50),
    assunto varchar(200),
    foreign key (assunto) references (email_html) assunto,
    primary key (destinatario_email, destinatario_cpf_cnpj, data_envio, assunto)
);
sqlite> .read ".\\Scripts SQL\\esquema_email.sql"
在第 8 行附近发生解析错误:近 "(": 语法错误
  o, assunto),     foreign key (assunto) references (email_html) assunto );
                                      错误在此处 ---^

我漏掉了什么?

英文:

I'm creating a table with a foreign key but I get a syntax error:

drop table if exists enviada;

create table if not exists enviada 
(
    destinatario_email varchar(300),
    destinatario_nome varchar(200),
    destinatario_cpf_cnpj varchar(14),
    data_envio datetime,
    remetente varchar(50),
    assunto varchar(200),
    foreign key (assunto) references (email_html) assunto,
    primary key (destinatario_email, destinatario_cpf_cnpj, data_envio, assunto)
);
sqlite> .read ".\\Scripts SQL\\esquema_email.sql"
Parse error near line 8: near "(": syntax error
  o, assunto),     foreign key (assunto) references (email_html) assunto );
                                      error here ---^

What am I missing?

答案1

得分: 1

引用应该是:

references email_html (assunto)
英文:

The syntax should be:

references email_html (assunto)

答案2

得分: 1

手册显示您必须在括号中切换列与表名,如下所示:

create table if not exists enviada 
(
    destinatario_email varchar(300),
    destinatario_nome varchar(200),
    destinatario_cpf_cnpj varchar(14),
    data_envio datetime,
    remetente varchar(50),
    assunto varchar(200),
    外键 (assunto) 引用 assunto (email_html) ,
    主键 (destinatario_email, destinatario_cpf_cnpj, data_envio, assunto)
);
英文:

The manual shows you that you have to switch column in parentehsis with the table name like

create table if not exists enviada 
(
    destinatario_email varchar(300),
    destinatario_nome varchar(200),
    destinatario_cpf_cnpj varchar(14),
    data_envio datetime,
    remetente varchar(50),
    assunto varchar(200),
    foreign key (assunto) references assunto (email_html) ,
    primary key (destinatario_email, destinatario_cpf_cnpj, data_envio, assunto)
);

huangapple
  • 本文由 发表于 2023年7月7日 03:49:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定