将主键映射为数据库中的外键

时间:2022-10-17 22:47:13

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.

我的数据库设计存在逻辑问题。我有一个包含2个字段的表,其中一个整数字段是主键,它在所有其他表中充当外键。

The table structure is

表结构是

  1. A table with an ID as primary key
  2. 一个ID为主键的表
  3. ID may have the basic data types as values
  4. ID可以具有基本数据类型作为值
  5. Based on these data types the tables are mapped to the main table having ID as primary key
  6. 基于这些数据类型,表被映射到具有ID作为主键的主表

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

如何在数据库创建中映射它?如何根据此要求设计表格。

1 个解决方案

#1


2  

standard SQL can handle such a mapping simply:

标准SQL可以简单地处理这样的映射:

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
 );

#1


2  

standard SQL can handle such a mapping simply:

标准SQL可以简单地处理这样的映射:

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
 );