请输入您要查询的百科知识:

 

词条 Foreign key
释义

  1. Referential actions

      CASCADE    RESTRICT    NO ACTION    SET DEFAULT , SET NULL    Triggers  

  2. Example

  3. See also

  4. References

  5. External links

{{lead too long|date=June 2018}}

In the context of relational databases, a foreign key is a field (or collection of fields) in one table that uniquely identifies a row of another table or the same table.[1][2][3] In simpler words, the foreign key is defined in a second table, but it refers to the primary key or a unique key in the first table. For example, a table called Employees has a primary key called employee_id. Another table called Employee Details has a foreign key which references employee_id in order to uniquely identify the relationship between the two tables.

The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table.[4] In database relational modeling and implementation, a unique key is a set of zero or more attributes, the values of which are guaranteed to be unique for each tuple (row) in a relation. The value or combination of values of unique key attributes for any tuple cannot be duplicated for any other tuple in that relation.

When more than one column is combined to form a unique key, their combination is used to access each row and maintain uniqueness. The values of the columns are not combined, they are compared using their data types.

Since the purpose of the foreign key is to identify a particular row of referenced table, it is generally required that the foreign key is equal to the candidate key in some row of the primary table, or else have no value (the NULL value.[2]). This rule is called a referential integrity constraint between the two tables.[5]

Because violations of these constraints can be the source of many database problems, most database management systems provide mechanisms to ensure that every non-null foreign key corresponds to a row of the referenced table.[6][7][8]

For example, consider a database with two tables: a CUSTOMER table that includes all customer data and an ORDER table that includes all customer orders. Suppose the business requires that each order must refer to a single customer. To reflect this in the database, a foreign key column is added to the ORDER table (e.g., CUSTOMERID), which references the primary key of CUSTOMER (e.g. ID). Because the primary key of a table must be unique, and because CUSTOMERID only contains values from that primary key field, we may assume that, when it has a value, CUSTOMERID will identify the particular customer which placed the order. However, this can no longer be assumed if the ORDER table is not kept up to date when rows of the CUSTOMER table are deleted or the ID column altered, and working with these tables may become more difficult. Many real world databases work around this problem by 'inactivating' rather than physically deleting master table foreign keys, or by complex update programs that modify all references to a foreign key when a change is needed.

Foreign keys play an essential role in database design. One important part of database design is making sure that relationships between real-world entities are reflected in the database by references, using foreign keys to refer from one table to another.[9]

Another important part of database design is database normalization, in which tables are broken apart and foreign keys make it possible for them to be reconstructed.[10]

Multiple rows in the referencing (or child) table may refer to the same row in the referenced (or parent) table. In this case, the relationship between the two tables is called a one to many relationship between the referenced table and the referencing table.

In addition, the child and parent table may, in fact, be the same table, i.e. the foreign key refers back to the same table. Such a foreign key is known in 2003 as a self-referencing or recursive foreign key. In database management systems, this is often accomplished by linking a first and second reference to the same table.

A table may have multiple foreign keys, and each foreign key can have a different parent table. Each foreign key is enforced independently by the database system. Therefore, cascading relationships between tables can be established using foreign keys.

ALTER TABLE

   ADD [ CONSTRAINT  ]      FOREIGN KEY (  {, }... )      REFERENCES 
[ ( {, }... ) ] [ ON UPDATE ] [ ON DELETE ]

Likewise, foreign keys can be defined as part of the CREATE TABLE SQL statement.

CREATE TABLE table_name (

   id    INTEGER  PRIMARY KEY,   col2  CHARACTER VARYING(20),   col3  INTEGER,   ...   FOREIGN KEY(col3)      REFERENCES other_table(key_col) ON DELETE CASCADE,   ... )

If the foreign key is a single column only, the column can be marked as such using the following syntax:

CREATE TABLE table_name (

   id    INTEGER  PRIMARY KEY,   col2  CHARACTER VARYING(20),   col3  INTEGER REFERENCES other_table(column_name),   ... )

Foreign keys can be defined with a stored procedure statement.{{Elucidate|date=September 2012}}

sp_foreignkey tabname, pktabname, col1 [, col2] ... [, col8]

  • tabname: the name of the table or view that contains the foreign key to be defined.
  • pktabname: the name of the table or view that has the primary key to which the foreign key applies. The primary key must already be defined.
  • col1: the name of the first column that makes up the foreign key. The foreign key must have at least one column and can have a maximum of eight columns.

Referential actions

{{main|Propagation constraint}}

Because the database management system enforces referential constraints, it must ensure data integrity if rows in a referenced table are to be deleted (or updated). If dependent rows in referencing tables still exist, those references have to be considered. 2003 specifies 5 different referential actions that shall take place in such occurrences:

  • CASCADE
  • RESTRICT
  • NO ACTION
  • SET NULL
  • SET DEFAULT

CASCADE

Whenever rows in the master (referenced) table are deleted (or updated), the respective rows of the child (referencing) table with a matching foreign key column will be deleted (or updated) as well. This is called a cascade delete (or update).

RESTRICT

A value cannot be updated or deleted when a row exists in a referencing or child table that references the value in the referenced table.

Similarly, a row cannot be deleted as long as there is a reference to it from a referencing or child table.

To understand RESTRICT (and CASCADE) better, it may be helpful to notice the following difference, which might not be immediately clear. The referential action CASCADE modifies the "behavior" of the (child) table itself where the word CASCADE is used. For example, ON DELETE CASCADE effectively says "When the referenced row is deleted from the other table (master table), then delete also from me". However, the referential action RESTRICT modifies the "behavior" of the master table, not the child table, although the word RESTRICT appears in the child table and not in the master table! So, ON DELETE RESTRICT effectively says: "When someone tries to delete the row from the other table (master table), prevent deletion from that other table (and of course, also don't delete from me, but that's not the main point here)."

RESTRICT is not supported by Microsoft SQL 2012 and earlier.

NO ACTION

NO ACTION and RESTRICT are very much alike. The main difference between NO ACTION and RESTRICT is that with NO ACTION the referential integrity check is done after trying to alter the table. RESTRICT does the check before trying to execute the UPDATE or DELETE statement. Both referential actions act the same if the referential integrity check fails: the UPDATE or DELETE statement will result in an error.

In other words, when an UPDATE or DELETE statement is executed on the referenced table using the referential action NO ACTION, the DBMS verifies at the end of the statement execution that none of the referential relationships are violated. This is different from RESTRICT, which assumes at the outset that the operation will violate the constraint. Using NO ACTION, the triggers or the semantics of the statement itself may yield an end state in which no foreign key relationships are violated by the time the constraint is finally checked, thus allowing the statement to complete successfully.

SET DEFAULT , SET NULL

In general, the action taken by the DBMS for SET NULL or SET DEFAULT is the same for

both ON DELETE or ON UPDATE: The value of the affected referencing attributes is

changed to NULL for SET NULL, and to the specified default value for SET DEFAULT.

Triggers

Referential actions are generally implemented as implied triggers (i.e. triggers with system-generated names, often hidden.) As such, they are subject to the same limitations as user-defined triggers, and their order of execution relative to other triggers may need to be considered; in some cases it may become necessary to replace the referential action with its equivalent user-defined trigger to ensure proper execution order, or to work around mutating-table limitations.

Another important limitation appears with transaction isolation: your changes to a row may not be able to fully cascade because the row is referenced by data your transaction cannot "see", and therefore cannot cascade onto. An example: while your transaction is attempting to renumber a customer account, a simultaneous transaction is attempting to create a new invoice for that same customer; while a CASCADE rule may fix all the invoice rows your transaction can see to keep them consistent with the renumbered customer row, it won't reach into another transaction to fix the data there; because the database cannot guarantee consistent data when the two transactions commit, one of them will be forced to roll back (often on a first-come-first-served basis.)

CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));

CREATE TRIGGER ins_sum BEFORE INSERT ON account

Example

As a first example to illustrate foreign keys, suppose an accounts database has a table with invoices and each invoice is associated with a particular supplier. Supplier details (such as name and address) are kept in a separate table; each supplier is given a 'supplier number' to identify it. Each invoice record has an attribute containing the supplier number for that invoice. Then, the 'supplier number' is the primary key in the Supplier table. The foreign key in the Invoices table points to that primary key. The relational schema is the following. Primary keys are marked in bold, and foreign keys are marked in italics.

   Supplier ( '''SupplierNumber''', Name, Address, Type )   Invoices ( '''InvoiceNumber''', ''SupplierNumber'', Text )

The corresponding Data Definition Language statement is as follows.

  CREATE TABLE Supplier (     SupplierNumber  INTEGER NOT NULL,     Name            VARCHAR(20) NOT NULL,     Address         VARCHAR(50) NOT NULL,     Type            VARCHAR(10),     CONSTRAINT supplier_pk PRIMARY KEY(SupplierNumber),     CONSTRAINT number_value CHECK (SupplierNumber > 0) )
  CREATE TABLE Invoices (     InvoiceNumber   INTEGER NOT NULL,     SupplierNumber  INTEGER NOT NULL,     Text            VARCHAR(4096),     CONSTRAINT invoice_pk PRIMARY KEY(InvoiceNumber),     CONSTRAINT inumber_value CHECK (InvoiceNumber > 0),     CONSTRAINT supplier_fk FOREIGN KEY(SupplierNumber)        REFERENCES Supplier(SupplierNumber)        ON UPDATE CASCADE ON DELETE RESTRICT )

See also

  • Candidate key
  • Compound key
  • Superkey
  • Junction table

References

1. ^{{cite book|last=Coronel|first=Carlos|title=Database Systems: Design, Implementation, and Management|year=2010|publisher=South-Western/Cengage Learning|location=Independence KY|isbn=978-0-538-74884-1|page=65}}
2. ^{{cite book|last=Elmasri|first=Ramez|title=Fundamentals of Database Systems|year=2011|publisher=Addison-Wesley|isbn=978-0-13-608620-8|pages=73–74}}
3. ^{{cite book|last=Date|first=C. J.|title=A guide to the SQL standard|year=1996|publisher=Addison-Wesley|isbn=978-0201964264|page=206}}
4. ^{{cite book|last=Sheldon|first=Robert|title=Beginning MySQL|year=2005|publisher=John Wiley & Sons|isbn=0-7645-7950-9|pages=119–122}}
5. ^{{Cite web | url = http://www.visualcase.com/kbase/database_basics_-_foreign_keys.htm | title = Database Basics — Foreign Keys | accessdate = 2010-03-13}}
6. ^{{cite book|last=MySQL AB|title=MySQL Administrator's Guide and Language Reference|year=2006|publisher=Sams Publishing|isbn=0-672-32870-4|page=40}}
7. ^{{cite book|last=Powell|first=Gavin|title=Oracle SQL: Jumpstart with Examples|year=2004|publisher=Elsevier|asin= B008IU3AHY|page=11}}
8. ^{{cite book|last=Mullins|first=Craig|title=DB2 developer's guide|year=2012|publisher=IBM Press|asin=B007Y6K9TK}}
9. ^{{cite book|last=Sheldon|first=Robert|title=Beginning MySQL|year=2005|publisher=John Wiley & Sons|isbn=0-7645-7950-9|page=156}}
10. ^{{cite book|last=Garcia-Molina|first=Hector|title=Database Systems: The Complete Book|year=2009|publisher=Prentice Hall|isbn=978-0-13-187325-4|pages=93–95}}

External links

  • [https://mariadb.com/kb/en/constraint_type-foreign-key-constraint/ SQL-99 Foreign Keys]
  • PostgreSQL Foreign Keys
  • MySQL Foreign Keys
  • FirebirdSQL primary Keys
  • SQLite support for Foreign Keys
  • [https://technet.microsoft.com/en-us/library/ms188066.aspx Microsoft SQL 2012 table_constraint (Transact-SQL)]
{{Databases}}{{DEFAULTSORT:Foreign Key}}Schlüssel (Datenbank)#FremdschlüsselNøkkel (database)#Fremmednøkkel关系键#外键

4 : Data modeling|Databases|SQL|Articles with example SQL code

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/10 11:02:55