Solved

MS SQL Server QUery

Posted on 2013-06-16
5
391 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_
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Addition to SQL for dynamic fields 6 37
Why Delete * statement wont work with sql server ? 6 38
T-SQL: "HAVING CASE" Clause 1 23
insert wont work in SQL 14 19
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

806 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