简体   繁体   English

MySQL Workbench:如何为某些属性设置约束

[英]MySQL Workbench: how to set constraints for certain attributes

In my database, certain attributes must only be set to certain values. 在我的数据库中,某些属性只能设置为某些值。 For example, an attribute "status" can only be set to full-time, part-time, or temporary. 例如,属性“状态”只能设置为全职,兼职或临时。 How and where do I enforce this in MySQL Workbench? 如何在MySQL Workbench中以及在何处强制执行此操作? (as you can probably tell, I'm very new to MySQL) (您可能知道,我是MySQL的新手)

You can do this in several ways. 您可以通过多种方式执行此操作。 The least desirable way would be to set up a trigger. 最不希望的方法是设置触发器。 Another way is to define them as enumerated types. 另一种方法是将它们定义为枚举类型。 I'm not really a fan of enumerated types, because they are not easily shared among different tables. 我并不真正喜欢枚举类型,因为它们不容易在不同的表之间共享。

You can also do this with a reference table and a foreign key constraint: 您也可以使用参考表和外键约束来执行此操作:

create table ReferenceStatus (
    status varchar(255) primary key
);

alter table t add constraint fk_t_status foreign key (status) references ReferenceStatus(status);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM