开发者

TSQL Column Constraints [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

So, I have a table with three columns, and I want to be able to say, that if column 1 has "A" in it, then Column 2 MUST be "B" and Column 3 MUST be "C"

So the table would be like...

"A" "B" "C"

"D" "E" "F"

"G" "H" "I"

The first column should determine the value of the other two

I'd just google this, but I'm not sure what it's called exactly.

I really hope that m开发者_Go百科akes sense.

I'm using Microsoft SQL Management Studio

EDIT

Apparently my question is very confusing, so I'll try and explain more: Let's say we have a table of animals and what they eat.

We have a giraffe, a fish, and a lion

A giraffe eats leaves

A fish eats water

A lion eats meat

(ignore the inaccuracy of any of that).

When I fill in a new Giraffe into the table I want to GUARANTEE that ONLY leaves can be put into the other column. An animal should ONLY be able to eat the food it is assigned. How do I ensure that?


You don't need a constraint: just proper design. There are too many permutations of food and animals to have as code: for a new animal of food, you'd need to change your constraints. And what if an animal eats more than one food?

I'd add a

  • food table
  • a link (many-many) table between food and animals

The link table is loaded with what each animal can eat. The food info is not part of the animal table.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜