在 SQL 中指定表时出现问题
我对数据库设计比较陌生。我有一个具有以下设计的数据库,
我已在 SQL 中指定了表,如下所示。
CREATE TABLE Piece
(identifier INT NOT NULL Unique,
value INT NOT NULL,
the date it was acquited DATE,
the date it was made DATE,
PRIMARY KEY (identifier));
CREATE TABLE Person
(name VARCHAR(50),
person_id INT NOT NULL Unique,
biography VARCHAR (50),
date of birth DATE,
date of death DATE,
PRIMARY KEY (person_id));
CREATE TABLE Jewel
(code INT NOT NULL Unique,
gem type VARCHAR (50),
weight INT,
quality VARCHAR (50),
color VARCHAR (50),
description VARCHAR (50),
PRIMARY KEY (code));
CREATE TABLE Gem
(type VARCHAR (50) NOT NULL,
hardness INT,
density INT,
FOREIGN KEY (type) references JEWEL(gem type));
CREATE TABLE Ownership
(person VARCHAR (50),
piece INT,
start of ownership DATE,
end of ownership DATE,
FOREIGN KEY (person) references PERSON(person_id),
FOREIGN KEY (piece) references PIECE(identifier));
我的问题是
1.) 如何指定 GEM 表的主键,因为所有 3 个属性都不是唯一的,我是否应该创建一个像 Gem_id 这样的新属性,因为我更愿意使用现有属性而不是添加新属性。
2.)我在 person 表中使用了 person_id 属性来使其唯一并将其用作主键,是否有另一种方法可以为 person 表创建主键而不添加额外的属性,显然我不能包含对现有属性并使其唯一
3.) 所有完整性约束和数据类型都正确吗?我的设计有什么缺陷吗?
I am relatively new to database design. I have a database which has the following design
I have specified the the tables in SQL as follows.
CREATE TABLE Piece
(identifier INT NOT NULL Unique,
value INT NOT NULL,
the date it was acquited DATE,
the date it was made DATE,
PRIMARY KEY (identifier));
CREATE TABLE Person
(name VARCHAR(50),
person_id INT NOT NULL Unique,
biography VARCHAR (50),
date of birth DATE,
date of death DATE,
PRIMARY KEY (person_id));
CREATE TABLE Jewel
(code INT NOT NULL Unique,
gem type VARCHAR (50),
weight INT,
quality VARCHAR (50),
color VARCHAR (50),
description VARCHAR (50),
PRIMARY KEY (code));
CREATE TABLE Gem
(type VARCHAR (50) NOT NULL,
hardness INT,
density INT,
FOREIGN KEY (type) references JEWEL(gem type));
CREATE TABLE Ownership
(person VARCHAR (50),
piece INT,
start of ownership DATE,
end of ownership DATE,
FOREIGN KEY (person) references PERSON(person_id),
FOREIGN KEY (piece) references PIECE(identifier));
My question is
1.) How can I specify a primary key to the GEM table as all the 3 attributes are not unique, should I have to create a new attribute like Gem_id, as I would prefer to use the existing attributes and not add a new attribute.
2.) I have used person_id attribute in the person table to make it unique and use it as a primary key, is there another way to create a primary key for the person table without adding the extra attribute and obviously I cant include constraints to the existing attribute and make it UNIQUE
3.) Is all integrity constraints and data types right ? Is my design flawed in any way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)是的,您可以创建多列主键(复合主键),但它将是唯一的。像这样的事情应该可以做到:
2)还有其他方法可以为 Person 表创建主键,但我不推荐它。 Person_id 是我在这里使用的。
3)我将对您的设计进行以下更改:
person_id
的外键,那么您不希望它是 VARCHAR。GemTypeId
之类的。gem type
列替换为新的GemTypeId
列。可能还有更多,但这些是让我震惊的。
看起来您试图不使用 ID 来表示您的数据。这是有原因的吗?如果您继续沿着这条路走下去,您可能会遇到数据完整性问题。
1) Yes you can create a multi column primary key (composite primary key) but it will be unique. Something like this should do it:
2) There are other ways to create a primary key for the Person table but I would not recommend it. Person_id is what I would use here.
3) I would make the following changes to your design:
person_id
, then you don't want it to be a VARCHAR.GemTypeId
or something.gem type
column with your newGemTypeId
column.There might be more, but these were the ones that jumped out at me.
It seems like you are trying not to use IDs to represent your data. Is there a reason for this? If you continue down this road you may run into data integrity issues.