开发者

help in solving a problem in sql server

I have aquestions from you. Does SQL server have any features so we can limit a field to fill with just specific values? For example assume that you have a field named "Name" then we want SQL just let us to fill this field with the following values: "Bella", "Jack", "Rose". is 开发者_如何学JAVAthere any featues to do it? please guide me. thanks


You can use a CHECK constraint:

ALTER TABLE dbo.YOUR_TABLE
ADD CONSTRAINT chk_name CHECK (name IN ('Rose', 'Bella', 'Jack'));

...but you might want to use a separate table & foreign key if you need to add identical CHECK constraints to numerous tables:

NAMES

  • name_id (primary key)
  • name

Foreign Key Constraint:

ALTER TABLE dbo.YOUR_TABLE
ADD CONSTRAINT fk_names FOREIGN KEY (name)
REFERENCES dbo.NAMES (name_id) ;


Use Check Constraint

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜