Database Primary Key and Foreign key [from Internet]

Source: Internet
Author: User

Database Primary Key and Foreign key

--create referenced Table
CREATE TABLE Department
(
DeptID int PRIMARY key,--define PRIMARY key
Name varchar (not NULL),
Address varchar (+) NULL
)

--create referencing Table
CREATE TABLE Employee
(
EmpID int PRIMARY key,--define PRIMARY key
Name varchar (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 was a combination of columns in a table that uniquely identify a row of the table. Foreign key in a relational database was 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 this uniquely defines a row in a table of a relational database. A table can has at most one primary key. Primary key enforces the implicit not NULL constraint. So a column, which is defined as the primary key cannot has NULL values in it. Primary key can be a normal attribute in the table it's guaranteed to be unique such as a social security number or it Could is a unique value generated by the database management system such as a globally unique Identifier (GUID) in Microso FT SQL Server. Primary keys is defined through the Primary KEY constraint in ANSI SQL standard. Primary key can also is defined when creating the table. SQL allows primary key to being made up by one or more columns and each column that's included in the primary key is Implici Tly defined to is not NULL. But some database management systems require making the primary key columns explicitly is not NULL.

what is Foreign Key?

Foreign key is a referential constraint between and tables. It identifies a column or a set of columns in one table, called the referencing table, refers to a set of columns in a nother table, called the referenced table. The foreign key or the columns in the referencing table must is the primary key or a candidate key (a key that can is used As the primary key) in the referenced table. Foreign keys is used to link data across several tables. Therefore, the foreign key cannot contain values that does not appear in the table that it refers to. Then the reference provided by the foreign key can is used to link information in several tables and this would become ESS Ential with normalized databases. Multiple rows in the referencing table is refer to a and a single row in the referenced table. In ANSI SQL Standard, foreign keys is defined using the foreign KEY constraint. Further, foreign keys can be defined when creating the table itself. A table can has 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 so the primary key is a column or a set of columns that CA n be used to uniquely identify a row in a table while the foreign key was a column or a set of columns that refer to a prim ary Key or a candidate key of another table. Foreign key mainly provides a method to link information in several tables. Another difference is a table can has a single primary key, but it can has multiple foreign keys that can reference Different tables.

Database Primary Key and Foreign key [from Internet]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.