SQL DB创建错误

时间:2017-08-01 19:08:46

标签: sql sql-server tsql sql-view

我使用BIT DEFAULT 0,作为布尔值,不知道它是否正确。好吧,问题是我不能创建任何表,因为FK有问题,不明白为什么?我还添加了ERD图像以获得更好的视图。

也许有人会/可以这么好并向我解释,ty!

也许有人知道任何关于turtnind ERD到MSSQL代码或MySQL代码的好程序或在线沙箱?如果免费使用它会很好。提前谢谢!

enter image description here

enter image description here

CREATE DataBase LO;
GO
USE LO;

CREATE TABLE Weapon
(
  ClassWeapon CHAR(50) NULL,
  Level INT NULL,
  MinDmg INT NULL,
  MaxDmg INT NULL,
  AtkSpeed NUMERIC(3,2) NULL,
  PerSecondDmg NUMERIC(6,2) NULL,
  PlayerLevel INT NULL,
  SkillLevel INT NULL,
  Image BIT DEFAULT 0 NULL,
  Name CHAR(100) NULL,
  Weapons CHAR(50) NOT NULL,
  Monsters CHAR(50) NULL,
  PRIMARY KEY (Weapons),
  FOREIGN KEY (Monsters) REFERENCES Monster(Monsters)
);

CREATE TABLE Armor
(
  ClassArmor CHAR(50) NULL,
  Name CHAR(100) NULL,
  Level INT NULL,
  Defense INT NULL,
  Image BIT DEFAULT 0 NULL,
  Armors CHAR(50) NOT NULL,
  Monsters CHAR(50) NULL,
  PRIMARY KEY (Armors),
  FOREIGN KEY (Monsters) REFERENCES Monster(Monsters)
);

CREATE TABLE Menu
(
  Weapons CHAR(50) NULL,
  Armors CHAR(50) NULL,
  Monsters CHAR(50) NULL,
  FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons),
  FOREIGN KEY (Armors) REFERENCES Armor(Armors),
  FOREIGN KEY (Monsters) REFERENCES Monster(Monsters)
);

CREATE TABLE Monster
(
  Name CHAR(100) NULL,
  Level INT NULL,
  MinHealth INT NULL,
  MaxHealth INT NULL,
  MinDmg INT NULL,
  MaxDmg INT NULL,
  AtkSpeed NUMERIC(3,2) NULL,
  Location CHAR(100) NULL,
  Monsters CHAR(50) NOT NULL,
  DropItems CHAR(100) NULL,
  Image BIT DEFAULT 0 NULL,
  PRIMARY KEY (Monsters),
  FOREIGN KEY (DropItems) REFERENCES Items(DropItems)
);

CREATE TABLE Items
(
  DropItems CHAR(100) NOT NULL,
  Weapons CHAR(50) NULL,
  Armors CHAR(50) NULL,
  PRIMARY KEY (DropItems),
  FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons),
  FOREIGN KEY (Armors) REFERENCES Armor(Armors)
);

2 个答案:

答案 0 :(得分:2)

  

嗯,问题是我无法创建任何表,因为FK存在问题,不明白为什么?

简单地说,你不能引用不存在的对象。

您应首先创建表,然后添加FK关系:

CREATE DataBase LO;
GO
USE LO;

CREATE TABLE Weapon
(
  ClassWeapon CHAR(50) NULL,
  Level INT NULL,
  MinDmg INT NULL,
  MaxDmg INT NULL,
  AtkSpeed NUMERIC(3,2) NULL,
  PerSecondDmg NUMERIC(6,2) NULL,
  PlayerLevel INT NULL,
  SkillLevel INT NULL,
  Image BIT DEFAULT 0 NULL,
  Name CHAR(100) NULL,
  Weapons CHAR(50) NOT NULL,
  Monsters CHAR(50) NULL,
  PRIMARY KEY (Weapons),
  --FOREIGN KEY (Monsters) REFERENCES Monster(Monsters)
);

CREATE TABLE Armor
(
  ClassArmor CHAR(50) NULL,
  Name CHAR(100) NULL,
  Level INT NULL,
  Defense INT NULL,
  Image BIT DEFAULT 0 NULL,
  Armors CHAR(50) NOT NULL,
  Monsters CHAR(50) NULL,
  PRIMARY KEY (Armors),
 ---- FOREIGN KEY (Monsters) REFERENCES Monster(Monsters)
);

CREATE TABLE Menu
(
  Weapons CHAR(50) NULL,
  Armors CHAR(50) NULL,
  Monsters CHAR(50) NULL,
  --FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons),
 -- FOREIGN KEY (Armors) REFERENCES Armor(Armors),
 -- FOREIGN KEY (Monsters) REFERENCES Monster(Monsters)
);

CREATE TABLE Monster
(
  Name CHAR(100) NULL,
  Level INT NULL,
  MinHealth INT NULL,
  MaxHealth INT NULL,
  MinDmg INT NULL,
  MaxDmg INT NULL,
  AtkSpeed NUMERIC(3,2) NULL,
  Location CHAR(100) NULL,
  Monsters CHAR(50) NOT NULL,
  DropItems CHAR(100) NULL,
  Image BIT DEFAULT 0 NULL,
  PRIMARY KEY (Monsters),
  --FOREIGN KEY (DropItems) REFERENCES Items(DropItems)
);

CREATE TABLE Items
(
  DropItems CHAR(100) NOT NULL,
  Weapons CHAR(50) NULL,
  Armors CHAR(50) NULL,
  PRIMARY KEY (DropItems),
  --FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons),
 -- FOREIGN KEY (Armors) REFERENCES Armor(Armors)
);

ALTER TABLE Weapon
ADD FOREIGN KEY (Monsters) REFERENCES Monster(Monsters);

ALTER TABLE Armor
ADD FOREIGN KEY (Monsters) REFERENCES Monster(Monsters);

ALTER TABLE Menu
ADD 
  FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons),
  FOREIGN KEY (Armors) REFERENCES Armor(Armors),
  FOREIGN KEY (Monsters) REFERENCES Monster(Monsters);

ALTER TABLE Monster
ADD FOREIGN KEY (DropItems) REFERENCES Items(DropItems);

ALTER TABLE Items
ADD FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons),
 FOREIGN KEY (Armors) REFERENCES Armor(Armors);

答案 1 :(得分:0)

谢谢@ lad2025

这是如何通过添加表然后改变FK来实现的。

CREATE DataBase LO;
GO
USE LO;

CREATE TABLE Weapon
(
  ClassWeapon CHAR(50) NULL,
  Level INT NULL,
  MinDmg INT NULL,
  MaxDmg INT NULL,
  AtkSpeed NUMERIC(3,2) NULL,
  PerSecondDmg NUMERIC(6,2) NULL,
  PlayerLevel INT NULL,
  SkillLevel INT NULL,
  Image BIT DEFAULT 0 NULL,
  Name CHAR(100) NULL,
  Weapons CHAR(50) NOT NULL,
  Monsters CHAR(50) NULL,
  PRIMARY KEY (Weapons)  
);
ALTER TABLE Weapon ADD FOREIGN KEY (Monsters) REFERENCES Monster(Monsters);

CREATE TABLE Armor
(
  ClassArmor CHAR(50) NULL,
  Name CHAR(100) NULL,
  Level INT NULL,
  Defense INT NULL,
  Image BIT DEFAULT 0 NULL,
  Armors CHAR(50) NOT NULL,
  Monsters CHAR(50) NULL,
  PRIMARY KEY (Armors)  
);
ALTER TABLE Armor ADD FOREIGN KEY (Monsters) REFERENCES Monster(Monsters);

CREATE TABLE Menu
(
  Weapons CHAR(50) NULL,
  Armors CHAR(50) NULL,
  Monsters CHAR(50) NULL 
);
ALTER TABLE Menu ADD FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons);
ALTER TABLE Menu ADD FOREIGN KEY (Armors) REFERENCES Armor(Armors);
ALTER TABLE Menu ADD FOREIGN KEY (Monsters) REFERENCES Monster(Monsters);

CREATE TABLE Monster
(
  Name CHAR(100) NULL,
  Level INT NULL,
  MinHealth INT NULL,
  MaxHealth INT NULL,
  MinDmg INT NULL,
  MaxDmg INT NULL,
  AtkSpeed NUMERIC(3,2) NULL,
  Location CHAR(100) NULL,
  Monsters CHAR(50) NOT NULL,
  DropItems CHAR(100) NULL,
  Image BIT DEFAULT 0 NULL,
  PRIMARY KEY (Monsters)  
);
ALTER TABLE Monster ADD FOREIGN KEY (DropItems) REFERENCES Items(DropItems);

CREATE TABLE Items 
(
  DropItems CHAR(100) NOT NULL,
  Weapons CHAR(50) NULL,
  Armors CHAR(50) NULL,
  PRIMARY KEY (DropItems)
);
ALTER TABLE Items ADD FOREIGN KEY (Weapons) REFERENCES Weapon(Weapons);
ALTER TABLE Items ADD  FOREIGN KEY (Armors) REFERENCES Armor(Armors);