简体   繁体   English

在SQL表中使用复合键和主键

[英]Having composite and primary keys in SQL table

I am having a problem with creating SQL table with composite and primary keys in it. 我在使用复合和主键创建SQL表时遇到问题。 This is what I have for now: 这就是我现在所拥有的:

CREATE TABLE Racunar (
  IPAdresa char(15) PRIMARY KEY,
  CPU char(20),
  HD char(20)
);

CREATE TABLE Program (
  Naziv char(15),
  Verzija char(20),
  Datum char(11),
  PRIMARY KEY(Naziv, Verzija)
);

CREATE TABLE Operater (
  SifO int PRIMARY KEY,
  Ime char(20),
  BrTel char(20)
);

CREATE TABLE JeInstaliran (
  IPAdresa char(15) FOREIGN KEY REFERENCES Racunar(IPAdresa),
  SifO int FOREIGN KEY REFERENCES Operater(SifO),
  FOREIGN KEY(Naziv, Verzija) REFERENCES Program(Naziv, Verzija),
  DatumInstalacije char(11)
);

The problem is somewhere in the last table. 问题出在最后一个表格中。 I am using SQL Fiddle and it returned the next error message after trying to build schema: 我正在使用SQL Fiddle ,它在尝试构建模式后返回了下一条错误消息:

Schema Creation Failed: You have an error in your SQL syntax; 架构创建失败:您的SQL语法出错; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOREIGN KEY REFERENCES Racunar(IPAdresa), SifO int FOREIGN KEY REFERENCES Oper' at line 2: 查看与您的MySQL服务器版本对应的手册,以便在第2行的'FOREIGN KEY REFERENCES Racunar(IPAdresa),SifO int FOREIGN KEY REFERENCES Oper'附近使用正确的语法:

I don't know why this doesn't work, so I would appreciate any help. 我不知道为什么这不起作用,所以我将不胜感激。 Thanks in advance. 提前致谢。

The syntax is incorrect. 语法不正确。 Instead of defining the FOREIGN KEY along with the column, define it after the column definition using the syntax FOREIGN KEY (colname) REFERENCES tablename (foreign_colname) 不使用列定义FOREIGN KEY ,而是使用语法FOREIGN KEY (colname) REFERENCES tablename (foreign_colname)在列定义之后定义它。

CREATE TABLE JeInstaliran (
  IPAdresa char(15),
  SifO int,
  Naziv char(15),
  Verzija char(20),
  FOREIGN KEY (IPAdresa) REFERENCES Racunar(IPAdresa),
  FOREIGN KEY (SifO) REFERENCES Operater(SifO),
  FOREIGN KEY(Naziv, Verzija) REFERENCES Program(Naziv, Verzija),
  DatumInstalacije char(11)
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM