Structuring a database

Asked

Viewed 33 times

0

Good Morning. I am structuring a database for a project and would like to know if it is possible to have a database(or table) with only foreign keys to connect to all other tables, or is it mandatory to have a primary key?

  • The ideal is that every table has a primary key, this is a way to identify each row in this table. Note that it can be a primary key composed of all its attributes. The non-existence of a primary key allows you to include duplicate lines, which doesn’t seem to make much sense in your model.

1 answer

0


Yes, it is possible, it is not mandatory to have a primary key, but with the primary key you can identify which specific row of the table you want.

Example:

CREATE TABLE [dbo].People(
    ID int NOT NULL,
    PRIMARY KEY (ID))

CREATE TABLE [dbo].Persons(
    ID int NOT NULL,
    PRIMARY KEY (ID))


CREATE TABLE Orders (
    PersonID int FOREIGN KEY REFERENCES Persons(ID),
    PeopleID int FOREIGN KEY REFERENCES People(ID)
);

select * from People
select * from Persons
select * from Orders 

Drop table Orders
Drop table People
Drop table Persons

Browser other questions tagged

You are not signed in. Login or sign up in order to post.