Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS SQL Server QUery

Posted on 2013-06-16
5
Medium Priority
?
408 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 1336 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 49

Assisted Solution

by:PortletPaul
PortletPaul earned 664 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 1336 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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

721 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