In SQL, theFOREIGN KEYconstraint is used to create a relationship between two tables. A foreign key is defined using theFOREIGN KEYandREFERENCESkeywords. Example -- this table doesn't contain foreign keysCREATETABLECustomers (idINTEGERPRIMARYKEY,nameVARCHAR(100), ageINTEGER);-- create another tabl...
SQLForeignKeys 會以標準結果集的形式傳回結果。 如果要求與主鍵相關聯的外鍵,結果集會依FKTABLE_CAT、FKTABLE_SCHEM、FKTABLE_NAME和KEY_SEQ排序。 如果要求與外鍵相關聯的主鍵,結果集會依PKTABLE_CAT、PKTABLE_SCHEM、PKTABLE_NAME和KEY_SEQ排序。 下表列出結果集中的資料行。
SQLForeignKeys會以標準結果集的形式傳回結果。 如果要求與主鍵相關聯的外鍵,結果集會依FKTABLE_CAT、FKTABLE_SCHEM、FKTABLE_NAME和KEY_SEQ排序。 如果要求與外鍵相關聯的主鍵,結果集會依PKTABLE_CAT、PKTABLE_SCHEM、PKTABLE_NAME和KEY_SEQ排序。 下表列出結果集中的資料行。
Example query See also Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics Analytics Platform System (PDW) Warehouse in Microsoft Fabric SQL database in Microsoft Fabric Contains a row for each column, or set of columns, that comprise a foreign key. ...
This following example relates parent and child tables through a single-column foreign key and shows how a foreign key constraint enforces referential integrity. Create the parent and child tables using the following SQL statements: CREATE TABLE parent ( id INT NOT NULL, PRIMARY KEY (id) ) ...
SQL QuizSQL FOREIGN KEY Constraint« Previous Next Chapter » SQL FOREIGN KEY ConstraintA FOREIGN KEY in one table points to a PRIMARY KEY in another table.Let's illustrate the foreign key with an example. Look at the following two tables:The "Persons" table:P...
For example, a foreign key prevents the insertion of values that do not have corresponding values in the referenced table. Additionally, a foreign key maintains consistency by automatically updating or deleting related rows in the child table when changes occur in the parent table. A table can ha...
EXAMPLE : Let’s say we want to create a table name Users. ThePRIMARY KEYwill be user_id for that table. SQL statement is: CREATE TABLEUSERS( user_idSMALLINTNOT NULLPRIMARY KEY, user_nameVARCHAR(50), ) OR CREATE TABLEUSERS( user_idSMALLINTNOT NULL , ...
SQL statement ordering to match foreign key constraints is disabled by default. If you use foreign keys in your schema, we recommend configuring your database to use deferred foreign keys whenever possible. Deferred constraints are not evaluated until the end of the transaction, giving you greater...
The following example returns foreign key information about the Department table in the AdventureWorks2022 database on the linked server, Seattle1.SQL Kopiera EXEC sp_foreignkeys @table_server = N'Seattle1', @pktab_name = N'Department', @pktab_catalog = N'AdventureWorks2022'; ...