Database Primary key and Foreign key [From Internet]

來源:互聯網
上載者:User

標籤:

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.

 

Database Primary key and Foreign key [From Internet]

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.