如何在SQL ORACLE中向REF和REF的SET中插入值

whhtz7ly  于 2023-04-11  发布在  Oracle
关注(0)|答案(1)|浏览(175)

我有问题插入值到表的属性是REF和设置的REF类型在SQL
我有tsportifs类型:

create type tsportifs as object (
idsportif integer, 
nom varchar2(50), 
prenom varchar2(50),
sexe varchar2(50),
age integer
);
/

和tsport类型:

create type tsport as object (idsport integer, libelle VARCHAR2(50));
/

以及我所拥有的联想:

alter type tsportifs add attribute sportifs_sport t_set_ref_sport cascade;
alter type tsportifs add attribute sportifs_coneiller ref tsportifs cascade;

对于表:

create table sportifs of tsportifs (
    primary key(idsportif),
    CONSTRAINT chk_sexe CHECK (sexe IN ('F', 'M'))
    )nested table sportifs_sport store as table_sportifs_sport;
    
create table sport of tsport (primary key(idsport))nested table sport_sportifs store as table_sport_sportifs;

当我试图插入:

INSERT INTO Sportifs VALUES(1,'BOUTAHAR','Abderahim','M',30,REF(tsportifs(NULL)),REF(tsport(2)));

我得到这个错误:

SQL> INSERT INTO Sportifs VALUES(1,'BOUTAHAR','Abderahim','M',30,REF(tsportifs(NULL)),REF(tsport(2)));
INSERT INTO Sportifs VALUES(1,'BOUTAHAR','Abderahim','M',30,REF(tsportifs(NULL)),REF(tsport(2)))
                                                                         *
ERROR at line 1:
ORA-00907: missing right parenthesis

我不知道如何在嵌套表中插入多个值。

s2j5cfk0

s2j5cfk01#

您的DDL语句没有意义,因为:

  • 你从来没有定义过t_set_ref_sport;和
  • sport表是用嵌套表定义的,但该类型不包含任何集合。

此外,INSERT语句没有意义(忽略它在语法上是无效的),因为您没有将任何行INSERT写入sport以供其引用。
如果您有以下类型:

CREATE TYPE tsport AS OBJECT(
  idsport integer,
  libelle VARCHAR2(50)
);

CREATE TYPE t_set_ref_sport IS TABLE OF REF TSPORT;

CREATE TYPE tsportifs AS OBJECT (
  idsportif          integer, 
  nom                varchar2(50), 
  prenom             varchar2(50),
  sexe               varchar2(50),
  age                integer,
  sportifs_coneiller REF tsportifs,
  sportifs_sport     t_set_ref_sport
);

然后是表:

create table sport of tsport (
  primary key(idsport)
);

create table sportifs of tsportifs (
  primary key(idsportif),
  CONSTRAINT chk_sexe CHECK (sexe IN ('F', 'M'))
)
nested table sportifs_sport store as table_sportifs_sport;

然后,您可以向引用添加范围约束:

ALTER TABLE sportifs ADD SCOPE FOR ( sportifs_coneiller ) IS sportifs;
ALTER TABLE table_sportifs_sport ADD SCOPE FOR ( COLUMN_VALUE ) IS sport;

然后插入数据:

INSERT INTO sport (idsport, libelle) VALUES (2, 'Test');

INSERT INTO Sportifs (
  idsportif,
  nom,
  prenom,
  sexe,
  age,
  sportifs_coneiller,
  sportifs_sport
) VALUES (
  1,
  'BOUTAHAR',
  'Abderahim',
  'M',
  30,
  NULL,
  t_set_ref_sport(
    (SELECT REF(s) FROM sport s WHERE idsport = 2)
  )
);

fiddle
如果你想要一个NULL引用,那么只需要插入NULL
如果要插入对行的引用,请用途:

( SELECT REF(table_alias)
  FROM table_name table_alias
  WHERE primary_key_column = something )

如果要将引用集合插入到嵌套表中,请用途:

collection_data_type(
  (SELECT REF(table_alias) FROM table_name table_alias WHERE primary_key_column = first_thing),
  (SELECT REF(table_alias) FROM table_name table_alias WHERE primary_key_column = third_thing),
  NULL,
  (SELECT REF(table_alias) FROM table_name table_alias WHERE primary_key_column = fourth_thing)
)

相关问题