简体   繁体   English

是否可以在继承的ORACLE DBMS的表中插入一行?

[英]Is it possible to insert a row in a table in ORACLE DBMS which has been inherited?

I just had a quick question, The below three tables have inheritance using UNDER,我只是有一个简单的问题,下面三个表有 inheritance 使用 UNDER,

CREATE TYPE MenuType AS OBJECT(
 MenuCode VARCHAR2(5),
 Description VARCHAR2(20)) NOT FINAL;
/

CREATE TABLE Menu of MenuType (
MenuCode PRIMARY KEY);

CREATE TYPE VegetarianType UNDER MenuType (
 ProteinLevel VARCHAR2(20));
/

CREATE TABLE Vegetarian of VegetarianType;

CREATE TYPE LowFatType UNDER MenuType (
 KCal VARCHAR2(20));
/

CREATE TABLE LowFat of LowFatType;

Is it possible to insert a row in the Menu table which represents a Vegetarian diet?是否可以在菜单表中插入代表素食的行? or would it be just inserting a row in the Vegetarian Table?还是只是在素食表中插入一行?

Thanks in advance,提前致谢,

Ahsan阿山

You do not need separate tables for the sub-types and can just insert them into the MENU table and then use the TREAT function to convert to the sub-type as needed:子类型不需要单独的表,只需将它们插入到MENU表中,然后根据需要使用TREAT function 转换为子类型:

CREATE TYPE MenuType AS OBJECT(
 MenuCode VARCHAR2(5),
 Description VARCHAR2(20)
) NOT FINAL;
/

CREATE TYPE VegetarianType UNDER MenuType (
 ProteinLevel VARCHAR2(20)
);
/

CREATE TYPE LowFatType UNDER MenuType (
 KCal VARCHAR2(20)
);
/

CREATE TABLE Menu of MenuType (
  MenuCode PRIMARY KEY
);

Then you can INSERT :然后你可以INSERT

INSERT INTO Menu
VALUES ( VegetarianType( 'VEGE1', 'Vegetarian Meal 1', 'Lots of Protein' ) );

INSERT INTO Menu VALUES ( LowFatType( 'LFAT1', 'Low Fat Meal 1', '500kCal' ) );

Then you can use TREAT to get the values from the sub-type's fields:然后您可以使用TREAT从子类型的字段中获取值:

SELECT m.*,
       TREAT( VALUE(m) AS VegetarianType ).ProteinLevel AS ProteinLevel,
       TREAT( VALUE(m) AS LowFatType ).KCal AS KCal
FROM   Menu m;

Which outputs:哪个输出:

 MENUCODE |菜单代码 | DESCRIPTION |说明 | PROTEINLEVEL |蛋白质水平 | KCAL:------- |:---------------- |:-------------- |:------ VEGE1 |千卡:------- |:---------------- |:------------- |:----- - VEGE1 | Vegetarian Meal 1 |素食餐1 | Lots of Protein |大量蛋白质 | null LFAT1 | null LFAT1 | Low Fat Meal 1 |低脂餐1 | null | null | 500kCal 500千卡

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM