Question

I'm trying to write 2 queries to create 2 tables. Both have :

One query:

   create table table1
    (ID integer NOT NULL IDENTITY(1,1),    
    Code varchar(50) NOT NULL,    
    Primary key(ID,Code));

second query:

 create table table2
    (ID integer NOT NULL IDENTITY(1,1),    
    Code varchar(50) NOT NULL,    
    Primary key(ID,Code)
    CONSTRAINT fkey FOREIGN KEY (Code) REFERENCES table2(Code));

But I want to make the foreign key to be only on the code. Is it possible, it didnt work when I tried. Thank you

Was it helpful?

Solution

Based on your edit, I'm thinking this is waht you're after?

CREATE TABLE table1
    (
      ID INTEGER NOT NULL
                 IDENTITY(1, 1) ,
      Code VARCHAR(50) NOT NULL ,
      PRIMARY KEY ( ID) , --code makes no sense in the Primary Key when ID is
      UNIQUE ( code )
    );


CREATE TABLE table2
    (
      ID INTEGER NOT NULL
                 IDENTITY(1, 1) ,
      Code VARCHAR(50) NOT NULL ,
      PRIMARY KEY ( ID, Code ) ,
      CONSTRAINT fkey FOREIGN KEY ( Code ) REFERENCES table1 ( Code )
    );

Code needs to be unique in the key table.

OTHER TIPS

I think what you want is this:

ID integer NOT NULL IDENTITY(1,1),    
Code varchar(50) NOT NULL,    
Primary key(ID),
Unique (Code)

That is, ID, by itself, is sufficient to identify a row in this table (by definition) and Code, by itself, is sufficient to identity a row in this table

I think you misunderstand what a foreign key is. Your primary key in both tables is the ID, if you want both to have code as foreign key you need a 3rd table containing all codes, using the code as a primary key.

Better would be to use a 3rd table with 2 fields, id and code and use this id as your foreign key in the other 2 tables.

You'll get something like this:

Table 1: ID - code_id - field1 - ...

Table 2: ID - code_id - field1 - ...

Table 3: ID - code

You have two columns(ID,Code) in 1st table and make them as a primary key. In 2nd Table you also make a primary key which contain two columns(ID,Code). Foreign key must have same number of columns as primary key in references table.

In your query you must have declare 2 columns in foreign key in 2nd table like.

    create table table1
    (ID integer NOT NULL IDENTITY(1,1),    
    Code varchar(50) NOT NULL,    
    Primary key(ID,Code));

--second query:

 create table table2
    (ID integer NOT NULL IDENTITY(1,1),    
    Code varchar(50) NOT NULL,    
    )
   ALTER TABLE table2
   ADD CONSTRAINT pk_table2_idCode PRIMARY KEY(ID,Code);  
    ALTER TABLE table2 
    ADD CONSTRAINT fkey FOREIGN KEY (ID,Code) REFERENCES table1(ID,Code);

Now if you want fk only on codes then you must follow given query

 create table table1
    (ID integer NOT NULL IDENTITY(1,1),    
    Code varchar(50) NOT NULL,    
    Primary key(Code));

--second query:

 create table table2
    (ID integer NOT NULL IDENTITY(1,1),    
    Code varchar(50) NOT NULL,    
    )
   ALTER TABLE table2
   ADD CONSTRAINT pk_table2_idCode PRIMARY KEY(Code);  
    ALTER TABLE table2 
    ADD CONSTRAINT fkey FOREIGN KEY (Code) REFERENCES table1(Code);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top