Constraint ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Constraint ?

what is constraint , I found that is the rule for the data type of the table, but i'm still confused , when I have to use it ? for example : CREATE TABLE Orders (     OrderID int NOT NULL,     OrderNumber int NOT NULL,     PersonID int,     PRIMARY KEY (OrderID),     CONSTRAINT FK_PersonOrder FOREIGNKEY (PersonID)     REFERENCES Persons(PersonID) );

12th Jan 2018, 8:25 PM
Rayen Jbahi
Rayen Jbahi - avatar
4 Answers
+ 2
It says that in order to INSERT a line, an entry with the same PersonID must be in table Persons.
12th Jan 2018, 9:18 PM
1of3
1of3 - avatar
+ 2
OK but why I have to put the constraint key , why I can't do just this ..... FK_PersonOrder FOREIGNKEY (PersonID) REFERENCES Persons(PersonID) ?
12th Jan 2018, 9:33 PM
Rayen Jbahi
Rayen Jbahi - avatar
+ 2
Because you have to start with some keyword. Fk_PersonOrder is the name for the foreign key. If the CONSTRAINT wasn't there the line would start with the name.
12th Jan 2018, 10:10 PM
1of3
1of3 - avatar
+ 2
AAA I get it now , thank you , it was really helpful , because I am going to pass an exam on it
13th Jan 2018, 12:05 PM
Rayen Jbahi
Rayen Jbahi - avatar