So I have an Employee table, where your PK is this Cod_func.
After I created this Seller table, I created the attributes manually, I did not put Cod_func from the Seller Table as PK,
because this is an attribute that already exists in the Employee Table.
What is defined in the Employee table is only valid for it; it is not inherited by the Seller table, even if one of the columns of the Seller table refers to another column in the Employee table.
To define an existing column as a primary key it is necessary that it does not allow the absence of values; that is, the column must be declared as NOT NULL. The demonstration is simple.
Considering the relationship 1:1 between Employee and Seller, and the following table declarations have already been defined previously:
-- código #1
CREATE TABLE Funcionário (
Cód_Func int,
Nome_Func varchar(50),
constraint I1_Funcionário primary key (Cód_Func)
);
and
-- código #2
CREATE TABLE Vendedor (
Cód_Func int,
Num_Percentual_Comissão numeric(3,1),
constraint FK_Vend_Func foreign key (Cód_Func)
references Funcionário (Cód_Func)
);
when executing code with ALTER TABLE instruction, to create the primary key of the Seller table
-- código #3
ALTER TABLE Vendedor
add constraint I1_Vendedor primary key (Cód_Func);
the following error messages are displayed:
Mensagem 8111, Nível 16, Estado 1, Linha 1
Não é possível definir a restrição PRIMARY KEY em coluna anulável na tabela 'Vendedor'.
Mensagem 1750, Nível 16, Estado 0, Linha 1
Não foi possível criar a restrição. Consulte os erros anteriores.
That is, the Code column of the Seller table must be defined as NOT NULL, which is simple to execute:
-- código #4
ALTER TABLE Vendedor
alter column Cód_Func int not null;
Once this is done, you can run code #3 above again and the Seller table will accept the Cód_func column as the primary key.
When declaring the columns of a table, the usual is to define as NOT NULL all those that are mandatory, even if it is not used to compose the primary key. In code #1, the Code_func column of the Employee table was purposely not declared as NOT NULL. But in the statement of the table there is the restriction I1_employee as Primary key, which implicitly makes the Code column function as NOT FULL.
The ideal is to explicitly define the attributes of each object during the declaration:
-- código #5 v2
CREATE TABLE Funcionário (
Cód_Func int not null,
Nome_Func varchar(50) not null,
constraint I1_Funcionário primary key (Cód_Func)
);
CREATE TABLE Vendedor (
Cód_Func int not null,
Num_Percentual_Comissão numeric(3,1) not null,
constraint I1_Vendedor primary key (Cód_Func),
constraint FK_Vend_Func foreign key (Cód_Func)
references Funcionário (Cód_Func)
);
Wouldn’t it be ideal for you to have a Cod_seller in this table (Seller) as PK ? Also not understood what you want to do, you want to add what in the seller tab? You already have the FK no ?
– Thiago Loureiro
I believe what he wants is to create a 1-to-1 relationship between employee and seller.
– Pablo Tondolo de Vargas