简体   繁体   中英

Column name or number of supplied values does not match table definition. Reviewed my code multiple times. What do I do?

I am learning how to use MySQL currently, and as an extra bonus assignment we are creating a database with foreign keys, primary keys, and composite keys. I keep running into the same exact error and it's starting to really get to me, I have no idea how to solve it.

I am making an SQL database and I have created the Query. When I execute it I get the error message:

Column name or number of supplied values does not match table definition.

I am not sure where I am going wrong. I don't think I have any typos, I don't seem to have any stray characters.

What do I do?

EDIT: I fixed the small issues in my code, yet it's still broken.

/* POPULATING THE DATABASE */
/*- The following is a series of short hand insert statements for populating the tables with records. When using this method it is important to list the values in your statement in the same order their intended fields occur in the table you are populating. There are other more complicated ways to populate and update fields with data, this is a basic example.  */

CREATE DATABASE Students;

/* CREATE tblMajors Table */
CREATE TABLE tblMajors (
        MajorCode CHAR(3),
        MajorDescription CHAR(50),
        PRIMARY KEY (MajorCode));

/* POPULATE tblMajors Table - Due to REFERENCES constraints, this table MUST be populated before we insert our student records. */
INSERT INTO tblMajors VALUES('CST', 'Computer Systems Technologist');
INSERT INTO tblMajors VALUES('CPA', 'Computer Programmer Analyst');

/* CREATE tblInstructors Table */
CREATE TABLE tblInstructors (
        InstructorNumber INT,
        InstructorFirst CHAR(20),
        InstructorLast CHAR(20),
        ContractStatus CHAR(1),
        PhoneNumber CHAR(10),
        PRIMARY KEY (InstructorNumber));

/* POPULATE tblInstructors Table - Due to REFERENCES constraints, this table MUST be populated before we insert our grades records. */
INSERT INTO tblInstructors VALUES(99990, 'Chuck', 'Yaeger', 'P', '5468743248');
INSERT INTO tblInstructors VALUES(99991, 'Cierra', 'Dande', 'F', '4674318435');
INSERT INTO tblInstructors VALUES(99992, 'James', 'Garner', 'F', '4674318435');
INSERT INTO tblInstructors VALUES(99993, 'Steve', 'McQueen', 'F', '4674318435');
INSERT INTO tblInstructors VALUES(99994, 'Paul', 'Newman', 'P', '4674318435');
INSERT INTO tblInstructors VALUES(99995, 'Lynn', 'St. James', 'P', '4674318435');

/* CREATE tblCourses Table */
CREATE TABLE tblCourses (
        CourseCode CHAR(8),
        CourseDescription CHAR(50),
        PRIMARY KEY (CourseCode));

/* POPULATE tblCourses Table - Due to REFERENCES constraints, this table MUST be populated before we insert our student or grades records.  */
INSERT INTO tblCourses VALUES('ACCT1000', 'Accounting I');
INSERT INTO tblCourses VALUES('DBAS1201', 'Introduction to Databases');
INSERT INTO tblCourses VALUES('COMM2201', 'Communications II');
INSERT INTO tblCourses VALUES('DCOM4201', 'Data Communications III');
INSERT INTO tblCourses VALUES('VISB3001', 'Visual Basic II');
INSERT INTO tblCourses VALUES('PROG2000', 'Introduction to Programming');

/* CREATE tblStudents Table */
CREATE TABLE tblStudents (
        StudentNumber INT,
        StudentFirst CHAR(20),
        StudentLast CHAR(20),
        MajorCode CHAR(3),
        PRIMARY KEY (StudentNumber),
        FOREIGN KEY (MajorCode) REFERENCES tblMajors(MajorCode));

/* POPULATE tblStudents Table - Each record in this series of INSERTs contains a Major Code. In order for the record to successfully reach the table, any major code value here must match one from the tblMajors table. This is the result of us configuring it as a foreign key in tblStudents to ensure we don't have any students with Major Codes that do not exist. */
INSERT INTO tblStudents VALUES(200000001, 'Jerry', 'Seinfeld', 'CST');
INSERT INTO tblStudents VALUES(200000002, 'Caesar', 'Augustus', 'CPA');
INSERT INTO tblStudents VALUES(200000003, 'Brianne', 'Madill', 'CPA');
INSERT INTO tblStudents VALUES(200000004, 'Geoff', 'Brabham', 'CST');
INSERT INTO tblStudents VALUES(200000005, 'Cavale', 'Mangusta', 'CPA');
INSERT INTO tblStudents VALUES(200000006, 'Salvador', 'Allende', 'CST');
INSERT INTO tblStudents VALUES(200000007, 'Donald', 'Trump', 'CPA');
INSERT INTO tblStudents VALUES(200000008, 'Paul', 'Machnau', 'CST');
INSERT INTO tblStudents VALUES(200000009, 'Roy', 'Simpsonian', 'CPA');
INSERT INTO tblStudents VALUES(200000010, 'Geoff', 'Bridges', 'CPA');

/* CREATE tblGrades Table */
CREATE TABLE tblGrades (
        StudentNumber INT,
        CourseCode CHAR(8),
        Grade DECIMAL NOT NULL,
        InstructorNumber INT,
        FOREIGN KEY (StudentNumber) REFERENCES tblStudents(StudentNumber),
        FOREIGN KEY (CourseCode) REFERENCES tblCourses(CourseCode),
        FOREIGN KEY (InstructorNumber) REFERENCES tblInstructors(InstructorNumber),
        PRIMARY KEY (StudentNumber, CourseCode));

/* POPULATE tblGrades Table - In tblGrades referential integrity has an additional field to worry about. Due to our CREATE TABLE statement for this table,
any new record will have it's student ID and course code checked against the referenced fields stated from our CREATE script.....meaning, no student number 
will be accepted if it doesn't already exist in tblStudents. Same goes for course codes here when comparedt to course codes in tblCourses. */
INSERT INTO tblGrades VALUES (200000002, 'ACCT1000', 80.0, 99991);
INSERT INTO tblGrades VALUES (200000002, 'DBAS1201', 43.0, 99993);
INSERT INTO tblGrades VALUES (200000002, 'COMM2201', 65.0, 99990);      
INSERT INTO tblGrades VALUES (200000002, 'DCOM4201', 87.0, 99994);      
INSERT INTO tblGrades VALUES (200000002, 'VISB3001', 54.0, 99990);      
INSERT INTO tblGrades VALUES (200000001, 'PROG2000', 84.0, 99990);      
INSERT INTO tblGrades VALUES (200000003, 'ACCT1000', 83.0, 99991);
INSERT INTO tblGrades VALUES (200000003, 'DBAS1201', 43.0, 99993);      
INSERT INTO tblGrades VALUES (200000003, 'COMM2201', 62.0, 99990);      
INSERT INTO tblGrades VALUES (200000003, 'DCOM4201', 61.0, 99994);      
INSERT INTO tblGrades VALUES (200000003, 'VISB3001', 23.0, 99990);      
INSERT INTO tblGrades VALUES (200000003, 'PROG2000', 81.0, 99990);  
INSERT INTO tblGrades VALUES (200000004, 'ACCT1000', 80.0, 99991);
INSERT INTO tblGrades VALUES (200000004, 'DBAS1201', 43.0, 99993);      
INSERT INTO tblGrades VALUES (200000004, 'COMM2201', 63.0, 99990);      
INSERT INTO tblGrades VALUES (200000004, 'DCOM4201', 89.0, 99994);      
INSERT INTO tblGrades VALUES (200000004, 'VISB3001', 57.0, 99990);      
INSERT INTO tblGrades VALUES (200000004, 'PROG2000', 86.0, 99990);  
INSERT INTO tblGrades VALUES (200000005, 'ACCT1000', 81.0, 99991);
INSERT INTO tblGrades VALUES (200000005, 'DBAS1201', 43.0, 99993);      
INSERT INTO tblGrades VALUES (200000005, 'COMM2201', 65.0, 99990);          
INSERT INTO tblGrades VALUES (200000005, 'VISB3001', 57.0, 99990);      
INSERT INTO tblGrades VALUES (200000005, 'PROG2000', 80.0, 99990);  
INSERT INTO tblGrades VALUES (200000006, 'ACCT1000', 80.0, 99991);
INSERT INTO tblGrades VALUES (200000006, 'DBAS1201', 43.0, 99993);      
INSERT INTO tblGrades VALUES (200000006, 'COMM2201', 65.0, 99990);      
INSERT INTO tblGrades VALUES (200000006, 'DCOM4201', 30.0, 99994);      
INSERT INTO tblGrades VALUES (200000006, 'VISB3001', 40.0, 99990);      
INSERT INTO tblGrades VALUES (200000006, 'PROG2000', 80.0, 99990);  
INSERT INTO tblGrades VALUES (200000007, 'ACCT1000', 50.0, 99992);
INSERT INTO tblGrades VALUES (200000007, 'DBAS1201', 47.0, 99993);      
INSERT INTO tblGrades VALUES (200000007, 'COMM2201', 87.0, 99990);      
INSERT INTO tblGrades VALUES (200000007, 'DCOM4201', 67.0, 99994);      
INSERT INTO tblGrades VALUES (200000007, 'VISB3001', 62.0, 99990);      
INSERT INTO tblGrades VALUES (200000007, 'PROG2000', 82.0, 99990);  
INSERT INTO tblGrades VALUES (200000008, 'ACCT1000', 80.0, 99992);
INSERT INTO tblGrades VALUES (200000008, 'DBAS1201', 43.0, 99993);      
INSERT INTO tblGrades VALUES (200000008, 'COMM2201', 65.0, 99990);      
INSERT INTO tblGrades VALUES (200000008, 'VISB3001', 50.0, 99990);      
INSERT INTO tblGrades VALUES (200000008, 'PROG2000', 80.0, 99990);  
INSERT INTO tblGrades VALUES (200000009, 'ACCT1000', 80.0, 99992);
INSERT INTO tblGrades VALUES (200000009, 'COMM2201', 65.0, 99990);      
INSERT INTO tblGrades VALUES (200000009, 'DCOM4201', 80.0, 99994);      
INSERT INTO tblGrades VALUES (200000009, 'VISB3001', 50.0, 99990);      
INSERT INTO tblGrades VALUES (200000009, 'PROG2000', 80.0, 99990);  
INSERT INTO tblGrades VALUES (200000010, 'ACCT1000', 80.0, 99992);
INSERT INTO tblGrades VALUES (200000010, 'DBAS1201', 43.0, 99993);      
INSERT INTO tblGrades VALUES (200000010, 'COMM2201', 65.0, 99990);      
INSERT INTO tblGrades VALUES (200000010, 'DCOM4201', 80.0, 99994);      
INSERT INTO tblGrades VALUES (200000010, 'PROG2000', 80.0, 99990);

There are two issues:

  • In CREATE TABLE statement for tblGrades , you are missing specifying Char length for CourseCode
  • In CREATE TABLE statement for tblStudents , you are missing specifying Char length for MajorCode

Change to following:

CREATE TABLE tblStudents (
        StudentNumber INT,
        StudentFirst CHAR(20),
        StudentLast CHAR(20),
        MajorCode CHAR(3), -- you missed specifying length as 3 here
        PRIMARY KEY (StudentNumber),
        FOREIGN KEY (MajorCode) REFERENCES tblMajors(MajorCode));

CREATE TABLE tblGrades (
        StudentNumber INT,
        CourseCode CHAR(8),  -- you missed specifying length as 8 here
        Grade DECIMAL NOT NULL,
        InstructorNumber INT,
        FOREIGN KEY (StudentNumber) REFERENCES tblStudents(StudentNumber),
        FOREIGN KEY (CourseCode) REFERENCES tblCourses(CourseCode),
        FOREIGN KEY (InstructorNumber) REFERENCES tblInstructors(InstructorNumber),
        PRIMARY KEY (StudentNumber, CourseCode));

First, you need to tell how many decimal places and digits you have on grade. Let's say 2 decimal places.

Grade DECIMAL(3,2) NOT NULL

Then, try inserting this way

INSERT INTO tblGrades
(StudentNumber, CourseCode, Grade, InstructorNumber)
VALUES (200000002, 'COMM2201', 65.00, 99990);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM