Advertisement

Latest Post

Saturday, 24 September 2016

What is FOREIGN KEY

FOREIGN KEY

FOREIGN  is column in a table that is the primary key column in another table. a table  can have  multiple foreign column in a table . It can hold multiple null values.

Example: Below are the EMPLOYEE and EMPSALARY table.

EMPLOYEE table:

CREATE TABLE EMPLOYEE(
       ID   INT              NOT NULL,
       NAME VARCHAR (20)     NOT NULL,
       AGE  INT              NOT NULL,
       ADDRESS  CHAR (25) ,
       SALARY   DECIMAL (18, 2),      
      CONSTRAINT pk_EmployeeID PRIMARY KEY (ID)
);
EMPSALARY table:

CREATE TABLE EMPSALARY (
       ID          INT       NOT NULL,
       DATE        DATETIME,
       EMPLOYEE_ID INT references EMPLOYEE(ID),
       AMOUNT     double,
       CONSTRAINT pk_ EMPSALARYID PRIMARY KEY (ID)
);


in above  EMPSALARY table EMPLOYEE_ID is the foreign key that refrenece to the primary key in the EMPLOYEE table.

More SQL Constraints


Government Jobs