Results 1 to 4 of 4

Thread: Different data type for primary and foreign keys

  1. #1
    Geek_Guest
    Guest

    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


  2. #2
    Expert Member
    Join Date
    Sep 2007
    Answers
    697

    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.


  3. #3
    Moderator
    Join Date
    Jun 2007
    Answers
    2,074

    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.


  4. #4
    Expert Member
    Join Date
    Apr 2007
    Answers
    500
    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
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact