blog




  • Essay / Primary and Foreign Keys

    Table of ContentsIntroductionA primary key follows these rulesReferential IntegrityIntroductionA primary key is an exclusive data element that allows the user to identify any item in the record. When you specify a primary key constraint for a table, the database engine enforces data exclusivity by automatically creating a unique index for the primary key columns. This is useful for identifying the data you need quickly and efficiently. For example, you can identify a student in a database full of students with only their ID rather than their contact information. Say no to plagiarism. Get a tailor-made essay on “Why Violent Video Games Should Not Be Banned”? Get an original essay A primary key follows these rules A table can only have one primary key, this is usually the “ID” key. It cannot exceed 16 columns and a key length of 900 bytes. The index generated by a primary key constraint cannot cause the number of indexes on the table to exceed 999 nonclustered indexes and 1 clustered index. If clustered or nonclustered is not specified for a primary key constraint, clustered is used if there is no clustered index on the table. All columns defined in a primary key constraint must be defined as non-zero. If nullability is not specified, all columns participating in a primary key constraint have their nullability set to non-zero. If a primary key is defined on a CLR user-defined type column, the type implementation must support binary ordering. A foreign key is a column or combination of columns used to establish and enforce a link between data in two tables to control what data can be stored in the foreign key table. A link is created between two tables when the column(s) containing the primary key value of one table are referenced by the column(s) of another table. This column becomes a foreign key in the second table. Referential Integrity Referential integrity is a relational database concept, which states that relationships between tables must always be consistent. In other words, any foreign key field must match the primary key referenced by the foreign key. Therefore, any primary key field changes must be applied to all foreign keys, or not at all. The same restriction also applies to foreign keys in that any update (but not necessarily deletion) must be propagated to the primary parent key. Keep in mind: this is just an example. Get a personalized article from our expert writers now. Get a custom essayFor example, deleting a record containing a value referenced by a foreign key in another table would break referential integrity. Some relational database management systems can enforce referential integrity, either by also deleting foreign key rows to maintain integrity, or by returning an error and not performing the deletion. The method used can be determined by a referential integrity constraint defined in a data dictionary..