-
Different data type for primary and foreign keys
Is it possible that Primary key and foreign keys hav diff. data type?
if No then is below eg valid...what length of deptid is diff in both the tableseg.emp table having (empid varchar2(10),deptid varchar2(25)) --deptid is foreign key dept table having (deptid varchar2(10),deptname varchar2(20) --deptid is primary key
Question asked by visitor sant
-
Expert Member
Re: Different data type for primary and foreign keys
Varchar2 data type does pad values with blank spaces. If number of digits is same then it does not matter.
-
Re: Different data type for primary and foreign keys
Yes of course they can have different data types in a table.
But the foreign key and the PK or unique key of the referenced table should have same datatype.
-
Expert Member
The nth FOREIGN KEY column corresponds to the nth column in the primary key of the REFERENCES table, and the data types and lengths of corresponding columns must be identical. Columns may not be used more than once in the same FOREIGN KEY clause.
Example
create table customer(
id varchar2(6) primary key,
NAME VARCHAR2(15 BYTE))
create table account (
id varchar2(5) references customer(id),
type VARCHAR2(20 BYTE)
)
While inserting id(same as custormer(id)) into account we will get an error
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules