What is PK, FK in rational databases like Oracle, MySQL?

Loading...

This post will show you what is PK, FK in rational databases such as Oracle, MySQL, Postgre, MSSQLServer. They are basics but they are also important.

What is PK – Primary Key

PK or Primary Key is the unique, not null column in the table. Unique means with one value of this column, we can identify one, and no more than one row in the table.

A column satisfied those conditions might not become PK, but if the column is a PK then, it MUST satisfy those conditions.

idnamepassword
1John123456
2Jay654321
3John

In this table above, only id column can be PK since other column is not unique (name column) or is null (password column). In this case, we can asign ID column as PK.

What is FK – Foreign Key

FK is another very common definition in rational databases world. FK or foreign keys are keys which aren primary key in another table.

Take a look at this example: We have had table 1 defined above, now we have another table called table 2 holds user classes

iduser_idclass_id
136
217
328

In this table, we can tell that, ID is the PK of table 2, user_id is a PK of table 1 above, here in table 2, user_id is a FK. Also with class_id, we can asume that class_id also an FK here.

Loading...