Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old January 17th, 2005, 07:41 AM
Lagamorph Lagamorph is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 5 Lagamorph User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
SQL Foreign Keys

Ok, now I can't get my Foreign keys to work >_< I've tried compiling my code and I get an error message saying "Foreign key 'received_fkey' references invalid column 'product_order_ID' in referencing table 'customer'". Below is my code for creating my tables. Could I maybe have them in the wrong order or something?


create table customer
(customer_ID varchar(7),
customer_name varchar(30),
customer_street varchar(30),
customer_town varchar(30),
customer_county varchar(30),
customer_postcode varchar(8),
customer_telephone varchar(12),
customer_email varchar(30),
constraint customerkey primary key (customer_ID),
constraint cust_ID check ((customer_ID like '[0-9][0-9][0-9][-][0-9][0-9][0-9]')),
constraint cust_postcode check ((customer_postcode like '[A-Z][A-Z][0-9][0-9][ ][0-9][A-Z][A-Z]') or (customer_postcode like '[A-Z][A-Z][0-9][ ][ ][0-9][A-Z][A-Z]')),
constraint cust_phone check ((customer_telephone like '[0-9][0-9][0-9][0-9][0-9][ ][0-9][0-9][0-9][0-9][0-9][0-9]')),
constraint received_fkey foreign key (product_order_ID) references product_order
on delete cascade
on update cascade);

create table product_order
(product_order_ID char(7) not null,
constraint product_orderkey primary key (product_order_ID),
constraint prodorder_ID check ((product_order_ID like '[0-9][0-9][0-9][-][0-9][0-9][0-9]')),
constraint sentby_fkey foreign key (warehouse_ID) references warehouse
on update cascade
on delete cascade);

create table customer_order
(customer_order_ID char(7) not null,
order_date char(10) not null,
delivery_date char(10) not null,
constraint customer_orderkey primary key (customer_order_ID),
constraint custorder_ID check ((customer_order_ID like '[0-9][0-9][0-9][-][0-9][0-9][0-9]')),
constraint orddate check ((order_date like '[1-9[0-9][/][0-1][1-9][/][1-9][0-9][0-9][0-9]') or (order_date like '[1-9][/][0-1][1-9][/][1-9][0-9][0-9][0-9]')),
constraint delivdate check ((delivery_date like '[1-9[0-9][/][0-1][1-9][/][1-9][0-9][0-9][0-9]') or (delivery_date like '[1-9][/][0-1][1-9][/][1-9][0-9][0-9][0-9]')),
constraint makes_fkey foreign key (customer_ID) references customer
on update cascade
on delete cascade);

create table product
(product_ID char(7) not null,
product_description char not null,
constraint productkey primary key (product_ID),
constraint prod_ID check ((product_ID like '[0-9][0-9][0-9][-][0-9][0-9][0-9]')),
constraint orders_fkey foreign key (customer_order_ID) references customer_order
on update cascade
on delete cascade);

create table supplier_order
(supplier_order_ID char(7) not null,
constraint supplier_orderkey primary key (supplier_order_ID),
constraint supporder_ID check ((supplier_order_ID like '[0-9][0-9][0-9][-][0-9][0-9][0-9]')),
constraint creates_fkey foreign key (product_ID) references product
on update cascade
on delete cascade);

create table supplier
(supplier_ID char(7) not null,
supplier_name char(30) not null,
supplier_street char(30) not null,
supplier_town char(30) not null,
supplier_county char(30) not null,
supplier_postcode char(8) not null,
supplier_telephone int not null,
supplier_email char(30) not null,
VAT_code int not null,
supplier_contact char(30) not null,
constraint supplierkey primary key (supplier_ID),
constraint supp_ID check ((supplier_ID like '[0-9][0-9][0-9][-][0-9][0-9][0-9]')),
constraint supp_postcode check ((supplier_postcode like '[A-Z][A-Z][0-9][0-9][ ][0-9][A-Z][A-Z]') or (supplier_postcode like '[A-Z][A-Z][0-9][ ][ ][0-9][A-Z][A-Z]')),
constraint supp_phone check ((supplier_telephone like '[0-9][0-9][0-9][0-9][0-9][ ][0-9][0-9][0-9][0-9][0-9][0-9]')),
constraint sentto_fkey foreign key (supplier_order_ID) references supplier_order
on update cascade
on delete cascade);

create table warehouse
(warehouse_ID char(7) not null,
product_stock int not null,
warehouse_name char(30) not null,
warehouse_street char(30) not null,
warehouse_town char(30) not null,
warehouse_county char(30) not null,
warehouse_postcode char(8) not null,
warehouse_telephone int not null,
warehouse_email char(30) not null,
constraint warhousekey primary key (warehouse_ID),
constraint ware_ID check ((warehouse_ID like '[0-9][0-9][0-9][-][0-9][0-9][0-9]')),
constraint ware_postcode check ((warehouse_postcode like '[A-Z][A-Z][0-9][0-9][ ][0-9][A-Z][A-Z]') or (warehouse_postcode like '[A-Z][A-Z][0-9][ ][ ][0-9][A-Z][A-Z]')),
constraint ware_phone check ((warehouse_telephone like '[0-9][0-9][0-9][0-9][0-9][ ][0-9][0-9][0-9][0-9][0-9][0-9]')),
constraint ware_fkey foreign key (supplier_ID) references supplier
on update cascade
on delete cascade);

create table supplier_service
(supplier_service_ID char(7) not null,
constraint supplier_servicekey primary key (supplier_service_ID),
constraint suppservice_ID check ((supplier_service_ID like '[0-9][0-9][0-9][-][0-9][0-9][0-9]')),
constraint offers_fkey foreign key (supplier_ID) references supplier
on update cascade
on delete cascade);

create table web_service
(web_service_ID char(7) not null,
web_service_desription char not null,
web_service_URL char not null,
constraint web_servicekey primary key (web_service_ID),
constraint webserv_ID check ((web_service_ID like '[0-9][0-9][0-9][-][0-9][0-9][0-9]')),
constraint webserv_fkey foreign key (supplier_service_ID) references supplier_service
on update cascade
on delete cascade);

Reply With Quote
  #2  
Old January 17th, 2005, 11:37 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
When the script runs, it tries to create a constraint on a table that doesn't exist yet.
Create the tables first, then create the constraints.

Reply With Quote
  #3  
Old January 17th, 2005, 12:44 PM
Lagamorph Lagamorph is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 5 Lagamorph User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
So remove the Foreign keys, run the code, then put them back in and run the foreign key constraints on there own? I didn't think you could run statements individually like that, just the entire create statement at once

Reply With Quote
  #4  
Old January 17th, 2005, 01:17 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
You can do it like this, after the tables have been created.
Code:
ALTER TABLE customer 
ADD CONSTRAINT received_fkey
FOREIGN KEY (product_order_ID) 
REFERENCES product_order (product_order_ID)

the customer table must have a column named product_order_ID, in order to make it a foreign key relationship.

Reply With Quote
  #5  
Old January 17th, 2005, 02:13 PM
Lagamorph Lagamorph is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 5 Lagamorph User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ah, thanks. Using the Alter command might get me an extra mark or two in the projcet since it's not something we were taught in class, lol. As long as I can manage 40% that's a pass ^_^

Thanks pal, you may just have helped me pass, lol

Reply With Quote
  #6  
Old January 17th, 2005, 03:09 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
If I had known this was for your homework.
I wouldn't have helped you.
If you can't do it yourself, then you don't deserve to pass the class.

Reply With Quote
  #7  
Old January 18th, 2005, 04:01 AM
Lagamorph Lagamorph is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 5 Lagamorph User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The only reason I'm doing it is because it's a required class, I didn't pick it :P

Reply With Quote
  #8  
Old January 18th, 2005, 10:02 PM
Doug G Doug G is offline
Grumpier Old Moderator
ASP Free God 11th Plane (10000 - 10499 posts)
 
Join Date: Sep 2003
Posts: 10,143 Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 23 h 29 m 58 sec
Reputation Power: 181
Quote:
Originally Posted by Lagamorph
The only reason I'm doing it is because it's a required class, I didn't pick it :P

Ah, tell that during your first job interview
__________________
======
Doug G
======
I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain

Reply With Quote
  #9  
Old January 19th, 2005, 07:51 PM
Lagamorph Lagamorph is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 5 Lagamorph User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
lol, at interviews my motto is "Tell them what they want to hear, not what you think"

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > SQL Foreign Keys


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT