I've got a hierarchy pretty similar to this one in an Oracle Database
CREATE TYPE PersonUdt AS OBJECT(
personId number(10),
Name varchar(20)
)INSTANTIABLE NOT FINAL;
CREATE OR REPLACE TYPE PatientUdt under PersonUdt(
insurancePlan varchar(100)
);
CREATE OR REPLACE TYPE DoctorUdt under PersonUdt(
area varchar(20)
);
CREATE TABLE Person of PersonUdt(
personId PRIMARY KEY
)OBJECT IDENTIFIER IS SYSTEM GENERATED;
I've got some insertions on the table,like
INSERT INTO person VALUES(DoctorUdt(1, 'Alfred', 'Area One'));
I would like to create a constraint to limit the area in which the a doctor can work. Let's say, limit it to Area one, two and three.
Is it possible to do this using ALTER TABLE x ADD CONSTRAINT... functions?
Thanks in advance!
Related
I have created an object type and a table. I would like to know how select, insert, update and delete operation on it.
create table employee_info (
empid number,
emp_name varchar2(50),
department varchar2(20),
designation varchar2(50),
salary number
);
create type employee_info_obj is object (
empid number,
department varchar2(50),
designation varchar2(50),
salary number
);
create type employee_info_obj_t is
table of employee_info_obj ;
You have only created an object type and an unrelated database table. If you want a database table based on the type, you need to create one:
create table employee_info of employee_info_obj;
While it can be nice in certain programming scenarios to have a type synced to a table, there are some downsides such as it being harder to add columns later, and third party tool support since the object table will not be listed in user_tables but only in user_object_tables and user_all_tables, so I would question the usefulness of this approach.
dbFiddle
I am getting an error ORA-00909 in when inserting an object of a superType in an object table. These are the definitions for the objects:
CREATE OR REPLACE TYPE address AS OBJECT (
street VARCHAR(20),
country VARCHAR(20),
province VARCHAR(20),
city VARCHAR2(20),
zipcode VARCHAR(10)
) FINAL;
CREATE OR REPLACE TYPE company AS OBJECT (
CIF VARCHAR2(9),
code VARCHAR2(10),
name VARCHAR2(20),
signUpDate DATE,
email VARCHAR2(20),
adminAddress address
) NOT FINAL;
CREATE OR REPLACE TYPE inCourseCompany UNDER company (
postalAddress address,
numEmployees NUMBER
) FINAL;
And the object table:
CREATE TABLE companies_objtab OF company (PRIMARY KEY CIF) OBJECT IDENTIFIER IS PRIMARY KEY;
I try to insert an object with the following statement:
INSERT INTO companies_objtab VALUES (
company('J12345678','000001','Test Company',TO_DATE(sysdate, 'dd/mm/yyyy hh24:mi:ss'),'',address('','','','',''))
);
and I get error Error
SQL: ORA-00909: invalid number of arguments
00909. 00000 - "invalid number of arguments"
However, when I insert an object of the subtype inCourseCompany it is inserted correctly:
INSERT INTO companies_objtab VALUES (
inCourseCompany('G11111111','','',TO_DATE(sysdate, 'dd/mm/yyyy hh24:mi:ss'),'',address('','','','',''), address('','','','',''), 100)
);
Any hint about what causes the error?
I am using Oracle SQL Developer 4.0.2.15.21 and Oracle Database Express Edition 11g Release 2.
Thank you in advance.
i tried all your statements right as you posted them (copy-paste). Both on 12c and 11gR2. Everything worked, both inserts. The only thing I noticed is that your create table is a bit incorrect. This is the correct one (but this does not explain the error you get). HTH KR
CREATE TABLE companies_objtab OF company (CIF PRIMARY KEY) OBJECT IDENTIFIER IS PRIMARY KEY;
Maybe try dropping the table and creating it again.
Consider the following relational schema.
Words in bold color are primary keys.
branches(**bname**:char(15),city:varchar(12), phone:varchar(10))
customers(**custno**:integer,cname:char(15),gender:char(1), bdate:date)
accounts(**accno**:integer, branch:char(15),acctype:char(1),balance:real)
ac(**accont:integer,customer**:integr)
columns in bold color are primary keys.
in ac, account and customer references account_ty and customer_ty respectively.
My problem is in ac table (account,customer) is a composite primary key. Since they are ref types, how can i add primary key in ac table.
I want to create object tables. First I created types and then tables from them.Below are my code
create type branch_ty as object(
bname char(15),
city varchar(12),
phone varchar(12)
)
/
create type customer_ty as object(
custno integer,
cname char(15),
gender char(1),
bdate date
)
/
create type account_ty as object(
accno integer,
branch REF branch_ty,
acctype char(1),
balance real
)
/
create type ac_ty as object(
account REF account_ty,
customer REF customer_ty
)
/
create table branch of branch_ty(
constraint br_pk primary key(bname)
)
/
create table customers of customer_ty(
constraint cu_pk primary key(custno),
constraint cu_ch check (gender='F' or gender='M')
)
/
create table accounts of account_ty(
constraint acc_pk primary key(accno),
constraint acc_fk foreign key(branch) references branch,
constraint acc_ch check(acctype='I' or acctype='J')
)
/
I am OK up to this point. But when I try to execute the following code segment it gives this error message.ORA-02329: column of datatype REF cannot be unique or a primary key
create table ac of ac_ty(
constraint ac_fk1 foreign key(account) references accounts,
constraint ac_fk2 foreign key(customer) references customers,
primary key(account,customer)
)
/
Please help me. I know i can do this without using object tables.But I want to create it using object tables. Is this possible? Please help me........
I have created two objects T_PERSONS,T_BUSINESS_PERSON where T_BUSINESS PERSON is a subtype and T_PERSONS is a supertype.
---Creating T_PERSONS OBJECT---
CREATE OR REPLACE
TYPE T_PERSONS AS OBJECT
( id integer,
first_name varchar2(10),
last_name varchar2(10),
dob DATE,
phone varchar2(12),
address t_address,
) NOT FINAL;
---Creating T_BUSINESS_PERSON OBJECT---
CREATE TYPE t_business_person UNDER t_persons
(title varchar2(20),
company varchar2(20)
);
Now I created an object table object_customers
CREATE TABLE object_customers OF t_persons
INSERTING DATA INTO object_customers
INSERT INTO object_customers VALUES
(t_persons(1,'Jason','Bond','03-APR-1955','800-555-1211',
t_address('21 New Street','Anytown','CA','12345')
));
in this case,data has been inserted properly
INSERT INTO object_customers VALUES
(t_business_person(2,'Steve','Edwards','03-MAR-1955','800-555-1212',
t_address('1 Market Street','Anytown','VA','12345'),'Manager','XYZ Corp'
));
Now in this case an error has occured
Error-attribute or element value is larger than specified in type.
Please Help.
The second insertion will work only if you create the table as follows:
CREATE TABLE object_customers OF t_business_person;
Click here to read more about NOTFINAL clause.
I got the following sql:
create or replace type MEDIUM_TYPE AS OBJECT
(
me_movie REF MOVIE_TYPE,
me_rating varchar2(2),
me_runtime number(3,0),
me_release_year number(4,0),
me_list_price number(3,2),
me_our_price number(3,2),
me_availability varchar2(128),
me_aspect_ratio varchar2(8),
me_encoding number (1,0),
me_subtitle_language SUBTITLE_LANGUAGE_TYPE,
me_number_of_discs number (1,0)
)not final
/
create table DVD of MEDIUM_TYPE
object id system generated
/
How can i make sure that me_movie in the dvd table is unique?
And also, how can i do something like this?
mo_release_year number(4,0) BETWEEN 1900 AND 2100,
It's pretty much the same syntax as for relational tables:
create table DVD of MEDIUM_TYPE
( me_movie primary key )
object id system generated
/
The one problem you have is that you will run into this error:
ORA-02329: column of datatype REF cannot be unique or a primary key
Which admittedly is a bit of a showstopper. You will need to re-think your whole model. Sorry about that.