Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS SQL Server QUery

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

885 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