Create Primary key on View?
Hi
Can we create a Primary or Unique key on a simple view? while actual table doesn't have any primary or unique key
Regards,
Nasir.
But what about this Example.
CREATE VIEW emp_sal (emp_id, last_name,
email UNIQUE RELY DISABLE NOVALIDATE,
STRAINT id_pk PRIMARY KEY (emp_id) RELY DISABLE
NOVALIDATE)
AS SELECT employee_id, last_name, email FROM
employees;This creates a view and tells Oracle that the optimizer is free to rely on email and emp_id being unique but that your code will enforce both constraints. Since you know that your data violates these constraints, that basically means that you're lying to the optimizer and allowing it to make incorrect assumptions about the data. That, in turn, permits the optimizer to either generate incorrect query plans or deliver you incorrect data should it happen to rely incorrectly on one of these constraints. Realistically, this sort of view would take an existing data quality issue and layer on a bunch of optimizer issues rather than addressing the root cause and fixing the data.
Justin
Similar Messages
-
Primary Key Materlized View and primary key in master tables
We are using Oracle 11g2 and I created a materlized view on several master tables. The master tables all have primary key, but I did not include all primary key columns in the defining query.
According to Oracle Manual e10592 Specify WITH PRIMARY KEY to create a primary key materialized view. This is the default ....
The master table must contain an enabled primary key constraint, and the defining query of the materialized view
must specify all of the primary key columns directly.My MV created OK and works. My question is what can be the impact on my MV? Could it hit the performance. It takes 40 minutes for the MV to be created and 5 minutes to refresh for single row insert in each mater table. I would like to see it refresh in seconds.Retrieving rows from the mview is purely a function of the number of rows and indexing on the mview itself. Refreshing the mview wall time depends on the quality of the query used to refresh the mview. Whether you include PKs from the source table in the mview is of little, or no, consequence here. What matters is the execution path of the refresh query. Is there proper indexing on the underlying tables to support the joins. Did you use mlog$_s to enable fast refresh - are mlog$_s even usable for your mview. There are a number of possible causes to your problem and we do not have enough information to make an estimate of the problem.
BTW, if the mview could refresh in seconds w/o mlog$_s, you probably do not need an mview. -
What is the best practice for creating primary key on fact table?
what is the best practice for primary key on fact table?
1. Using composite key
2. Create a surrogate key
3. No primary key
In document, i can only find "From a modeling standpoint, the primary key of the fact table is usually a composite key that is made up of all of its foreign keys."
http://download.oracle.com/docs/cd/E11882_01/server.112/e16579/logical.htm#i1006423
I also found a relevant thread states that primary key on fact table is necessary.
Primary Key on Fact Table.
But, if no business requires the uniqueness of the records and there is no materilized view, do we still need primary key? is there any other bad affect if there is no primary key on fact table? and any benifits from not creating primary key?Well, natural combination of dimensions connected to the fact would be a natural primary key and it would be composite.
Having an artificial PK might simplify things a bit.
Having no PK leads to a major mess. Fact should represent a business transaction, or some general event. If you're loading data you want to be able to identify the records that are processed. Also without PK if you forget to make an unique key the access to this fact table will be slow. Plus, having no PK will mean that if you want to used different tools, like Data Modeller in Jbuilder or OWB insert / update functionality it won't function, since there's no PK. Defining a PK for every table is a good practice. Not defining PK is asking for a load of problems, from performance to functionality and data quality.
Edited by: Cortanamo on 16.12.2010 07:12 -
Do I need to Create Primary Key Class for Entity beans with Local interface
Do I need to Create Primary Key Class for Entity beans with Local interface?
I have created my entity bean with the wizard in Sun One Studio and it does not create a primary key class for you.
I thought that the primary key class was required. My key is made up of only one field.
Anyone know the answer.
Jimu dont need to create a primary key class for a entity bean.if ur table's primary key feild(int ,float) is a built in or primitive data type,then u dont need one.But if ur table has a primary key field which is non primitive (for example StudentID,ItemID etc)then u have to create a primary key class.
hope this helps :-) -
How to Create primary key index with duplicate rows.
Hi All,
While rebuilding an index on a table , I am getting error that there are duplicate rows in a table.
Searching out the reason led me to an interesting observation.
Please follow.
SELECT * FROM user_ind_columns WHERE table_name='SERVICE_STATUS';
INDEX_NAME TABLE_NAME COLUMN_NAME COLUMN_POSITION COLUMN_LENGTH CHAR_LENGTH DESCEND
SERVICE_STATUS_PK SERVICE_STATUS SUBSCR_NO_RESETS 2 22 0 ASC
SERVICE_STATUS_PK SERVICE_STATUS STATUS_TYPE_ID 3 22 0 ASC
SERVICE_STATUS_PK SERVICE_STATUS ACTIVE_DT 4 7 0 ASC
SERVICE_STATUS_PK SERVICE_STATUS SUBSCR_NO 1 22 0 ASC
SELECT index_name,index_type,table_name,table_type,uniqueness, status,partitioned FROM user_indexes WHERE index_name='SERVICE_STATUS_PK';
INDEX_NAME INDEX_TYPE TABLE_NAME TABLE_TYPE UNIQUENESS STATUS PARTITIONED
SERVICE_STATUS_PK NORMAL SERVICE_STATUS TABLE UNIQUE VALID NO
SELECT constraint_name ,constraint_type,table_name,status,DEFERRABLE,DEFERRED,validated,index_name
FROM user_constraints WHERE constraint_name='SERVICE_STATUS_PK';
CONSTRAINT_NAME CONSTRAINT_TYPE TABLE_NAME STATUS DEFERRABLE DEFERRED VALIDATED INDEX_NAME
SERVICE_STATUS_PK P SERVICE_STATUS ENABLED NOT DEFERRABLE IMMEDIATE VALIDATED SERVICE_STATUS_PK
1. Using index scan:
SELECT COUNT (*)
FROM (SELECT subscr_no, active_dt, status_type_id, subscr_no_resets
FROM service_status
GROUP BY subscr_no, active_dt, status_type_id, subscr_no_resets
HAVING COUNT (*) > 1) ;
no rows returned
Explain plan:
Operation OBJECT Name ROWS Bytes Cost OBJECT Node IN/OUT PStart PStop
SELECT STATEMENT Optimizer MODE=CHOOSE 519 K 14756
FILTER
SORT GROUP BY NOSORT 519 K 7 M 14756
INDEX FULL SCAN ARBOR.SERVICE_STATUS_PK 10 M 158 M 49184
2. Using Full scan:
SELECT COUNT (*)
FROM (SELECT /*+ full(s) */ subscr_no, active_dt, status_type_id, subscr_no_resets
FROM service_status s
GROUP BY subscr_no, active_dt, status_type_id, subscr_no_resets
HAVING COUNT (*) > 1) ;
71054 rows returned.
Explain Plan:
Operation Object Name Rows Bytes Cost Object Node In/Out PStart PStop
SELECT STATEMENT Optimizer Mode=CHOOSE 1 24123
SORT AGGREGATE 1
VIEW 519 K 24123
FILTER
SORT GROUP BY 519 K 7 M 24123
TABLE ACCESS FULL ARBOR.SERVICE_STATUS 10 M 158 M 4234
Index SERVICE_STATUS_PK is a unique and composite primary key VALID index. And the constraint is ENABLED and VALIDATED still having duplicate rows in table.
How it is possible?
Is it an Oracle soft Bug??
Regards,
Saket Bansalsaket bansal wrote:
Values are inserted as single rows inserts through an GUI interface.And you still claim to have over 71K duplicate records, without the GUI getting any kind of errors?
That does not add up and can only be explained by a "bug".
I tried inserting a duplicate record but failed.
SQL> insert into service_status (select * from service_status where rownum <2);
insert into service_status (select * from service_status where rownum <2)
ERROR at line 1:
ORA-00001: unique constraint (ARBOR.SERVICE_STATUS_PK) violatedAre you really sure there is no other way data in this table is populated/manipulated in bulk? -
SQL Data Modeler 3.0 EA1 trying to create primary key on OT
Hi,
I'm trying to modify a primary key of an object table that was reversed (imported) from an Oracle 11.2.x RDBMS and am getting the error message "Incomplete Index definition". It is showing in the window "Index Columns Assignment". I get to the 'Primary Key Properties' window by pressing the 'Properties' button on the 'Table Properties' -> 'Primary Key'. If I try to edit the expression and then press the Ok or Apply buttons I get the error. With this be fixed in the production version? Will we be able to create a model in SQL Developer Data Modeler that generates a SQL statement similar to the following:
ALTER TABLE x_flags
ADD (CONSTRAINT x_flags_pk PRIMARY KEY (flags.id))
Thanks,
Scott KHi Scott,
I'm trying to modify a primary key of an object table that was reversed (imported) from an Oracle 11.2.x RDBMS and am getting the error message "Incomplete Index definition"... With this be fixed in the production version?yes
Will we be able to create a model in SQL Developer Data Modeler that generates a SQL statement similar to the following:
ALTER TABLE x_flags
ADD (CONSTRAINT x_flags_pk PRIMARY KEY (flags.id))I assume "flags" is column of object type and "id" is attribute of that type. I logged bug for that, so there will be support however not for 3.0.
Philip -
Primary key on view - ORA-20505
I'm getting this error when I try to update a form that is based on a view:
ORA-20505: Error in DML: p_rowid=69, p_alt_rowid=VENDOR_ITEM_ID, p_rowid2=, p_alt_rowid2=. ORA-01779: cannot modify a column which maps to a non key-preserved table
Error Unable to process row of table SH_PURCHASING_VW.
OK
Can you tell me how to add a pk to this view? I'd like to use VENDOR_ITEM_ID or a completely new column.
CREATE OR REPLACE FORCE VIEW "SH_PURCHASING_VW" ("ITEMNO", "VENDNO", "ITEM_DESC", "VEND_NAME", "PRIORITY", "VEND_ITEMNO", "VEND_ITEM_NOTES", "VENDOR_ITEM_ID", "VENDOR_ID", "VEND_NOTES", "ITEM_NOTES_ID", "ITEMNO_N", "ITEM_NOTES") AS
select "SH_VENDOR_ITEM"."ITEMNO" as "ITEMNO",
"SH_VENDOR_ITEM"."VENDNO" as "VENDNO",
"SH_VENDOR_ITEM"."ITEM_DESC" as "ITEM_DESC",
"SH_VENDOR_ITEM"."VEND_NAME" as "VEND_NAME",
"SH_VENDOR_ITEM"."PRIORITY" as "PRIORITY",
"SH_VENDOR_ITEM"."VEND_ITEMNO" as "VEND_ITEMNO",
"SH_VENDOR_ITEM"."VEND_ITEM_NOTES" as "VEND_ITEM_NOTES",
"SH_VENDOR_ITEM"."VENDOR_ITEM_ID" as "VENDOR_ITEM_ID",
"SH_VENDOR"."VENDOR_ID" as "VENDOR_ID",
"SH_VENDOR"."VEND_NOTES" as "VEND_NOTES",
"SH_ITEM_NOTES"."ITEM_NOTES_ID" as "ITEM_NOTES_ID",
"SH_ITEM_NOTES"."ITEMNO_N" as "ITEMNO_N",
"SH_ITEM_NOTES"."ITEM_NOTES" as "ITEM_NOTES"
from "SH_ITEM_NOTES" "SH_ITEM_NOTES",
"SH_VENDOR" "SH_VENDOR",
"SH_VENDOR_ITEM" "SH_VENDOR_ITEM"
where "SH_VENDOR_ITEM"."VENDNO"="SH_VENDOR"."VENDNO"
and "SH_VENDOR_ITEM"."ITEMNO"="SH_ITEM_NOTES"."ITEMNO_N"
ORDER BY VENDOR_ITEM_ID
Thanks in advance!Here are the updateable columns
SPICE_HUNTER1 SH_PURCHASING_VW ITEMNO NO NO NO
SPICE_HUNTER1 SH_PURCHASING_VW VENDNO NO NO NO
SPICE_HUNTER1 SH_PURCHASING_VW ITEM_DESC NO NO NO
SPICE_HUNTER1 SH_PURCHASING_VW VEND_NAME NO NO NO
SPICE_HUNTER1 SH_PURCHASING_VW PRIORITY NO NO NO
SPICE_HUNTER1 SH_PURCHASING_VW VEND_ITEMNO NO NO NO
SPICE_HUNTER1 SH_PURCHASING_VW VEND_ITEM_NOTES NO NO NO
SPICE_HUNTER1 SH_PURCHASING_VW VENDOR_ITEM_ID NO NO NO
SPICE_HUNTER1 SH_PURCHASING_VW VENDOR_ID NO NO NO
SPICE_HUNTER1 SH_PURCHASING_VW VEND_NOTES NO NO NO
VENDOR_ID and ITEM_ID are the pks for those tables. I'm guessing I need to add a VENDOR_ID and an ITEM_ID to the VENDOR_ITEM table, then write a couple of update statements to get those fields popluated with their respective data. I'm reading up on 'key-preserved' tables and it's making sense. You will probably laugh, but I AM the "DBA." I'm actually the report developer for our company and we're trying out APEX to see if we can use it to redo a bunch of old poorly-designed Access databases. I'm flying by the skin of my teeth, the seat of my pants, and a bunch of deep sighs. I went through the basic APEx training about a year ago, am designing tables as I go only through my knowledge of query/report writing and keep coming up against things in APEX that keep me saying, "Huhhhhhhh?"
Our purchasing department has an old poorly designed Access DB from which they can search vendors or items then see the results on one page. They need to be able to view the three areas of notes based on each search, i.e. they search by item then get this:
Item # and Description
Item Specific Notes Text Area
Vendor # and Name
Vendor Item Notes Text Area
Vendor Specific Notes Text Area
They search by Vendor they get this:
Vendor # and Name
Vendor Specific Notes Text Area
Item # and Description
Vendor Item NOtes Text Area
Item Speicfic Notes Text Area
Then, they need to be able to update those fields with any new information and that needs to update to their respective tables. I'm sure it's easy, and I'm sure it has to do with my LIMITED prowess of setting up my tables and learning that I can't have one form update more than one table (I learned that somewhere on this journey, hence the tries with the view) so I keep getting these ideas of how to do it, then running into snags.
So thanks for your help!
Alexandra -
Creating primary key dynamically ?
hi
I have say table t1 which has pk constraint say pk1 using say 4 columns c1,c2,c3,c4 in database1.
I need to create same pk constraint with same parameters and name on same table t1 which is into another database.
I need a generic function / proc to do this as the tables can change and so as number of columns used in the primary key constraint .
How do I go about this ?I need a generic function / proc If I may say so, this is completely the wrong way to manage change in the database. What you really need is a configuration management procedure that checks change scripts into a source control repository and makes sure they get run against all the affected databases.
Cheers, APC -
MSSQL2000 to 8i hangs on Create Primary Keys
I am trying to create the repository on an Oracle 8i DB coming from SQL Server 2000. The process runs fine for awhile, but when it gets to where it is done creating the info for primary keys it hangs and I have to cancel the process. Any ideas on what to check?
Problem seems to be related to having dbo as the table owner. The product was not able to write the table info with dba as the owner. Once I changed this to something else, it worked fine.
-
Hi,
Iam trying to create a Primary Index on Large table and Iam using the Parallelism to make it fast. I have used the following script to create a Primary key Index.
CREATE UNIQUE INDEX FLX_ARCH.FUND_SEC_RESULT_STAT_ARCH_PK
ON FLX_ARCH.FUND_SEC_RESULT_STAT_ARCH
(FUND_SEC_RESULT_MAP_INST,RATES_RULE_ID,BASE_LOCAL_FLAG,EFFECTIVE_DATE)
TABLESPACE FLX_PERF_I
parallel;
Index created.
Elapsed: 01:43:37.37
Index got created in 1 hour 43 minutes. But when Iam adding constarint to those columns, it is taking almost 12hours with or without parallelism.
ALTER TABLE FLX_ARCH.FUND_SEC_RESULT_STAT_ARCH
ADD CONSTRAINT FUND_SEC_RESULT_STAT_ARCH_PK
PRIMARY KEY
(FUND_SEC_RESULT_MAP_INST,RATES_RULE_ID,BASE_LOCAL_FLAG,EFFECTIVE_DATE)
parallel
Would you please let me know what could be the problem and how can we add the Primary constraint fast.
Your early response is much appreciated.
Thanks and Regards
Suresh. DWould you please let me know what could be the problem and You have presented no details that indicate any problem exists.
how can we add the Primary constraint fast. Fast is relative.
If I asked you to make my snail fast, could you do so?
ALTER SESSION SET SQL_TRACE=TRUE;
ALTER TABLE FLX_ARCH.FUND_SEC_RESULT_STAT_ARCH
ADD CONSTRAINT FUND_SEC_RESULT_STAT_ARCH_PK
PRIMARY KEY
(FUND_SEC_RESULT_MAP_INST,RATES_RULE_ID,BASE_LOCAL_FLAG,EFFECTIVE_DATE)
parallel
ALTER SESSION SET SQL_TRACE=FALSE;
now find the trace file within ./udump folder
tkprof <trace_file.trc> trace_results.txt explain=<username>/<password>
By inspecting the trace file you will be able to see where time is being spent.
With this knowledge, you may or may not be able to determine the bottleneck & therefore improve it -
Creating Primary key Index as Local Partitioned Index.
CREATE TABLE T1(
x NUMBER,
y NUMBER
)PARTITION BY LIST(x)
PARTITION P1 VALUES (1),
PARTITION P2 VALUES (2),
PARTITION Pmax VALUES (default)
ALTER TABLE T1 ADD CONSTRAINT T1_PK PRIMARY KEY(Y) LOCAL;
Error starting at line 1 in command:
ALTER TABLE T1 ADD CONSTRAINT T1_PK PRIMARY KEY(Y) LOCAL
Error report:
SQL Error: ORA-01735: invalid ALTER TABLE option
01735. 00000 - "invalid ALTER TABLE option"
*Cause:
*Action:
Can you please help me how to achieve this in Oracle 10g R2?LOCAL keyword is invalid with ALTER TABLEDepends:
SQL> create table t1 (x number, y number)
partition by list (x)
(partition p1
values (1),
partition p2
values (2),
partition pmax
values (default))
Table created.
SQL> alter table t1 add constraint t1_pk primary key(x, y) using index local
Table altered. -
Creating primary key constraint
hi,
if i have a table created, and i inserted say around 10 records ( there are no primary key or any other constraints ) , after inserting those 10 records now can i create a primary key constraint with enable novalidate option ?
thanksNo, you can create a PK Constraint which is initially disabled:
alter table t add constraint t_pk primary key(owner) disable;But when you try:
alter table t enable novalidate constraint t_pk;You will get a ORA-02437 Error if there are duplicat values (Oracle 10g).
Dim -
I want to insert HOSTID that has unique values in a new application in a primary key column in database.
I am starting with h000000001
and I want to go from that to h000000002
and .... h0000000010 , 11, 12 etc etc.
How do I do this?
I did this:
rs = stmt.executeQuery("select wlbmach_host_id from wlbmach order by wlbmach_host_id desc");
// I am getting the greatest value eg: h000999999
if (rs.next())
getHostID = (String)rs.getString("wlbmach_host_id");
//Get the h0009999999
getStringNumber = getHostID.substring(1,10);
//This gives me 9999999
try
{getNum = Integer.parseInt(getStringNumber);
//change number to integer so I can add 1 to it
getNum=getNum+1;
getStringNumber=Integer.toString(getNum);//make that a string
getHostID="h"+getNum;//add h to it
}//try
catch(Exception e)
{System.out.println("NumberFormatException might have occured");%>
else
System.out.println("Assigning first HostID");
getHostID = "h00000001";
System.out.println("HostID is "+getHostID);
But gives Exception:String index out of range: 10.. My column can take 10 varchar ??
Can someone help .. Is this the right way to do it ?
Is there a better way to generate primay keys?Why are you making life difficult??? Why not use the SEQUENCE object (if you are using Oracle)? You can retrieve next value by "Select sequencename.nextval from dual" as the query...you will get your number. Also, when two requests are made at the same time, Oracle takes care of this problem for you...you do not need to synchronize the method in JAVA. Do some research in this...I think you will like it.
-
Hi all.
This one I can't handle. Do you guys know where I can find this constraint?
SQL> desc marcos
Name Null? Type
COD VARCHAR2(40)
DSC VARCHAR2(30)
SQL> select * from marcos;
COD DSC
1 adfasdfas
2 asdfasfdas
3 asfasklfjd
1 safasjfasdf
SQL> alter table marcos
2 add (constraint pk_marcos primary key(cod))
3 /
alter table marcos
ERROR at line 1:
ORA-01408: such column list already indexed
SQL> select * from user_cons_columns
2 where upper(table_name) like '%MARCOS%'
3 /
no rows selectedTIA
MarcosEven you don't have bitmap index on it the query will still fail, you had duplicate record in this column :)
COD DSC
1 adfasdfas
2 asdfasfdas
3 asfasklfjd
1 << safasjfasdf -
Hello - New to Oracle and was wondering what is a good way to create an auto-incrementing primary key for a table. I have done this so far:
Created a new sequence cst_swq begin with 1 increment by 1.
CREATE OR REPLACE TRIGGER CST_TRIG
BEFORE INSERT
ON COOKIE_SALES
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT CST_SEQ.nextval INTO NEW.SALE_ID FROM dual;
This does not really work for me. Help with the code or a better idea to create primary key is much appreciated, thank you.adi26 wrote:
This does not really work for me. What does that mean?
In which way does this approch not meet your requirements?
BTW: after 200+ posts you should have learned how to use tags...
bye
TPD
Maybe you are looking for
-
Line 390 Under the (Insert into SALES_TRADEIN Table) I need to insert (TradeIn_1_VIN) Values If there is no values in (TradeIn_1_VIN) then i have to insert (TradeIn_2_VIN) values to the (SALES_TRADEIN) Table. After i run then below script only (Trade
-
How to I put a Windows 8.1 ISO file into a disk if I want to run Windows through Bootcamp
I have bought Windows 8.1 but I need to put the ISO onto a disk to install. How do I get the ISO and what should I do after that?
-
I can't open or save file with Java Web Start
Hi, i can't open or save file with Java Web Start: import java.io.*; import java.util.*; public class MetaDataFileCreator { public String fileNameSpace = null; public String fileName = null; protected Properties file = null; public MetaDataFileCreato
-
How the text elements of the SMARTFORMS can generate a field?
Hello guys I have got this problem of not getting the value of the fields from the smartform. i wrote the text element to populate the field by inserting as the following &IV_BID_HEADER-PROCESS_TYPE& first it comes with the shaded background bu
-
I have a few problems with my GUI design First, i have a JPanel added to the JScrollPane When I run it, the JPanel did not appear (but there is the border of JScrollPane) Then if i click the maximize screen button on top right corner, the JPanel show