Problem with table creation with more foreign keys

hi

I am creating sql table with 3 foreign keys… But sql server is not allowing me to create table…

table1

col1 primary key

table2

col2 primary key

table3

col3 primary key

table4

create table table4(
col4 int not null,
col5 varchar(5) not null,
col6 varchar(4) not null,
foreign key(col4) references table1(col1),
foreign key(col5) references table2(col2),
foreign key(col6) references table3(col3))

error message:
Incorrect syntax near the keyword ‘foreign’

can anyone help to create table 4…

not unless you give the actual table layouts and the actual CREATE statement and the actual error message

:slight_smile:

i didn’t understand what you are saying…

is it possible to create table or is there any syantax error…

is there any alternative to create table…

could you please show the exact query that received the error message

@r937

I mentioned that in red clor…

Which is the same query… except field name changes …

are you sure it is exactly the same? no missing or extra commas?

then it should work fine

:slight_smile:

yes sir…the same code i am using to create table <table4>…
what’s wrong with my code? I am unable to figure out the error…

i can’t see anything wrong with what you posted

that’s not what got the error, though

:wink:

try creating the table without the foreign keys, and then ALTER the table to add them

Now i want to add one more primary key to table3…

Now table3 is like this
table3

create table table3(
col3 int not null,
col4 varchar(5) not null,primary key(col3,col4))

now table4 is

table4

create table table4(
col5 int not null,
col6 varchar(5) not null,
col7 varchar(4) not null,
foreign key(col5) references table1(col1),
foreign key(col6) references table2(col2),
foreign key(col7) references table3(col4))

Now i have added one more column to table 3

those two columns have primary key…

Now i am executing table4 … here the error came up…

do i need to specify unique key in table3 for col4…

but i need this col4 in table4… how can i get that into table4…

if table3 has a two-column primary key, that’s okay

but then in order for table4 to reference table3, the foreign key needs two columns as well

CREATE TABLE table3
( col3 INTEGER NOT NULL
, col4 VARCHAR(5) NOT NULL
, [COLOR="Blue"]PRIMARY KEY ( col3 , col4 )[/COLOR]
)

CREATE TABLE table4
( col5 INTEGER NOT NULL
, col6 VARCHAR(5) NOT NULL
, col7 VARCHAR(4) NOT NULL
...
, [COLOR="blue"]FOREIGN KEY ( col5, col7 ) 
      REFERENCES table3( col3, col4 )[/COLOR]
)

Now i used your code…

this is the error i am getting now…

[COLOR=“Red”]There are no primary or candidate keys in the referenced table <table3> that match the referencing column list in the foreign key ‘FK__table4___7A87F4FA’

Could not create constraint. See previous errors.[/COLOR]i am giving correct column names with out giving mestakes…

Still my code is not executing…:frowning:

if you do not show the real table definitions, i can’t help you

I found the way …

I used joins to get the values

Thanks rudy…

Hi what is sql? I don’t understand what are you drawn here?:frowning:

here you go –> http://lmddgtfy.com/?q=what+is+sql

:cool: