如何将表列定义为其他表的列?

时间:2022-09-19 18:58:17

just starting to learn databases ...

刚开始学习数据库......

I have two tables
1. Table user => contain UserName[string], Id[int], Category[string]
2. Table possible Category => contain Category[string]

我有两个表1.表user =>包含UserName [string],Id [int],Category [string] 2.表可能Category => contains Category [string]

I want to restrict on my SQL server ( express ) that the third column of the first table ( table user => Category column ) will contain possible value of the second table.

我想限制我的SQL服务器(快递)第一个表的第三列(表user => Category列)将包含第二个表的可能值。

Is it possible ?
How can i do it ?

可能吗 ?我该怎么做 ?

{ i mean that there is no possible way to set Category on the
user table that will not appear on the second table. }

{我的意思是没有可能的方法在用户表上设置不会出现在第二个表上的类别。 }

1 个解决方案

#1


1  

You can set a foreign key constraint:

您可以设置外键约束:

alter table [user] 
add constraint fk_category foreign key ([Category])
    references [possible Category]([Category]);

#1


1  

You can set a foreign key constraint:

您可以设置外键约束:

alter table [user] 
add constraint fk_category foreign key ([Category])
    references [possible Category]([Category]);