A primary key is a unique column, or group of columns, used to identify a row of data. When creating a table using SQL, specify which column or columns you want to be the primary key. The primary key values cannot be NULL and must be unique and identify to at most one row.
If just one column is used as the primary key, that is called a simple primary key. Sometimes multiple columns, called a composite primary key, are necessary to uniquely identify a row. Composite primary keys surrounded with parentheses.
A minimal primary key uses all the columns uniqueness.
A foreign key is one or more columns that refer to a primary key. The data types of the foreign and primary keys must be the same, but the names may be different.
Foreign keys have different rules than primary keys:
Foreign key values may be repeated.
Foreign key values must match up to a primary key value.
Foreign key values can be NULL
Foreign keys also have some special cases:
- Multiple foreign keys may refer to the same primary key.
- A foreign key may refer to a primary key within the same table.
- A foreign key that refers to a composite primary key (multiple columns) must also be composite.
Subscribe to Appficial for more programming videos coming soon. Also, don't forget to click LIKE and comment on the video if it helped you out!
Ещё видео!