Database Primary key and Foreign key

--Create Referenced Table
CREATE TABLE Department
(
DeptID int PRIMARY KEY, --define primary key
Name varchar (50) NOT NULL,
Address varchar(100) NULL
)

--Create Referencing Table
CREATE TABLE Employee
(
EmpID int PRIMARY KEY, --define primary key
Name varchar (50) NOT NULL,
Salary int NULL,
--define foreign key
DeptID int FOREIGN KEY REFERENCES Department(DeptID)
)

Foreign key vs Primary key
A column or a set of columns, which can be used to identify or access a row or a set of rows in a database is called a key. A primary key in a relational database is a combination of columns in a table that uniquely identify a row of the table. Foreign key in a relational database is a field in a table that matches the primary key of another table. The foreign key is used to cross reference tables.

What is primary Key?

Primary key is a column or a combination of columns that uniquely defines a row in a table of a relational database. A table can have at most one primary key. Primary key enforces the implicit NOT NULL constraint. So a column that is defined as the primary key cannot have NULL values in it. Primary key can be a normal attribute in the table that is guaranteed to be unique such as a social security number or it could be a unique value generated by the database management system such as a Globally Unique Identifier (GUID) in Microsoft SQL Server. Primary keys are defined through the PRIMARY KEY constraint in ANSI SQL Standard. Primary key can also be defined when creating the table. SQL allows primary key to be made up of one or more columns and each column that is included in the primary key is implicitly defined to be NOT NULL. But some database management systems require making the primary key columns explicitly NOT NULL.

What is Foreign Key?

Foreign key is a referential constraint between two tables. It identifies a column or a set of columns in one table, called the referencing table that refers to a set of columns in another table, called the referenced table. The foreign key or the columns in the referencing table must be the primary key or a candidate key (a key that can be used as the primary key) in the referenced table. Foreign keys are used to link data across several tables. Therefore, the foreign key cannot contain values that do not appear in the table that it refers to. Then the reference provided by the foreign key can be used to link information in several tables and this would become essential with normalized databases. Multiple rows in the referencing table may refer to a single row in the referenced table. In ANSI SQL standard, foreign keys are defined using the FOREIGN KEY constraint. Further, foreign keys can be defined when creating the table itself. A table can have multiple foreign keys and they can reference different tables.

What is the difference between Foreign key and Primary key?

The main diference between primary key and the foreign key is that the primary key is a column or a set of columns that can be used to uniquely identify a row in a table while the foreign key is a column or a set of columns that refer to a primary key or a candidate key of another table. Foreign key mainly provides a method to link information in several tables. Another difference is that a table can have a single primary key, but it can have multiple foreign keys that can reference different tables.

最新文章

  1. jQuery hover事件
  2. WebApi系列~QQ互联的引入(QConnectSDK)
  3. Centos 6.4 python 2.6 升级到 2.7
  4. C# 窗体间传值方法大汇总
  5. 调bug时候应该提高思维深度(多问二十个为什么)
  6. 【php】记录一次生产环境bug的调试
  7. MQ在高并发环境下,如果队列满了,如何防止消息丢失?
  8. ROS中使用Kinect摄像头和usb摄像头
  9. dos命令大全 黑客必知的DOS命令集合
  10. 来自于一个问题的回答对自己的反思 php怎么发送邮件?发送邮件插件PHPMailer
  11. python错误、调试、测试
  12. Huffman Implementation with Python
  13. 17秋 SDN课程 第一次作业
  14. swift - 3D 视图,截图,关键字搜索
  15. s4-8 虚拟局域网
  16. Swift 里 Set (三)Inspecting a Set
  17. Gnome排序
  18. Date、String、Calendar相互转化
  19. Mac下配置Hadoop环境
  20. 洛谷P3178 [HAOI2015]树上操作(dfs序+线段树)

热门文章

  1. testMarkDown
  2. 【leetcode】Unique Paths
  3. HTML5之Canvas时钟(网页效果--每日一更)
  4. Task:取消异步计算限制操作 & 捕获任务中的异常
  5. .net开发笔记(十八) winform中的等待框
  6. 扩展GridView控件——为内容项添加拖放及分组功能
  7. Visual Studio 2015速递(3)——ASP.NET 新特性
  8. 我心中的核心组件(可插拔的AOP)~第二回 缓存拦截器
  9. PHP数据库操作:从MySQL原生API到PDO
  10. Node.js入门:文件查找机制