Search code examples
mysqlsqlorganizationdrop-table

SQL Code not running: Drop Tables and Organization issue maybe?


So, this is my code. When I attempt to compile it in the school provided engine, I've gotten a couple errors, and they do change each time I run it, but the one I currently have is this:

ERROR 1064 (42000) at line 123: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '9:30, 'L210', 103, 15), (86, 25, 2, 6/10/2007 9:30, 'L210', 107, 15), (89, 25, 5' at line 2

I've already tried to take out the spaces, but it makes no difference. Would this have anything to do with the order in which I take information? Or is it something else?

I'm also not sure that the tables are dropping at step one.

/*Step One: Drop Tables*/
DROP TABLE IF EXISTS Student;
DROP TABLE IF EXISTS Zipcode;
DROP TABLE IF EXISTS Instructor;
DROP TABLE IF EXISTS Enrollment;
DROP TABLE IF EXISTS Sections;
DROP TABLE IF EXISTS Course;


/*Step Two: Create Tables*/
CREATE TABLE Zipcode
(
zip             int(11), 
city            varchar(25)     NOT NULL,  
state           varchar(2)      NOT NULL,
PRIMARY KEY (zip)
);

CREATE TABLE Student
(
student_ID          int(6)          UNIQUE,
salutation          varchar(5), 
first_name          varchar(25)     NOT NULL, 
last_name           varchar(25)     NOT NULL, 
street_address      varchar(50), 
phone               int(15)         NOT NULL, 
employer            varchar(50) , 
registration_date   date            NOT NULL, 
zip                 int(11),
PRIMARY KEY (student_ID),
FOREIGN KEY (zip) REFERENCES Zipcode (zip)
);

CREATE TABLE Course
(
    course_ID           int(6)          UNIQUE,
    description         varchar(50)     NOT NULL,
    cost                dec(8,2)        NOT NULL,
    prerequisite        int(6),
    PRIMARY KEY (course_ID),
    FOREIGN KEY (prerequisite) REFERENCES Course (course_ID)
);

CREATE TABLE Instructor
(
Instructor_ID   int(6)              UNIQUE,
salutation      varchar(5), 
first_name      varchar(25)         NOT NULL, 
last_name       varchar(25)         NOT NULL, 
street_address  varchar(50), 
zip             int(11)             NOT NULL,
PRIMARY KEY (Instructor_ID),
FOREIGN KEY (zip) REFERENCES zipcode (zip)
);

CREATE TABLE Sections
(
section_ID          int(8)          UNIQUE,
course_id           int(6)          NOT NULL,
course_section_num  int(6)          NOT NULL,
start_date_time     datetime        NOT NULL,
location            varchar(10),
instructor_ID       int(6)          NOT NULL,
capacity            int(3),
PRIMARY KEY (section_ID),
FOREIGN KEY (instructor_ID) REFERENCES Instructor (instructor_ID),
FOREIGN KEY (course_ID) REFERENCES Course (course_ID)   
);

CREATE TABLE Enrollment
(
student_ID          int(6)          UNIQUE,
section_ID          int(8)          UNIQUE,
enroll_date         datetime        NOT NULL,
final_grade         char(1),
PRIMARY KEY (section_ID, student_ID),
FOREIGN KEY (section_ID) REFERENCES sections (section_ID),
FOREIGN KEY (student_ID) REFERENCES student (student_ID)
);


/*Step Three: Insert Rows*/
INSERT INTO Zipcode VALUES
(7024, 'Ft. Lee', 'NJ'),
(7047, 'North Bergen', 'NJ'),
(10005, 'New York', 'NY'),
(10015, 'New York', 'NY'),
(10025, 'New York', 'NY'),
(10035, 'New York', 'NY'),
(11419, 'Richmond Hill', 'NY'),
(11435, 'Jamaica', 'NY');

INSERT INTO Student VALUES
(102, 'Mr.', 'Fred', 'Crocitto', '101-09 120th St.', 718-555-5555, 'Albert Hildegard Co.', 1/22/2007, 11419),
(103, 'Ms.', 'J.', 'Landry', '7435 Boulevard East #45', 201-555-5555, 'Albert Hildegard Co.', 1/22/2007, 7047),
(104, 'Ms.', 'Laetia', 'Enison', '144-61 87th Ave', 718-555-5555, 'Albert Hildegard Co.', 1/22/2007, 11435),
(105, 'Mr.', 'Angel', 'Moskowitz', '320 John St.', 201-555-5555, 'Alex. & Alexander', 1/22/2007, 7024),
(163, 'Ms.', 'Nicole', 'Gillen', '4301 N Ocean #103', 904-555-5555, 'Oil of America Corp.', 2/2/2007, 10025),
(223, 'Mr.', 'Frank', 'Pace', '13 Burlington Dr.', 203-555-5555, 'Board Utilities', 2/8/2007, 10025),
(399, 'Mr.', 'Jerry', 'Abdou', '460 15th St. #4', 718-555-5555, 'Health Mgmt.Systems', 2/23/2007, 10025);

INSERT INTO Course VALUES
(330, 'Network Administration', 1195, 130),
(310, 'Operating Systems', 1195, NULL),
(142, 'Project Management', 1195, 20),
(140, 'Systems Analysis', 1195, 20),
(130, 'Intro to Unix', 1195, 310),
(25, 'Intro to Programming', 1195, 140),
(20, 'Intro to Information Systems', 1195, NULL);

INSERT INTO Instructor VALUES
(101, 'Mr.', 'Fernand', 'Hanks', '100 East 87th', 10015),
(102, 'Mr.', 'Tom', 'Wojick', '518 West 120th', 10025),
(103, 'Ms.', 'Nina', 'Schorin', '210 West 101st', 10025),
(104, 'Mr.', 'Gary', 'Pertez', '34 Sixth Ave', 10035),
(105, 'Ms.', 'Anita', 'Morris', '34 Maiden Lane', 10015),
(106, 'Rev.', 'Todd', 'Smythe', '210 West 101st', 10025),
(107, 'Dr.', 'Marilyn', 'Frantzen', '254 Bleeker', 10005);

INSERT INTO Sections VALUES
(81, 20, 2, 7/24/2007 9:30, 'L210', 103, 15),
(86, 25, 2, 6/10/2007 9:30, 'L210', 107, 15),
(89, 25, 5, 5/15/2007 9:30, 'L509', 103, 25),
(92, 25, 8, 6/13/2007 9:30, 'L509', 106, 25),
(104, 330, 1, 7/14/2007 10:30, 'L511', 104, 25),
(119, 142, 1, 7/14/2007 9:30, 'L211', 103, 25),
(155, 122, 4, 5/4/2007 9:30,  'L210', 107, 15);

INSERT INTO Enrollment VALUES
(102, 86, 1/30/2007, NULL, 'B'),
(102, 89, 1/30/2007, 92,'A'),
(103, 81, 1/30/2007, NULL),
(104, 81, 1/30/2007, NULL, 'A'),
(163, 92, 2/10/2007, NULL),
(223, 104, 2/16/2007, NULL,'C'),
(223, 119, 2/16/2007, NULL);


/*Step Four: Select Statements*/
SELECT * FROM Student;
SELECT * FROM Zipcode;
SELECT * FROM Instructor;
SELECT * FROM Course;
SELECT * FROM Sections;
SELECT * FROM Enrollment;

Solution

  • The issue identified by the error message is the datetime literal, here:

     INSERT INTO Sections VALUES
     (81, 20, 2, 7/24/2007 9:30, 'L210', 103, 15)
                 ^^^^^^^^^^^^^^
    

    To get a value assigned to a datetime column, you could do this:

     (81, 20, 2, '2007-07-24 09:30', 'L210', 103, 15)
                 ^^^^^^^^^^^^^^^^^^
    

    Datetime literals should be enclosed in single quotes, and represented in a format like 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MI:SS'.

    (It is also possible to pass numeric decimal values in. But no one really does that. Just use a string literal in the correct format. I believe newer versions of MySQL are more lenient than earlier versions, regarding the strictness of two characters for month, and two characters for day, and using a delimiter other than the dash. I think its also possible to omit the delimiter for a datetime

    I think MySQL would also accept something like this:

       '20070724093000'
    

    But again, no one really does that. Just supply the values as strings in the standard 'YYYY-MM-DD HH:MI:SS' format.