Database: one-to-many relationship (or one-to-one relationship)

Im modeling a database in MSSQL 2008.

I have 4 tables.

**User**
userID
userName

**NewsCategory**
newsCategoryID
newsCategoryName

**News**
newsID
newsText
newsCategoryID

**Subscription**
userID
categoryID

I understand that I must have foreign keys between news and category tables. But what should I do with the inscriptions? Should I have a foreign key between user and subscription tables, although it is not necessary to subscribe to something?

+3
source share
5 answers

Yes you should. A foreign key is used to ensure that a subscription is created for an existing user. A foreign key does not mean that the user must subscribe to something.

+7
source

, , , .

.

+2

Subscription - (-) " " , / .

, , .

. - () FK NULLable "".

+2

, User SubCription .

- . , "" , "", "", "NewsCategory". , .

0

. .

, . null. - , .

SUBSCRIPTIONS, USERS; , . SUBSCRIPTIONS :

ALTER TABLE SUBSCRIPTIONS ADD CONSTRAINT FK_SUBSCRIPTIONS_USERS FOREIGN KEY (userid) (userid) ALTER TABLE SUBSCRIPTIONS ADD CONSTRAINT FK_SUBSCRIPTIONS_CATEGORIES FOREIGN KEY (categoryid) ()

, : , , , . BTW, , (), , ; .

, : , ​​ . ( ), . , , , .

0

All Articles