Since I'm new to SQL Server, I think I need a little help here... Since I've rewritten the code below many times there might be other errors as well, sorry about that. Removed the 'GO' for now.
Problem:
I don't know how to create a foreign key from "model" in the table Cars
to the table Model
.
My broken code:
create table [Cars]
(
[id] [int] not null primary key identity,
[name] [nvarchar](50) not null,
[weight] [int] not null,
[length] [int] not null,
[model] [nvarchar](50) not null,
[color] [nvarchar](50) not null
);
create table [Colors]
(
[id] [int] not null primary key identity,
[name] [nvarchar](50) not null
);
create table [Model]
(
[id] [int] primary key not null identity,
[name] [nvarchar](50) not null
);
insert into Cars(name, weight, length, model, color)
values('Ferrari', '1500', '4000', '360', 'Red');
insert into Colors values('Red');
insert into Colors values('Blue');
insert into Colors values('Yellow');
insert into Model values('Volvo');
insert into Model values('Fiat');
insert into Model values('Saab');
alter table Model
add foreign key (id)
references Cars(id)
alter table Colors
add foreign key (id)
references Cars(id)