Lets assume I have the following table
create table Article
(
ART_ID bigint not null auto_increment,
ART_NAME varchar(25) not null,
ART_COST decimal(8,2) not null,
ART_DESC text,
primary key(ART_ID)
);
and also this table
create table Fruits
(
FRUITS_ID bigint not null auto_increment,
FRUITS_TYPE varchar(15),
FRUITS_FROZEN timestamp DEFAULT CURRENT_TIMESTAMP,
primary key(FRUITS_ID )
);
I want the Fruits table to have all the fields of the table Article. How can I implement this? Should I reference it by some relation or is there another trick?
I think what you need here is to add an
ARTICLE_IDfield toFruits, and then reference it toArticles.ART_IDvia a FOREIGN KEY constraint to ensure data consistency.Note that this means that for each fruit, to have some “article” fields set, a corresponding record in
Articlesmust exist. If you don’t want article fields to be mandatory for a fruit, you can make theARTICLE_IDfield nullable. This still allows a foreign key to be defined, but it will only enforce a reference if the value stored inARTICLE_IDis notNULL.