How do you create a multi-attribute table and link to the main table? - oracle

I have a table called customer. The customer, Here are the details for the table
Table name: customers
Attributes:
cus_id -- primary key
cus_street_num -- multi-attribute (home_address)
cus_street_name -- multi-attribute (home_address)
cus_suburb -- multi-attribute (home_address)
cus_gender
Here us my code for the customer table creation. But how do I create the multi-attribute and link it to the customer table?
What is a multi-attribute? For example a home address is a multi-attribute. Why? Because a home address is made up of 3 components. The street number, the street name and the suburb. These 3 things can be separate attributes, but they are useless without if they aren't together. However, together, they make up the home address.
CREATE TABLE customers (
cus_id NUMBER(5,0)
CONSTRAINT cus_id_pk
PRIMARY KEY (cus_id),
cus_gender NUMBER(1,0)
);

Related

error no master data blocks are available

I try create project but I have get to proplem
in form builder when I click right and chose data block then chose table and click in add realshenshep I get these error said 'error no master data blocks are available'
Please help me
I am waiting to reply me
Thanx
If you want the Wizard to create relationship between master and detail block, underlying tables have to be related - master table has to have a primary key, and it has to be referenced by detail table's foreign key.
Forms is capable of detecting that and, if it exists, can create the relationship.
Otherwise, if there's no such a relation between those tables, you'll have to create it manually.
[EDIT]
How to create a foreign key?
SQL> create table master
2 (id number,
3 name varchar2(20));
Table created.
SQL> create table detail
2 (id number,
3 idm number,
4 datum date
5 );
Table created.
SQL> -- create a primary key on the master table
SQL> alter table master add constraint pk_mas primary key (id);
Table altered.
SQL> -- create a foreign key on the detail table
SQL> alter table detail add constraint fk_det_mas foreign key (idm)
2 references master (id);
Table altered.
SQL>
As of manually creating a relationship: put the join condition in there, such as
detail.idm = master.id

create a table with composed primary keys

I have a table:
course
---------
id_course
id_groupe (references a table named group)
id_module (references a table named module)
In my case a course can be shared by many groups.
My question is :
it is correct to choose the primary key composed of (id_course , id_group )?
and is there any other way to make the same table ?
Lets assume that the course has another attribute - its title.
Ask yourself the question:
Will the title of the course change if it is being taken by a member of "group A" or "group B"?
For most institutions, a course will have a set title and it does not matter who is taking that course so with your table structure you will have to duplicate the title across every course/group combination and breaks the concept of database normalisation.
You would be better splitting the table into two tables:
course
---------
id_course (Primary Key)
title
course_group
------------
id_course (FK referencing course) } composite PK
id_groupe (FK referencing groupe) }
Then you can set the title for the course and it is independent of the groups associated with the course. You can, of course, have a composite primary key on the course_group table consisting of both ids to enforce uniqueness.
If you have a course that may be shared by many groups, I'd think of a sligthly different design, with a relation table linking COURSE and GROUPE; this table could have a composite PK, based on the two foreing key columns:
COURSE
id_course PK
id_module KF references MODULE
MODULE
id_module PK
GROUPE
id_groupe PK
COURSE_REL_GROUPE
id_course FK references COURSE
id_groupe FK references GROUPE
PRIMARY KEY (id__course, id_groupe)

How to Create a Foreign Key from a Table to a Custom Datatype

Working with Oracle Express 11g, learning the interactions for object-relational databases.
Running into trouble while trying to create a foreign key from a normal table, to a table of a custom object (customers)
The customer object is as follows (customer_id NUMBER, fname VARCHAR2, lname VARCHAR2), for our setup, we wish to join a table Applications via customer_id NUMBER. Within the SQL Workshop, selecting the customers table is not even an option, and we cannot find syntax to manually create this relationship.
Any experts have some clues to help figure this one out?
create or replace type customer is object
(
customer_id NUMBER,
fname VARCHAR2(100),
lname VARCHAR2(100)
);
/
create table customers of customer
(
constraint customer_pk primary key(customer_id)
);
create table applications
(
application_id number,
customer_id number,
constraint applications_fk1 foreign key (customer_id)
references customers(customer_id)
);

Oracle composite key deletion very slow

I have one table with a composite key (REGION) and another table (CITY) that references that table. Inserts, queries, and individual deletions work quickly. The problem is that when I try to bulk-delete the contents of CITY using sqlplus, delete from CITY, it takes forever. This table will have ~400,000 entries and it takes 15-20 minutes just to delete 50,000 entries. Here is my setup using Oracle 11:
create table COUNTRY
(
id varchar2(32) NOT NULL -- PK
...
);
create table REGION -- about 4000 entries
(
country varchar2(32) NOT NULL -- PK, FK to COUNTRY
regionCode char(2) NOT NULL -- PK
...
);
create table CITY -- about 400,000 entries
(
id number NOT NULL -- PK
country varchar2(32) NOT NULL -- FK to COUNTRY
regionCountry varchar2(32) NULL -- FK to REGION
regionCode char(2) NULL -- FK to REGION
...
);
create table LOCATION -- about 2,500,000 entries
(
id varchar2(32) NOT NULL -- PK
country varchar2(32) NOT NULL -- FK to COUNTRY
city number NULL -- FK to CITY
...
);
ALTER TABLE COUNTRY ADD CONSTRAINT PK_COUNTRY PRIMARY KEY (id) USING INDEX;
ALTER TABLE REGION ADD CONSTRAINT PK_REGION PRIMARY KEY (country, regionCode) USING INDEX;
ALTER TABLE CITY ADD CONSTRAINT PK_CITY PRIMARY KEY (id) USING INDEX;
ALTER TABLE IPGeoLoc ADD CONSTRAINT PK_LOCATION PRIMARY KEY (id) USING INDEX;
ALTER TABLE REGION ADD CONSTRAINT FK_REGION_COUNTRY
FOREIGN KEY (country) REFERENCES COUNTRY (id);
ALTER TABLE CITY ADD CONSTRAINT FK_CITY_COUNTRY
FOREIGN KEY (country) REFERENCES COUNTRY (id);
ALTER TABLE CITY ADD CONSTRAINT FK_CITY_REGION
FOREIGN KEY (regionCountry, regionCode) REFERENCES REGION (country, regionCode);
ALTER TABLE LOCATION ADD CONSTRAINT FK_LOCATION_COUNTRY
FOREIGN KEY (country) REFERENCES COUNTRY (id);
ALTER TABLE LOCATION ADD CONSTRAINT FK_LOCATION_CITY
FOREIGN KEY (city) REFERENCES CITY (id);
The varchar2(32) fields are GUIDs. I know I should not use GUIDs as a PK but I cannot change that unless I have proof that this is the problem.
I can bulk-delete entries from LOCATION with no problem, 300,000 in a couple of seconds, so this leads me to believe it is the composite key that is giving me trouble.
The secondary issue is that I currently have two country columns in the CITY table - one linked directly to COUNTRY and the other linked as part of the composite key to REGION. I know how I would do this in code and only have one country column but I have to use Hibernate. This works the way it is except for the delete problem so I can't change it unless I can prove this is causing an issue. I'm using sqlplus to try the deletions so I know Hibernate is not causing the delete problem.
My wager is that the problem has nothing to do with the presence of a composite key and everything to do with an unindexed foreign key.
Unless you've omitted it from your question, the CITY column in the LOCATION table is not indexed. That means that every time you try to delete a row from CITY, Oracle has to do a full table scan on the LOCATION table looking for rows in LOCATION that would be orphaned in order to enforce the foreign key constraint. In general, if you want to ever delete from the parent, the foreign key in the child table needs to be indexed. So LOCATION should have indexes on both CITY and COUNTRY, the CITY table should have indexes on COUNTRY and (regionCountry, regionCode), etc.
Even if all the rows from LOCATION have been deleted, if Oracle has to do a full table scan on LOCATION, it has to read up to the high water mark of the table. If the table previously had 2.5 million rows and you just did a DELETE, you would still have to read however many blocks were required to store those 2.5 million rows every time you delete a row from CITY.
You can test whether my hunch is correct in a few different ways
You can index the CITY column in the LOCATION table.
You can drop the foreign key constraint on LOCATION that references the CITY table.
You can truncate the LOCATION table instead of deleting the rows so that the high water mark gets reset and a table scan will take much less time.

How to have unique primary key in two tables?

I have two tables in my system EMPLOYEE and EMPLOYEE_FORECAST. Both have the same columns, entire structure is same. I have another archive table with same structure called EMPLOYEE_ARCHIVE table.
I need to put data from both tables to this archive table. Since records in EMPLOYEE and EMPLOYEE_FORECAST may have same primary key e.g. a record in EMPLOYEE will have a pk of say 100 and another record in EMPLOYEE_FORECAST may also have pk of 100 and this will definitely happen so when they are inserted into archive table I will have a duplicate primary key.
The problem is I will also have some relation table like employee_products, employee_forecast_products and also employee_archive_products. These tables will have emp_id and product_id. So with same emp_id I wont be able to figure out the exact employee.
So, is there any way to have a unique primary key both the EMPLOYEE and EMPLOYEE_FORECAST tables.
So you cannot not use the PK column of EMPLOYEE table as a PK column of the archive table.
You can add a new PK column to the archive table.
If, for some reason, you want the EMPLOYEE table's PK column to be the PK in the archive table, then you could add a flag column to the archive table which would indicate from which table the record comes from. And you could have a composite PK in the archive table containing the original PK and the flag column. (In general, I discourage composite PK-s, so, even if you want to have this flag column, you could have an additional normal PK column in the archive as well.)
To expand on my comment, set up you archive table as:
EMPLOYEE
EMPID NUMBER PK
EMPNAME VARCHAR2(30)
...
EMPLOYEE_FORECAST
EMPID NUMBER PK
EMPNAME VARCHAR2(30)
...
EMPLOYEE_ARCHIVE
ORIG_TABLE VARCHAR2(30) PK
EMPID NUMBER PK
EMPNAME VARCHAR2(30)
...
Data in EMPLOYEE_ARCHIVE:
ORIG_TABLE EMPID EMPNAME
------------------------------------
EMPLOYEE 100 JO BLOGGS
EMPLOYEE_FORECAST 100 JO BLOGGS
As the archive table PK is across both the original table and empid columns it will remain unique for all your data.
Obviously this is just an example and you can use whatever derived column you want to enforce the uniqueness in your archive table.
Hope it helps...
Create a Common Super-Table. Make another table EMPLOYEE_ID with only a primary key. Let both EMPLOYEE, EMPLOYEE_FORECAST and EMPLOYEE_ARCHIVE reference it.
Your data model seems a tad confused. If EMPLOYEE and EMPLOYEE_FORECAST have identical structures why have two tables? What is the business rule here?
And if they are supposed to be two separate tables why store them in a common archive table? Why not have separate archives for each table?
I agree with #Ollie. You need to rethink your data model so it clearly expresses how your business operates. If you post your business rules here I'm sure we can help you untangle things. But here is probably the crucial question: do the following keys identify one employee (i.e one person in the real world) or two?
employee.emp_id = 100
employee_forecast.emp_id = 100

Resources