Replacing int value with null - mysql

When I have an int column and most of the data is a number, but I have a few where there is no data and I am trying to fill it in with a null. how do you go about this? (In summary a null value in place of an int).
CREATE TABLE EmployeeSalary
(EmployeeID int,
JobTitle varchar(50),
YearsOfService int,
Salary int
);
INSERT INTO EmployeeSalary VALUES
(101, 'Manager', 20, 80000),
(102, 'Desk Clerk', 5, 30000),
(103, 'Maintanence', 10, 60000),
(104, 'Sales', 'null', 'null'),
(105, 'Operations Manager', 'null', 'null')
;

Remove quotes around null and put NULL in place of 'null'...
INSERT INTO EmployeeSalary (emp_id, emp_post, how_much_year, emp_salary) VALUES
(1, 'Computer Engineer', 20, NULL),
(2, 'Computer Scientist', NULL, 100000);

The value 'NULL', with single quotes around it, is a string literal. You want to use NULL, without quotes:
INSERT INTO EmployeeSalary (EmployeeID, JobTitle, YearsOfService, Salary)
VALUES
(101, 'Manager', 20, 80000),
(102, 'Desk Clerk', 5, 30000),
(103, 'Maintanence', 10, 60000),
(104, 'Sales', NULL, NULL),
(105, 'Operations Manager', NULL, NULL);
Note that I also added the target colums of the insert, which is considered best practice.

You are trying to save text value in YearOfService column. Whenever you wrap any value in-between apostrophe i.e. ' then it becomes a text value .
Use null instead of 'null' it should work.
Valid insert query:
INSERT INTO EmployeeSalary VALUES
(101, 'Manager', 20, 80000),
(102, 'Desk Clerk', 5, 30000),
(103, 'Maintanence', 10, 60000),
(104, 'Sales', null, null),
(105, 'Operations Manager', null, null)
;
Please refer this db-fiddle

Related

How to find the Median per GROUP (where the GROUP BY has multiple column conditions)

I've encountered a bunch of similar pages which find the median of the column and median per group but never median per group by where there are multiple conditions.
How can I do this?
Here is an example of the dataset I'm working with:
CREATE TABLE trans
(`order_entry_date` datetime, `brand` varchar(2), `customer_id` int, `webshop_order_number` int, `store_country` varchar(2), `product_id` bigint, `units_ordered` int, `gross_order_entry` int, `net_order_entry` int)
;
INSERT INTO trans
(`order_entry_date`, `brand`, `customer_id`, `webshop_order_number`, `store_country`, `product_id`, `units_ordered`, `gross_order_entry`, `net_order_entry`)
VALUES
('2020-03-23 00:00:00', 'aa', 304044, 700112, 'ds', 8719852058986, 1, 41.24, 41.24),
('2020-03-23 00:00:00', 'aa', 304041, 700112, 'ds', 8719852051230, 1, 61.12, 84.22),
('2020-03-23 00:00:00', 'js', 304041, 700112, 'jp', 8719852051230, 2, 61.12, 84.22),
('2019-03-23 00:00:00', 'js', 302012, 700112, 'th', 8719852058770, 2, 61.88, 51.66),
('2019-03-23 00:00:00', 'TH', 302011, 700112, 'th', 8719852058770, 3, 61.88, 51.66),
('2020-03-23 00:00:00', 'TH', 304040, 700112, 'DE', 8719852051230, 1, 61.12, 84.22)
;

Sum of multiple rows in sql with join statement

I am trying to sum multiple rows of the same athlete so it will return the total amount of medals that they have won overall. I have the following code:
CREATE TABLE athlete (
athlete_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name TINYTEXT NOT NULL,
country TINYTEXT NOT NULL,
birthdate DATE NOT NULL,
age INT UNSIGNED,
height_inch INT UNSIGNED,
PRIMARY KEY (athlete_id)
);
INSERT INTO athlete (name, country, birthdate, age, height_inch) VALUES ('Simone Biles', 'United States', '1997-03-14', 24, 56);
INSERT INTO athlete (name, country, birthdate, age, height_inch) VALUES ('Michael Phelps', 'United States', '1985-06-30', 36, 76);
CREATE TABLE sport (
sport_id INT UNSIGNED NOT NULL,
sport TINYTEXT NOT NULL,
PRIMARY KEY (sport_id)
);
INSERT INTO sport VALUES (101, 'Skiing');
INSERT INTO sport VALUES (102, 'Biathlon');
INSERT INTO sport VALUES (103, 'Curling');
INSERT INTO sport VALUES (104, 'Skating');
INSERT INTO sport VALUES (105, 'Ice Hockey');
INSERT INTO sport VALUES (106, 'Luge');
INSERT INTO sport VALUES (107, 'Snowboard');
INSERT INTO sport VALUES (108, 'Basketball');
INSERT INTO sport VALUES (109, 'Gymnastics');
INSERT INTO sport VALUES (110, 'Swimming');
INSERT INTO sport VALUES (111, 'Diving');
INSERT INTO sport VALUES (112, 'Track and Field');
INSERT INTO sport VALUES (113, 'Badminton');
INSERT INTO sport VALUES (114, 'Tennis');
INSERT INTO sport VALUES (115, 'Volleyball');
INSERT INTO sport VALUES (116, 'Skateboard');
INSERT INTO sport VALUES (117, 'Soccer');
INSERT INTO sport VALUES (118, 'Golf');
INSERT INTO sport VALUES (119, 'Cycling');
INSERT INTO sport VALUES (120, 'Climbing');
INSERT INTO sport VALUES (121, 'Surfing');
INSERT INTO sport VALUES (122, 'Water Polo');
INSERT INTO sport VALUES (123, 'Karate');
CREATE TABLE olympics (
olympics_id INT UNSIGNED NOT NULL,
season TINYTEXT NOT NULL,
year YEAR NOT NULL,
city TINYTEXT NOT NULL,
PRIMARY KEY (olympics_id)
);
INSERT INTO olympics VALUES (1001, 'Summer', 1936, 'Berlin');
INSERT INTO olympics VALUES (1002, 'Summer', 1956, 'Melbourne');
INSERT INTO olympics VALUES (1003, 'Summer', 1960, 'Rome');
INSERT INTO olympics VALUES (1004, 'Summer', 1964, 'Tokyo');
INSERT INTO olympics VALUES (1005, 'Summer', 1976, 'Montreal');
INSERT INTO olympics VALUES (1006, 'Summer', 1984, 'Los Angelos');
INSERT INTO olympics VALUES (1007, 'Summer', 1996, 'Atlanta');
INSERT INTO olympics VALUES (1008, 'Summer', 2000, 'Sydney');
INSERT INTO olympics VALUES (1009, 'Summer', 2004, 'Athens');
INSERT INTO olympics VALUES (1010, 'Summer', 2008, 'Beijing');
INSERT INTO olympics VALUES (1011, 'Summer', 2012, 'London');
INSERT INTO olympics VALUES (1012, 'Summer', 2016, 'Rio de Janeiro');
INSERT INTO olympics VALUES (1013, 'Summer', 2020, 'Tokyo');
CREATE TABLE sport_events (
sport_id INT UNSIGNED NOT NULL,
event_id INT UNSIGNED NOT NULL,
event TINYTEXT NOT NULL,
PRIMARY KEY (event_id),
FOREIGN KEY (sport_id) REFERENCES sport (sport_id)
);
INSERT INTO sport_events VALUES (101, 501, 'Alpine Skiing');
INSERT INTO sport_events VALUES (101, 502, 'Cross-Country Skiing');
INSERT INTO sport_events VALUES (104, 503, 'Figure Skating');
INSERT INTO sport_events VALUES (101, 504, 'Freestyle Skiing');
INSERT INTO sport_events VALUES (104, 505, 'Short Track Speed Skating');
INSERT INTO sport_events VALUES (101, 506, 'Ski Jumping');
INSERT INTO sport_events VALUES (107, 507, 'Half-pipe');
INSERT INTO sport_events VALUES (101, 508, 'Half-pipe');
INSERT INTO sport_events VALUES (104, 509, 'Speed Skating');
INSERT INTO sport_events VALUES (109, 510, 'Artistic Gymnastics');
INSERT INTO sport_events VALUES (109, 511, 'Rhythmic Gymnastics');
INSERT INTO sport_events VALUES (115, 512, 'Beach Volleyball');
INSERT INTO sport_events VALUES (112, 513, 'High Jump');
INSERT INTO sport_events VALUES (112, 514, '100m');
INSERT INTO sport_events VALUES (112, 515, '200m');
INSERT INTO sport_events VALUES (112, 516, '400m');
INSERT INTO sport_events VALUES (112, 517, '800m');
INSERT INTO sport_events VALUES (112, 518, '4x100m relay');
INSERT INTO sport_events VALUES (112, 519, 'Triple Jump');
CREATE TABLE athlete_sport (
athlete_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
sport_id INT UNSIGNED NOT NULL,
PRIMARY KEY (athlete_id),
FOREIGN KEY (athlete_id) REFERENCES athlete (athlete_id),
FOREIGN KEY (sport_id) REFERENCES sport (sport_id)
);
INSERT INTO athlete_sport (sport_id) VALUES (109);
INSERT INTO athlete_sport (sport_id) VALUES (110);
CREATE TABLE compete (
athlete_id INT UNSIGNED NOT NULL,
olympics_id INT UNSIGNED NOT NULL,
sport_id INT UNSIGNED NOT NULL,
event_id INT UNSIGNED,
gold INT UNSIGNED,
silver INT UNSIGNED,
bronze INT UNSIGNED,
FOREIGN KEY (olympics_id) REFERENCES olympics (olympics_id),
FOREIGN KEY (athlete_id) REFERENCES athlete (athlete_id),
FOREIGN KEY (event_id) REFERENCES sport_events (event_id)
);
INSERT INTO compete VALUES (1, 1012, 109, 510, 4, 0, 1);
INSERT INTO compete VALUES (1, 1013, 109, 510, 0, 1, 1);
INSERT INTO compete VALUES (2, 1009, 110, NULL, 6, 0, 2);
INSERT INTO compete VALUES (2, 1010, 110, NULL, 8, 0, 0);
INSERT INTO compete VALUES (2, 1011, 110, NULL, 4, 2, 0);
INSERT INTO compete VALUES (2, 1012, 110, NULL, 5, 1, 0);
I have looked at other answers people have posted and most of them are just saying to use group by, but when I use that it just orders the either names or number of medals together just in a different order. I'm trying to get it so it says that the total number of medals simone biles has is 7 and michael phelps is 28 just in a single table.
This is the query I have that returns the sum of the medals for each olympic games they've been in, but again if I use group by it just orders them.
select a.name, gold+silver+bronze as medalTotal from athlete a join compete c using (athlete_id) group by medalTotal;
+----------------+------------+
| name | medalTotal |
+----------------+------------+
| Simone Biles | 2 |
| Simone Biles | 5 |
| Michael Phelps | 6 |
| Michael Phelps | 8 |
+----------------+------------+
you need to do the following:
group by athlete
sum each medal type
add the sum of each medal type
You need to group by athlete, not total.
You have to use an aggregation function to combine values from all the rows in a group. Use SUM() to add them together.
select a.name, SUM(b.gold+b.silver+b.bronze) as medalTotal
from athlete a
join compete c using (athlete_id)
group by a.athlete_id;
You should apply group by using something unique like athelet id.
This get all atheletes with all medals number including with zero medals
if you want just athelet with medals only use join only.
select
a.name, sum(gold+silver+bronze) as medalTotal
from
athlete a
left join
compete c on c.athlete_id = a.athlete_id
group by
a.athlete_id

When I try to create a table in MySQL, it gives me error code 1215: “Cannot add foreign key constraint”

I am trying to create a database, but when I run this code, I get this error:
Code 1215: “Cannot add foreign key constraint"
Code:
drop database if exists kat_db;
create database if not exists kat_db;
use kat_db;
create table if not exists Patients
(
PatientID int not null PRIMARY KEY,
PatientName varchar(50),
TypeOfAnimal varchar(50),
DateOfLastApt date,
PatientAge int,
Microchipped boolean,
OwnerID int not null,
DoctorID int not null,
foreign key (OwnerID) references Owners(OwnerID),
foreign key (DoctorID) references Doctors(DoctorID)
);
create table if not exists Owners
(
OwnerID int not null PRIMARY KEY,
OwnerFirstName varchar(50),
OwnerLastName varchar(50),
PatientID int,
OwnerPhone int,
OwnerZip int,
PreferredPaymentMethod varchar(50),
foreign key (PatientID) references Patients(PatientID)
);
create table if not exists Doctors
(
DoctorID int not null PRIMARY KEY,
DoctorFirstName varchar(50),
DoctorLastName varchar(50),
PatientID int,
DoctorPhone int,
DoctorZip int,
AnimalSpecialty varchar(50),
foreign key (PatientID) references Patients(PatientID)
);
insert into Patients (PatientID, TypeOfAnimal, PatientAge, PatientName, DateOfLastApt, Microchipped, DoctorID, OwnerID)
values (01, 'dog', 6, 'Dima', '2017-02-05', 1, 101, 1001),
(02, 'dog', 3, 'Misha', '2017-02-05', 1, 102, 1002),
(03, 'cat', 2, 'Pistol', '2017-06-09', 0, 103, 1003),
(04, 'cat', 1, 'Nessie', '2017-09-04', 0, 104, 1004),
(05, 'cat', 2, 'Charlie', '2016-06-04', 1, 105, 1005),
(06, 'cat', 12, 'Francis', '2015-05-07', 1, 106, 1006),
(07, 'rabbit', 5, 'Bunny', '2015-05-06', 0, 107, 1007),
(08, 'turtle', 6, 'Aqua', '2017-06-08', 0, 108, 1008),
(09, 'dog', 16, 'Sammie', '2012-09-07', 1, 109, 1009),
(10, 'dog', 14, 'Dog', '2016-04-06', 0, 110, 1010);
insert into Owners (OwnerID, OwnerFirstName, OwnerLastName, OwnerPhone, OwnerZip, PreferredPaymentMethod, PatientID)
values (101, 'Kat', 6, 'Dima', '2017-02-05', 1, 101, 1001),
(102, 'Hunter', 3, 'Misha', '2017-02-05', 1, 102, 1002),
(103, 'Vicky', 2, 'Pistol', '2017-06-09', 0, 103, 1003),
(104, 'Vanessa', 1, 'Nessie', '2017-09-04', 0, 104, 1004),
(105, 'Weston', 2, 'Charlie', '2016-06-04', 1, 105, 1005),
(106, 'Jonas', 12, 'Francis', '2015-05-07', 1, 106, 1006),
(107, 'Devin', 5, 'Bunny', '2015-05-06', 0, 107, 1007),
(108, 'Grego', 6, 'Aqua', '2017-06-08', 0, 108, 1008),
(109, 'Jackson', 16, 'Sammie', '2012-09-07', 1, 109, 1009),
(110, 'dog', 14, 'Dog', '2016-04-06', 0, 110, 1010);
insert into Patients (PatientID, TypeOfAnimal, PatientAge, PatientName, DateOfLastApt, Microchipped, DoctorID, OwnerID)
values (01, 'dog', 6, 'Dima', '2017-02-05', 1, 101, 1001),
(02, 'dog', 3, 'Misha', '2017-02-05', 1, 102, 1002),
(03, 'cat', 2, 'Pistol', '2017-06-09', 0, 103, 1003),
(04, 'cat', 1, 'Nessie', '2017-09-04', 0, 104, 1004),
(05, 'cat', 2, 'Charlie', '2016-06-04', 1, 105, 1005),
(06, 'cat', 12, 'Francis', '2015-05-07', 1, 106, 1006),
(07, 'rabbit', 5, 'Bunny', '2015-05-06', 0, 107, 1007),
(08, 'turtle', 6, 'Aqua', '2017-06-08', 0, 108, 1008),
(09, 'dog', 16, 'Sammie', '2012-09-07', 1, 109, 1009),
(10, 'dog', 14, 'Dog', '2016-04-06', 0, 110, 1010);
I tried creating the tables in different order. I also created a different database, which worked, In fact, I used this to create the database on the top.
drop database if exists kat3_db;
create database if not exists kat3_db;
use kat3_db;
create table if not exists Book(
BookNumber int not null,
BookName varchar(50),
BookPrice decimal(5,2),
CoverType varchar(15) default 'hardcover',
PublicationDate date,
primary key (BookNumber)
);
create table if not exists Course(
CourseNo int not null,
CourseName varchar(20),
Semester varchar(10),
BookNumber int not null,
primary key (CourseNo),
foreign key (BookNumber) references Book(BookNumber)
);
insert into Book
(BookNumber, BookName, BookPrice, CoverType, PublicationDate)
values
(1, 'Math', 023.64, 'paperback', '1999-02-05'),
(3, 'English', 999.36, 'e-book', '2013-06-04'),
(5, 'Calc', 056.69, 'paperback', '1964-05-05'),
(6, 'C++', 053.98, 'paperback', '2016-03-04'),
(7, 'Programming', 113.02, 'paperback', '2001-10-11'),
(9, 'Art', 250.99, 'paperback', '1996-11-11'),
(10, 'Networking', 036.64, 'e-book', '2014-05-06'),
(12, 'Drawing', 111.36, 'paperback', '2013-06-04'),
(13, 'Robotics', 012.03, 'e-book', '2016-06-05'),
(14, 'Computer', 25.03, 'e-book', '2001-06-04')
;
insert into Book
(BookNumber, BookName, BookPrice, PublicationDate)
values
(2, 'Reading', 364.20, '2016-06-05'),
(4, 'Database', 036.64, '2017-06-05'),
(8, 'Wellness', 050.00, '1999-10-10'),
(11, 'Linux', 55.36, '2013-06-08'),
(15, 'FYE', 03.01, '1991-02-05')
;
insert into Course
(CourseNo, CourseName, Semester, BookNumber)
values
(111, 'IntroToMath', 'Spring', 1),
(222, 'IntroToReading', 'Fall', 2),
(333, 'IntroToEnglish', 'Spring', 3),
(444, 'IntroToDatabase', 'Fall', 4),
(555, 'IntroToCalc', 'Spring', 5),
(666, 'IntroToC++', 'Spring', 6),
(777, 'IntroToProgramming', 'Fall', 7),
(888, 'IntroToWellness','Spring', 8),
(999, 'IntroToArt', 'Fall', 9),
(010, 'IntroToNetworking', 'Spring', 10),
(011, 'IntroToLinux', 'Fall', 11),
(012, 'IntroToDrawing', 'Spring', 12),
(013, 'IntroToRobotics', 'Fall', 13),
(014, 'IntroToComputer', 'Spring', 14),
(015, 'IntroToFYE', 'Summer', 15)
;
I do not see what I did differently. Any suggestions would be greatly appreciated. If someone can run the first code on top and tell me if it works for them or not, that would be big help also. Thank you!
At the time of creating Patients table, owner and Doctor table information is not available. So it is not allowing you.
So just create the table first.
and then add FK with alter table.
You're adding you tables in the wrong order.
You create the Patients table with foreign key constraints before you create the tables/fields they rely on to exist.

Syntax Problems? Insert Into

I'm creating an online store/database in MySQL, and I'm fairly new to the system.
Please tell me what is wrong with this statement:
CREATE TABLE IF NOT EXISTS Customers (
ID INT(15) NOT NULL AUTO_INCREMENT, -- Connects to table 'Orders' andOrderUserID
Email VARCHAR(100) NOT NULL,
PersonName VARCHAR(100) NOT NULL,
City VARCHAR(90) NOT NULL,
Zip INT(10),
CustomerState VARCHAR(50),
Address VARCHAR(200) NOT NULL,
Country VARCHAR(20),
Phone INT(50),
PRIMARY KEY (ID)
);
INSERT INTO Customers (ID, Email, PersonName, City, Zip, CustomerState, Address, Country, Phone) VALUES
(1, '1#gmail.com', 'Fred', 'Brisbane', 6543, 'QLD', '20 Not telling avenue', 'Australia', 932363626),
(2, '2#gmail.com', 'Mark', 'Perth', 3212, 'WA', ' 122 cralic road', 'Australia', 93223463221),
(3, '3#gmail.com', 'Leo', 'Melbourne', 2342, 'VIC', '90 normanly drive', 'Australia', 932645321),
(4, '4#gmail.com', 'Nick', 'Perth', 4294, 'WA', '44 happylane drive', 'Australia', 932222565621),
(5, '5#gmail.com', 'Mary', 'Perth', 6383, 'WA', '3 iverno crescent', 'Australia', 93365543321),
(6, '6#gmail.com', 'Julie', 'Perth', 9563, 'WA', '100 richmane lane', 'Australia', 9323456421),
(7, '7#gmail.com', 'Cynthia', 'Hobart', 3456, 'TAS', '12 trump avenue', 'Australia', 9322364641),
(8, '8#gmail.com', 'Mike', 'Albany', 7544, 'WA', '23 mapatazzie road', 'Australia', 9322236421),
(9, '9#gmail.com', 'Brett', 'Adelaide', 8953, 'SA', '983 chindes lane', 'Australia', 9322234651),
(10, '10#gmail.com', 'Paul', 'Sydney', 7853, 'VIC', '78 yives road', 'Australia', 932223463621),
1.) Always post which error you get when asking for help
2.) You left a comma , at the last line
3.) int(50) does not do what you think it does. It doesn't mean a number that has 50 digits. int means data type, and it will use 4 bytes. 50, in your case, means display length. Not storage length. int is 4 bytes. It can display up to 11 digits only (with minus sign included). Therefore, your int(50) doesn't make it 50 digits long, it will hold at maximum (2^32 - 1) = 4294967295.
Make your Phone a varchar
Update Last line with below line:
(10, '10#gmail.com', 'Paul', 'Sydney', 7853, 'VIC', '78 yives road', 'Australia', 932223463621);
add semicolon
Solution:
The problem is, you mentioned Phone field as integer and given values like 93223463221.
The integer value is greater than 2147483647, hence the error.
To solve,
alter table Customers modify column Phone varchar(50);
Use the above query to modify the Phone(Integer) filed as varchar field.
Then try to insert the values.

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;
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.