Normalizing Database to 3NF - mysql

I'm creating a database that as to have at least 5 tables in 3NF. From what I can gather the database is 3NF when I look at the specifications for a 3NF daabase, but I'm not 100% sure which is why I'm asking here, and if it's not I'm wondering how to make it 3NF, so I can move on and try test queries, triggers and procedures on the database.
I have been basing my work off this sample database, which I assume is in 3NF from my understanding.
Any help or advice at all is really appreciated as I've been having this problem for a few days now.
CREATE TABLE IF NOT EXISTS xbox (
game_id char(4) NOT NULL default '',
game_name varchar(32) default NULL,
developer varchar(32) default NULL,
PRIMARY KEY (game_id)
);
INSERT INTO xbox (game_id, game_name, developer) VALUES
('1001', 'Sunset Overdive', 'Insomniac Games'),
('1002', 'Quantum Break', 'Remedy Entertainment'),
('1003', 'Gears of War 4', 'The Coalition'),
('1004', 'Halo 5', '343 Industries'),
('1005', 'Forza 6', 'Turn10 Studios');
CREATE TABLE IF NOT EXISTS playstation (
game_id char(4) NOT NULL default '',
game_name varchar(32) default NULL,
developer varchar(32) default NULL,
PRIMARY KEY (game_id)
);
INSERT INTO playstation (game_id, game_name, developer) VALUES
('2001', 'Bloodborne', 'From Software'),
('2002', 'The Witness', 'Thekla'),
('2003', 'Destiny', 'Bungie'),
('2004', 'Black Ops 3', 'Treyarch'),
('2005', 'DriveClub', 'Evolution');
CREATE TABLE IF NOT EXISTS pc (
game_id char(4) NOT NULL default '',
game_name varchar(32) default NULL,
developer varchar(32) default NULL,
PRIMARY KEY (game_id)
);
INSERT INTO pc (game_id, game_name, developer) VALUES
('3001', 'Fallout 4', 'Bethesda'),
('3002', 'Tomb Raider', 'Crystal Dynamics'),
('3003', 'Overwatch', 'Blizzard'),
('3004', 'Rocket League', 'Psyonix'),
('3005', 'DiRT Rally', 'Codemasters');
CREATE TABLE IF NOT EXISTS nintendo (
game_id char(4) NOT NULL default '',
game_name varchar(32) default NULL,
developer varchar(32) default NULL,
PRIMARY KEY (game_id)
);
INSERT INTO nintendo (game_id, game_name, developer) VALUES
('4001', 'Mario Kart 8', 'Nintendo'),
('4002', 'Mario Maker', 'Nintendo'),
('4003', 'Mario Party 9', 'Nintendo'),
('4004', 'Bayonetta 2', 'Nintendo'),
('4005', 'Splatoon', 'Nintendo');
CREATE TABLE IF NOT EXISTS sales (
customer_id char(10) NOT NULL default '',
game_id char(4) NOT NULL default '',
quantity int(11) default NULL,
paid double default NULL,
payment varchar(32) default NULL,
PRIMARY KEY (game_id)
);
INSERT INTO sales (customer_id, game_id, quantity, paid, payment) VALUES
('1234567890', '1002', 1, 79.99, 'Debit'),
('0987654321', '2003', 1, 50, 'Credit'),
('1122334455', '2001', 2, 120, 'Cash'),
('6677889900', '4004', 3, 110.49, 'Cash'),
('1357924680', '3002', 1, 50, 'Credit');
CREATE TABLE IF NOT EXISTS products (
game_id char(10) default NULL,
platform varchar(32) default NULL,
stock int(11) default NULL,
price double default NULL,
PRIMARY KEY (game_id)
);
INSERT INTO products (game_id, platform, stock, price) VALUES
('1001', 'xbox', 33, 69.99),
('1002', 'xbox', 42, 79.99),
('1003', 'xbox', 50, 89.99),
('1004', 'xbox', 80, 59.99),
('1005', 'xbox', 10, 49.99),
('2001', 'playstation', 12, 55.99),
('2002', 'playstation', 44, 34.99),
('2003', 'playstation', 89, 45.99),
('2004', 'playstation', 65, 39.99),
('2005', 'playstation', 73, 19.99),
('3001', 'pc', 50, 69.99),
('3002', 'pc', 41, 49.99),
('3003', 'pc', 77, 59.99),
('3004', 'pc', 24, 19.99),
('3005', 'pc', 19, 24.99),
('4001', 'nintendo', 10, 19.99),
('4002', 'nintendo', 17, 39.99),
('4003', 'nintendo', 33, 28.99),
('4004', 'nintendo', 53, 34.99),
('4005', 'nintendo', 97, 15.99);

Since your xbox, pc, nintendo and playstation tables are all identical you may want to consider combining them into one and adding some sort of "type" column (or, as Prix mentioned, maybe have a one-to-many between platform and type in case the game can run on more than one platform).
With the current design you will start running into problems with any queries that are not type specific. If I am following your sample correctly, you are going to have to use the value of the products.platform column in order to determine which platform table contains the details you are looking for. This leads to row-at-a-time processing which is very inefficient.

Related

Struggling to SELECT all elements from a table

MySQL is a new language to me and I struggle with selecting more data from my loans table when I do this query:
My objective is to print all elements of the Loans table that match the Bank IDs, all I get is outputs 1-10 where I have over 13 elements in my loans table.
EDIT 1: Bank Table serves as a link between all tables, I know the problem resides in my DML query however cluelessly not sure what to do.
When running my query, only matching primary key to foreign key appears. That is if Bank ID is 1 and Loans ID is 1 it shows, but when Bank ID is 1 and Loans ID is 13 it does not appear in the query.
Please save your criticism, as mentioned above, my experience is green.
My DML:
SELECT bank.bankID, bankcustomer.FirstName, bankcustomer.LastName, loans.FirstPaymentDate
FROM bank
JOIN bankcustomer ON bank.bankID = bankcustomer.customerID
JOIN loans ON loans.LoansID = bank.bankID;
Tables DDL:
CREATE TABLE bankCustomer(
CustomerID int(11) NOT NULL AUTO_INCREMENT,
FirstName varchar(20) DEFAULT NULL,
MiddleName varchar(20) DEFAULT NULL,
LastName varchar(20) DEFAULT NULL,
Address_Line1 varchar(50) DEFAULT NULL,
Address_Line2 varchar(50) DEFAULT NULL,
City varchar(20) DEFAULT NULL,
Region varchar(20) DEFAULT NULL,
PostCode varchar(20) DEFAULT NULL,
Country varchar(30) DEFAULT NULL,
DateOfBirth DATE DEFAULT NULL,
telephoneNumber int(13) DEFAULT 0,
openingAccount int CHECK (openingAccount >= 50),
PRIMARY KEY (CustomerID),
KEY CustomerID (CustomerID)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
CREATE TABLE bank(
BankID int(11) NOT NULL AUTO_INCREMENT,
customerID int,
PRIMARY KEY (BankID),
FOREIGN KEY (CustomerID) REFERENCES bankCustomer(CustomerID) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
CREATE TABLE loans(
LoansID int(11) NOT NULL AUTO_INCREMENT,
BankID int,
PaymentRate int(100) DEFAULT 300,
NumOfMonthlyPayments int(12) DEFAULT NULL,
FirstPaymentDate DATE DEFAULT NULL,
MonthlyDueDate DATE DEFAULT NULL,
PRIMARY KEY (LoansID),
FOREIGN KEY (BankID) REFERENCES bank(BankID) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
INSERT DML's:
INSERT INTO bank (BankID, CustomerID) VALUES (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10);
INSERT INTO loans (LoansID, BankID, PaymentRate, NumOfMonthlyPayments, FirstPaymentDate, MonthlyDueDate) VALUES (1, 1, 400, 12, '2008-02-03', '2008-03-25'),
(11, 1, 150, 10, '2008-02-04', '2008-04-25'),
(12, 1, 150, 10, '2008-02-07', '2008-04-25'),
(2, 2, 100, 20, '2011-04-01', '2011-04-25'),
(3, 3, 85, 5, '2015-07-03', '2015-08-25')...
Thank you all for your dear help, I managed to resolve my issue. The problem was the order of JOINing clauses.
SELECT loans.LoansID, bankcustomer.FirstName, customerbankcard.AccountNumber, loans.FirstPaymentDate
FROM bank
JOIN loans ON loans.BankID = bank.bankID
JOIN bankcustomer ON bankcustomer.customerID = bank.customerID
JOIN customerbankcard ON customerbankcard.bankID = bank.bankID
GROUP BY loans.LoansID ASC;
The outcome was to avoid loop, repeating wrongly assigned account numbers with customers whose IDs did not match.
If you want to select all tables use *. Also, you are joining tables incorrectly.
SELECT bank.*, bankcustomer.*, loans.*
FROM bank
JOIN bankcustomer ON bank.customerID = bankcustomer.customerID --Since you want to join data on customer ID you select custemerID in both tables
JOIN loans ON loans.BankID = bank.bankID; --The same problem here when joining tables
Goodluck and happy coding!
Firstly, joins could only be used between the tables when the columns are common between them (though they might have different names in different tables). That is why the concept of foreign key is of paramount importance as it references the column to the referenced table as you have duly done in your DDL commands.
Secondly, try using semicolon (;) as it denotes the end of any command and might get you out of the looping.

Code working on MySQL Workbench but not in liveSQL by oracle

I'm creating a DB with 2 tables.
When I run the code in MySQL Workbench it works but when I run it in Oracle's liveSQL (I'm not posting a link because I don't think it is permitted) it doesn't.
The script is:
CREATE TABLE supervisores (
supid INT(6) NOT NULL,
suparea VARCHAR(20) NOT NULL,
supnombre VARCHAR(40) NOT NULL,
supemail VARCHAR(50) NOT NULL,
PRIMARY KEY (supid)
);
CREATE TABLE nomina (
empid INT(6) NOT NULL,
empnombre VARCHAR(30) NOT NULL,
empedad INT(2) NOT NULL,
emparea VARCHAR(20) NOT NULL,
supid INT(6),
PRIMARY KEY (empid),
FOREIGN KEY (supid) REFERENCES supervisores (stupid)
);
INSERT INTO supervisors
VALUES
(100001, 'Ventas', 'Jaime Rodriguez', 'jrodriguz#empresa.com'),
(100002, 'IT', 'Camila Jimenez', 'cjimenez#empresa.com'),
(100003, 'Operaciones', 'Diana Delgado', 'ddelgado#empresa.com'),
(100004, 'RRHH', 'Ana Silgado', 'asilgado#empresa.com'),
(100005, 'IT', 'Pepito perez', 'cjimenez#empresa.com');
INSERT INTO nomina
VALUES
(106218, 'Jairo Caldera', 32, 'Ventas', 100001),
(107773, 'Carlos Saldarriaga', 31, 'IT', 100005),
(102801, 'Javier Zúñiga', 25, 'Operaciones', 100003),
(101845, 'Julian Hurtado', 27, 'RRHH', 100004),
(105358, 'Viviana Solano', 28, 'RRHH', 100004),
(101351, 'Karen Castillo', 35, 'Operaciones', 100003),
(104278, 'Sebastián Paternina', 42, 'IT', 100002),
(105377, 'Marina Álvarez', 19, 'IT', 100002),
(105002, 'Ricardo Colorado', 28, 'Operaciones', 100003),
(100178, 'Andrés Domínguez', 35, 'Operaciones', 100003),
(103710, 'Daniel Olaya', 22, 'Operaciones', 100003),
(101499, 'Crisrina Navarrete', 24, 'Operaciones', 100003);
SELECT empnombre, empedad, empid, emparea, supnombre, supemail
FROM supervisores, nomina
WHERE supervisores.supid = nomina.supid
ORDER BY empnombre;
RESULT IN MySQL Workbench:
MySQL Workbench
RESULT IN Oracle's LiveSQL:
ORA-00907: missing right parenthesis
Any help will be much appreciated.
Thanks in advance!

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

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.

MySQL. Average price, connecting two databases

I am learning MySQL and I currently do not understand how to do something.
I have two tables and I want to display some stuff out of it, it's pretty hard to explain so I'd rather show you.
These are my tables:
CREATE TABLE IF NOT EXISTS `proprietate` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`serie_buletin` varchar(8) NOT NULL,
`cnp` bigint(20) NOT NULL,
`nr_vehicul` int(11) NOT NULL,
`data_cumpararii` date NOT NULL,
`pret` int(11) NOT NULL,
`id_persoana` int(11) NOT NULL,
PRIMARY KEY (id),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;
CREATE TABLE IF NOT EXISTS `vehicul` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nr_vehicul` int(11) NOT NULL,
`marca` varchar(30) NOT NULL,
`id_marca` int(11) NOT NULL,
`tip` varchar(15) NOT NULL,
`culoare` varchar(15) NOT NULL,
`capacitate_cilindrica` int(11) NOT NULL,
`id_proprietate` int(11) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (id_proprietate) REFERENCES proprietate(id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;
And these are my values inside my tables:
INSERT INTO `proprietate` (`id`, `serie_buletin`, `cnp`, `nr_vehicul`, `data_cumpararii`, `pret`, `id_persoana`) VALUES
(1, 'AK162332', 2006036035087, 4, '2014-05-01', 35000, 1),
(2, 'AK162332', 2006036035087, 10, '2014-05-02', 90000, 2),
(3, 'AK176233', 6548751520125, 2, '2014-05-03', 55000, 3),
(4, 'BZ257743', 6548751520125, 2, '2014-05-04', 25000, 4),
(5, 'BZ257743', 2006036035087, 15, '2014-05-05', 63000, 5),
(6, 'DC456542', 2003564784513, 7, '2014-05-06', 30000, 6),
(7, 'EN654872', 2012654879521, 6, '2014-05-07', 50000, 7);
INSERT INTO `vehicul` (`id`, `nr_vehicul`, `marca`, `id_marca`, `tip`, `culoare`, `capacitate_cilindrica`, `id_proprietate`) VALUES
(1, 4, 'Mercedes', 1, 'CLK 350', 'negru', 3500, 1),
(2, 10, 'Mercedes', 1, 'S 500', 'silver', 5000, 2),
(3, 2, 'Mercedes', 1, 'ML 550', 'alb', 5500, 3),
(4, 2, 'BMW', 2, '325', 'galben', 2500, 4),
(5, 15, 'BMW', 2, 'X5', 'negru', 3500, 5),
(6, 7, 'Audi', 3, 'R5', 'mov', 5000, 6),
(7, 6, 'Audi', 3, 'Q5', 'metalic', 3000, 7);
What I want to display is:
marca | nr_vehicul | average_price
Audi | 13 | 40000
BMW | 17 | 44000
Mercedes | 16 | 60000
How can I do that? So far I have managed to display the first two columns but I have no idea how to reference the first table in the second and calculate the average price.
This is what I have so far:
SELECT marca, SUM(nr_vehicul) AS nr_vehicul FROM vehicul GROUP BY marca
Can anyone help me please?
You should join your tables to get combined information from both of them:
SELECT marca, SUM(vehicul.nr_vehicul) AS nr_vehicul, avg(pret) as pret
FROM vehicul
LEFT OUTER JOIN proprietate on (id_proprietate = proprietate.id)
GROUP BY marca;
see this sql fiddle session for the output.
First you select the data (column names with appropriate functions used) you need: marca, SUM(vehicul.nr_vehicul), AVG(pret), then you construct the joined structure from where mysql should retrieve these informations: vehicul, proprietate.
For this structure you need primarily the vehicul table, by which you will group the result set. You want to join the proprietate table to the vehicul table properly, to make sure the correct data structure is created. Since you have foreign key from one table to the other, the easiest way to do it is to use that key: LEFT OUTER JOIN proprietate on (id_proprietate = proprietate.id).
For more information on understanding the different JOIN types, please see this article by Craig Buckler.
$query = "SELECT type, AVG(pret) FROM vehicul GROUP BY marca";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "The average price of ". $row['type']. " is $".$row['AVG(price)'];}</code>
should return the average price per marca