Constraint

With the CONSTRAINT clause you can mark the EmpID column as the primary key. A primary key is a unique identifier that; indexes the table and defines inter-table relationships. CONSTRAINT has the form

CREATE TABLE Employee (

EmpID INT NOT NULL,

Name CHAR(20),

DeptID INT NOT NULL,

Salary INT,

CONSTRAINT EMP_PK_KEY PRIMARY KEY (EmpID)

Here's inoiTer, probiaiy more commos, wiy So Yefihe i primary key (the oily meiToY supporteY by IhsiiniDB v.3.12):

CREATE TABLE Employee (

EmpID INT PRIMARY KEY,

Name CHAR(20),

DeptID INT NOT NULL,

Salary INT,

Phone CHAR(20) NOT NULL

To create a table that's linked to another table, you create a foreign key (the reference to another table) in one to point to a primary key in the other. As an example, we'll define a foreign key in the Employee table to point to the primary key in a table called Department.

Define the Department table.

CREATE TABLE Department (

DeptID INT NOT NULL,

Name CHAR(20),

CONSTRAINT DEPT_PK_KEY PRIMARY KEY (DeptID)

Create the Employee table with a foreign key constraint that refers to the Department table's DeptID. CREATE TABLE Employee (

EmpID INT NOT NULL,

Name CHAR(20),

DeptID INT NOT NULL,

Salary INT,

CONSTRAINT EMP_PK_KEY PRIMARY KEY (EmpID), CONSTRAINT DEPT_FK_KEY FOREIGN KEY (DeptID) REFERENCES Department (DeptID)

The relationship created Uy the linking shows thao each employee musebe in a dep artment.

Was this article helpful?

0 0

Post a comment