+ 1
Can someone explain FOREIGN KEY?
And how make one on CREATE TABLE and ALTER TABLE. I am lost here.
3 Respostas
+ 2
Foreign key represents relation between two tables.A foreign key is a column whose values are derived from primary key of some other table.
The table in which the foreign key is defined is called foreign table or digital table .
+ 1
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables, it also prevents invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the table it points to.
Create example:
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
)
Alter table example:
ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
0
la foreign key garantiza la integridad referencial.