Can I change an attribute name from a table derived from a type? - sql

Folowing the Object-Relational Database model, I wanted to create the tables or_doctor and or_recepcionist derived from the type t_employee.
Here, follows the type structure:
DROP TYPE t_employee FORCE;
CREATE OR REPLACE TYPE t_employee AS OBJECT (
num_employee INTEGER,
name_employee VARCHAR2(50),
birthdate_employee DATE
);
And here, the tables' structure:
DROP TABLE or_doctor CASCADE CONSTRAINTS;
CREATE TABLE or_doctor OF t_employee (
PRIMARY KEY (num_employee),
name_employee NOT NULL,
birthdate_employee NOT NULL
) OBJECT IDENTIFIER IS SYSTEM GENERATED;
DROP TABLE or_recepcionist CASCADE CONSTRAINTS;
CREATE TABLE or_recepcionist OF t_employee (
PRIMARY KEY (num_employee),
name_employee NOT NULL,
birthdate_employee NOT NULL
) OBJECT IDENTIFIER IS SYSTEM GENERATED;
Doing so, the attributes names, on both tables, will end up with "employee". Could I change the attribute name so they are specific in each table at the moment I'm creating the table?
E.G.:
Table or_doctor: num_doct, name_doct, birthdate_doct.
Table or_recepcionist: num_recep, name_recep, birthdate_recep.

As a frame challenge, don't add a suffix to your identifiers then you don't need to worry about the suffix being incorrect:
CREATE TYPE t_employee AS OBJECT (
num INTEGER,
name VARCHAR2(50),
birthdate DATE
);
CREATE TABLE or_doctor OF t_employee (
PRIMARY KEY (num),
name NOT NULL,
birthdate NOT NULL
) OBJECT IDENTIFIER IS SYSTEM GENERATED;
CREATE TABLE or_receptionist OF t_employee (
PRIMARY KEY (num),
name NOT NULL,
birthdate NOT NULL
) OBJECT IDENTIFIER IS SYSTEM GENERATED;
If you try to rename the column:
ALTER TABLE or_doctor RENAME COLUMN name TO name_doctor;
Then you will get the error:
ORA-23291: Only base table columns may be renamed
If you are using object-derived tables then you appear to be stuck with the identifiers from the object; so, make the object names generic so that they are appropriate in every place they are going to be used.

Related

Getting an error of invalid identifier when try to create relationship between two tables in Oracle SQL developer? [duplicate]

This question already has an answer here:
A CREATE statement with quoted fields in Oracle
(1 answer)
Closed 1 year ago.
This is my device table
CREATE TABLE "DEVICE" (
"IMEI_Number" varchar(15),
"Device_Model" varchar(30),
"Device_Description" varchar(500),
"Assigned_Sim_Number" varchar(11),
"Activation_Date" timestamp,
"Deactivation_Date" timestamp,
"Manufacturer_ID" int,
"Customer_ID" int,
PRIMARY KEY ("IMEI_Number")
);
Then I have the manufacturer table which is
CREATE TABLE "MANUFACTURER" (
"Manufacturer_ID" int,
"Manufacturer_Name" varchar(30),
PRIMARY KEY ("Manufacturer_ID")
);
and I trying to create a relationship between these and getting the ORA-00904: "MANUFACTURER_ID": invalid identifier
My relationship code is
ALTER TABLE DEVICE
ADD FOREIGN KEY (Manufacturer_ID) REFERENCES MANUFACTURER(Manufacturer_ID);
That's just awful. Don't use double quotes when creating objects in Oracle as you'll have to use them every time you reference those objects, and match letter case every time.
SQL> alter table device add constraint fk_mf foreign key ("Manufacturer_ID")
2 references manufacturer ("Manufacturer_ID");
Table altered.
SQL>
A better option would be
SQL> create table device (
2 imei_number varchar2(15),
3 device_model varchar2(30),
4 device_description varchar2(500),
5 assigned_sim_number varchar2(11),
6 activation_date timestamp,
7 deactivation_date timestamp,
8 manufacturer_id int,
9 customer_id int,
10 primary key (imei_number)
11 );
Table created.
SQL> create table manufacturer (
2 manufacturer_id int,
3 manufacturer_name varchar2(30),
4 primary key (manufacturer_id)
5 );
Table created.
SQL> alter table device add constraint fk_mf foreign key (manufacturer_id)
2 references manufacturer (manufacturer_id);
Table altered.
SQL>
(Note also VARCHAR2 datatype; use that instead of VARCHAR).
By default, Oracle stores names in uppercase into data dictionary, but you can reference them using any case you want (upper, lower, mixed). If you do use double quotes, then you have to use their names exactly as during creation phase.
The columns in the tables you've created are surrounded by double-quotes, making their names case-sensitive, while the alter statement does not use quotes, and thus can't match the case-sensitive column name.
The best practice would be to drop these quotes and make the column names case-insensitive. If this is not an option, you could use the same quotes in the alter statement too:
ALTER TABLE DEVICE
ADD FOREIGN KEY ("Manufacturer_ID") REFERENCES MANUFACTURER("Manufacturer_ID");
-- Here ---------^---------------^--------------------------^---------------^

How to CREATE TABLE with disjoint relationship in SQL

I am trying to create a table using a disjoint subtype relationship.
For example, if the Supertype is furniture, and I have 3 Subtypes of furniture: chair, couch, and table.
Then:
CREATE TABLE Furniture
(order_num NUMBER(15), desc VARCHAR2(20), type VARCHAR2(10));
How do I make an option to pick type of chair, couch or table?
You can use REFERENCES in the CREATE TABLE.
CREATE TABLE Furniture_SubTypes
(
sub_type VARCHAR(10) PRIMARY KEY
);
INSERT INTO Furniture_SubTypes VALUES ('Chair');
INSERT INTO Furniture_SubTypes VALUES ('Couch');
INSERT INTO Furniture_SubTypes VALUES ('Table');
CREATE TABLE Furniture
(
order_num NUMBER,
description VARCHAR(20),
sub_type REFERENCES Furniture_SubTypes(sub_type)
);
Use a check constraint:
CREATE TABLE Furniture (
order_num NUMBER(15),
description VARCHAR2(20),
type VARCHAR2(10),
check (type in ('chair', 'couch', 'table'))
);
Note that desc is a poor choice for a column name, because it is a keyword in SQL (used for order by).

Oracle object relational DB: Access columns of referenced rows

I have some tables and need to create object views on top of them.
CREATE TABLE Person (
Nr NUMBER(4) PRIMARY KEY,
Name VARCHAR2(15));
CREATE TABLE Professor (
Nr NUMBER(4) PRIMARY KEY,
HabilThema VARCHAR2(30));
I also created types:
CREATE OR REPLACE TYPE Person_Typ AS OBJECT(
Nr INTEGER,
Name VARCHAR2(15)
);
CREATE OR REPLACE TYPE Professor_Typ AS OBJECT(
Nr INTEGER,
Habilthema VARCHAR2(30),
Person REF Person_Typ
);
As you can see there is a reference from a professor row to a person row. The view of the professors now should also include the columns of the person. Something like:
CREATE VIEW People OF Person_Typ
WITH OBJECT IDENTIFIER(Nr) AS
SELECT p.Nr, p.Name FROM Person p;
CREATE VIEW Professors OF Professor_Typ
WITH OBJECT IDENTIFIER(Nr) AS
SELECT p.Nr, p.Habilthema, p.Person.Name, MAKE_REF(People,p.Nr) FROM Professor p;
^
|_ Name of professor from Person table
But this does not work:
"P"."PERSON"."NAME" invalid identifier
Can this even be achieved by an object view? How?

Inserting a ref value into an object table

I have an assignment for college and I'm having trouble doing one of my inserts.
I have created an object called memeber
CREATE TYPE memeber AS OBJECT
(
member_id INTEGER,
member_name VARCHAR(30),
member_jobtitle VARCHAR(30),
member_skills skills_list,
past_projects past_projects_NTT
)
Then I created this object table
CREATE TABLE project_resources of MEMEBER
(
MEMBER_ID PRIMARY KEY,
MEMBER_NAME NOT NULL
)NESTED TABLE past_projects STORE AS PROJ_EXT;
Then I had to create an object called project as follows
CREATE TYPE project as OBJECT
(
PROJECT_ID INTEGER,
PROJECT_ASSIGNED_MEMBER REF MEMEBER,
PROJECT_TITLE VARCHAR2(30)
);
I had to create an object table of type project and alter it
CREATE TABLE PROJECT_TABLE OF PROJECT;
ALTER TABLE PROJECT_TABLE ADD PRIMARY KEY(PROJECT_ID);
ALTER TABLE PROJECT_TABLE ADD (CONSTRAINT NULL_CHK CHECK(PROJECT_TITLE IS NOT NULL);
No this is where I start to have trouble, I have been shown how to insert values to a table when all the values are of type ref, but when I include the other types I'm unsure of the syntax.
This was my attempt:
INSERT INTO PROJECT_TABLE
SELECT 1, REF(M)
FROM MEMEBER M
WHERE M.MEMBER_ID =1, 'KING KONG';
Could someone shed some light on the syntax for me please?
After some more research I found the solution:
INSERT INTO PROJECT_TABLE
(PROJECT_ID,
PROJECT_ASSIGNED_MEMBER,
PROJECT_TITLE)VALUES(
2,
(SELECT REF(M) FROM project_resources M WHERE M.member_id = 2),
'Bomb');

oracle11g sql: Warning: Type created with compilation errors

I'm trying to create a supertype customer service and subtype agent and supervisor, so they can inherent values however when I try to run this in oracle sql: a message comes up
Warning: Type created with compilation errors.
What is wrong with the code below?
Create or replace type customer_s_type as object (
csID number,
csName varchar(15),
csType number ) NOT FINAL;
Create or replace type supervisor_type UNDER customer_s_type ( title varchar (10) );
Create or replace type agent_type UNDER customer_s_type (title varchar (10));
Create table supervisor of supervisor_type (
CONSTRAINT supervisor_PK PRIMARY KEY (csID));
Create table agent of agent_type (CONSTRAINT agent_PK PRIMARY KEY (csID));
create table customer_service(
csID number(10),
csType number(10),
constraint supervisor_pk primary key(csID) );
You can use show errors in SQL*Plus or SQL Developer, or select * from user_errors, to see the error details.
Since you've shown six commands and the warning is about one of the first three (since it refers to type), and they appear OK independently apart from the constraint pointed put in comments, it looks like the whole script is being imterpreted as one command. It depends on your client settings, but you probably just need to seperate the commands with a / to cause them to execute. Becuase types can include PL/SQL the ; isn't treated as a statement seperator. So:
Create or replace type customer_s_type as object (
csID number,
csName varchar(15),
csType number ) NOT FINAL;
/
Create or replace type supervisor_type UNDER customer_s_type ( title varchar (10) );
/
Create or replace type agent_type UNDER customer_s_type (title varchar (10));
/
Create table supervisor of supervisor_type (
CONSTRAINT supervisor_PK PRIMARY KEY (csID));
Create table agent of agent_type (CONSTRAINT agent_PK PRIMARY KEY (csID));
create table customer_service(
csID number(10),
csType number(10),
constraint customer_service_pk primary key(csID) );