Index and foreign key referring to itself
Hello!I'm working on an application that uses a basket stored in a database. The shopping cart uses two tables:
BASKET: Contains the common information for a shopping cart: the user is logged in etc.
-Primary key: CART_ID
CART_ROW: A line in the shopping cart, for example a new product to buy.
-Primary key: ROW_ID
-Foreign key: CART_ROW. References CART_ID CART. CART_ID
On the code lines in the basket are taken by cart, as is modeled by the foreign key. There is a relationship, that we use in the code, but which is not modelled by a foreign key in the database. A line can be dependent on another line, which makes the other row a parent.
CART_ROW has a PARENT_ID column that refers to CART_ROW. ROW_ID.
Would you add a foreign key to PARENT_ID? Or are there issues to consider when it is a foreign key to the same table?
Some time ago, we added the index, both ROW_ID PARENT_ID. The index on PARENT_ID could have harmful, since there is no foreign key?
Best regards!
user9005175 wrote:
Hello!I'm working on an application that uses a basket stored in a database. The shopping cart uses two tables:
BASKET: Contains the common information for a shopping cart: the user is logged in etc.
-Primary key: CART_IDCART_ROW: A line in the shopping cart, for example a new product to buy.
-Primary key: ROW_ID
-Foreign key: CART_ROW. References CART_ID CART. CART_IDOn the code lines in the basket are taken by cart, as is modeled by the foreign key. There is a relationship, that we use in the code, but which is not modelled by a foreign key in the database. A line can be dependent on another line, which makes the other row a parent.
CART_ROW has a PARENT_ID column that refers to CART_ROW. ROW_ID.
Would you add a foreign key to PARENT_ID? Or are there issues to consider when it is a foreign key to the same table?
I suggest to add the foreign key, it will not harm performance (except during an insert when there is validation of the foreign key). But it would avoid users to insert bad data / corrupted by code or directly on loggin in the database.
Some time ago, we added the index, both ROW_ID PARENT_ID. The index on PARENT_ID could have harmful, since there is no foreign key?
Index on parent_id would be dangerous if you do not use index after it is created (i.e. There is no application that can make use of this index).
And if you decide to have a foreign key parent_id then I suggest to have too many indexes on parent_id because it would be at least useful when you delete a record in this table.
Best regards!
Tags: Database
Similar Questions
-
I just read an article on the index.
Jonathan Lewis - during indexing
The author says that if you update the primary key or a unique key at the end of 'parent' of the referential integrity constraint Oracle locks the "child" table to find the rows of children committed and uncommitted if a suitable index does not exist. After that he says do not create indexes 'foreign key '. I mean, there is a contradiction. What do you think?
delete this thread!
-
Hi, we have a RAC Oracle 10 g 2 EE on SLES10.
I have tables with 300.000.000 of records. These paintings clues because it access it takes very very slow. I check what he need an index for each foreign key. I have a lot of tables and I'm looking for some tools or SQL or similar phrases that tell me, tables for each foreign key index. For example, I need to know what a lot of index left complete me the process and that it.
Table A
ID (PK)
B1_ID
B2_ID
C_ID
D_ID
...
...
(B1_ID, B2_ID) FK TO TABLE_B
(C_ID) FK IN TABLE C
(D_ID) FK TO TABLE_D
I create the index in table A, table B, but I don't have the index for the table C and table.
I have a lot of tables, with several foreign keys.
How can I find out what indexes that I need to create?
Thank you very much.You can also refer to http://blog.go-faster.co.uk/2007/10/tm-locking-checking-for-missing-indexes.html
HTH
Anand -
Hello
I want to join the two tables to retrieve the data from the columns of the two table passing parameters to the join query. Tables have primary and foreign key relationships
Details of the table
Alert-1 - AlertCode (FK), AlerID (PK)
2 AlertCode-AlertDefinition-(PK)
Help, please
----------
Hi Vincent,.
I think that you have not worked on adf 12.1.3. In adf 12.1.3 you don't have to explicitly create the association. When you create the EO to your table, Association xxxxFkAssoc, will be created by ADF12.1.3 for you automatically. Please try this and do not answer anything... You can also follow the links below. I solved the problem by using the following link
---
-
I have a large table which basically consists of the foreign keys to other tables (see below). Initially, I decided to create indexes of separate table for each of my foreign key columns. Because of the amount of records in the table, my index tablespace is taking up more data tablespace. What is the recommendation to add indexes for foreign key columns in a table?
Thank you
CREATE TABLE STU_ENROLLMENT
(PK_ID VARCHAR2 (32), sys_guid() by default)
CONSTRAINT pk_stu_enrollment
KEY ELEMENTARY SCHOOL
With the HELP of INDEX TABLESPACE sis_express_index
STORAGE (4096 INITIAL
NEXT 4096
PCTINCREASE 0),.
Fk_stu_enrollment_stu_school FK_STU_SCHOOL VARCHAR2 (32) CONSTRAINT
REFERENCES
stu_school (PK_ID)
ON DELETE CASCADE,
Fk_stu_enrollment_schl_cal FK_SCHOOL_CALENDAR VARCHAR2 (32) CONSTRAINT
REFERENCES
school_calendar (PK_ID)
ON DELETE CASCADE,
Fk_stu_enrollment_pcc_code FK_PCC_CODE VARCHAR2 (32) CONSTRAINT
REFERENCES
pcc_code (PK_ID)
ON DELETE CASCADE,
Fk_stu_enrollment_grade_level FK_GRADE_LEVEL VARCHAR2 (32) CONSTRAINT
REFERENCES
grade_level (PK_ID)
REMOVE THE NULL VALUE,
PCC_NOTES VARCHAR2 (1000)
CREATE INDEX STU_ENROLLMENT_I2 ON STU_ENROLLMENT (FK_SCHOOL_CALENDAR)
TABLESPACE sis_express_index
STORAGE (4096 INITIAL
NEXT 4096
PCTINCREASE 0);
CREATE INDEX STU_ENROLLMENT_I3 ON STU_ENROLLMENT (FK_PCC_CODE)
TABLESPACE sis_express_index
STORAGE (4096 INITIAL
NEXT 4096
PCTINCREASE 0);
CREATE INDEX STU_ENROLLMENT_I4 ON STU_ENROLLMENT (FK_GRADE_LEVEL)
TABLESPACE sis_express_index
STORAGE (4096 INITIAL
NEXT 4096
PCTINCREASE 0);
CREATE INDEX STU_ENROLLMENT_I5 ON STU_ENROLLMENT (FK_VOTECH)
TABLESPACE sis_express_index
STORAGE (4096 INITIAL
NEXT 4096
PCTINCREASE 0);
CREATE INDEX STU_ENROLLMENT_I6 ON STU_ENROLLMENT (FK_SPECIAL_ED)
TABLESPACE sis_express_index
STORAGE (4096 INITIAL
NEXT 4096
PCTINCREASE 0);
CREATE INDEX STU_ENROLLMENT_I7 ON STU_ENROLLMENT (FK_TUITION_CODE)
TABLESPACE sis_express_index
STORAGE (4096 INITIAL
NEXT 4096
PCTINCREASE 0);Salvation;
Please see entery Tom site
http://asktom.Oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:292016138754Respect of
HELIOS -
Unique key and foreign key in the view of materialzied
Hello world
I have one main site and 4 materialized view sites.
In my main site, I did a main group that contains my tables participating in replication.
I have a table that has a primary key, foreign key constraint unique.
In my first materialized view site I did group for my opinion
BEGIN
DBMS_REPCAT. () CREATE_MVIEW_REPGROUP
gname = > "hr_repg"
Master = > "orc1.com"
propagation_mode = > 'ASYNCHRONOUS');
END;
/
BEGIN
DBMS_REFRESH. DO)
name = > 'mviewadmin.hr_refg ',.
list = > ",
next_date = > SYSDATE,.
interval = > 'SYSDATE + 1/144',.
implicit_destroy = > FALSE,
rollback_seg = > ",
push_deferred_rpc = > TRUE,
refresh_after_errors = > FALSE);
END;
/
Then I created the materialized view
HR.test CREATE MATERIALIZED VIEW
COOL OFF QUICKLY WITH A PRIMARY FOR THE UPDATE KEY
AS SELECT * FROM [email protected]
/
BEGIN
DBMS_REPCAT. () CREATE_MVIEW_REPOBJECT
gname = > "hr_repg"
sName = > 'hr ',.
oname = > 'test ',.
Type = > "SNAPSHOT."
min_communication = > TRUE);
END;
/
BEGIN
DBMS_REFRESH. ADD)
name = > 'mviewadmin.hr_refg ',.
list = > "hr_test.test"
Lax = > TRUE);
END;
/
When I check the test (materialized view) table in the site of the materialized view to a primary key, but it has no foreign key forced unique having the base table and because my test materialized view can be changed I need to have these 2 constraint on my my materialized view. You have an idea why these 2 constraint were not created on my materialized view? And what should I do to have these two constraints.
Thanks in advanceOn MView is created automatically only PK. Other constraints (check, not null, unique or FK) should be created manulay. Indexes also.
But be careful with the FK.
On ORACLE8, I found lines that are inserted to updatable mview back in deleted updating process, then inserted (downloaded on the main site). When FK is with on delete cascade that the lines of the child tables (mviews are removed too). And when the child mview is updated before mview parent you found the child missing lines.
When CF is on delete restrict that you refresh the process fails. -
Primary key and foreign key between the different schemas
Hi all;
SCOTT user have a 'rank' of table-> it contains the primary key-> users tablespace
The U1 user have a 'emp' table-> it contains the foreign key-> test tablespace
U1 > select constraint_name, constraint_type, r_owner, owner, r_constraint_name of all_constraints where table_name = 'EMP ';
OWNER CONSTRAINT_NAME C R_OWNER R_CONSTRAINT_NAME
------------ ------------------------------ - -------------------------- ----------------------- -----------------------------------------------------
U1 EMP_EMPLEV_C12_FK R SCOTT GRADE_GRL_C1_PK
If I have connection U1, no possibility to find the name of the primary key without connection scott table?
Thank you
Hello
So, you want to know what picture of the SCOTT schema has the constraint that is named "GRADE_GRL_C1_PK." Isn't it?
If so,.
SELECT table_name
Of all_constraints
Owner WHERE = 'SCOTT '.
AND the argument constraint_name = 'GRADE_GRL_C1_PK. '
;
-
Add the foreign key refers to a different schema
Hello
We have a database with schemaA and schemaB and we use a data model for schemaA with SQL Data Modeler.
Our data model is based on SchemaA and we do not want add schemaB or create subview in our current model for schemaB since our dev team do not want schemaB in the model
We have created a synonym in tableB schemaB shcemaA after granting select them, references to schemaA
We have added a foreign key on tableA in referencing TableB on schemaB schemaA.
for example. Add ALTER table tableA foreign constraint key(column-name) references tableB(colum-name)
So far so good.
But if we try to produce the DDL for tableA it has not provided the ALTAR of the foreign key that is based on shcmeaB
We know that we cannot add foreign key on dblinks.
How can I get the Modeler DDL from SQL data for tableA, including the foreign key
We appreciate the answers
Kind regards
Zafar
Hi zak,.
We have created a synonym in tableB schemaB shcemaA after granting select them, references to schemaA
We have added a foreign key on tableA in referencing TableB on schemaB schemaA.
You can do it in the database because TableB exists in the database and DB knows its definition, including the existing constraint of PK/UK.
This is the same model of data Modeler - you must have the definition of the table referenced in order to create the FK that refer to the constraint of PK/UK.
As it works now (we can change it) you can have modeled for SchemaA and ModelB for SchemaB. Then, you can drag & drop tableB form the Navigator to the diagram of the ModelA and TableB there will exist as an object remote read-only, you can use to create the FK.
With the help of subviews, DDL generation and configurations of reports, you can display the dev team than objects that they want to see - on the charts in generated DDL and reports.
Philippe
-
Hi all
I'm confused. If I create a primary key of table constraints, it willl create a unique index by default. If I create a foreign key, it also creates an index? of any kind?
Thank you very much
KinzLaughing out loud. foreign key does not have a unique index on the table.
-
CFTransaction, several inserts and foreign keys
Here is the joke of what I want to do
< cfset commitit = yes >
< cftransaction action = start >
< cftry >
< cfquery >
making my first insertion on the main table - primary key is the unique sequence number
< / cfquery >
< cfquery >
do my next insertion in the second, but it has a foreign key to the intial insert constraint
< cfquery >
< cfquery >
do my next insertion in the third table, but it has a foreign key to the intial insert constraint
< cfquery >
< cfcatch >
< commitit = no >
Rollback
< / cfcatch >
If commitit = yes
commit the changes
endif
< / cftry >
< / cftransaction >
Problem is when I try to insert the record in the second table, the first record (parent) is not here because no validation has yet arrived. I get a foreign key error. I want to do within a transaction so that I can do a restore if there are questions. Is it possible to do in a cftransaction?
I want to thank you all for your answers. The code I had was OK. I kept changing the code for serialization, because it makes sense in this case. I needed to get back to the basics - my definition of foreign key was in trouble. Thanks to you all.
-
Import / Export and foreign keys
Hi there;
I am facing the problem more common online backup (I think), when we export a database online;
I wonder if there are a few steps to that end, or maybe we have an export option to make a snapshot on export database begins;
Any tips?
Thank you
Marcos Ortega
BrasilMore LOGIC - a comparable to CONSISTENT parameter is not necessary. Use FLASHBACK_SCN and FLASHBACK_TIME for this feature.
http://download.Oracle.com/docs/CD/B19306_01/server.102/b14215/dp_export.htm#i1005864
-André
-
Get all the documents referenced by a self-referential foreign key
Suppose I have a table as follows:
The "alias" column is a foreign key referring to itself, then 103 and 104 people are actually the same person as 100 - so there are only four unique individuals mentioned in the data.ID NAME ALIAS ---- ------------- ----------------- 100 Jack Jones 101 Fred Smith 102 James Connor 103 J. Jones 100 104 Jimmy Jones 100 105 Jack E. Jones
How to query this table therefore providing a version of Jack Jones of the name (and it could be one of the three variants), I get all three records, without inadvertently also pulling the 'Jack E. Jones' record to 105?Hello
Here's another way to apply the idea of John, but without all the golds:
SELECT c.id, c.name FROM t c JOIN t p ON NVL (p.alias, p.id) = NVL (c.alias, c.id) WHERE p.name = :nm ;
If performance is a problem, you can create an index based on a function on NVL (alias, id), or you can modify the table so that alias was never NULL; make the same ID when there is no alias.
-
Key Reg primary and foreign at the same table
Hello
It is - ok if create primary and foreign keys on the same table itself?
For example:
In a table X, I have two columns such as Codeunite and ParentUnitcode.
Is it ok if create Mt10 as principal and ParentUnitcode as foreign keys in the table itself?
The foreign key base table: OrganizationalUnit
Foreign key columns: ParentUnitCode
Primary/Unique key base: OrganizationalUnit
Primary/unique key col: Mt10
Can anyone answer me on that?
Concerning
RajeshOWB,
If the two tables are favouring a one-to-one relationship so there no need to have two separate tables and so you can have free referential foreign keys in the same table where the primary key.
-
I read a book on the index, he said
what I don't understand, I should create index on the foreign key in parent-child table or table column or both?You should almost always index foreign keys because they are frequently used in joins. In addition, if you intend to delete or update unique or primary keys on the parent table, you should index the foreign keys to improve the locking of child records.
Thank youHello
806540 wrote:
I read a book on the index, he saidYou should almost always index foreign keys because they are frequently used in joins. In addition, if you intend to delete or update unique or primary keys on the parent table, you should index the foreign keys to improve the locking of child records.
what I don't understand, I should create index on the foreign key in parent-child table or table column or both?
They mean that you need to create an index in the child table, that is the table that contains the foreign key constraint.
In order to have a foreign key constraint, it must have a primary key constraint, or a unique constraint on the column, or columns referenced in the parent table. Either type (primary key or unique) automatically creates an index, so there is nothing more you need to do in the parent table.
-
How to set the condition of rule to check the Primary Key, Foreign Key and Unique
I use Oracle 10.2
Could someone tell me please how to set the rule condition to check the primary key, foreign key constraint of uniqueness?Hello
Primary key, unique constraints and foreign key are better applied using the native functionality of database constraint. If you like the rules to check if a key exists at the time of the evaluation, you can use a function defined by the user in a rule condition and this function can ask to turn the table keys.
Hope this helps,
-Aravind.
Maybe you are looking for
-
The latest version of Firefox is compatible with Sibelius Scorch?
I resisted the temptation to update the version of Firefox that I use (16), because previously it was not compatible with Scorch. Is it now compatible, so I can download music and if need be, listen to it.Thank you very much for your help,Kate
-
700 - 060ea Office: stop the usb ports go to sleep when the rest of the PC
When I get my laptop connected via usb, the system constantly charge the phone when the pc is asleep. Have you tried to uncheck "allow the computer to turn off this device to save power" in the Control Panel/Device Manager/USB controllers/Properties
-
HP Touchsmart 610-1250xt run SmartCenter 5.0 or 4.0?
A pretty good question, does the software where we can see windows 7 in the background or not? Thank you
-
Parental control: disappered Contact Management
Im trying to add my childs account, so I can control it. His looks that the management of the contacts on the left side is missing Could someone help me?
-
Windows or Microsoft Update does not work
This is WindowsUpdate.log for your reference. I have marked some line in bold that I felt could help in the analysis of the reason for this problem. [2014-02 / 02 01:37:09:281 3180 d70 Misc = logging initialized (build: 7.6.7600.256, tz: + 0530) =.20