SQL 数据库创建错误
SQL DB Creation Error
我用 BIT DEFAULT 0,
作为布尔值,不知道这样对不对。好吧,问题是我无法创建任何 table 因为 FK 中存在问题,不明白为什么?我还添加了 ERD 图像以获得更好的视图。
也许有人 will/can 这么好,给我解释一下,ty!
也许有人知道将 ERD 转换为 MSSQL 代码或 MySQL 代码的好程序或在线沙箱?如果能免费使用就好了。提前致谢!
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)
);
Well, the problem is that I cannot create any table because there is a problem in FK, dont get why?
简单地说,您不能引用不存在的对象。
您应该先创建表,然后添加 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);
感谢@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);
我用 BIT DEFAULT 0,
作为布尔值,不知道这样对不对。好吧,问题是我无法创建任何 table 因为 FK 中存在问题,不明白为什么?我还添加了 ERD 图像以获得更好的视图。
也许有人 will/can 这么好,给我解释一下,ty!
也许有人知道将 ERD 转换为 MSSQL 代码或 MySQL 代码的好程序或在线沙箱?如果能免费使用就好了。提前致谢!
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)
);
Well, the problem is that I cannot create any table because there is a problem in FK, dont get why?
简单地说,您不能引用不存在的对象。
您应该先创建表,然后添加 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);
感谢@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);