- SQL - Discussion
- SQL - Useful Resources
- SQL - Useful Functions
- SQL - Quick Guide
- SQL - Questions and Answers
- SQL - Datatype Functions
- SQL - Conversion Functions
- SQL - JSON Functions
- SQL - Cursor Functions
- SQL - Logical Functions
- SQL - Statistical Functions
- SQL - Text & Image Functions
- SQL - Numeric Functions
- SQL - Aggregate Functions
- SQL - String Functions
- SQL - Date Functions
- SQL - Database Tuning
- SQL - IN vs EXISTS
- SQL - Group By vs Order By
- SQL - Common Table Expression
- SQL - Cursors
- SQL - Date & Time
- SQL - Auto Increment
- SQL - Using Sequences
- SQL - Handling Duplicates
- SQL - Sub Queries
- SQL - Transactions
- SQL - NULL Values
- SQL - Stored Procedures
- SQL - Default Constraint
- SQL - Check Constraint
- SQL - Null Functions
- SQL - Min & Max
- SQL - Hosting
- SQL - Injection
- SQL - Comments
- SQL - Wildcards
- SQL - Non-Clustered Index
- SQL - Clustered Index
- SQL - Unique Index
- SQL - Primary Key
- - 工会诉Join
- SQL - Inner Join
- SQL - Using Joins
- SQL - Aliases
- SQL - EXCEPT Operator
- SQL - INTERSECT Operator
- SQL - UNION vs UNION ALL
- SQL - UNION Operator
- SQL - BETWEEN Operator
- SQL - NOT NULL
- SQL - IS NOT NULL
- SQL - IS NULL
- SQL - NOT EQUAL
- SQL - NOT Operator
- SQL - CASE
- SQL - EXISTS Operator
- SQL - ANY, ALL Operators
- SQL - IN Operator
- SQL - LIKE Operator
- SQL - BOOLEAN (BIT) Operator
- SQL - AND & OR
- SQL - Having Clause
- SQL - Group By Clause
- SQL - Order By Clause
- SQL - Distinct Clause
- SQL - Top Clause
- SQL - Where Clause
- SQL - Rename Views
- SQL - Drop Views
- SQL - Update Views
- SQL - Create Views
- SQL - Sorting Results
- SQL - Delete Query
- SQL - Update Query
- SQL - Insert Into Select
- SQL - Select Into
- SQL - Select Query
- SQL - Insert Query
- SQL - Constraints
- SQL - Delete Table
- SQL - Drop Table
- SQL - Alter Tables
- SQL - Temporary Tables
- SQL - Clone Tables
- SQL - Truncate Table
- SQL - Rename Table
- SQL - Show Tables
- SQL - Create Table
- SQL - Backup Database
- SQL - Show Database
- SQL - Rename Database
- SQL - Select Database
- SQL - Drop Database
- SQL - Create Database
- SQL - Expressions
- SQL - Operators
- SQL - Data Types
- SQL - Syntax
- SQL - Databases
- SQL - RDBMS Concepts
- SQL - Overview
- SQL - Home
5. 图瓦卢
- 页: 1
- 页: 1
- 结构-创建指数
- 页: 1
- 页: 1
- 页: 1
- SQL - Foreign Key
- 文 件
- ∗ E/CN.6/2009/1。
- 页: 1
- 页: 1
- 文 件
- 页: 1
- 页: 1
- 文 件
- 页: 1
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SQL - Foreign Key
表中“Foreign Key Foreign Key是一栏(或一栏的组合),其数值与另一个表格中一个主要栏的数值相符。 利用外国钥匙,我们可以把两个表格联系起来。
www.un.org/Depts/DGACM/index_french.htm
In addition to pnking to tables, The Foreign key constraint ensures referential integrity by preventing changes to data in the primary key table from invapdating the pnk to data in the foreign key table. i.e, a Foreign key prevents operations, pke “dropping the table”, that would epminate the connection between two tables.
A KEY is an attribute that allows us to uniquely identify a row in a table. In addition to avoiding redundant records, SQL keys are used to estabpsh a relationship between multiple tables.
让我们考虑一个更好的了解的范例,设想我们有两个表格:STD_ADD & STD_MARKS。 ——
STU_ADD , 包括栏目/attributes RO_no、名称、年龄、地址和Pin。
STU_MARKS。 页: 1
然后,Roll_no,STU_ADD/b>。 表格为主要数据,而Roll_no栏为Foreign key。 下表列出了上述表格中外国钥匙和主要钥匙的图表:
以下是Foreign Key-
A Foreign Key is used to reduce the redundancy (or duppcates) in the table.
它有助于宣布 (或在一个数据库中组织数据)数据载于多个表格。
具有主要钥匙的表格称为母体表,而与外国钥匙有关的钥匙称为儿童表。
Primary key vs Foreign Key
尽管主要钥匙和外国钥匙都指同一栏,但以工作方式观察到许多差异。 清单如下。
Primary key | Foreign Key |
---|---|
The primary key is always unique. | The foreign key can be duppcated. |
The primary key can not be NULL. | The Foreign can be NULL. |
A table can contain only one Primary Key. | We can have more than one Foreign Key per table. |
Syntax
下面是表格一栏中附加的外国关键限制的星号:
CREATE TABLE TABLE_2(COLUMN_NAME FOREIGN KEY REFERENCES TABLE_1(COLUMN_NAME));
Example
让我们制作两个表格,名称分别为CUSTOMERS和ORDERS。 The following query estabpsh a table with the name CUSTOMERS -
CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Output
下面是上述表格的产出:
(0 rows affected)
现在,让我们创建ORDERS。 表格 在这样做的同时,我们在CUSTOMER_ID查询ID栏上添加了外国的关键限制。 下表:
CREATE TABLE ORDERS ( ID INT NOT NULL, DATE DATETIME, CUSTOMER_ID INT FOREIGN KEY REFERENCES CUSTOMERS(ID), AMOUNT DECIMAL, PRIMARY KEY (ID) );
Output
上述说明产生了以下产出:
(0 rows affected)
Verification
我们创建了Foreign Key。 对CUSTOMER_ID的栏目的限制 表格中注明ID
首先,请不要忘记CUSTOMERS。 页: 1 执行以下声明的表格:
DROP TABLE CUSTOMERS;
如果你核实以下错误信息,你将指出,该表不能删除,因为其参考是 FOR E IGN KEY。 制约因素
Could not drop object CUSTOMERS because it is referenced by a FOREIGN KEY constraint.
Foreign key constraint on an existing column
我们还可以在现有表格一栏中设定外国关键限制。 如果你忘记在建立一个表格时对一栏添加一个外国关键制约因素,或者如果你想要在另一个栏中添加这一限制,即使表格中有一个外国关键栏。
Syntax
Using the ALTER TABLE statement we can add a foreign key constraint on an existing column in a table as shown below −
ALTER TABLE TABLE1 ADD COLUMN_NAME INT FOREIGN KEY REFERENCES TABLE1(COLUMN_NAME); OR ALTER TABLE TABLE2 ADD CONSTRAINT FK_ORDERS FOREIGN KEY(COLUMN_NAME) REFERENCES TABLE1(COLUMN_NAME);
Example
随着CUSTOMERS和ORDERS表已在数据库中设立,我们将在ORDERS表中的任何栏目上添加一个外在关键词。
Following is the SQL query to add the foreign key constraint on an existing column if the table has already been created −
ALTER TABLE ORDERS ADD CONSTRAINT FK_ORDERS FOREIGN KEY(ID) REFERENCES CUSTOMERS(ID);
Output
以上方案的产出如下:
(0 rows affected)
Verification
我们创建了Foreign Key。 对CUSTOMER_ID的栏目的限制 表格中注明ID
首先,请不要忘记CUSTOMERS。 页: 1 执行以下声明的表格:
DROP TABLE CUSTOMERS;
如果你核实以下错误信息,你将指出,该表不能删除,因为其参考是 FOR E IGN KEY。 制约因素
Could not drop object CUSTOMERS because it is referenced by a FOREIGN KEY constraint.
Dropping a FOREIGN KEY
由于我们在“CUSTOMER_ID
Syntax
利用ALTER TABLE声明,我们可以从表格一栏中删除“签字”关键限制。
ALTER TABLE TABLE_NAME DROP FK_NAME;
在FK_NAME是你必须放弃的外国关键制约因素的名称。
Example
下表一栏列出外国关键制约因素:
ALTER TABLE ORDERS DROP FK_ORDERS;
Output
Following is the output of the above SQL query −
(0 rows affected)
Verification
自此以后,我们从ORDERS上排除了外国的主要制约因素。 现在,你可以直截了CUSTOMERS的桌子,而不必放弃文件目录。
Following is the SQL query to drop the CUSTOMERS table −
DROP TABLE CUSTOMERS;
如果你核实上述卡勒克指挥所投掷的以下身份代码,你就会发现,科罗塞斯电台的表格已经消失。
(0 rows affected)Advertisements