Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping a primary key as foreign key in database

I have a logical problem in my database design. I have got a table with 2 field, one of the integer field is the primary key and it acts as foreign key in all other tables.

The table structure is

  1. A table with an ID as primary key
  2. ID may have the basic data types as values
  3. Based on these data types the tables are mapped to the main table having ID as primary key

How can I map this in the database creation? How can i design the table with this requirement.

like image 323
Gapchoos Avatar asked Nov 04 '22 03:11

Gapchoos


1 Answers

standard SQL can handle such a mapping simply:

CREATE TABLE employee (
    first_name varchar,
    last_name varchar,
    date_started date,
    id int primary key
);
create table salary (
    employee_id int primary key references employee(id),
    yearly_amount numeric
);
CREATE TABLE wage (
    employee_id int primary key references employee(id),
    hourly_amount numeric
 );
like image 113
Chris Travers Avatar answered Nov 15 '22 07:11

Chris Travers