将列代码设置为 table 上的外键并引用另一个 table
set the column code as foreign key on table with a reference to another table
enter image description here我的两个 table 有问题。一个 table tblShoes 的名称,另一个是 tblplayer。问题是 我想将 table tblPLAYERS 中的列代码设置为 FOREIGN KEY,并引用 table tblSHOES 列代码 。但我试图在 table tblPLAYER 中插入我的代码,但它不起作用,你能帮我解释一下为什么吗?
这是我的两个 table.
create table tbl_Shoe
(
code varchar not null primary key,
brand varchar not null,
model int not null,
size varchar not null
);
create table tbl_PLAYER
(
RosterNo int primary key,
Name varchar ,
Position varchar,
Code varchar
primary key (code),
foreign key (Code) references tblShoe (code)
);
请帮助我!你对我帮助很大。多谢。
这是我的代码
http.clickimage
当你定义一个外键时,你就是在说 "hey database, guarantee that this value exists in the other table"。在你的例子中,你是说无论你在 tblPlayer.Code
列中输入什么值,都需要有一个在 tblShoe
中具有相同值的匹配行。例如,如果我在 tblShoe 中只有代码 A、B 和 C,并且我尝试用代码 D 向 tblPlayer
中插入一行,它将不起作用。
外键是对另一个 table 的引用。它用于建立 table 之间的关系。例如,tbl_Shoe 和 tbl_PLAYER table 之间的关系。一个 tbl_Shoe 可以有多个代码。 tbl_Shoe 的主键成为另一个 table 代码的外键,即 tbl_PLAYER.so 这是我的答案。
create table tbl_Shoes
Codes varchar (20) not null primary key,
brand varchar(50) not null,
model int not null,
size varchar(10) not null
);
create table tbl_PLAYERS
(
RosterNo int primary key,
Name varchar(20) ,
Position varchar(20),
code varchar (20) not null,
Codes varchar (20) not null constraint fk_code foreign key references tblShoes(Codes)
);
enter image description here我的两个 table 有问题。一个 table tblShoes 的名称,另一个是 tblplayer。问题是 我想将 table tblPLAYERS 中的列代码设置为 FOREIGN KEY,并引用 table tblSHOES 列代码 。但我试图在 table tblPLAYER 中插入我的代码,但它不起作用,你能帮我解释一下为什么吗? 这是我的两个 table.
create table tbl_Shoe
(
code varchar not null primary key, brand varchar not null, model int not null, size varchar not null
);
create table tbl_PLAYER
(
RosterNo int primary key, Name varchar , Position varchar, Code varchar
primary key (code), foreign key (Code) references tblShoe (code)
);
请帮助我!你对我帮助很大。多谢。 这是我的代码 http.clickimage
当你定义一个外键时,你就是在说 "hey database, guarantee that this value exists in the other table"。在你的例子中,你是说无论你在 tblPlayer.Code
列中输入什么值,都需要有一个在 tblShoe
中具有相同值的匹配行。例如,如果我在 tblShoe 中只有代码 A、B 和 C,并且我尝试用代码 D 向 tblPlayer
中插入一行,它将不起作用。
外键是对另一个 table 的引用。它用于建立 table 之间的关系。例如,tbl_Shoe 和 tbl_PLAYER table 之间的关系。一个 tbl_Shoe 可以有多个代码。 tbl_Shoe 的主键成为另一个 table 代码的外键,即 tbl_PLAYER.so 这是我的答案。
create table tbl_Shoes
Codes varchar (20) not null primary key,
brand varchar(50) not null,
model int not null,
size varchar(10) not null
);
create table tbl_PLAYERS
(
RosterNo int primary key,
Name varchar(20) ,
Position varchar(20),
code varchar (20) not null,
Codes varchar (20) not null constraint fk_code foreign key references tblShoes(Codes)
);