[英]Primary keys in sql
I have here some problems undersanding how this works. 我在这里有一些问题,不知道它是如何工作的。 So I have 2 tables Students and Enrolled like this:
所以我有2个学生表并注册如下:
CREATE TABLE Students
(sid CHAR(20),
name CHAR(50),
email CHAR(30),
age INTEGER,
gr INTEGER)
CREATE TABLE Enrolled
(sid CHAR(20),
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid,cid))
So I don't understand this particulary row PRIMARY KEY (sid,cid)
Can someone explain to me how it works? 所以我不明白这个特殊的行
PRIMARY KEY (sid,cid)
有人可以向我解释它是如何工作的吗? I have to specify that I have another table Courses from where the cid.
我必须指定我有另一个表Courses的课程
cid.
Is is equivalent saying like this: 是等同的说法是这样的:
CREATE TABLE Enrolled
(sid CHAR(20) foreign key references Students(sid),
cid CHAR(20) foreign key references Courses(cid)
)
PRIMARY KEY
means both UNIQUE
and NOT NULL
. PRIMARY KEY
表示UNIQUE
和NOT NULL
。
If you want sid
and cid
to also be FOREIGN KEY
you have to specify that separately. 如果您希望
sid
和cid
也是FOREIGN KEY
,则必须单独指定。
Having two fields for a primary key is often used for tables that are the physical representation of a many-to-many relation. 具有用于主键的两个字段通常用于表,其是多对多关系的物理表示。 In your database design diagram you would have
STUDENT
and COURSE
as entities and ENROLLMENT
as a many-to-many relationship between them. 在您的数据库设计图中,您将把
STUDENT
和COURSE
作为实体,将ENROLLMENT
作为它们之间的多对多关系。
In the physical database diagram many-to-many relationships are modelled as tables, often with a composite PRIMARY KEY
and with FOREIGN KEY
constraints to the entity tables. 在物理数据库图中,多对多关系被建模为表,通常具有复合
PRIMARY KEY
和对实体表的FOREIGN KEY
约束。
PRIMARY KEY(sid,cid)表示复合主键..这些字段的组合应该是唯一的。
A PRIMARY KEY
is used to identify a table. PRIMARY KEY
用于标识表。 A field or column that is defined as PRIMARY KEY
will contains different values on each row in that table, and is mandatory to have a value (so, PRIMARY KEY
is equivalent to UNIQUE
and NOT NULL
). 定义为
PRIMARY KEY
的字段或列将在该表的每一行中包含不同的值,并且必须具有值(因此, PRIMARY KEY
等效于UNIQUE
且NOT NULL
)。
PRIMARY KEY
can be a single field, or multiple fields, but it always satisfy that "each row will have a different PRIMARY KEY
". PRIMARY KEY
可以是单个字段,也可以是多个字段,但它始终满足“每行将具有不同的PRIMARY KEY
”。
If you declare as PRIMARY KEY
a combination of 2 columns, you will be able to have this for example: 如果您将2列的组合声明为
PRIMARY KEY
,则可以使用此示例:
CREATE TABLE Enrolled
(sid CHAR(20),
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid,cid)) --PRIMARY KEY with combination of 2 columns
sid | cid | grade
1 1 XX
1 2 XX
2 1 XX
2 2 XX
2 3 XX
In this example, you can see that the column sid
or the column cid
has repeated values individually, but there isn't a combination of (sid, cid)
that was repeated. 在此示例中,您可以看到列
sid
或列cid
分别重复了值,但没有重复的(sid, cid)
组合。
Like a PRIMARY KEY
is used to identify a row in a table, when we want to relate two tables we can define a FOREIGN KEY
in one table to link this one with the other table. 就像
PRIMARY KEY
用于标识表中的行一样,当我们想要关联两个表时,我们可以在一个表中定义一个FOREIGN KEY
来将这个表与另一个表相关联。
Your case is that the ENROLLED
table is identified by a composite PRIMARY KEY
to represent a many-to-many relationship. 您的情况是
ENROLLED
表由复合PRIMARY KEY
标识,以表示多对多关系。 That is the way to say that: 这就是说:
Note*: Is a best practice to define the PRIMARY KEYS
as numeric values, such integer
, bigint
, etc. because it is better to improve the indexes performance (all PRIMARY KEYS
have defined inherently an INDEX
, and they are faster working with "numeric" values than working with "string" values). 注意*:最佳做法是将
PRIMARY KEYS
定义为数值,例如integer
, bigint
等,因为它可以更好地提高索引性能(所有PRIMARY KEYS
本身都定义了一个INDEX
,它们使用“numeric”更快地工作“值比使用”字符串“值)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.