Key Constraints in the Index Tables
Hey,
I am a starter in the Database Application Development.
I have created few indexes on columns of different tables, and I wanted to look into the ALL_INDEXES and the USER_INDEXES to find the indexes that I created.
I noticed that there are some primary keys listed in the ALL_INDEXES or USER_INDEXES tables.
Can somebody tell me why the Primary Keys that are defined on tables are also getting listed in the USER_INDEXES or ALL_INDEXES tables.
Thanks a Million
That's very simple.
To be a primary key a column must verify two conditions:
1) is not nullable
2) is unique
Indexes can be declared unique, hence the simplest way oracle has to create a primary key constraint is to create an authomatuc unique index on it:
SQL> create table x (a number);
Table created.
SQL> select * from user_indexes where table_name='X';
no rows selected
SQL> desc x;
Name Null? Type
A NUMBER
SQL> alter table x add constraint x_pk primary key (a);
Table altered.
SQL> desc x;
Name Null? Type
A NOT NULL NUMBER
SQL> -- A has become NOT NULL
SQL> select index_name, uniqueness
2 from user_indexes where table_name='X';
INDEX_NAME UNIQUENES
X_PK UNIQUE
SQL> -- an authomatic index has been created
SQL> drop index X_PK;
drop index X_PK
ERROR at line 1:
ORA-02429: cannot drop index used for enforcement of unique/primary key
SQL> -- You cannot drop it because it enforces the primary key
SQL> alter table x drop constraint x_pk;
Table altered.
SQL> -- but if you drop the primary key...
SQL> select index_name, uniqueness
2 from user_indexes where table_name='X';
no rows selected
SQL> -- ...the index too is automatically dropped.Max
http://oracleitalia.wordpress.com
Similar Messages
-
Create a materized view without primary key constraint on the base table?
Hi
I tried to create a materized view but I got this error:
SQL> CREATE MATERIALIZED VIEW TABLE1_MV REFRESH FAST
START WITH
to_date('04-25-2009 03:00:13','MM-dd-yyyy hh24:mi:ss')
NEXT
sysdate + 1
AS
select * from TABLE1@remote_db
SQL> /
CREATE MATERIALIZED VIEW TABLE1_MV REFRESH FAST
ERROR at line 1:
ORA-12014: table 'TABLE1' does not contain a primary key constraint.
TABLE1 in remote_db doesn't have a primary key constraint. Is there anyway that I can create a materized view on a base table which doesn't have a primary key constraint?
Thanks
LizHi,
Thanks for your helpful info. I created a materialized view in the source db with rowid:
SQL> CREATE MATERIALIZED VIEW log on TABLE1 with rowid;
Materialized view log created.
Then I created a MV on the target DB:
CREATE MATERIALIZED VIEW my_schema.TABLE1_MV
REFRESH FAST
with rowid
START WITH
to_date('04-25-2009 03:00:13','MM-dd-yyyy hh24:mi:ss')
NEXT
sysdate + 1
AS
select * from TABLE1@remote_db
SQL> /
CREATE MATERIALIZED VIEW my_schema.TABLE1_MV
ERROR at line 1:
ORA-12018: following error encountered during code generation for
"my_schema"."TABLE1_MV"
ORA-00942: table or view does not exist
TABLE1 exists in remote_db:
SQL> select count(*) from TABLE1@remote_db;
COUNT(*)
9034459
Any clue what is wrong?
Thanks
Liz -
Knowing the primary key columns of the given table
Hi,
How can I get the primary key columns of the given table?
Regards,
Sachin R.K.You can find the constraint_name from all_constraints/user_constraints for constraint_type = 'P' (which is the primary key constraint).
And then see which columns are in for the constriant_name
in all_cons_columns/user_cons_columns view.
Below is the example
select acc.column_name from
all_cons_columns acc, all_constraints ac
where acc.constraint_name = ac.constraint_name
and acc.table_name = 'DEPT' AND acc.owner = 'SCOTT'
and ac.constraint_type = 'P'
Hope this helps
Srinivasa Medam -
Can we have more than one primary key constraint to a Oracle Table?
Hi,
Can we have more than one primary keys to a single table in oracle? ( Not the composite key)
Please somebody answer..
Regards,
Alaka811935 wrote:
Can we have more than one primary keys to a single table in oracle? ( Not the composite key)
In principle a table can have multiple keys if you need them. It is a very strong convention that just one of those keys is designated to be "primary" but that's just a convention and it doesn't stop you implementing other keys as well.
Oracle provides two uniqueness constraints for creating keys: the PRIMARY KEY constraint and the UNIQUE constraint. The PRIMARY KEY constraint can only be used once per table whereas the UNIQUE constraint can be used multiple times. Other than that the PRIMARY KEY and UNIQUE constraints serve the same function (always assuming the column(s) they are applied to are NOT NULL). -
How add primary key constraint to already existing table with data
I want apply primary key constraint to already existing table with data
is there any command or way to doAlternatively, assuming you want to ensure uniqueness in your primary key column you can do this:
alter table <table name> add constraint <cons name> primary key (col1,col2) exceptions into <exception_table>
If the altter table statement fails this will populate the EXCEPTIONS table with the rows that contain duplicate values for (col1,col2).
You will need to run (or get a DBA to run) a script called UTLEXCPT.SQL (which will be in the $ORACLE_HOME/rdbms/admin directory) if you don't already have an EXCEPTIONS table.
Cheers, APC -
Ora-2270 Error while adding foreign key constraint on top a view
While adding a constraint, referencing a foreign key column from a view raises error. However, it works fine over a table.
Here's the sample script:
create table t_temp (
sample_id number,
text varchar2(40))
alter table t_temp add constraint temp_pk
primary key (sample_id);
create view tempvw as select sample_id,text from t_temp;
create table t_sample (
uniq_id number,
sample_id number,
sample_text varchar(40));
=========
ALTER TABLE t_sample ADD CONSTRAINT FK_sample_temp
FOREIGN KEY (sample_id) REFERENCES t_temp (sample_id);
This works fine.
=========
==============
The following raises ERROR....
ALTER TABLE t_sample ADD CONSTRAINT FK_sample_temp1
FOREIGN KEY (sample_id) REFERENCES tempvw (sample_id);
Error starting at line 1 in command:
ALTER TABLE t_sample ADD CONSTRAINT FK_sample_temp1
FOREIGN KEY (sample_id) REFERENCES tempvw (sample_id)
Error report:
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog viewWhat exactly are you trying to accomplish by having a
referential integrity constraint on a view (which is
not allowed). PKs and FKs are allowed on views. DISABLE NOVALIDATE is the only allowed state for them. View constraints are used for query rewriting: http://download.oracle.com/docs/cd/B19306_01/server.102/b14223/qradv.htm#sthref1457
Regards,
Dima -
IDOC to MSsql, error in mapping(Violation of PRIMARY KEY constraint)
Hi All,
I'm working with MATMAS IDOC to MSSql. My SQL structure is of 8 tables(multiple statements) with primary key. In mapping i have used UPDATE_INSERT in action field for all the tables but still im getting "Violation of PRIMARY KEY constraint" for the last table structure. first 7 tables are single occurances but the 8th structure data is coming multple times from IDOC(E1MARMM).
my 8th table structure is :
STATEMENT8 IDOC(MAPPED)
TABLE8
ACTION UPDATE_INSERT
TABLE TABLENAME
ACCESS 0 to Unbounded MAPPED with E1MARMM
Item_CD used oneasmany +splitbyvalue with MATNR
Plant_ID used oneasmany +splitbyvalue with WERKS
EAN_CAT used oneasmany +splitbyvalue with NUMTP
EAN used oneasmany +splitbyvalue with EAN11
Numerator_For_Conversion_To_BaseUOM
Display_UOM
Denominator_for_conversion_To_baseUOM
KEY 0 to Unbounded MAPPED with E1MARMM
Item_CD used oneasmany +splitbyvalue with MATNR
Plant_ID used oneasmany +splitbyvalue with WERKS
EAN_CAT used oneasmany +splitbyvalue with NUMTP
EAN used oneasmany +splitbyvalue with EAN11
Display_UOM
in test tab its fine and fetching number of times according to MARMM segments but in END to END testing its triggering an error stating that
""Message processing failed. Cause: com.sap.engine.interfaces.messaging.api.exception.MessagingException: Error processing request in sax parser: Error when executing statement for table/stored proc. 'MM_EAN' (structure 'STATEMENT8'): com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK_MM_EAN'. Cannot insert duplicate key in object 'dbo.MM_EAN'. ""
Plz help me regarding this..Hi team,
How resolve the below error
Violation of PRIMARY KEY constraint 'PK_test'. Cannot insert duplicate key in object 'dbo.test'. The duplicate key value is (12610). (Source: MSSQLServer, Error number: 2627) ?
Thanks,
Ram
RAM
There can be two reasons
1. The insert script used is having multiple instances of the records with Key as 12610 returned from the source query. If this is the issue add a logic to include only the unique set of id values for records by avoiding duplicates. There are several approaches
for this like using ROW_NUMBER with PARTITION BY, using a join with derived table etc
2. The record with Key 12610 already exist in your destination table and your script is again trying to insert another instances of record with same key. This can be avoided by adding a NOT EXISTS condition with a subquery which will check and return only
those records which doesnt already exist in the source
Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh
My Wiki User Page
My MSDN Page
My Personal Blog
My Facebook Page -
Publish: dropping index when it is used for foreign key constraint enforcement
Hi,
I'm trying to update a target schema from a reference database via Publish. Among the changes to apply, there's an index that needs to be dropped. Since it's linked to a foreign key constraint, it cannot be deleted unless the foreign key is temporarily dropped
(I saw somewhere that disabling the foreign key should be enough but it doesn't seem to work either).
Since there are other changes to be made on the same table, this foreign key also has to be dropped before the script can delete the table and re-create it. This part of the script is correctly generated. The problem is that this part appears after the 'DROP
INDEX' instruction.
So when generating the update script, SSDT tries to drop the index BEFORE dropping the foreign key. And I can't drop the foreign key in my custom pre-deployment script, otherwise the update script would fail when trying to delete it again.
Shouldn't SSDT be smart enough to drop the constraint before the index? Is it a bug or did I forget to set an option? If it's not a bug, what can I do apart from doing it manually?
Thank you for your helpHi Elsa,
That sounds like a bug. Could you please file a Connect issue for this at
https://connect.microsoft.com/SQLServer/feedback/CreateFeedback.aspx using the category "Developer Tools (SSDT, BIDS, etc.)"? We're trying to track all bugs through
Connect so that you can tell when we have fixed the issue and we can request more information.
A workaround for this issue might be to write a pre-deployment script for your project to drop the foreign key prior to deployment. A pre-deployment script can be added to your project by right-clicking on the
project in solution explorer and then clicking on Add > Script... and selecting Pre-Deployment Script from the list.
Thanks! -
Add a foriegn key constraint to to a sub type on an object table
Hi,
I'm trying to set up a new table of an object type in our DB at work and on it I want to create a foreign key constraint on one of the attributes of one of the sub types
The table is called documents and is a table of document_ot
Definition:
create or replace
TYPE document_ot AS OBJECT (
-- ATTRIBUTES
created_date DATE,
created_by VARCHAR2( 32 ),
doc_id NUMBER,
-- CONSTRUCTORS
) NOT INSTANTIABLE NOT FINAL;
This type is extended by the type funding_certificates_ot
create or replace
TYPE funding_certificate_ot UNDER document_ot (
-- ATTRIBUTES
fc_prefix VARCHAR2( 2 ),
fc_sequence NUMBER,
fc_suffix VARCHAR2( 1 ),
fc_pr_id NUMBER,
-- CONSTRUCTORS
-- METHODS
) NOT FINAL;
I want to add a constraint to the to documents table that says that the funding_certificate_ot column fc_pr_id references pr_id in the table payruns.
I've managed to create an index on the fc_pr_id column using:
CREATE INDEX fc_pr_idx
ON documents d (treat (value(d) as funding_certificate_ot).fc_pr_id);
The code I thought I'd use for the foreign key constraint would be:
ALTER TABLE documents d
ADD CONSTRAINT doc_pr_fk FOREIGN KEY
treat (value(d) as funding_certificate_ot).fc_pr_id
REFERENCES payruns
(pr_id)
ENABLE;
But that doesn't work - would someone be able to give me the correct syntax please?
Many thanksHi,
I'm trying to set up a new table of an object type in our DB at work and on it I want to create a foreign key constraint on one of the attributes of one of the sub types
The table is called documents and is a table of document_ot
Definition:
create or replace
TYPE document_ot AS OBJECT (
-- ATTRIBUTES
created_date DATE,
created_by VARCHAR2( 32 ),
doc_id NUMBER,
-- CONSTRUCTORS
) NOT INSTANTIABLE NOT FINAL;
This type is extended by the type funding_certificates_ot
create or replace
TYPE funding_certificate_ot UNDER document_ot (
-- ATTRIBUTES
fc_prefix VARCHAR2( 2 ),
fc_sequence NUMBER,
fc_suffix VARCHAR2( 1 ),
fc_pr_id NUMBER,
-- CONSTRUCTORS
-- METHODS
) NOT FINAL;
I want to add a constraint to the to documents table that says that the funding_certificate_ot column fc_pr_id references pr_id in the table payruns.
I've managed to create an index on the fc_pr_id column using:
CREATE INDEX fc_pr_idx
ON documents d (treat (value(d) as funding_certificate_ot).fc_pr_id);
The code I thought I'd use for the foreign key constraint would be:
ALTER TABLE documents d
ADD CONSTRAINT doc_pr_fk FOREIGN KEY
treat (value(d) as funding_certificate_ot).fc_pr_id
REFERENCES payruns
(pr_id)
ENABLE;
But that doesn't work - would someone be able to give me the correct syntax please?
Many thanks -
Can't delete from parent table if FK constraint on the out-of-line table
Using Oracle XML DB 11g
I am using out-of-line storage table to store a collection of XML elements in conjuction with storeVarrayAsTable="true". So what I have is a parent table containing a nested table of VARRAY of REFs to rows in the out-of-line table.
In addition, I have a foreign key constraint placed on a column in the out-of-line table.
My problem is that I am not able to delete rows from the parent table when the FK constraint is placed on the out-of-line table. Only when I drop the FK constraint does the deletion work - all associated rows in the nested table and out-of-line table
gets deleted correctly.
With the FK constraint, deleting the child document like this
dbms_xdb.deleteResource('/project-1.xml')
gives me this error:
ORA-31018: Error deleting XML document
ORA-03001: unimplemented feature
*Cause: The XMLType object pointed to by the given REF could not
be deleted because either the REF was invalid or it pointed to a non-existent table.
*Action: Either use FORCE deletion or supply a valid REF.
I have tried deleting with the FORCE options as well with no success.
Using DBMS_XDB.DELETE_RECURSIVE_FORCE option doesn't produce an error, but doesn't perform the deletion either.
Here is the XML Schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xdb="http://xmlns.oracle.com/xdb" xmlns:xlink="http://www.w3.org/1999/xlink"
elementFormDefault="qualified" attributeFormDefault="unqualified" xdb:storeVarrayAsTable="true">
<xs:import namespace="http://www.w3.org/1999/xlink" schemaLocation="http://www.w3.org/1999/xlink.xsd"/>
<xs:element name="project" xdb:defaultTable="PROJECT_TAB">
<xs:complexType xdb:SQLType="PROJECT_TYP">
<xs:sequence>
<xs:element name="resourceList" minOccurs="0" xdb:SQLName="RESOURCE_LIST">
<xs:complexType xdb:SQLType="RESOURCE_LIST_TYP">
<xs:sequence>
<xs:element name="aResource" type="refType" minOccurs="0" maxOccurs="unbounded" xdb:SQLInline="false"
xdb:SQLName="A_RESOURCE_REF" xdb:defaultTable="RESOURCE_REF_TAB"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="aguid" type="xs:string" use="required" xdb:SQLName="AGUID"/>
</xs:complexType>
</xs:element>
<xs:element name="aResource" xdb:defaultTable="A_RESOURCE_TAB">
<xs:complexType xdb:SQLType="A_RESOURCE_TYP">
<xs:complexContent>
<xs:extension base="contactType">
<xs:attribute name="aguid" type="xs:string" use="required" xdb:SQLName="AGUID"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:complexType name="contactType" xdb:SQLType="CONTACT_TYP">
<xs:sequence>
<xs:element name="name" type="xs:string" xdb:SQLName="NAME"/>
<xs:element name="email" type="xs:string" minOccurs="0" xdb:SQLName="EMAIL"/>
<xs:element name="phone" type="xs:string" xdb:SQLName="PHONE"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="refType" xdb:SQLType="REF_TYP">
<xs:attribute ref="xlink:href" use="required"/>
<xs:attribute name="oref" type="xs:string" use="optional" xdb:SQLName="OREF"/>
</xs:complexType>
</xs:schema>
I registered the schema using structured storage with these options:
BEGIN
DBMS_XMLSCHEMA.registerschema(
SCHEMAURL => 'LSDProjects.xsd',
SCHEMADOC => xdbURIType('/home/LSDProject2/LSDProjects.xsd').getClob(),
LOCAL => TRUE, -- local
GENTYPES => TRUE, -- generate object types
GENBEAN => FALSE, -- no java beans
GENTABLES => TRUE -- generate object tables
END;
The PK and FK constraints were added like this:
-- Add PK constraints on aResource/@aguid attributes
ALTER TABLE A_RESOURCE_TAB ADD CONSTRAINT A_RESOURCE_AGUID_IS_UNIQUE UNIQUE (XMLDATA."AGUID");
-- Add FK constraint on out-of-line table
ALTER TABLE RESOURCE_REF_TAB
ADD (CONSTRAINT ref_resource_aguid_is_valid FOREIGN KEY (XMLDATA."OREF")
REFERENCES A_RESOURCE_TAB(XMLDATA."AGUID"));
Here are the XML instance documents:
where resource-1.xml looks like this :
<aResource aguid="resource-1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="LSDProjects.xsd">
<name>Jane Doe</name>
<email>[email protected]</email>
<phone/>
</aResource>
and project-1.xml looks like this :
<project aguid="project-1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="LSDProjects.xsd"
xmlns:xlink="http://www.w3.org/1999/xlink">
<resourceList>
<aResource xlink:href="/resource-1.xml" oref="resource-1"/>
</resourceList>
</project>
where <project> document contains a collection of <aResource> elements stored in the out-of-line table and
project/@oref attribute is a foreign key to the primary aResource/@aguid attribute.
Can someone shed some light on why I am unable to delete "project-1.xml" from the repository when the FK constraint is applied onto the out-of-line table.
Any advice/suggestions would be appreciated. Thanks!Thank you for the quick reply, mdrake.
I am currently prototyping and testing out (best) ways to achieve data integrity with XML data in XML DB.
I have instance documents that makes cross references to values in other instance documents
(not of the same root nodes). What I'm trying to enforce is a referential constraint between the documents
so that a document cannot be inserted if the referenced document doesn't exists.
In this situation, a <project> document contains a collection of elements with @oref attributes.
The collection is stored in an out-of-line table.
For example:
<project>
<resourceList>
<aResource ... oref="resource-1"/>
<aResource ... oref="resource-2"/>
</resourceList>
</project>
The @oref (FK) attribute above references a @aguid (PK) attribute in <aResource> document (below):
<aResource aguid="resource-1"> .... </aResource>
Basically, I want to ensure that the value of
/project/resourceList/aResource/@oref
correspond to a valid
/aResource/@aguid
Note that I was able to add the FK constraint on the OOL table without any issues.
And the constraint did work as expected as I was not allowed to insert a <project> document
that referenced a non-existent /aResource/@aguid.
The problem was that I could not delete the <project> document from the XML DB repository
once the FK contraint was added. Deleting with the DELETE_RECURSIVE_FORCE option did not give
me any errors - is just didn't do anything.
I guess there are certain limitations with using out-of-line tables.
BTW, sorry the examples are hard to read as the indentations are removed when posting. -
Using FOreign key constraints on tables in database.
I am student and novice in the field of ORACLE and PL/SQL and Database Creation. I had created a database consisting tables and got problem while applying foreign key constraints.
CUST_MSTR
CREATE TABLE "DBA_BANKSYS"."CUST_MSTR"("CUST_NO" VARCHAR2(10),
"FNAME" VARCHAR2(25), "MNAME" VARCHAR2(25), "LNAME" VARCHAR2(25),
"DOB_INC" DATE NOT NULL, "OCCUP" VARCHAR2(25), "PHOTOGRAPH" VARCHAR2(25),
"SIGNATURE" VARCHAR2(25), "PANCOPY" VARCHAR2(1), "FORM60" VARCHAR2(1));
(CUST_NO is PRIMARY KEY, )
-- EMP_MSTR
CREATE TABLE "DBA_BANKSYS"."EMP_MSTR"("EMP_NO" VARCHAR2(10),
"BRANCH_NO" VARCHAR2(10), "FNAME" VARCHAR2(25), "MNAME" VARCHAR2(25),
"LNAME" VARCHAR2(25), "DEPT" VARCHAR2(30), "DESIG" VARCHAR2(30));
(EMP_NO is primary key )
--NOMINEE_MSTR
CREATE TABLE "DBA_BANKSYS"."NOMINEE_MSTR"("NOMINEE_NO" VARCHAR2(10),
"ACCT_FD_NO" VARCHAR2(10), "NAME" VARCHAR2(75), "DOB" DATE,
RELATIONSHIP" VARCHAR2(25));
(NOMINEE_NO is primary key )
--ADDR_DTLS
CREATE TABLE "DBA_BANKSYS"."ADDR_DTLS"("ADDR_NO" NUMBER(6),
"CODE_NO" VARCHAR2(10), "ADDR_TYPE" VARCHAR2(1), "ADDR1" VARCHAR2(50),
"ADDR2" VARCHAR2(50), "CITY" VARCHAR2(25), "STATE" VARCHAR2(25),
"PINCODE" VARCHAR2(6));
( ADDR_NO is primary key )
Problem: I want to apply foreign key constraints on ADDR_DTLS table so that Before inserting value in ADDR_DTLS table it must check, VALUE in ADDR_DTLS.CODE_NO must be PRESENT either in attribute value CUST_MSTR.CODE_NO or EMP_MSTR.CODE_NO or NOMINEE_MSTR.CODE_NO table .
I applied the foreign key constraints using this syntax
CREATE TABLE "DBA_BANKSYS"."ADDR_DTLS"("ADDR_NO" NUMBER(6),
"CODE_NO" VARCHAR2(10), "ADDR_TYPE" VARCHAR2(1), "ADDR1" VARCHAR2(50),
"ADDR2" VARCHAR2(50), "CITY" VARCHAR2(25), "STATE" VARCHAR2(25),
"PINCODE" VARCHAR2(6),
constraints fk_add foreign key CODE_NO references CUST_MSTR. CODE_NO,
constraints fk_add1 foreign key CODE_NO references EMP_MSTR. CODE_NO,
constraints fk_add2 foreign key CODE_NO references NOMINEE_MSTR.CODE_NO);
(foreign key)
ADDR_DTLS.CODE_NO ->CUST_MSTR.CUST_NO
ADDR_DTLS.CODE_NO ->NOMINEE_MSTR.NOMINEE_NO
ADDR_DTLS.CODE_NO ->BRANCH_MSTR.BRANCH_NO
ADDR_DTLS.CODE_NO ->EMP_MSTR.EMP_NO
When I applied foreign key constraints this way, its gives a error called foreign key constraints violation. (I understand that, its searches the attribute value of ADDR_DTLS.CODE_NO in all the three tables must be present then the value will be inserted. But I want, if the value is in any of the three table then its should insert the value or its gives an error.)
Please help me out, though i put the question and i want too know how to apply the forign key in this way. and is there any other option if foreign key implementation is not pssible.If you are on 11g you can use ON DELETE SET NULL:
CREATE TABLE addr_dtls
( addr_no NUMBER(6) CONSTRAINT addr_pk PRIMARY KEY
, addr_cust_no CONSTRAINT addr_cust_fk REFERENCES cust_mstr ON DELETE SET NULL
, addr_emp_no CONSTRAINT addr_emp_fk REFERENCES emp_mstr ON DELETE SET NULL
, addr_nominee_no CONSTRAINT addr_nominee_fk REFERENCES nominee_mstr ON DELETE SET NULL
, addr_type VARCHAR2(1)
, addr1 VARCHAR2(50)
, addr2 VARCHAR2(50)
, city VARCHAR2(25)
, state VARCHAR2(25)
, pincode VARCHAR2(6) );In earlier versions you'll need to code some application logic to do something similar when a parent row is deleted, as otherwise the only options are to delete the dependent rows or raise an error.
btw table names can be up to 30 characters and don't need to end with MSTR or DTLS, so for example CUSTOMERS and ADDRESSES might be more readable than CUST_MSTR and ADDR_DTLS. Also if the Customer/Employee/Nominee PKs are generated from a sequence they should be numeric.
Edited by: William Robertson on Aug 15, 2010 6:47 PM -
Join 2 tables which are not related with any primary-foriegn key constraint
Hello,
How to join 2 tables which are not related with any primary key foreign key constraint.
Ex.Consider Table A has 5 columns->A_ID,A_Name,A_Address,A_City,A_Pin(Total 10 rows)
Table B has 5 columns->B_ID,B_Name,B_Adress,B_City,B_City(Total 30 rows)
From both the table i want the data,which i need to use in curosr to display finally as "Address Label".
Both the table are entirely different,but there can be some names of Table A which may come in Table B also,if the name,address and city is also same.That means same person in both the table.
So finally i want total number of distinct records(distinct data of Table A which is not in B+all the data of table B) to come under Address Label.
How can i write the select query for this condition?
Thanks
SwapnaHi, Swapna,
user11018268 wrote:
Hello,
How to join 2 tables which are not related with any primary key foreign key constraint.
Ex.Consider Table A has 5 columns->A_ID,A_Name,A_Address,A_City,A_Pin(Total 10 rows)
Table B has 5 columns->B_ID,B_Name,B_Adress,B_City,B_City(Total 30 rows)
Both the table are entirely different,but there can be some names of Table A which may come in Table B also,if the name,address and city is also same.That means same person in both the table.I think you want a UNION, not a join.
If you have 10 rows in table_a, and 30 rows in table_b, then
SELECT A_ID, A_Name, A_Address, A_City, A_Pin
FROM table_a
UNION
SELECT B_ID, B_Name, B_Adress, B_City, B_City
FROM table_b
ORDER BY 2, 4
;will produce up to 40 rows; 40 if there are no duplicates. (UNION implies DISTINCT.)
The corresponding column types should be similar, if not exactly the same.
There's no problem if A_Name is VARCHAR2 (50) and B_Name is CHAR (30).
However, there will be a problem if A_ID is a NUMBER and B_ID is a TIMESTAMP.
You must have the same number of columns in all branches of the UNION.
If you want an ORDER BY clause, put it at the very end, after the last branch of the UNION.
You can use positional notation (where 2 means the 2nd column) like I did, or you can use names from the first prong of the UNION (such as A_Name).
From both the table i want the data,which i need to use in curosr to display finally as "Address Label".
So finally i want total number of distinct records(distinct data of Table A which is not in B+all the data of table B) to come under Address Label.I'm not sure what your mean about "Address Label".
Whenever you have a question, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables, and the exact results (formatted) that you want from that data.
You can concatenate all 5 columns into one VARCHAR2 column, if you want to.
You'll probably want to use RPAD (or simillar functions) to make the output line up nicely.
If any of the columns are not strings, use TO_CHAR to make string versions of them.
For example:
SELECT TO_CHAR (A_ID, '9999999')
|| ' ' -- leave a little space between the left-justified a_id and the right-justified a_name
|| RPAD (A_Name, 25)
|| RPAD (A_Address, 50)
...Edited by: Frank Kulash on Nov 14, 2009 10:11 AM -
Confused by the removal of aggregate/index tables in HANA
Hiya,
I've heard that HANA removes the need for aggregate and index tables like BSIS, BSAS, VRPMA etc.
Is HANA smart enough to do this? Or does Simple finance (sFIN) do this?
I mean if we were to replace our Oracle DB with HANA would the index tables no longer exist?
Or would we then need to implement simple finance to then remove the need for index tables?
Thanks in advance!"HANA is just the DB."
I wouldn't say it's "just" a DB, but for the point we're making here you can say that SAP HANA is mainly used as a DBMS and not as a application platform.
"To remove index tables, aggregate tables etc you can activate business functions through SFW5 to improve specific lines of business."
That's not generally true. Some enhancements (like the VBOX removal) are delivered via enhancement packages. These change the software, but typically don't make a new product out of your system.
"Installation of sFIN, or S/4HANA contain more deeper changes to both the tables and ABAP code - that improve speed and efficiency."
Nope, these are actual new implementations. These are new programs that do similar things as the former NetWeaver programs. But they are newly written code.
This allows for much more radical redesign of functionality leading (hopefully) to even more improvements.
Coming back to your first sentence: the more radical the software changes the more tightly integrated the solution will be with SAP HANA. More and more function will actually run "in" SAP HANA, making it more than just the DB.
Ok, I hope this didn't increase the confusion again.
Cheers,
Lars -
SPLIT PARTITION creating the index in different Table Space
I am splitting the table every month. After splitting the Index is also Splitting. (It is natural). But it is creating in default table Space.
I want to split INDEX and creating into the index table space “ACTIVITY_IND_TS_1”
CREATE TABLE PART
PK_KEY NUMBER(15) NOT NULL,
PK_ID NUMBER(1) NOT NULL
TABLESPACE USERS PARTITION BY RANGE (PK_ID)
PARTITION PK_ID_PRD1 VALUES LESS THAN (1)
LOGGING
TABLESPACE ACTIVITY_TAB_TS_1,
PARTITION PK_ID_PRD2 VALUES LESS THAN (2)
LOGGING
TABLESPACE ACTIVITY_TAB_TS_1,
PARTITION PK_ID_PRD3 VALUES LESS THAN (3)
LOGGING
TABLESPACE ACTIVITY_TAB_TS_1,
PARTITION PK_ID_PRD4 VALUES LESS THAN (4)
LOGGING
TABLESPACE ACTIVITY_TAB_TS_1,
PARTITION PK_ID_PRD_MAXVALUE VALUES LESS THAN (MAXVALUE)
LOGGING
TABLESPACE ACTIVITY_TAB_TS_1
NOCACHE
NOPARALLEL
ENABLE ROW MOVEMENT;
CREATE INDEX PART_IX ON PART(PK_ID)
LOCAL (PARTITION PK_ID_PRD1 TABLESPACE ACTIVITY_IND_TS_1,
PARTITION PK_ID_PRD2 TABLESPACE ACTIVITY_IND_TS_1,
PARTITION PK_ID_PRD3 TABLESPACE ACTIVITY_IND_TS_1,
PARTITION PK_ID_PRD4 TABLESPACE ACTIVITY_IND_TS_1,
PARTITION PK_ID_PRD_MAXVALUE TABLESPACE ACTIVITY_IND_TS_1)
PARALLEL
NOLOGGING;
select unique tablespace_name from user_tab_partitions; -- ACTIVITY_TAB_TS_1
select unique tablespace_name from user_ind_partitions; -- ACTIVITY_IND_TS_1
ALTER TABLE PART SPLIT PARTITION PK_ID_PRD_MAXVALUE
AT (5) INTO (PARTITION PK_ID_PRD5, PARTITION PK_ID_PRD_MAXVALUE);
select unique tablespace_name from user_tab_partitions; -- ACTIVITY_TAB_TS_1
select unique tablespace_name from user_ind_partitions; -- ACTIVITY_TAB_TS_1 ACTIVITY_IND_TS_1
Edited by: sk123 on May 5, 2009 1:05 PMHello,
Once you split partition you need to rebuild indexes if you want indexes to be in their own tablespaces
ALTER TABLE PART
SPLIT PARTITION PK_ID_PRD_MAXVALUE AT
(6)
INTO (PARTITION PK_ID_PRD5
TABLESPACE ACTIVITY_TAB_TS_1
PARTITION PK_ID_PRD_MAXVALUE);
ALTER INDEX PART_IX
REBUILD PARTITION PK_ID_PRD5
TABLESPACE ACTIVITY_IND_TS_1;
ALTER INDEX PART_IX
REBUILD PARTITION PK_ID_PRD_MAXVALUE
TABLESPACE ACTIVITY_IND_TS_1;Regards -
How do I create a target table with the same PK as the source table?
I am trying to create a target table in a mapping that will end up with the same primary key as the source table.
It is a simple map that simply uses a subset of the columns of the source table in the target table. I was wanting to create and bind a new table by dragging the columns I want from the source to the initially blank target table operator, change the column names and create a primary key to match the source table.
I can't seem to be able to create a constraint on the table in the mapping. I can create the constraint after the table is created and boound to the database object but the PK doesn't carry back into the mapping.
I need it in the mapping so I can use the UPDATE/INSERT operation and use the 'All Constraints' implementation. The mapping won't let me validate the object without the PK on it in the map.
Believe it or not folks, I am getting better at this.
Thanks very much for the guidance.
GaryHi Gary
You are close, you are really close... :-))
You need to do exactly as you propose plus one extra step. Build the map as you describe, binding the new table to the target. Then you edit the table definition to add the primary key and any other constraints you need. After this is the step that you are missing.
You need to do the following:
1. Go back and re-edit the map
2. Right click on the table
3. From the pop up menu, select Reconcile Inbound
4. Set any operators that you need for the UPDATE/INSERT
5. Save the map
6. Commit your changes
The first three steps above make the map read in the indexes and constraints that you set on the table. Finally, you need to deploy the table and then deploy the map.
Hope this helps
Regards
Michael
Maybe you are looking for
-
HT204085 How can i use iCloud service without providing credit card information on my iphone
Sir I have no crdit card I want to use iCloud and iTunes service please help me
-
I have a set of cloned APEX servers, how should I get them working?
Hey there, I have been asked to configure a set of servers which are clones of an existing APEX environment and get them working as a separate development environment. This APEX setup has three servers which are all running RedHat Linux: a single sig
-
WHY DO I HAVE SO MUCH ' INACTIVE ' MEMORY?????
i was just looking at the activity monitor seeing how much memory my applications were using up and i noticed a hugeee amount of inactive memory... at the time i had thses applications running: toast titanium PowerPC azureus Intel msn messenger Power
-
Upgrade issue from 11.5.3300 to 11.5.3700
I currently have Crystal Reports Server 11.5.3300.0, and was looking to upgrade to 11.5.3700.0. When I login to the support site, I only see the full download for 11.5.3700. Is there a Serivce Pack or Fix Pack that will allow the upgrade?
-
IPOD touch/games cannot be synchronized
I just bought a game for iPOD touch, which is for iPhone and iPOD touch compatible. My iPOD has the newest version update (2.2.1) and I'm not able to synchronize the game. error message: iPOD is not activated to synchronize this game. How can I activ