Primary key and Foreign key on same column
Hi, I am able to create below tables , primary and foreign keys as below.
But Is this valid design? Can I define a column (ckey2 in table "child") as
primary key and as well as foreign key?
CREATE TABLE parent (
col1 NUMBER,
col2 NUMBER
CREATE TABLE child (
ckey1 NUMBER,
ckey2 NUMBER,
ckey3 NUMBER
alter table parent add constraint parent_pk primary key( col1 );
alter table child add constraint child_pk primary key( ckey2 );
alter table child add constraint child_fk foreign key( ckey2 ) references parent( col1);
Thanks.
Can I define a column (ckey2 in table "child") as primary key and as well as foreign key?You mean you want to define a one-to-one relationship between parent and child tables.. why would you want to do that ? You might as well merge the 2 tables into one.
Similar Messages
-
No matching unique or primary key for this column-list --error
CREATE TABLE CUSTOMER(CUS_NUM NUMBER(10) PRIMARY KEY,CUS_NAME VARCHAR2(50),ADDRESS VARCHAR2(50));
CREATE TABLE ITEM(ITEM_NUM NUMBER(10),ITEM_DESC VARCHAR2(50),TYPE VARCHAR2(50),ITEM_STATUS VARCHAR2(50),
CONSTRAINT PK_ITEM PRIMARY KEY(ITEM_NUM,TYPE));
CREATE TABLE ITEM_TYPE(TYPE VARCHAR2(50) PRIMARY KEY,TYPE_NAME VARCHAR2(50),SKU VARCHAR2(50),RATING VARCHAR2(50));
CREATE TABLE ORDERS(ORDER_NUMBER NUMBER(10),CUS_NUM NUMBER(10),ITEM_NUM NUMBER(10),ORDER_POINT VARCHAR2(50),DISPATCH_POINT VARCHAR2(50),
PRIORITY VARCHAR2(50),ORDER_STATUS VARCHAR2(50),
CONSTRAINT PK_ORDERS PRIMARY KEY(ORDER_NUMBER,ITEM_NUM),
CONSTRAINT FK1_ORDERS FOREIGN KEY (CUS_NUM) REFERENCES CUSTOMER(CUS_NUM),
CONSTRAINT FK2_ORDERS FOREIGN KEY (ITEM_NUM) REFERENCES ITEM(ITEM_NUM));
Hi,friends i created tables as above when i creating 'ORDERS' table the error will fir as "ORA-02270: no matching unique or primary key for this column-list".
can any body solve this issue.
Cheers,
Sudhir reddy.For your own sanity, you should learn to format your code, or use a tool to do it for you. For the sanity of the forum members, you should preserve that code using the \ tagsCREATE TABLE CUSTOMER
CUS_NUM NUMBER(10) PRIMARY KEY,
CUS_NAME VARCHAR2(50),
ADDRESS VARCHAR2(50)
CREATE TABLE ITEM
ITEM_NUM NUMBER(10),
ITEM_DESC VARCHAR2(50),
TYPE VARCHAR2(50),
TYPE is a reserved word. Call it ITEM_TYPEITEM_STATUS VARCHAR2(50),
CONSTRAINT PK_ITEM PRIMARY KEY(ITEM_NUM,TYPE)
CREATE TABLE ITEM_TYPE
TYPE VARCHAR2(50) PRIMARY KEY,
again, with the reserved word.TYPE_NAME VARCHAR2(50),
SKU VARCHAR2(50),
RATING VARCHAR2(50)
CREATE TABLE ORDERS
ORDER_NUMBER NUMBER(10),
be consistent. NUM or NUMBER. Pick one and stick with it.CUS_NUM NUMBER(10),
ITEM_NUM NUMBER(10),
ORDER_POINT VARCHAR2(50),
DISPATCH_POINT VARCHAR2(50),
PRIORITY VARCHAR2(50),
ORDER_STATUS VARCHAR2(50),
CONSTRAINT PK_ORDERS PRIMARY KEY(ORDER_NUMBER,ITEM_NUM),
CONSTRAINT FK1_ORDERS FOREIGN KEY (CUS_NUM) REFERENCES CUSTOMER(CUS_NUM),
CONSTRAINT FK2_ORDERS FOREIGN KEY (ITEM_NUM) REFERENCES ITEM(ITEM_NUM) -
Primary Key With Four Column ,How to use that Column As Primary Key In Form
Dear All,
i have create composite Key as primary Key With Four Column .
How can i use these column in Form With Report Page.
How can i use these four Column as primary Key to Edit Record .
How can i do this.
Thanks
Vedantwrap a view around your table with this bogus primary key. Use an instead of trigger to handle the inserts and updates. And then look at using surrogate keys to resolve these bad design issues...
5 out of 4 people don't understand fractions.
Thank you,
Tony Miller
Webster, TX -
Primary key for an column consisting duplicates
hi,
------->i have created a table and
-------> i have a column consisting of 1000 records (but where i have duplicates)
and now i want to create a primary key for the column
how can i do it....Hi,
You can find records which contains duplicate values for the table column using Oracle exceptions table. Please see a small demonstration:-
SQL> create table test1(id number);
Table created.
SQL> insert into test1 values(&id);
Enter value for id: 1
old 1: insert into test1 values(&id)
new 1: insert into test1 values(1)
1 row created.
SQL> /
Enter value for id: 2
old 1: insert into test1 values(&id)
new 1: insert into test1 values(2)
1 row created.
SQL> /
Enter value for id: 3
old 1: insert into test1 values(&id)
new 1: insert into test1 values(3)
1 row created.
SQL> /
Enter value for id: 1
old 1: insert into test1 values(&id)
new 1: insert into test1 values(1)
1 row created.
SQL> /
Enter value for id: 3
old 1: insert into test1 values(&id)
new 1: insert into test1 values(3)
1 row created.
SQL> /
Enter value for id: 4
old 1: insert into test1 values(&id)
new 1: insert into test1 values(4)
1 row created.
SQL> /
Enter value for id: 5
old 1: insert into test1 values(&id)
new 1: insert into test1 values(5)
1 row created.
SQL> commit;
Commit complete.
SQL> alter table test1 add constraint id_pk primary key(id);
alter table test1 add constraint id_pk primary key(id)
ERROR at line 1:
ORA-02437: cannot validate (SYS.ID_PK) - primary key violated
SQL> alter table test1 add constraint id_pk primary key(id) exceptions into exceptions;
alter table test1 add constraint id_pk primary key(id) exceptions into exceptions
ERROR at line 1:
ORA-02445: Exceptions table not found
SQL> @?/rdbms/admin/utlexcpt
Table created.
SQL> alter table test1 add constraint id_pk primary key(id) exceptions into exceptions;
alter table test1 add constraint id_pk primary key(id) exceptions into exceptions
ERROR at line 1:
ORA-02437: cannot validate (SYS.ID_PK) - primary key violated
SQL> desc exceptions
Name Null? Type
ROW_ID ROWID
OWNER VARCHAR2(30)
TABLE_NAME VARCHAR2(30)
CONSTRAINT VARCHAR2(30)
SQL> select * from exceptions;
ROW_ID OWNER TABLE_NAME CONSTRAINT
AAAc95AABAAA9EpAAD SYS TEST1 ID_PK
AAAc95AABAAA9EpAAA SYS TEST1 ID_PK
AAAc95AABAAA9EpAAE SYS TEST1 ID_PK
AAAc95AABAAA9EpAAC SYS TEST1 ID_PK
SQL> select * from test1 where rowid in(select row_id from exceptions);
ID
3
1
1
3
Thanks
Edited by: rarain on May 28, 2013 12:10 PM -
Table creation from type - Primary key with 2 columns
Hello,
I have to create a table from a type that I created.
The thing is that I want to create a primary key using two column.
Because this table is junction table.
How can I do this ?
CREATE TABLE CONTENT OF CONTENT_T (ID_COMP primary key, ID_CHAR_J NOT NULL PRIMAR
Y KEY);
Oracle return:
CREATE TABLE CONTENT OF CONTENT_T (ID_COMP primary key,ID_CHAR_J NOT NULL PRIMARY KEY)
ERROR at line 1:
ORA-02260: table can have only one primary key
So I tried:
CREATE TABLE CONTENT OF CONTENT_T (ID_COMP,ID_CHAR_J NOT NULL PRIMARY KEY)
ERROR at line 1:
ORA-02338: missing or invalid column constraint specification
Tahnk's in advance.
Edited by: user8266437 on 29 avr. 2009 13:56
Edited by: user8266437 on 29 avr. 2009 13:56SCOTT@orcl_11g> CREATE OR REPLACE TYPE content_t AS OBJECT
2 (id_comp NUMBER,
3 id_char_j VARCHAR2 (10),
4 other_cols VARCHAR2 (10));
5 /
Type created.
SCOTT@orcl_11g> CREATE TABLE CONTENT OF CONTENT_T (PRIMARY KEY (ID_COMP, ID_CHAR_J))
2 /
Table created.
SCOTT@orcl_11g> INSERT INTO content VALUES (content_t (1, 'A', 'WHATEVER'))
2 /
1 row created.
SCOTT@orcl_11g> COMMIT
2 /
Commit complete.
SCOTT@orcl_11g> INSERT INTO content VALUES (content_t (1, 'A', 'DUPLICATE'))
2 /
INSERT INTO content VALUES (content_t (1, 'A', 'DUPLICATE'))
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C0062057) violated
SCOTT@orcl_11g> COMMIT
2 /
Commit complete.
SCOTT@orcl_11g> SELECT * FROM content
2 /
ID_COMP ID_CHAR_J OTHER_COLS
1 A WHATEVER
SCOTT@orcl_11g> -
How to create a primary key by 2 columns in sql??
as title
thx a lot.Chris,
Well you have two basic ways. One is to issue an alter table command and define a PRIMARY KEY (also called a PK). The other is to create a UNIQUE index. The PRIMARY KEY creates an index anyway, though I prefer to just use unique indexes as you can specify more options in this manner. A table can only have 1 PK where as you can have multiple UNIQUE indexes. I also do not combine the PK definition directly into the CREATE TABLE command, as normally one would keep a seperate PK script, even if they did contain only unique indexes commands and not PK alter table commands.
Anyway, below are the two methods of making a PK/Unique object for your table. The table name used here is T1 ...
Method #1 (Primary Key)
ALTER TABLE T1 DROP PRIMARY KEY;
ALTER TABLE T1 ADD PRIMARY KEY
(Column_1,Column_2, ...)
STORAGE
NEXT 512K
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
Method #2 (preferred) : Unique Index
DROP INDEX PK_T1;
CREATE UNIQUE INDEX PK_T1 ON T1
(Column_1,Column_2, ...)
TABLESPACE INDX
PCTFREE 10
STORAGE
INITIAL 1M
NEXT 512K
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
It's always best to define the storage clause for both the create table and it's indexes.
Hope this helps ya,
Tyler -
Making Primary Key on Null Column !
I am not happy the way Oracle treat adding constraint on existing table.
I have one column initially accept NULL Values. after I import the data into this table. this column does not have any NULL value. I want to make this column as Primary Key. How can I do this ?
In SQL Server you can make any column PK any time as long as long it is withen the critaria of PK.
Now whenever in Oracle I try to make one column PK , It gives me error that cannot modify a column from NULL to NOT NULL. Even though there is no NULL value.
I give you a scenerio:
I have 1000s of records in a flat file, Now I donot know if some field is null or not. so I initially make all columns to accept NULL value in Oracle table. After I import that data , I delete the rows where value is NULL. Now I want to make this field as PK. But Oracle will not let me do it. why??????????????
What can I do to modify one NULL column to PK even though there is no NULL value.
Thanks again for your feedback.
Abid MalikPerhaps I'm missing something, but it works for me
1 create table t1 (
2 col1 NUMBER,
3 col2 NUMBER
4* )
SQL> /
Table created.
SQL> insert into t1 values( 1, 2 );
1 row created.
SQL> commit;
Commit complete.
SQL> alter table t1
2 add constraint t1_pk PRIMARY KEY( col1 );
Table altered.Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
How to show two amount (based on value type and entry code) in same column
Hi, I have to show two amount in the same column how can i do that...
For example there is
1) premium amount which depends upon the entry code lets say entry code 1000 = Premium amount .
2) Now there is another premium amount but this amount is stored on basis of value type ...let us say for value type 'prem' = premium amount .
these amount are in different field can i shown it in same column in BEx query.
If you can you please help .use the selection criteria to restrict the field types in the query designer- use other variables to check the value
if it doesnt reconcile- you can alternatively use a multi provider for the same thing- that way you will get the same amount in 1 column and you could restrict using the infoprovider column -
How to display integer values and decimal values in same column in a table
hi
in my report amount column is there in that column integer values (2234) and decimal values (3562.34)
i want to print this values as 1000 separate as like 2,234 as integer and 3,562.34 as decimal
but this values print like 2,234.00 as integer and 3,562.34 as decimalHi akilreddy,
Per my understanding you have an field which data type may be "float" in the DB, so it have value which format like "2234" and "3562.34" in the same column, now you want to format them differently in the report, right?
I have tested on my local environement and you can use the expression in the custom format in the Textbox properties to do this:
Right click the field which you want to add the format and select the "Text Box Properties"
Select the "Number" on the left pane and using expression below in the Custom format:
=IIF(Split(Fields!yourfieldname.Value,".").Length=2,"#,###.##",Nothing)
Or you can just use "#,###.##" in the format.
Preview you will go the result like below:
If you still have any problem, please feel free to ask.
Regards
Vicky Liu
Vicky Liu
TechNet Community Support -
Received and Issued quantity in same column in PLD.
Hello All,
I've created PLD for Inventory Valuation method, the problem is both the received quantity and issued quantity are display in the same column.
But I want it in different columns, that is received quantity in one column and issued quantity in another column. Please suggest how to rectify it in the PLd.
Thanks,
PratheevirajHi Pratheeviraj,
In the PLD, create 2 formula field in the repetitive area -
1. Field_141 > 0
2. Field_141 < 0
where Field_141 refers to the Quantity field.
3. Link the Quantity field to formula field 1 above. This way, the quantity field will show only the >0 quantity
4. Duplicate the quantity field. Set it as a separate column and link it to the <0 formula field.
Now this column will show qty > 0
Rename the column headings acc.
Hope this helps.
Regards,
Hamsa -
Mix AVG and Total for the same column in ALV grid
Hi,
We are using CL_GUI_ALV_GRID=>SET_TABLE_FOR_FIRST_DISPLAY, and I'd like to know if the following situation is possible.
Let's say we have a column which contains a number of hours, and we set it up to subtotal by period, but as an average rather than a total (DO_SUM = "C"). This is not a problem so far.
The problem is that we'd like for the "Grand Total" at the bottom of the ALV grid to total all of the subtotal averages, instead of calculating an average.
Ex:
Period Hours
2010/01 6.0
2010/02 7.0
2010/03 2.0
5.0 (This is an average... 6.0 + 7.0 + 2.0 / 3)
2011/01 12.0
2011/02 5.0
2011/03 4.0
7.0 (This is an average... 12.0 + 5.0 + 4.0 / 3)
12.0 (We want this to be a total of 5.0 + 7.0)
So we want the "grand total" line to be 12.0 which is the sum, but it currently comes out as an average.
Therefore we're looking to average at the sub total level, but we want a total of the subtotal levels at the grand total level for the same column.
Thanks,
DenisRefer to the suggestion given by Naimesh here Can we modify a sub-total in ALV making use of function GET_GLOBALS_FROM_SLVC_FULLSCR and the method GET_SUBTOTALS. You can see the parameter ep_collect01 refered in case of subtotals. Can you please check ep_collect00 for grand total in your case.
As said about EP_COLLECT00,here goes a classic example Modify grand total in ALV GRID
BR
Keshav
Edited by: Keshav.T on Sep 21, 2011 9:39 AM -
Query to display integer and decimal and integer in the same column
Hello ,
Have a question,
is there a easier way to do this.. is the question
i have a column called units.. and i need to display either a integer in it if the value does not have any value after the decimal point .. so it shud be display as integer. But if it is a float then only display it with decimal values
like
Units
100
.123
1000.55
2000Check this out although formatting needs to be done in the client application:
DECLARE @t TABLE (units FLOAT)
INSERT INTO @t (units)
VALUES (100)
,(.123)
,(1000.55)
,(2000)
SELECT units
,convert(VARCHAR(30), units) AS DisplayOnly
FROM @t
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles -
Sorting Alpha and numerics in the same column
Post Author: Jimbob
CA Forum: General
I need to sort a column first by alpha then numerics. If I use the ascending sort, the records appear in the following order:
01, 02, AA, BB, ZZ
If I use the descending sort option, I get the following:
ZZ, BB, AA, 02, 01
What I need is the following order:
AA, BB, ZZ, 01, 02, 03
Any suggestions are greatly appreciated!
Thanks.Post Author: rickcf
CA Forum: General
What if you create a non printing sort variable field and append a 'Z' to the beginning of only the numeric fields.
IF Field is Numeric then append Z else just use field.
Then you sort ascending on the variable sort field instead of just the field itself. Im am not sure if it would put ZZ above Z01 but you can always add more Z's or play with it till it works.
Rick
Rick -
$ and % signs in a same column
Hi all,
I have this requirement in which my report is full of facts.
http://i422.photobucket.com/albums/pp302/Seetharam_photos/screen.gif
I'm able to achieve this using Union of three requests.
Now the problem is that, I need to display $ sign for the first two rows and % sign for the third row.
I tried changing the data format under column properties to display the respective signs but it gets applied to one column as a whole, which is not desired in my case.
So i tried changing data format in each n every request and since i'm using union, i'm not able to do tat.
Any workarounds to achieve it..?Hi Seetharam,
I want to confirm one thing from you... Income, Expenditure . Are these column headings or values?
If headings the...
It seems you should achieve this with Pivot view...
And need to drag Measure Labels into Rows block
And you need to apply data format of that column as $ or % whatever you want..
As you are saying you did union this would be done in Results set of criteria...
May helpful to you... -
The column in the table do not match an existing primary key
I've got two tables tbl_Workshop and tbl_Material
tbl_Workshop has columns workshopID, workshopTitle, materialID
tbl_Material has materialID, name, workshopTitle
when I'm trying to create a relationship between the workshopTitle of tbl1 and tbl2, it gives me an error that says the column in the table do not match an existing primary key.
What could be the reason for this error and how to overcome it.
ps. The datatypes and names of both the table's column match.Have you created primary key on workshopTitle column in tbl_Workshop
You can add foreign key relationship from tbl_Material.workshopTitle
to tbl_Workshop.workshopTitle
only if latter is a primary key of the table.
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs
Maybe you are looking for
-
I cant download apps to my ipod because it says something to do with my time
I go to the computer to fix this problem , & it says i owe money , but i need to switch debit cards , because i cant use that one that i regerstered with .. & everything is messed up .
-
Setting up an iphone on behalf of someone else. iPhone has never been backed up and has never been synced to another computer. I have not found any clear answers on the internet so far. Any response will be appreciated.
-
Change the report type.
I'm using a report for determine the top applications. How can modify the report to get session count and bytes trasmitted on the same report? Thanks, and happy new year! Andrea.
-
Google earth wont download or deleteon ipad
My Google app would not open and load properly. It would open but nothing was on the screen. So it thought well I'll just delete and download it again from the apps store. I went to download and it has frozen. It says waiting.... It has been thi
-
Hello Exports, When i do BDC for f-58 it is working till save, The moment i save the transaction BDC complete. but it is unable to capture doc no generated. So that i am unable to create check. pls can anybody help me in this. Thanks Murali