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

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.

Related

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

Many-to-Many Tables SQL

I have to do a many to many tables between a table MEAL and a table RESTAURANT. First I created the two tables:
CREATE TABLE RESTAURANT (
ID_Restaurant VARCHAR(10) NOT NULL,
ID_Hotel VARCHAR(10) NOT NULL,
Name VARCHAR(30) NOT NULL,
Number_of_Tables INT(3) NOT NULL,
PRIMARY KEY (ID_Restaurant),
FOREIGN KEY (ID_Hotel) REFERENCES HOTEL (ID_Hotel));
CREATE TABLE MEAL(
ID_Meal VARCHAR(10) NOT NULL,
Name VARCHAR(30) NOT NULL,
Preparation_Time VARCHAR(20),
Cooking_Time VARCHAR(20),
PRIMARY KEY (ID_Meal));
Then I created the 'joint' tables:
CREATE TABLE MEAL_SERVED(
ID_Meal VARCHAR(10) NOT NULL,
ID_Restaurant VARCHAR(10) NOT NULL,
Price INT(5) NOT NULL,
PRIMARY KEY (ID_Meal, ID_Restaurant, Price),
FOREIGN KEY(ID_Meal) REFERENCES MEAL(ID_Meal),
FOREIGN KEY(ID_Restaurant) REFERENCES RESTAURANT (ID_Restaurant));
I entered some data in the first two tables:
INSERT INTO RESTAURANT(ID_Restaurant, ID_Hotel, Name, Number_of_Tables)
VALUES ('REST1', 'H1', 'Benares Indisk Restaurant', 26);
('REST2', 'H2', 'La Gaichel', 35),
('REST3', 'H3', 'Tapas Restaurant', 17),
('REST4', 'H4', 'Faubourg 101', 19),
('REST5', 'H5', 'Pizzeria Roma', 38);
INSERT INTO MEAL(ID_Meal, Name, Preparation_Time, Cooking_Time)
VALUES ('MEAL1', 'Croque-Monsieur', '5 min', '4 min'),
('MEAL2', 'Salad', '6 min', NULL),
('MEAL3', 'Hot Dog', '3 min', '2min'),
('MEAL4', 'Panini', '6 min', '5 min'),
('MEAL5', 'Coca-Cola', NULL, NULL);
Until, now there is no problem, but when I tried to enter data in the third tables with:
INSERT INTO MEAL_SERVED(ID_Meal, ID_Restaurant, Price)
VALUES ('MEAL1', 'REST1', 50),
('MEAL4', 'REST1', 50),
('MEAL5', 'REST1', 35),
('MEAL1', 'REST2', 3.5),
('MEAL2', 'REST2', 3.5),
('MEAL4', 'REST2', 5);
for instance, then I have an error message:
FOREIGN KEY constraint failed: INSERT INTO MEAL_SERVED(ID_Meal, ID_Restaurant, Price)
I don't understand why I have this message and how to correct it. Thanks in advance.
As Shadow said in the comment section.
First, fix your typo in one of your SQL statements:
INSERT INTO RESTAURANT(ID_Restaurant, ID_Hotel, Name, Number_of_Tables)
VALUES ('REST1', 'H1', 'Benares Indisk Restaurant', 26);
('REST2', 'H2', 'La Gaichel', 35),
('REST3', 'H3', 'Tapas Restaurant', 17),
('REST4', 'H4', 'Faubourg 101', 19),
('REST5', 'H5', 'Pizzeria Roma', 38);
should be:
INSERT INTO RESTAURANT(ID_Restaurant, ID_Hotel, Name, Number_of_Tables)
VALUES ('REST1', 'H1', 'Benares Indisk Restaurant', 26),
('REST2', 'H2', 'La Gaichel', 35),
('REST3', 'H3', 'Tapas Restaurant', 17),
('REST4', 'H4', 'Faubourg 101', 19),
('REST5', 'H5', 'Pizzeria Roma', 38);
Second:
Do yourself a favor and use integers for primary keys and with the auto_increment option, you can just ignore the id.
CREATE TABLE RESTAURANT (
`id` INT NOT NULL AUTO_INCREMENT,
`hotel` INT NOT NULL,
`name` VARCHAR(30) NOT NULL,
`numberOfTables` INT NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (hotel) REFERENCES HOTEL (`id`));
CREATE TABLE MEAL(
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(30) NOT NULL,
`preparationTime` VARCHAR(20),
`cookingTime` VARCHAR(20),
PRIMARY KEY (`id`));
CREATE TABLE MEAL_SERVED(
`id` INT NOT NULL AUTO_INCREMENT,
`meal` INT NOT NULL,
`restaurant` INT NOT NULL,
Price FLOAT NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY(meal) REFERENCES MEAL(`id`),
FOREIGN KEY(restaurant) REFERENCES RESTAURANT (`id`));
Data:
INSERT INTO RESTAURANT(`hotel`, `name`, `numberOfTables`)
VALUES ('1', 'Benares Indisk Restaurant', 26),
(2, 'La Gaichel', 35),
(3, 'Tapas Restaurant', 17),
(4, 'Faubourg 101', 19),
(5, 'Pizzeria Roma', 38);
INSERT INTO MEAL(`name`, `preparationTime`, `cookingTime`)
VALUES ('Croque-Monsieur', '5 min', '4 min'),
('Salad', '6 min', NULL),
('Hot Dog', '3 min', '2min'),
('Panini', '6 min', '5 min'),
('Coca-Cola', NULL, NULL);
INSERT INTO MEAL_SERVED(`meal`, `restaurant`, `price`)
VALUES (1, 1, 50),
(4, 1, 50),
(5, 1, 35),
(1, 2, 3.5),
(2, 2, 3.5),
(4, 2, 5);
Third:
INT(3) has no effects. The space for the INT will be allocated anyway.
You can use TINYINT or SMALLINT instad, maybe. Look here: https://dev.mysql.com/doc/refman/5.7/en/integer-types.html
Fourth:
Use camel-case or snake-case but not both please ;-)

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.

Trying to perform an insert into

Been trying to perform an insert into command for a table called customers. However whenever I try to do a query I get the following error.
Error Code: 1364. Field 'customer_address' doesn't have a default value 0.000 sec
I'm not entirely sure how I would go about setting a default value. Any suggestions?
lock table customers write;
set foreign_key_checks = 0;
alter table customers modify customer_id integer auto_increment;
insert into customers (customer_first_name, customer_last_name)
values ("Nathan", "Rivera"),
("thom", "something");
Here is the script used to create my table
-- *************************************************************
-- This script creates the database
-- **REVISED by PRP to remove some orders
-- **REVISED by PRP to add artist table
-- **REVISED by PRP to link employee table to orders
-- *************************************************************
DROP TABLE IF EXISTS customers;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS order_details;
DROP TABLE IF EXISTS items;
DROP TABLE IF EXISTS artists;
DROP TABLE IF EXISTS employees;
-- create tables
CREATE TABLE customers
(
customer_id INT ,
customer_first_name VARCHAR(20),
customer_last_name VARCHAR(20) NOT NULL,
customer_address VARCHAR(50) NOT NULL,
customer_city VARCHAR(20) NOT NULL,
customer_state CHAR(2) NOT NULL,
customer_zip CHAR(5) NOT NULL,
customer_phone CHAR(10) NOT NULL,
customer_fax CHAR(10),
CONSTRAINT customers_pk
PRIMARY KEY (customer_id)
);
CREATE TABLE artists
(
artist_id INT NOT NULL,
artist_name VARCHAR(30),
CONSTRAINT artist_pk
PRIMARY KEY (artist_id)
);
CREATE TABLE items
(
item_id INT NOT NULL,
title VARCHAR(50) NOT NULL,
artist_id INT NOT NULL,
unit_price DECIMAL(9,2) NOT NULL,
CONSTRAINT items_pk
PRIMARY KEY (item_id),
CONSTRAINT items_fk_artists
FOREIGN KEY (artist_id) REFERENCES artists (artist_id)
);
CREATE TABLE employees
(
employee_id INT NOT NULL,
last_name VARCHAR(20) NOT NULL,
first_name VARCHAR(20) NOT NULL,
manager_id INT
,
CONSTRAINT employees_pk
PRIMARY KEY (employee_id),
CONSTRAINT emp_fk_mgr FOREIGN KEY (manager_id) REFERENCES employees(employee_id) );
CREATE TABLE orders
(
order_id INT NOT NULL,
customer_id INT NOT NULL,
order_date DATE NOT NULL,
shipped_date DATE,
employee_id INT,
CONSTRAINT orders_pk
PRIMARY KEY (order_id),
CONSTRAINT orders_fk_customers
FOREIGN KEY (customer_id) REFERENCES customers (customer_id),
CONSTRAINT orders_fk_employees
FOREIGN KEY (employee_id) REFERENCES employees (employee_id)
);
CREATE TABLE order_details
(
order_id INT NOT NULL,
item_id INT NOT NULL,
order_qty INT NOT NULL,
CONSTRAINT order_details_pk
PRIMARY KEY (order_id, item_id),
CONSTRAINT order_details_fk_orders
FOREIGN KEY (order_id)
REFERENCES orders (order_id),
CONSTRAINT order_details_fk_items
FOREIGN KEY (item_id)
REFERENCES items (item_id)
);
-- insert rows into tables
INSERT INTO customers VALUES
(1,'Korah','Blanca','1555 W Lane Ave','Columbus','OH','43221','6145554435','6145553928'),
(2,'Yash','Randall','11 E Rancho Madera Rd','Madison','WI','53707','2095551205','2095552262'),
(3,'Johnathon','Millerton','60 Madison Ave','New York','NY','10010','2125554800',NULL),
(4,'Mikayla','Davis','2021 K Street Nw','Washington','DC','20006','2025555561',NULL),
(5,'Kendall','Mayte','4775 E Miami River Rd','Cleves','OH','45002','5135553043',NULL),
(6,'Kaitlin','Hostlery','3250 Spring Grove Ave','Cincinnati','OH','45225','8005551957','8005552826'),
(7,'Derek','Chaddick','9022 E Merchant Wy','Fairfield','IA','52556','5155556130',NULL),
(8,'Deborah','Davis','415 E Olive Ave','Fresno','CA','93728','5595558060',NULL),
(9,'Karina','Lacy','882 W Easton Wy','Los Angeles','CA','90084','8005557000',NULL),
(10,'Kurt','Nickalus','28210 N Avenue Stanford','Valencia','CA','91355','8055550584','055556689'),
(11,'Kelsey','Eulalia','7833 N Ridge Rd','Sacramento','CA','95887','2095557500','2095551302'),
(12,'Anders','Rohansen','12345 E 67th Ave NW','Takoma Park','MD','24512','3385556772',NULL),
(13,'Thalia','Neftaly','2508 W Shaw Ave','Fresno','CA','93711','5595556245',NULL),
(14,'Gonzalo','Keeton','12 Daniel Road','Fairfield','NJ','07004','2015559742',NULL),
(15,'Ania','Irvin','1099 N Farcourt St','Orange','CA','92807','7145559000',NULL),
(16,'Dakota','Baylee','1033 N Sycamore Ave.','Los Angeles','CA','90038','2135554322',NULL),
(17,'Samuel','Jacobsen','3433 E Widget Ave','Palo Alto','CA','92711','4155553434',NULL),
(18,'Justin','Javen','828 S Broadway','Tarrytown','NY','10591','8005550037',NULL),
(19,'Kyle','Marissa','789 E Mercy Ave','Phoenix','AZ','85038','9475553900',NULL),
(20,'Erick','Kaleigh','Five Lakepointe Plaza, Ste 500','Charlotte','NC','28217','7045553500',NULL),
(21,'Marvin','Quintin','2677 Industrial Circle Dr','Columbus','OH','43260','6145558600','6145557580'),
(22,'Rashad','Holbrooke','3467 W Shaw Ave #103','Fresno','CA','93711','5595558625','5595558495'),
(23,'Trisha','Anum','627 Aviation Way','Manhatttan Beach','CA','90266','3105552732',NULL),
(24,'Julian','Carson','372 San Quentin','San Francisco','CA','94161','6175550700',NULL),
(25,'Kirsten','Story','2401 Wisconsin Ave NW','Washington','DC','20559','2065559115',NULL);
INSERT INTO artists(artist_id,artist_name) VALUES
(10, 'Umani'),
(11, 'The Ubernerds'),
(12, 'No Rest For The Weary'),
(13, 'Burt Ruggles'),
(14, 'Sewed the Vest Pocket'),
(15, 'Jess & Odie'),
(16, 'Onn & Onn');
INSERT INTO items (item_id,title,artist_id,unit_price) VALUES
(1,'Umami In Concert',10,17.95),
(2,'Race Car Sounds',11,13),
(3,'No Rest For The Weary',12,16.95),
(4,'More Songs About Structures and Comestibles',12,17.95),
(5,'On The Road With Burt Ruggles',13,17.5),
(6,'No Fixed Address',14,16.95),
(7,'Rude Noises',15,13),
(8,'Burt Ruggles: An Intimate Portrait',13,17.95),
(9,'Zone Out With Umami',10,16.95),
(10,'Etcetera',16,17);
INSERT INTO employees VALUES
(1,'Smith', 'Cindy', null),
(2,'Jones', 'Elmer', 1),
(3,'Simonian', 'Ralph', 2),
(9,'Locario', 'Paulo',1),
(8,'Leary', 'Rhea',9),
(4,'Hernandez','Olivia',9),
(5,'Aaronsen', 'Robert',4),
(6,'Watson', 'Denise',8),
(7,'Hardy', 'Thomas',2);
INSERT INTO orders VALUES
(19, 1, '2012-10-23', '2012-10-28', 6),
(29, 8, '2012-11-05', '2012-11-11', 6),
(32, 11, '2012-11-10', '2012-11-13', NULL),
(45, 2, '2012-11-25', '2012-11-30', NULL),
(70, 10, '2012-12-28', '2013-01-07', 5),
(89, 22, '2013-01-20', '2013-01-22', 7),
(97, 20, '2013-01-29', '2013-02-02', 5),
(118, 3, '2013-02-24', '2013-02-28', 7),
(144, 17, '2013-03-21', '2013-03-29', NULL),
(158, 9, '2013-04-04', '2013-04-20', NULL),
(165, 14, '2013-04-11', '2013-04-13', NULL),
(180, 24, '2013-04-25', '2013-05-30', NULL),
(231, 15, '2013-06-14', '2013-06-22', NULL),
(242, 23, '2013-06-24', '2013-07-06', 3),
(264, 9, '2013-07-15', '2013-07-18', 6),
(298, 18, '2013-08-18', '2013-09-22', 3),
(321, 2, '2013-09-09', '2013-10-05', 6),
(381, 7, '2013-11-08', '2013-11-16', 7),
(413, 17, '2013-12-05', '2014-01-11', 7),
(442, 5, '2013-12-28', '2014-01-03', 5),
(479, 1, '2014-01-30', '2014-03-03', 3),
(491, 16, '2014-02-08', '2014-02-14', 5),
(523, 3, '2014-03-07', '2014-03-15', 3),
(548, 2, '2014-03-22', '2014-04-18', NULL),
(550, 17, '2014-03-23', '2014-04-03', NULL),
(601, 16, '2014-04-21', '2014-04-27', NULL),
(607, 20, '2014-04-25', '2014-05-04', NULL),
(624, 2, '2014-05-04', '2014-05-09', NULL),
(627, 17, '2014-05-05', '2014-05-10', NULL),
(630, 20, '2014-05-08', '2014-05-18', 7),
(651, 12, '2014-05-19', '2014-06-02', 7),
(658, 12, '2014-05-23', '2014-06-02', 7),
(687, 17, '2014-06-05', '2014-06-08', NULL),
(693, 9, '2014-06-07', '2014-06-19', NULL),
(703, 19, '2014-06-12', '2014-06-19', 7),
(778, 13, '2014-07-12', '2014-07-21', 7),
(796, 17, '2014-07-19', '2014-07-26', 5),
(800, 19, '2014-07-21', '2014-07-28', NULL),
(802, 2, '2014-07-21', '2014-07-31', NULL),
(824, 1, '2014-08-01', NULL, NULL),
(827, 18, '2014-08-02', NULL, NULL),
(829, 9, '2014-08-02', NULL, NULL);
INSERT INTO order_details VALUES
(381,1,1),(601,9,1),
(442,1,1),
(523,9,1),
(630,5,1),
(778,1,1),
(693,10,1),
(118,1,1),
(264,7,1),
(607,10,1),
(624,7,1),
(658,1,1),
(800,5,1),
(158,3,1),
(321,10,1),
(687,6,1),
(827,6,1),
(144,3,1),
(479,1,2),
(630,6,2),(796,5,1),(97,4,1),
(601,5,1),
(800,1,1),
(29,10,1),
(70,1,1),
(165,4,1),
(180,4,1),
(231,10,1),
(413,10,1),
(491,6,1),
(607,3,1),
(651,3,1),
(703,4,1),
(802,3,1),
(824,7,2),
(829,1,1),
(550,4,1),
(796,7,1),
(693,6,1),
(29,3,1),
(32,7,1),
(242,1,1),
(298,1,1),
(479,4,1),
(548,9,1),
(627,9,1),
(778,3,1),
(19,5,1),
(89,4,1),
(242,6,1),
(264,4,1),
(550,1,1),
(693,7,3),
(824,3,1),
(829,5,1),
(829,9,1);
Error Code: 1364. Field 'customer_address' doesn't have a default value 0.000 sec
That means that the customer_address column is set to NOT allow NULL values and that no default value has been specified. Therefore your insert statement must include the customer address field.
insert into customers (customer_first_name, customer_last_name, customer_address)
values ("Nathan", "Rivera", "1000 Fred St."),
("thom", "something", "9659 Foobar Dr.");
Now that you've added your table defs it's clear that you'll need the other fields that you've marked NOT NULL as well in the insert statement, or you'll see a similar error for each one you leave out.
You can specify a default value using the DEFAULT option in the CREATE TABLE query.
CREATE TABLE customers
(
customer_id INT,
customer_first_name VARCHAR(20),
customer_last_name VARCHAR(20) NOT NULL,
customer_address VARCHAR(50) NOT NULL DEFAULT '',
customer_city VARCHAR(20) NOT NULL DEFAULT '',
customer_state CHAR(2) NOT NULL DEFAULT '',
customer_zip CHAR(5) NOT NULL DEFAULT '',
customer_phone CHAR(10) NOT NULL DEFAULT '',
customer_fax CHAR(10),
CONSTRAINT customers_pk
PRIMARY KEY (customer_id)
);

MySQL Shopping Cart Structure

I am looking to create a MySQL shop that is capable of handling multiple categories. I have all of the category facility etc sorted but the bit I am not getting anywhere with is this..
Each item can have multiple options, for example a T-Shirt should have the options 'Colour' and 'Size'. I then need to create a number of variations/ derived products from the parent product specifying that an Extra Large Blue T-Shirt has 20 in stock (for example). The problem is, it's not just clothes being sold, it could be any number of things. So I also need this schema to be able to handle an infinite number of variants such as '6mm' 'Large' Birthday Card with 'Sports Car' design. 6mm, Large, and 'Ace' being the variables. This way I am able to ensure that we do not have any stock control issues. If it is any use to you, below is my current site structure.
Existing Database Schema http://www.hallwaystudios.com/screenshots/uploads/g5B7SNKU.png
I hope you understand what I mean and that someone has an answer to my problem! Many thanks in advance (and after of-course)
Not too sure what the problem is here... I'd probably create four tables:
-- a table of item types (t-shirt, birthday card, etc.)
CREATE TABLE ItemTypes (
TypeID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
TypeName VARCHAR(20) NOT NULL
);
-- a table of associated properties
CREATE TABLE TypeProperties (
PropertyID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
TypeID INT NOT NULL,
PropName VARCHAR(20) NOT NULL,
INDEX(Property, TypeID),
FOREIGN KEY(TypeID) REFERENCES ItemTypes(TypeID)
);
-- a table of specific items (XL Blue t-shirt, large bday card w/sports car, etc.)
CREATE TABLE Items (
ItemID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
TypeID INT NOT NULL,
ItemName VARCHAR(100) NOT NULL,
ItemPrice DECIMAL UNSIGNED NOT NULL,
ItemStock INT UNSIGNED NOT NULL,
INDEX(ItemID, TypeID),
FOREIGN KEY(TypeID) REFERENCES ItemTypes(TypeID)
);
-- the dictionary of property values
CREATE TABLE ItemProperties (
ItemID INT NOT NULL,
TypeID INT NOT NULL,
PropertyID INT NOT NULL,
Value VARCHAR(20) NOT NULL,
PRIMARY KEY(ItemID, Property),
INDEX(ItemID, TypeID),
INDEX(PropertyID, TypeID),
FOREIGN KEY( TypeID) REFERENCES ItemTypes ( TypeID),
FOREIGN KEY(ItemID, TypeID) REFERENCES Items (ItemID, TypeID),
FOREIGN KEY(PropertyID, TypeID) REFERENCES TypeProperties(PropertyID, TypeID)
);
It ought to be fairly obvious, but just in case, the example data would look something like:
INSERT INTO ItemTypes (TypeID, TypeName) VALUES
(1, 'T-Shirt' ),
(2, 'Birthday Card'),
(3, 'Balloon' );
INSERT INTO TypeProperties(PropertyID, TypeID, PropName) VALUES
(51, 1, 'Colour' ), (52, 1, 'Size'),
(53, 2, 'Size/mm'), (54, 2, 'Size'), (55, 2, 'Design'),
(56, 3, 'Colour' );
INSERT INTO Items (ItemID, TypeID, ItemName, ItemPrice, ItemStock) VALUES
(101, 1, 'Extra Large Blue T-Shirt', 10.99, 20),
(102, 2, '6mm Large Birthday Card with Sports Car Design', 2.99, 17),
(103, 1, 'Extra Large Black T-Shirt', 10.99, 5),
(104, 3, 'Pink balloon', 0.10, 60);
INSERT INTO ItemProperties (ItemID, TypeID, PropertyID, Value) VALUES
(101, 1, 51, 'Blue' ),
(101, 1, 52, 'Extra Large'),
(102, 2, 53, '6' ),
(102, 2, 54, 'Large' ),
(102, 2, 55, 'Sports Car' ),
(103, 1, 51, 'Black' ),
(103, 1, 52, 'Extra Large'),
(104, 3, 56, 'Pink' );