Creating relationships between tables

My question is specifically about the sql server, but perhaps anyone can answer it with any database background

If I want table A to have a 1: 1 relationship with table B in a specific column, should I somehow modify the CREATE TABLE statement to determine this relationship, or is it something that doesn't work at all (but rather it is handled by logic )?

EDIT
The second part of my question: what's the point of embedding this in code? why not just handle it logically when selecting / updating?

+3
source share
3 answers

, , A, B:

create table TableB (
    Id int primary key identity(1,1),
    Name varchar(255))

create table TableA (
    Id int primary key identity(1,1),
    Name varchar(255),
    TableBRelation int unique,
    foreign key (TableBRelation) references TableB (Id))

SQL , .

, , :

  • . , , ? . .

  • - - . , , .

+5

1:1, B . , B , PK A, 1:1...

+1

You can customize the foreign foreign key and add a constraint for its uniqueness. This would set a 1: 1 ratio between your tables.

+1
source

Source: https://habr.com/ru/post/1768652/


All Articles