Solved

MS SQL Server QUery

Posted on 2013-06-16
5
382 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 31

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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 information from SQL Server on Database, Connection and Server properties

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now