Solved

MS SQL Server QUery

Posted on 2013-06-16
5
400 Views
Last Modified: 2013-07-02
Hi,

I am trying to run this query but keeps giving me an error on line 13, can anyone tell where the syntax issue is?

CREATE TABLE Degree_Classes
(DegreeClassID int NOT NULL,
ClassID INT,
DegreeID INT,
ClassCode VARCHAR(30),
ClassName VARCHAR(100),
Description TEXT,
CONSTRAINT DegreeClassID_PK
Primary KEY (DegreeClassID),
CONSTRAINT classID_FK
FOREIGN KEY (classID)
REFERENCES Classes(classID)
CONSTRAINT DegreeID_FK
FOREIGN KEY (DegreeID)
REFERENCES Degrees(DegreeID)
 );
0
Comment
Question by:Jack_son_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 8

Assisted Solution

by:rpkhare
rpkhare earned 334 total points
ID: 39251476
There is a comma missing. I tried this code and it is working:
CREATE TABLE Degree_Classes
(DegreeClassID int NOT NULL,
ClassID INT,
DegreeID INT,
ClassCode VARCHAR(30),
ClassName VARCHAR(100),
Description TEXT,
CONSTRAINT DegreeClassID_PK
Primary KEY (DegreeClassID),
CONSTRAINT classID_FK
FOREIGN KEY (classID)
REFERENCES Classes(classID),
CONSTRAINT DegreeID_FK
FOREIGN KEY (DegreeID)
REFERENCES Degrees(DegreeID)
 ); 

Open in new window


But prior to executing this code, make sure ClassId in table Classes is a primary key.

The same goes with DegreeId in table Degrees.

I also doubt Degrees is a reserved word in SQL Server. Not sure.
0
 

Author Comment

by:Jack_son_
ID: 39251941
Here is the error I get when trying to execute the query:

Msg 1769, Level 16, State 1, Line 1
Foreign key 'classID_FK' references invalid column 'classID' in referencing table 'Degree_Classes'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

Let me know what you think
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 166 total points
ID: 39252030
There is an implied sequence, those constraints won't work is the tables/fields being referenced do not exist

CONSTRAINT classID_FK
FOREIGN KEY (classID)            -- << this field must exist, as primary key
REFERENCES Classes(classID),  -- << in this table -- before this will work

CONSTRAINT DegreeID_FK
FOREIGN KEY (DegreeID)               -- << this field must exist, as primary key
REFERENCES Degrees(DegreeID)   -- << in this table -- before this will work

have you checked the tables: Classes & Degrees to see if those fields do exist?

The error message is pretty clear:
Foreign key 'classID_FK' references invalid column 'classID'

it is telling you that the field classID does not exist in table Classes
0
 
LVL 8

Accepted Solution

by:
rpkhare earned 334 total points
ID: 39252093
@Jack_son_:

As I wrote, first check:
(1) Do you have Classes table? If yes, make sure the ClassId column exists and it is a primary key.

(2) Do you have Degrees table? If yes, make sure the DegreeId column exists and is a primary key.

(3) Lastly, run the code I posted with comma correction.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39253447
Can you describe the tables, classes and degrees?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question