Re: SQL Database design question

From: David Portas <REMOVE_BEFORE_REPLYING_dportas_at_acm.org>
Date: 28 Jan 2005 02:22:32 -0800
Message-ID: <1106907752.312855.40980_at_c13g2000cwb.googlegroups.com>


You prevent duplication in the EmployeeBranches table as in any other table: with a PK or UNIQUE constraint. However, your constraints have to allow an employee to be added without a corresponding entry in EmployeeBranches, otherwise you could never add new rows to the Employees table.

CREATE TABLE Employees (ee_code CHAR(10) PRIMARY KEY, ee_name VARCHAR(50) NOT NULL) CREATE TABLE Branches (branch_id INTEGER PRIMARY KEY, branch_name VARCHAR(50) NOT NULL UNIQUE) CREATE TABLE EmployeeBranches (ee_code CHAR(10) NOT NULL REFERENCES Employees (ee_code), branch_id INTEGER NOT NULL REFERENCES Branches (branch_id), PRIMARY KEY (ee_code, branch_id))

-- 
David Portas
SQL Server MVP
--
Received on Fri Jan 28 2005 - 11:22:32 CET

Original text of this message