lucky20
November 10, 2010, 5:10pm
1
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…
r937
November 10, 2010, 5:36pm
2
not unless you give the actual table layouts and the actual CREATE statement and the actual error message
lucky20
November 10, 2010, 6:07pm
3
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…
r937
November 10, 2010, 6:26pm
4
could you please show the exact query that received the error message
lucky20
November 10, 2010, 7:06pm
5
@r937
I mentioned that in red clor…
Which is the same query… except field name changes …
r937
November 10, 2010, 7:17pm
6
are you sure it is exactly the same? no missing or extra commas?
then it should work fine
lucky20
November 10, 2010, 7:29pm
7
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…
r937
November 10, 2010, 7:30pm
8
i can’t see anything wrong with what you posted
that’s not what got the error, though
try creating the table without the foreign keys, and then ALTER the table to add them
lucky20
November 10, 2010, 7:39pm
9
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…
lucky20
November 10, 2010, 7:41pm
10
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…
r937
November 10, 2010, 7:46pm
11
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]
)
lucky20
November 10, 2010, 7:53pm
12
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…
r937
November 10, 2010, 8:41pm
13
if you do not show the real table definitions, i can’t help you
lucky20
November 11, 2010, 9:23pm
14
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?
r937
November 13, 2010, 8:54pm
16