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.

     

  • 相关阅读:
    celery的使用和原理
    内核通知链
    数据流中的中位数
    二叉搜索树的后序遍历序列
    Javascript设计模式系统讲解与应用,JS设计模式详解
    微服务系列之ZooKeeper注册中心和Nacos注册中心
    微信小程序开发详解:小程序入门与实战-纯正商业级应用技术
    Java零基础该怎么去学习Java?学好Java应该如何去做?
    Flutter从入门到进阶实战携程网App项目详解
    Python升级3.6强力Django+杀手级Xadmin打造在线教育平台
  • 原文地址:https://www.cnblogs.com/pugang/p/4815198.html
Copyright © 2011-2022 走看看