zoukankan      html  css  js  c++  java
  • Database Primary key and Foreign key [From Internet]

    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.

     

  • 相关阅读:
    基于深度学习的单目图像深度估计
    OpenCV探索之路(二十三):特征检测和特征匹配方法汇总
    TensorFlow练习24: GANs-生成对抗网络 (生成明星脸)
    深度估计&平面检测小结
    论文翻译——Rapid 2D-to-3D conversion——快速2D到3D转换
    Opencv改变图像亮度和对比度以及优化
    如何将OpenCV中的Mat类绑定为OpenGL中的纹理
    Eclipse控制台中文乱码
    给java中的System.getProperty添加新的key value对
    中文简体windows CMD显示中文乱码解决方案
  • 原文地址:https://www.cnblogs.com/pugang/p/4815198.html
Copyright © 2011-2022 走看看