locked
data table foriegn key problem RRS feed

  • Question

  • I am just learning how to program in SQL, so please be patient with me

     

    ok, here it is, i am getting the following errors with my script when i try to execute, and although i realize that this is not the best style to write my script in, i am trying to learn why this is erroring.  the errors are:

     

    Msg 1769, Level 16, State 1, Line 9

    Foreign key 'Employee2Job_Title' references invalid column 'Title' in referencing table 'Employee'.

    Msg 1750, Level 16, State 0, Line 9

    Could not create constraint. See previous errors.

     

     

    now i realize that the second error is because of the first, and that if i can fix the first, the second will go away.  Thanks for the help. 

     

    Here is my script

     

     

    use inventory

    go

    CREATE TABLE Job_Title

    (Job_Title_Title char(25) NOT NULL ,

    Job_Title_EEO1_Classification char(25) ,

    Job_Title_Job_Description Varchar(45) ,

    Job_Title_Exempt_Status Varchar(15)

    , PRIMARY KEY (Job_Title_Title)

    );

    CREATE TABLE Employee

    (Employee_Emp_ID integer NOT NULL ,

    Employee_last_name varchar(15) ,

    Employee_first_name varchar(15) ,

    Employee_address varchar(30) ,

    Employee_city varchar(15) ,

    Employee_state char(2) ,

    Employee_Telephone_area_code char(3) ,

    Employee_Telephone_number char(8) ,

    Employee_EEO1_Classification char(25) ,

    Employee_Hire_Date char(8) ,

    Employee_Salary char(6) ,

    Employee_Gender Varchar(1) ,

    Employee_Age char(2) ,

    Employee_Title char(25)

    , PRIMARY KEY (Employee_Emp_ID)

    , constraint Employee2Job_Title FOREIGN KEY (Title

    ) REFERENCES Job_Title

    );

     

    Insert INTO Employee (Emp_ID, Last_name, First_name, Address, City, State, Telephone_area_code, Telephone_number, EEO1_Classification, Hire_date, Salary, Gender, Age, Title) Values

    (1, 'Edelman', 'Glenn', '175 Bishop Lane', 'La Jolla', 'CA', 619, '555-0199', 'Sales Workers', 10/7/2003, 21500.00, 'M', 64, 'Cashier')

    Insert INTO Employee (Emp_ID, Last_name, First_name, Address, City, State, Telephone_area_code, Telephone_number, EEO1_Classification, Hire_date, Salary, Gender, Age, Title) Values

    (2, 'McMullen', 'Eric', '762 Church Street', 'Lemon Grove', 'CA', 619, '555-0133', 'Sales Workers', 11/1/2002, 13500.00, 'M', 20, 'Bagger')

    Insert INTO Employee (Emp_ID, Last_name, First_name, Address, City, State, Telephone_area_code, Telephone_number, EEO1_Classification, Hire_date, Salary, Gender, Age, Title) Values

    (3, 'Slenj', 'Raj', '123 Torrey Drive', 'North Clairmont', 'CA', 619, '555-0123', 'Officials & Managers', 6/1/2000, 48000.00, 'M', 34, 'Assistant Manager')

    Insert INTO Employee (Emp_ID, Last_name, First_name, Address, City, State, Telephone_area_code, Telephone_number, EEO1_Classification, Hire_date, Salary, Gender, Age, Title) Values

    (4, 'Broun', 'Erin', '2045 Parkway Apt 2b', 'Encinitas', 'CA', 760, '555-0100', 'Sales Workers', 3/12/2003, 10530.00, 'F', 24, 'Bagger - 30 hours/wk')

    Insert INTO Employee (Emp_ID, Last_name, First_name, Address, City, State, Telephone_area_code, Telephone_number, EEO1_Classification, Hire_date, Salary, Gender, Age, Title) Values

    (5, 'Carpenter', 'Donald', '927 Second Street', 'Encinitas', 'CA', 619, '555-0154', 'Office/Clerical', 11/1/2003, 15000.00, 'M', 18, 'Stocker')

    Insert INTO Employee (Emp_ID, Last_name, First_name, Address, City, State, Telephone_area_code, Telephone_number, EEO1_Classification, Hire_date, Salary, Gender, Age, Title) Values

    (6, 'Esquivez', 'David', '10983 North Coast Highway Apt 902', 'Encinitas', 'CA', 760, '555-0108', 'Operatives (Semi skilled)', 7/25/2003, 18500.00, 'M',25, 'Asst. - Butchers & Seafood Specialists')

    Insert INTO Employee (Emp_ID, Last_name, First_name, Address, City, State, Telephone_area_code, Telephone_number, EEO1_Classification, Hire_date, Salary, Gender, Age, Title) Values

    (7, 'Sharp', 'Nancy', '10793 Montecino Road', 'Ramona', 'CA', 858, '555-0135', 'Sales Workers', 7/12/2003, 21000.00, 'F', 24, 'Cashier')

    Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

    ('Accounting Clerk', 'Office/Clerical', 'Computes, Classifies, records, and verifies numerical data for use in maintaining accounting records.', 'Non-Exempt')

    Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

    ('Assistant store manager', 'Officials & Mangers', 'Supervises and coordintes activities of workers in department of food store. Assist store manager in daily operations of store.', 'Exempt')

    Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

    ('Bagger', 'Sales Worker', 'Places customer orders in bags. Performs carry out duties for customers.', 'Non-Exempt')

    Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

    ('Cashier', 'Sales Worker', 'Operates Cash register to itemize and total customers purchases in grocercy store', 'Non-Exempt')

    Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

    ('Computer Support Specialist', 'Technician', 'Installs, Modifies, and makes minor repairs to personal computer hardware and software systems and provides technical assistance and training to system users.', 'Non-Exempt')

    Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

    ('Director of Finance & Accounting', 'Officials & Mangers', 'Plans and directs finance and accounting activites for Kudlser Fine Foods.', 'Non-Exempt')

    Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

    ('Retail Asst. Bakery & Pastry', 'Craft Workers (Skilled)', 'Obtains or prepares Bakery and Pastry items requested by customers in retail food store.', 'Non-Exempt')

    Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

    ('Retail Asst. Butchers and Seafood Specialists', 'Operatives (Semi skilled)', 'Obtains or prepares Meat and Seafood items requested by customers in retail food store.', 'Non-Exempt')

    Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

    ('Stocker', 'Office/Clerical', 'Stores, prices, and restocks merchandise displays in store.', 'Non-Exempt')

    Sunday, August 26, 2007 9:11 PM

Answers

  • In your foreign key that you are creating with:

    constraint Employee2Job_Title FOREIGN KEY (Title) REFERENCES Job_Title

     

    The column named Title needs to exist in the Employee table on which you are creating the constraint. Then for the column it points to with the REFERENCES part, the needs to be in the format of ReferenceTable(ReferenceTableColumnName)

     

    If I'm guessing right at your tables, I think what you are looking for is more along the lines of

    constraint Employee2Job_Title FOREIGN KEY (Employee_Title) REFERENCES Job_Title(Job_Title_Title)

     

    -Sue

    Monday, August 27, 2007 4:08 AM

All replies

  • In your foreign key that you are creating with:

    constraint Employee2Job_Title FOREIGN KEY (Title) REFERENCES Job_Title

     

    The column named Title needs to exist in the Employee table on which you are creating the constraint. Then for the column it points to with the REFERENCES part, the needs to be in the format of ReferenceTable(ReferenceTableColumnName)

     

    If I'm guessing right at your tables, I think what you are looking for is more along the lines of

    constraint Employee2Job_Title FOREIGN KEY (Employee_Title) REFERENCES Job_Title(Job_Title_Title)

     

    -Sue

    Monday, August 27, 2007 4:08 AM
  • ok, here is the code i ended up with that cured that problem

    CREATE TABLE Job_Title
            (Job_Title_Title char(25) NOT NULL ,
                  Job_Title_EEO1_Classification char(25) ,
                  Job_Title_Job_Description Varchar(45) ,   
                  Job_Title_Exempt_Status Varchar(15)    
           , PRIMARY KEY (Job_Title_Title)   
           );

    CREATE TABLE Employee
                   (Employee_Emp_ID integer NOT NULL ,
                  Employee_last_name varchar(15)  ,
                  Employee_first_name varchar(15)  ,
                  Employee_address varchar(30)  ,
                  Employee_city varchar(15)  ,
                  Employee_state char(2)  ,
                  Employee_Telephone_area_code char(3)  ,
                  Employee_Telephone_number char(8)  ,
                  Employee_EEO1_Classification char(25)  ,
                  Employee_Hire_Date char(8) ,
                  Employee_Salary char(6) ,
                  Employee_Gender Varchar(1) ,
                  Employee_Age char(2) ,
                  Employee_Title char(25)          
           , PRIMARY KEY (Employee_Emp_ID)
           , constraint Employee2Job_Title FOREIGN KEY (Employee_Title
           ) REFERENCES Job_Title
           );



    but now i am on to new and even more frustrating errors, and i will do some work on them myself, on the morrow, then if i am still having problems, i will again avail myself of this resource. 

    thanks much sue, your assistance has been invaluable!

    Monday, August 27, 2007 4:35 AM