MySQL doesn't seem to work correct - mysql

I have this college database where i have to Retrieve names of instructors teaching Computer Science courses, the sections (course number, section number, year, semester) they are teaching, and the total number of students in the sections
So far this is what have managed but the output is not correct.
SELECT i.instructor_id,o.year,o.semester,o.section_number, o.number as CourseNumber,o.Total_num_student
FROM Instructor i JOIN
(SELECT t.instructor_id,t.year,t.semester,t.section_number,o1.number,o1.Total_num_student
FROM teaches t JOIN
(SELECT COUNT(s.student_id) as Total_num_student,e.section_number,e.number
FROM Student s JOIN
enrolls e
ON s.student_id=e.student_id
GROUP BY section_number,e.number) as o1
ON o1.section_number=t.section_number) as o
on i.instructor_id=o.instructor_id
WHERE department='Computer Science';
Can you see what is wrong with the output.
create table Course (
number int,
title varchar(255),
credits int,
syllabus varchar(255),
PRIMARY KEY (number)
);
INSERT INTO Course VALUES (620,'Algorithm',3,'XYZ');
INSERT INTO Course VALUES (621,'Algorithm',3,'XYZ');
INSERT INTO Course VALUES (232,'Java',2,'ABC');
INSERT INTO Course VALUES (420,'Cpp',2,'PQRS');
INSERT INTO Course VALUES (720,'Big Data',3,'NVGY');
INSERT INTO Course VALUES (120,'Intelligent System',4,'KJHU');
INSERT INTO Course VALUES (220,'Operating System',3,'GED');
INSERT INTO Course VALUES (480,'Graphics',4,'RSFN');
INSERT INTO Course VALUES (520,'Distributed Networks',3,'NHU');
INSERT INTO Course VALUES (820,'Data Mining',3,'TYU');
INSERT INTO Course VALUES (700,'Cryptography',1,'MNO');
create table Student (
student_id int,
name varchar(255),
department varchar(255),
PRIMARY KEY (student_id)
);
INSERT INTO Student VALUES (2345,'Mike','Computer Science');
INSERT INTO Student VALUES (346,'Rob','Computer Science');
INSERT INTO Student VALUES (789,'Nick','Game Design');
INSERT INTO Student VALUES (675,'Sara','Computer Science');
INSERT INTO Student VALUES (123,'Raj','Chemical');
INSERT INTO Student VALUES (5331,'Jack','Biotech');
INSERT INTO Student VALUES (1023,'Michelle','Chemical');
INSERT INTO Student VALUES (9800,'Jie','Game Design');
INSERT INTO Student VALUES (7834,'Dan','Petroleum');
INSERT INTO Student VALUES (4567,'Patrick','Computer Science');
create table Instructor (
instructor_id int,
name varchar(255),
department varchar(255),
title varchar(255),
PRIMARY KEY (instructor_id)
);
INSERT INTO Instructor VALUES (12,'John','Computer Science','Java');
INSERT INTO Instructor VALUES (23,'Bischof','Mechanical','Drawing');
INSERT INTO Instructor VALUES (56,'Kwon','Biotech','Biology');
INSERT INTO Instructor VALUES (78,'Deever','Security','Cryptography');
INSERT INTO Instructor VALUES (45,'Jin','Computer Science','Cpp');
INSERT INTO Instructor VALUES (67,'Bailey','Petroleum','Metalurgy');
INSERT INTO Instructor VALUES (90,'Richard','Industrial','Manufacture');
INSERT INTO Instructor VALUES (11,'Joe','Chemical','Chemistry');
INSERT INTO Instructor VALUES (24,'Roger','Game Design','Cpp');
INSERT INTO Instructor VALUES (55,'Zack','Computer Science','Design');
create table CourseOffering (
number int,
year int,
semester int,
section_number int,
classroom int,
PRIMARY KEY (number,year,semester,section_number),
FOREIGN KEY (number) REFERENCES Course (number)
);
INSERT INTO CourseOffering VALUES (620,2012,2,102,3540);
INSERT INTO CourseOffering VALUES (621,2013,3,103,3545);
INSERT INTO CourseOffering VALUES (232,2011,1,103,3455);
INSERT INTO CourseOffering VALUES (420,2013,3,101,2650);
INSERT INTO CourseOffering VALUES (720,2013,3,100,3455);
INSERT INTO CourseOffering VALUES (120,2014,1,102,3000);
INSERT INTO CourseOffering VALUES (220,2008,2,104,1450);
INSERT INTO CourseOffering VALUES (480,2009,4,106,1400);
INSERT INTO CourseOffering VALUES (520,2010,3,102,3500);
INSERT INTO CourseOffering VALUES (820,2008,3,100,2700);
INSERT INTO CourseOffering VALUES (700,2012,1,103,2540);
create table CourseOffering_Timing (
number int,
year int,
semester int,
section_number int,
timing int,
PRIMARY KEY (number,year,semester,section_number,timing),
FOREIGN KEY (number) REFERENCES Course (number)
);
INSERT INTO CourseOffering_Timing VALUES (620,2012,2,102,9);
INSERT INTO CourseOffering_Timing VALUES (621,2013,3,103,9);
INSERT INTO CourseOffering_Timing VALUES (232,2011,1,103,3);
INSERT INTO CourseOffering_Timing VALUES (420,2013,3,101,2);
INSERT INTO CourseOffering_Timing VALUES (720,2013,3,100,5);
INSERT INTO CourseOffering_Timing VALUES (120,2014,1,102,4);
INSERT INTO CourseOffering_Timing VALUES (220,2008,2,104,10);
INSERT INTO CourseOffering_Timing VALUES (480,2009,4,106,12);
INSERT INTO CourseOffering_Timing VALUES (520,2010,3,102,4);
INSERT INTO CourseOffering_Timing VALUES (820,2008,3,100,2);
INSERT INTO CourseOffering_Timing VALUES (700,2012,1,103,3);
create table PreRequisite (
number int,
prerequisite_number int,
PRIMARY KEY (number,prerequisite_number),
FOREIGN KEY (number) REFERENCES Course(number)
);
INSERT INTO PreRequisite VALUES (620,480);
INSERT INTO PreRequisite VALUES (621,481);
INSERT INTO PreRequisite VALUES (232,120);
INSERT INTO PreRequisite VALUES (420,320);
INSERT INTO PreRequisite VALUES (720,580);
INSERT INTO PreRequisite VALUES (120,110);
INSERT INTO PreRequisite VALUES (220,130);
INSERT INTO PreRequisite VALUES (480,380);
INSERT INTO PreRequisite VALUES (520,360);
INSERT INTO PreRequisite VALUES (820,700);
INSERT INTO PreRequisite VALUES (700,610);
create table enrolls (
student_id int,
number int,
year int,
semester int,
section_number int,
grade varchar(10),
PRIMARY KEY (student_id,number,year,semester,section_number),
FOREIGN KEY (number,year,semester,section_number) REFERENCES CourseOffering(number,year,semester,section_number),
FOREIGN KEY (student_id) REFERENCES Student (student_id)
);
INSERT INTO enrolls VALUES (2345,620,2012,2,102,'A');
INSERT INTO enrolls VALUES (346,232,2011,1,103,'B');
INSERT INTO enrolls VALUES (789,420,2013,3,101,'A');
INSERT INTO enrolls VALUES (675,621,2013,3,103,'C');
INSERT INTO enrolls VALUES (123,120,2014,1,102,'B');
INSERT INTO enrolls VALUES (5331,220,2008,2,104,'A');
INSERT INTO enrolls VALUES (1023,480,2009,4,106,'B');
INSERT INTO enrolls VALUES (9800,520,2010,3,102,'A');
INSERT INTO enrolls VALUES (7834,820,2008,3,100,'C');
INSERT INTO enrolls VALUES (4567,700,2012,1,103,'F');
create table teaches (
instructor_id int,
number int,
year int,
semester int,
section_number int,
PRIMARY KEY (instructor_id, number, year, semester, section_number),
FOREIGN KEY (instructor_id) REFERENCES Instructor(instructor_id),
FOREIGN KEY ( number, year, semester, section_number) REFERENCES CourseOffering( number, year, semester, section_number)
);
INSERT INTO teaches VALUES (12,620,2012,2,102);
INSERT INTO teaches VALUES (12,621,2013,3,103);
INSERT INTO teaches VALUES (23,232,2011,1,103);
INSERT INTO teaches VALUES (56,420,2013,3,101);
INSERT INTO teaches VALUES (78,720,2013,3,100);
INSERT INTO teaches VALUES (45,120,2014,1,102);
INSERT INTO teaches VALUES (67,220,2008,2,104);
INSERT INTO teaches VALUES (90,480,2009,4,106);
INSERT INTO teaches VALUES (11,520,2010,3,102);
INSERT INTO teaches VALUES (24,820,2008,3,100);
INSERT INTO teaches VALUES (55,700,2012,1,103);

I'm not completely sure what you are trying to achieve but this is if you like to retain the section info:
SELECT a.instructor_id, group_concat(DISTINCT a.year SEPARATOR ', ') YEAR, group_concat(DISTINCT a.semester SEPARATOR ', ') SEMESTER, group_concat(DISTINCT a.section_number SEPARATOR ', ') SECTION_NUMBER, group_concat(DISTINCT a.coursenumber SEPARATOR ', ') COURSENUMBER, count(a.student_id) as Total_num_student FROM (
SELECT e.student_id, i.instructor_id, t.year,t.semester,t.section_number, t.number as CourseNumber
FROM instructor i
JOIN teaches t
ON i.instructor_id = t.instructor_id
JOIN enrolls e
ON e.section_number=t.section_number
WHERE department='Computer Science') a
GROUP BY a.instructor_id
;
SELECT a.instructor_id, a.year, a.semester, a.section_number, a.coursenumber, count(a.student_id) as Total_num_student FROM (
SELECT e.student_id, i.instructor_id, t.year,t.semester,t.section_number, t.number as CourseNumber
FROM instructor i
JOIN teaches t
ON i.instructor_id = t.instructor_id
JOIN enrolls e
ON e.section_number=t.section_number
WHERE department='Computer Science') a
GROUP BY a.instructor_id, a.section_number
;
SQLFiddle demo

See if this will work for you
SELECT Instructor.name AS instructor_name, teaches.section_number AS section_number,
teaches.year AS year, teaches.semester AS semester, COUNT(enrolls.student_id) FROM Instructor
INNER JOIN teaches ON teaches.instructor_id = Instructor.instructor_id
INNER JOIN enrolls ON teaches.section_number = enrolls.section_number AND teaches.semester = enrolls.semester AND teaches.year = enrolls.year
WHERE department='Computer Science'
GROUP BY Instructor.name, teaches.section_number, teaches.year, teaches.semester;

Related

How to count repeated project_no in mysql

imagine i have 3 table employee detail, project details and Works on(where it's set that which employee will work on which project).
The question is To show the average salary of all the employees work on those project which are having more than three employees.
create table Employee
(
empid varchar(5) primary key,
ename varchar(20),
address varchar(50),
phone bigint(10),
job varchar(10),
salary varchar(10)
);
insert into Employee values ('e101','Ankita','Delhi',9987654321,'Manager','12000');
insert into Employee values ('e102','Rahul','Banglore',9219849816,'Developer','80000');
insert into Employee values ('e103','Aditya','Noida',9517536842,'Chef','16000');
insert into Employee values ('e104','Rachna','Mumbai',7539561682,'Teacher','45000');
insert into Employee values ('e105','Karan','Delhi',8529517463,'Driver','18000');
create table Project
(
project_no varchar(5) primary key,
pname varchar(20),
no_of_hours int(3)
);
Insert into Project values('p101', 'Data Handling', 5);
Insert into Project values('p102', 'Game Develop', 3);
Insert into Project values('p103', 'New Recipe', 6);
Insert into Project values('p104', 'Question Paper', 9);
Insert into Project values('p105', 'Ride to Shimla', 12);
create table Works_on
(
empid varchar(5),
project_no varchar(5),
start_date date,
end_date date,
manager_name varchar(20),
primary key(empid, project_no),
foreign key(empid) references Employee(empid),
foreign key(project_no) references Project(project_no)
);
Insert into Works_on value('e101', 'p101', '2021-02-24', '2021-03-25', 'Anshika');
Insert into Works_on value('e102', 'p101', '2021-03-4', '2021-04-2', 'Anshika');
Insert into Works_on value('e103', 'p102', '2021-02-4', '2021-03-5', 'Roshan');
Insert into Works_on value('e104', 'p101', '2021-05-20', '2021-05-29', 'Anshika');
Insert into Works_on value('e105', 'p103', '2021-02-4', '2021-02-5', 'Dharmesh');
Insert into Works_on value('e103', 'p101', '2021-02-4', '2021-02-5', 'Anshika');
Insert into Works_on value('e105', 'p104', '2021-02-4', '2021-02-5', 'Ritika');
Insert into Works_on value('e105', 'p105', '2021-02-4', '2021-02-5', 'Sid');
select w.project_no project_number,
count(w.project_no) number_of_employees,
avg(e.salary) average_salary
from
Employee e, Project p, Works_on w
where
w.project_no= p.project_no
and w.empid=e.empid
group by w.project_no
having(count(w.project_no))>3;
DBFiddle

MYSQL Not all inserted values being displayed

I should be able to show customer name, pet name, procedures, costs and total costs in one SQL SELECT statement. I am only getting one row of values, of which one of the values is NULL. I can't figure out what is wrong with my SQL statement or tables that it is causing it to not return all of the inputted values.
Here is an image of what I am getting as a result:
CREATE DATABASE IF NOT EXISTS vet;
USE vet;
CREATE TABLE IF NOT EXISTS customer (
customer_id INT PRIMARY KEY AUTO_INCREMENT,
customer_name VARCHAR(70)
);
CREATE TABLE IF NOT EXISTS invoice (
invoice_id INT PRIMARY KEY AUTO_INCREMENT,
invoice_date DATE,
customer_id INT ,
CONSTRAINT FOREIGN KEY (customer_id) REFERENCES customer(customer_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS pet (
pet_id INT PRIMARY KEY AUTO_INCREMENT,
pet_name VARCHAR(50),
customer_id INT,
CONSTRAINT FOREIGN KEY (customer_id) REFERENCES customer(customer_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS procedures (
procedure_id INT PRIMARY KEY AUTO_INCREMENT,
procedure_name VARCHAR(70),
amount DECIMAL
);
CREATE TABLE IF NOT EXISTS invoice_pet (
invoice_id INT,
pet_id INT,
CONSTRAINT FOREIGN KEY (invoice_id) REFERENCES invoice(invoice_id),
CONSTRAINT FOREIGN KEY (pet_id) REFERENCES pet(pet_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS procedures_pet (
procedure_id INT,
pet_id INT,
CONSTRAINT FOREIGN KEY (procedure_id) REFERENCES procedures(procedure_id),
CONSTRAINT FOREIGN KEY (pet_id) REFERENCES pet(pet_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
INSERT INTO invoice (invoice_date) VALUES ('2004-04-05');
INSERT INTO invoice (invoice_date) VALUES ('2014-12-05');
INSERT INTO invoice (invoice_date) VALUES ('2009-08-29');
INSERT INTO invoice (invoice_date) VALUES ('2016-07-15');
INSERT INTO customer (customer_name) VALUES ('John Garett');
INSERT INTO customer (customer_name) VALUES ('Mary Wist');
INSERT INTO customer (customer_name) VALUES ('Beth Smith');
INSERT INTO customer (customer_name) VALUES ('Rick Sanchez');
INSERT INTO pet (pet_name, customer_id) VALUES ('Rover', 1);
INSERT INTO pet (pet_name, customer_id) VALUES ('Max', 3);
INSERT INTO pet (pet_name, customer_id) VALUES ('Munchie', 4);
INSERT INTO pet (pet_name, customer_id) VALUES ('Dixon', 2);
INSERT INTO pet (pet_name, customer_id) VALUES ('Lucky', 4);
INSERT INTO procedures (procedure_name, amount) VALUES ('Rabies Vaccination', 30.00);
INSERT INTO procedures (procedure_name, amount) VALUES ('Sterilization', 190.00);
INSERT INTO procedures (procedure_name, amount) VALUES ('Dental Surgery', 120.00);
INSERT INTO procedures (procedure_name, amount) VALUES ('Cystotomy', 200.00);
INSERT INTO invoice_pet (invoice_id, pet_id) VALUES (1, 1);
INSERT INTO invoice_pet (invoice_id, pet_id) VALUES (2, 1);
INSERT INTO invoice_pet (invoice_id, pet_id) VALUES (1, 4);
INSERT INTO invoice_pet (invoice_id, pet_id) VALUES (3, 2);
INSERT INTO procedures_pet (procedure_id, pet_id) VALUES (1, 1);
INSERT INTO procedures_pet (procedure_id, pet_id) VALUES (3, 2);
INSERT INTO procedures_pet (procedure_id, pet_id) VALUES (4, 4);
INSERT INTO procedures_pet (procedure_id, pet_id) VALUES (2, 1);
SELECT inv.invoice_id, cust.customer_name, p.pet_name, pro.procedure_name,
pro.amount AS cost, SUM(amount) AS totalcost
FROM vet.procedures pro LEFT JOIN vet.procedures_pet propet
ON pro.procedure_id = propet.procedure_id
LEFT JOIN vet.pet p
ON propet.pet_id = p.pet_id
LEFT JOIN vet.invoice_pet invpet
ON p.pet_id = invpet.pet_id
LEFT JOIN vet.invoice inv
ON invpet.invoice_id = inv.invoice_id
LEFT JOIN vet.customer cust
ON inv.customer_id = cust.customer_id
The following gives each customer, their pets, procedures those pets have had (if any) and the cost of those procedures.
You don't say what you mean by "total cost" (total per customer? per pet? grand total?) so I have gone with cost per customer (see correlated sub query)
SELECT c.customer_name,
p.pet_name,
pr.procedure_name,
pr.amount,
(
SELECT SUM(amount)
FROM procedures pr1
JOIN procedures_pet pp1
ON pr1.procedure_id = pp1.procedure_id
JOIN pet p1
ON p1.pet_id = pp1.pet_id
JOIN customer c1
ON p1.customer_id = c1.customer_id
WHERE c1.customer_id = c.customer_id
) AS totalcost
FROM customer c
JOIN pet p
ON c.customer_id = p.customer_id
LEFT JOIN procedures_pet pp
ON pp.pet_id = p.pet_id
LEFT JOIN procedures pr
ON pr.procedure_id = pp.procedure_id
ORDER BY customer_name,
pet_name,
procedure_name;
The problem is that your invoices do never get any "customer_id" because you do not insert a value here in your insert statement.
So all tuples have "null" for customer_id.
Answering here as I cannot comment yet:
Of course you'll have to add your customers to the database first in this case. Otherwise they cannot be referenced.

updating date in mysql (phpMyAdmin)

i have created a table called vendor and imported the document with these commands:
DROP TABLE IF EXISTS vendor;
CREATE TABLE VENDOR (
V_CODE int,
V_NAME varchar(15),
V_CONTACT varchar(50),
V_AREACODE varchar(3),
V_PHONE varchar(8),
V_STATE varchar(2),
V_ORDER varchar(1)
);
INSERT INTO VENDOR VALUES('21225','Bryson, Inc.','Smithson','615','223-3234','TN','Y');
INSERT INTO VENDOR VALUES('21226','SuperLoo, Inc.','Flushing','904','215-8995','FL','N');
INSERT INTO VENDOR VALUES('21231','D&E Supply','Singh','615','228-3245','TN','Y');
INSERT INTO VENDOR VALUES('21344','Gomez Bros.','Ortega','615','889-2546','KY','N');
INSERT INTO VENDOR VALUES('22567','Dome Supply','Smith','901','678-1419','GA','N');
INSERT INTO VENDOR VALUES('23119','Randsets Ltd.','Anderson','901','678-3998','GA','Y');
INSERT INTO VENDOR VALUES('24004','Brackman Bros.','Browning','615','228-1410','TN','N');
INSERT INTO VENDOR VALUES('24288','ORDVA, Inc.','Hakford','615','898-1234','TN','Y');
INSERT INTO VENDOR VALUES('25443','B&K, Inc.','Smith','904','227-0093',null ,'N');
INSERT INTO VENDOR VALUES('25501','Damal Supplies','Smythe','615','890-3529','TN','N');
INSERT INTO VENDOR VALUES('25595','Rubicon Systems','Orton','904','456-0092','FL','Y');
DROP TABLE IF EXISTS product;
CREATE TABLE PRODUCT (
P_CODE varchar(10),
P_DESCRIPT varchar(35),
P_INDATE datetime,
P_QOH int,
P_MIN int,
P_PRICE float(8),
P_DISCOUNT float(8),
V_CODE int
);
INSERT INTO PRODUCT VALUES('11QER/31','Power painter, 15 psi., 3-nozzle','11/3/2007','8','5','109.98999786377','0','25595');
INSERT INTO PRODUCT VALUES('13-Q2/P2','7.25-in. pwr. saw blade','12/13/2007','32','15','14.9899997711182','0.05','21344');
INSERT INTO PRODUCT VALUES('14-Q1/L3','9.00-in. pwr. saw blade','11/13/2007','18','12','17.4899997711182','0','21344');
INSERT INTO PRODUCT VALUES('1546-QQ2','Hrd. cloth, 1/4-in., 2x50','1/15/2008','15','8','39.9500007629395','0','23119');
INSERT INTO PRODUCT VALUES('1558-QW1','Hrd. cloth, 1/2-in., 3x50','1/15/2008','23','5','43.9900016784668','0','23119');
INSERT INTO PRODUCT VALUES('2232/QTY','B&D jigsaw, 12-in. blade','12/30/2007','8','5','109.919998168945','0.05','24288');
INSERT INTO PRODUCT VALUES('2232/QWE','B&D jigsaw, 8-in. blade','12/24/2007','6','5','99.870002746582','0.05','24288');
INSERT INTO PRODUCT VALUES('2238/QPD','B&D cordless drill, 1/2-in.','1/20/2008','12','5','38.9500007629395','0.05','25595');
INSERT INTO PRODUCT VALUES('23109-HB','Claw hammer','1/20/2008','23','10','9.94999980926514','0.1','21225');
INSERT INTO PRODUCT VALUES('23114-AA','Sledge hammer, 12 lb.','1/2/2008','8','5','14.3999996185303','0.05','');
INSERT INTO PRODUCT VALUES('54778-2T','Rat-tail file, 1/8-in. fine','12/15/2007','43','20','4.98999977111816','0','21344');
INSERT INTO PRODUCT VALUES('89-WRE-Q','Hicut chain saw, 16 in.','2/7/2008','11','5','256.989990234375','0.05','24288');
INSERT INTO PRODUCT VALUES('PVC23DRT','PVC pipe, 3.5-in., 8-ft','2/20/2008','188','75','5.86999988555908','0','');
INSERT INTO PRODUCT VALUES('SM-18277','1.25-in. metal screw, 25','3/1/2008','172','75','6.98999977111816','0','21225');
INSERT INTO PRODUCT VALUES('SW-23116','2.5-in. wd. screw, 50','2/24/2008','237','100','8.44999980926514','0','21231');
INSERT INTO PRODUCT VALUES('WR3/TT3','Steel matting, 4''x8''x1/6", .5" mesh','1/17/2008','18','5','119.949996948242','0.1','25595');
DROP TABLE IF EXISTS CUSTOMER;
CREATE TABLE CUSTOMER (
CUS_CODE int,
CUS_LNAME varchar(15),
CUS_FNAME varchar(15),
CUS_INITIAL varchar(1),
CUS_AREACODE varchar(3),
CUS_PHONE varchar(8),
CUS_BALANCE float(8)
);
INSERT INTO CUSTOMER VALUES('10010','Ramas','Alfred','A','615','844-2573','0');
INSERT INTO CUSTOMER VALUES('10011','Dunne','Leona','K','713','894-1238','0');
INSERT INTO CUSTOMER VALUES('10012','Smith','Kathy','W','615','894-2285','345.859985351562');
INSERT INTO CUSTOMER VALUES('10013','Olowski','Paul','F','615','894-2180','536.75');
INSERT INTO CUSTOMER VALUES('10014','Orlando','Myron','','615','222-1672','0');
INSERT INTO CUSTOMER VALUES('10015','O''Brian','Amy','B','713','442-3381','0');
INSERT INTO CUSTOMER VALUES('10016','Brown','James','G','615','297-1228','221.190002441406');
INSERT INTO CUSTOMER VALUES('10017','Williams','George','','615','290-2556','768.929992675781');
INSERT INTO CUSTOMER VALUES('10018','Farriss','Anne','G','713','382-7185','216.550003051758');
INSERT INTO CUSTOMER VALUES('10019','Smith','Olette','K','615','297-3809','0');
DROP TABLE IF EXISTS invoice;
CREATE TABLE INVOICE (
INV_NUMBER int,
CUS_CODE int,
INV_DATE datetime,
INV_SUBTOTAL float(8),
INV_TAX float(8),
INV_TOTAL float(8)
);
INSERT INTO INVOICE VALUES('1001','10014','1/16/2008','24.8999996185303','1.99000000953674','26.8899993896484');
INSERT INTO INVOICE VALUES('1002','10011','1/16/2008','9.97999954223633','0.800000011920929','10.7799997329712');
INSERT INTO INVOICE VALUES('1003','10012','1/16/2008','153.850006103516','12.3100004196167','166.160003662109');
INSERT INTO INVOICE VALUES('1004','10011','1/17/2008','34.9700012207031','2.79999995231628','37.7700004577637');
INSERT INTO INVOICE VALUES('1005','10018','1/17/2008','70.4400024414062','5.6399998664856','76.0800018310547');
INSERT INTO INVOICE VALUES('1006','10014','1/17/2008','397.829986572266','31.8299999237061','429.660003662109');
INSERT INTO INVOICE VALUES('1007','10015','1/17/2008','34.9700012207031','2.79999995231628','37.7700004577637');
INSERT INTO INVOICE VALUES('1008','10011','1/17/2008','399.149993896484','31.9300003051758','431.079986572266');
DROP TABLE IF EXISTS line;
CREATE TABLE LINE (
INV_NUMBER int,
LINE_NUMBER int,
P_CODE varchar(10),
LINE_UNITS float(8),
LINE_PRICE float(8),
LINE_TOTAL float(8)
);
INSERT INTO LINE VALUES('1001','1','13-Q2/P2','1','14.9899997711182','14.9899997711182');
INSERT INTO LINE VALUES('1001','2','23109-HB','1','9.94999980926514','9.94999980926514');
INSERT INTO LINE VALUES('1002','1','54778-2T','2','4.98999977111816','9.97999954223633');
INSERT INTO LINE VALUES('1003','1','2238/QPD','1','38.9500007629395','38.9500007629395');
INSERT INTO LINE VALUES('1003','2','1546-QQ2','1','39.9500007629395','39.9500007629395');
INSERT INTO LINE VALUES('1003','3','13-Q2/P2','5','14.9899997711182','74.9499969482422');
INSERT INTO LINE VALUES('1004','1','54778-2T','3','4.98999977111816','14.9700002670288');
INSERT INTO LINE VALUES('1004','2','23109-HB','2','9.94999980926514','19.8999996185303');
INSERT INTO LINE VALUES('1005','1','PVC23DRT','12','5.86999988555908','70.4400024414062');
INSERT INTO LINE VALUES('1006','1','SM-18277','3','6.98999977111816','20.9699993133545');
INSERT INTO LINE VALUES('1006','2','2232/QTY','1','109.919998168945','109.919998168945');
INSERT INTO LINE VALUES('1006','3','23109-HB','1','9.94999980926514','9.94999980926514');
INSERT INTO LINE VALUES('1006','4','89-WRE-Q','1','256.989990234375','256.989990234375');
INSERT INTO LINE VALUES('1007','1','13-Q2/P2','2','14.9899997711182','29.9799995422363');
INSERT INTO LINE VALUES('1007','2','54778-2T','1','4.98999977111816','4.98999977111816');
INSERT INTO LINE VALUES('1008','1','PVC23DRT','5','5.86999988555908','29.3500003814697');
INSERT INTO LINE VALUES('1008','2','WR3/TT3','3','119.949996948242','359.850006103516');
INSERT INTO LINE VALUES('1008','3','23109-HB','1','9.94999980926514','9.94999980926514');
DROP TABLE IF EXISTS EMP;
CREATE TABLE EMP (
EMP_NUM int,
EMP_TITLE varchar(4),
EMP_LNAME varchar(15),
EMP_FNAME varchar(15),
EMP_INITIAL varchar(1),
EMP_DOB datetime,
EMP_HIRE_DATE datetime,
EMP_AREACODE varchar(3),
EMP_PHONE varchar(8),
EMP_MGR int
);
INSERT INTO EMP VALUES('100','Mr.','Kolmycz','George','D','6/15/1942','3/15/1985','615','324-5456','');
INSERT INTO EMP VALUES('101','Ms.','Lewis','Rhonda','G','3/19/1965','4/25/1986','615','324-4472','100');
INSERT INTO EMP VALUES('102','Mr.','VanDam','Rhett','','11/14/1958','12/20/1990','901','675-8993','100');
INSERT INTO EMP VALUES('103','Ms.','Jones','Anne','M','10/16/1974','8/28/1994','615','898-3456','100');
INSERT INTO EMP VALUES('104','Mr.','Lange','John','P','11/8/1971','10/20/1994','901','504-4430','105');
INSERT INTO EMP VALUES('105','Mr.','Williams','Robert','D','3/14/1975','11/8/1998','615','890-3220','');
INSERT INTO EMP VALUES('106','Mrs.','Smith','Jeanine','K','2/12/1968','1/5/1989','615','324-7883','105');
INSERT INTO EMP VALUES('107','Mr.','Diante','Jorge','D','8/21/1974','7/2/1994','615','890-4567','105');
INSERT INTO EMP VALUES('108','Mr.','Wiesenbach','Paul','R','2/14/1966','11/18/1992','615','897-4358','');
INSERT INTO EMP VALUES('109','Mr.','Smith','George','K','6/18/1961','4/14/1989','901','504-3339','108');
INSERT INTO EMP VALUES('110','Mrs.','Genkazi','Leighla','W','5/19/1970','12/1/1990','901','569-0093','108');
INSERT INTO EMP VALUES('111','Mr.','Washington','Rupert','E','1/3/1966','6/21/1993','615','890-4925','105');
INSERT INTO EMP VALUES('112','Mr.','Johnson','Edward','E','5/14/1961','12/1/1983','615','898-4387','100');
INSERT INTO EMP VALUES('113','Ms.','Smythe','Melanie','P','9/15/1970','5/11/1999','615','324-9006','105');
INSERT INTO EMP VALUES('114','Ms.','Brandon','Marie','G','11/2/1956','11/15/1979','901','882-0845','108');
INSERT INTO EMP VALUES('115','Mrs.','Saranda','Hermine','R','7/25/1972','4/23/1993','615','324-5505','105');
INSERT INTO EMP VALUES('116','Mr.','Smith','George','A','11/8/1965','12/10/1988','615','890-2984','108');
but when I entered commands:
SELECT INV_DATE FROM invoice;
the data only shows 0000-00-00 00:00:00
how do i update the whole column in INV_DATE to what I wish (example 1/16/2008)?
INSERT INTO INVOICE VALUES('1001','10014','1/16/2008','24.8999996185303','1.99000000953674','26.8899993896484');
INSERT INTO INVOICE VALUES('1002','10011','1/16/2008','9.97999954223633','0.800000011920929','10.7799997329712');
INSERT INTO INVOICE VALUES('1003','10012','1/16/2008','153.850006103516','12.3100004196167','166.160003662109');
INSERT INTO INVOICE VALUES('1004','10011','1/17/2008','34.9700012207031','2.79999995231628','37.7700004577637');
INSERT INTO INVOICE VALUES('1005','10018','1/17/2008','70.4400024414062','5.6399998664856','76.0800018310547');
INSERT INTO INVOICE VALUES('1006','10014','1/17/2008','397.829986572266','31.8299999237061','429.660003662109');
INSERT INTO INVOICE VALUES('1007','10015','1/17/2008','34.9700012207031','2.79999995231628','37.7700004577637');
INSERT INTO INVOICE VALUES('1008','10011','1/17/2008','399.149993896484','31.9300003051758','431.079986572266');
Type of INV_DATE in invoice table is 'datetime'. The default format of datetime is 'YYYY-MM-DD HH:MM:SS'.
So, if you insert values in different format, it will misinterpret it and show unexpected output.
Update the type of the INV_DATE column to any other favorable one or insert date and time formatted in this format : YYYY-MM-DD HH:MM:SS[.fraction]
Hope this helps.

mysql statement wont insert

Hey guys I've searched for answers through the forums but to no avail so I'm using MySql and I'm trying to insert statements for certain tables and they aren't going into the tables and I'm getting errors like "Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated. The statement has been terminated."
These are the statements I'm having problems with.`INSERT INTO Course VALUES
INSERT INTO Course VALUES (12345, 'DatabaseManagement', '2015-2-1', '2014-5-9');
INSERT INTO Course VALUES (12346, 'Calculus', '2015-1-12', '2015-5-9');
INSERT INTO Course VALUES (12347, 'Biology', '2015-1-3', '2015-5-9');
INSERT INTO Course VALUES (12348, 'Chemistry', '2015-1-2', '2015-5-9');
INSERT INTO Grade VALUES (10, 12345, 012, 'A');
INSERT INTO Grade VALUES (11, 12346, 013, 'B');
INSERT INTO Grade VALUES (12, 12347, 014, 'C');
INSERT INTO Grade VALUES (13, 12348, 015, 'D');
INSERT INTO Grade VALUES (14, 12345, 016, 'B');
INSERT INTO Student VALUES (54321, 'Rachel', 'Cotterel', '2013-4-15', '2016-3-4');
INSERT INTO Student VALUES (54320, 'John', 'Smith', '2012-1-23', NULL);
INSERT INTO Student VALUES (54319, 'Johny', 'Depp', '2010-5-12', '2012-10-10');
INSERT INTO Student VALUES (54318, 'Orlando', 'Bloom', '2014-6-24', NULL);
INSERT INTO Student VALUES (54317, 'Linda', 'Jacob', '2015-4-4', '2019-8-6');
I didn't get any error for insert into Course statements. I got error for INSERT INTO Grade statements. Its because there is no reference available for StudentID 012,013 etc in Student table. And you are trying to add them in grade table.
Try using this:
INSERT INTO table1 (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
These are the field types:
CREATE TABLE Course
(
CourseID int,
Description varchar(20) NOT NULL,
StartDate DATE NOT NULL,
EndDate DATE NOT NULL,
CONSTRAINT [PK_CourseID] PRIMARY KEY (CourseID)
);
CREATE TABLE Grade
(
GradeID integer(10) NOT NULL,
CourseID integer(10) NOT NULL,
StudentID integer(10) NOT NULL,
Grade varchar (10) NULL,
CONSTRAINT [PK_GradeID] PRIMARY KEY (GradeID),
CONSTRAINT [FK_CourseID] FOREIGN KEY (CourseID) REFERENCES Course(CourseID),
CONSTRAINT [FK_StudentID] FOREIGN KEY (StudentID) REFERENCES Student(StudentID)
);
CREATE TABLE Student
(
StudentID integer(10) NOT NULL,
FirstName varchar(45) NOT NULL,
LastName varchar(45) NOT NULL,
RegistrationDate varchar (45) NOT NULL,
GraduationDate DATE NULL,
CONSTRAINT [PK_StudentlID] PRIMARY KEY (StudentID)
);
String or binary data would be truncated
The reason that you get this message should be that you are trying to insert some value to some field to which you haven't assigned enough size to hold the value.
Can you send what the exact error message you get?
I tried to do it myself.But the error I got was from you insertion query to Grade table foreign key fails which refer Student table because you are trying to insert Student_IDs which are not there in you Student table

Find difference between 2 dates in MySQL

I have 2 different date columns: detail_returndate and detail_duedate. I am trying to find the difference between the two and return it as days_overdue.
Here is the basic SELECT. I have tried using diffday and subtracting the 2, but I am not having much luck.
select rent_num, vid_num, detail_returndate, detail_duedate
from detailrental
If you need the full sql code for the table let me know but i do not think that you will need it
To be clear I need the days overdue that is found out by taking the difference between detail_returndate and detail_duedate. This should yield the amount overdue, in days
/* Database Systems, 9th Ed., Coronel/MOrris/Rob */
/* Type of SQL : MySQL */
/*Create table price*/
CREATE TABLE price
(price_code INTEGER PRIMARY KEY AUTO_INCREMENT,
price_description VARCHAR(20) NOT NULL,
price_rentfee DECIMAL(5,2),
price_dailylatefee DECIMAL(5,2));
/*Insert data into price*/
INSERT INTO price VALUES(1,'Standard',2,1);
INSERT INTO price VALUES(2,'New Release',3.5,3);
INSERT INTO price VALUES(3,'Discount',1.5,1);
INSERT INTO price VALUES(4,'Weekly Special',1,.5);
/*Create table movie*/
CREATE TABLE movie
(movie_num INTEGER PRIMARY KEY AUTO_INCREMENT,
movie_title VARCHAR(75) NOT NULL,
movie_year INTEGER,
movie_cost DECIMAL(5,2),
movie_genre VARCHAR(50),
price_code INTEGER,
FOREIGN KEY(price_code) REFERENCES price(price_code));
/*Insert data into movie*/
INSERT INTO movie VALUES(1234,'The Cesar Family Christmas',2007,39.95,'FAMILY',2);
INSERT INTO movie VALUES(1235,'Smokey Mountain Wildlife',2004,59.95,'ACTION',1);
INSERT INTO movie VALUES(1236,'Richard Goodhope',2008,59.95,'DRAMA',2);
INSERT INTO movie VALUES(1237,'Beatnik Fever',2007,29.95,'COMEDY',2);
INSERT INTO movie VALUES(1238,'Constant Companion',2008,89.95,'DRAMA',NULL);
INSERT INTO movie VALUES(1239,'Where Hope Dies',1998,25.49,'DRAMA',3);
INSERT INTO movie VALUES(1245,'Time to Burn',2005,45.49,'ACTION',1);
INSERT INTO movie VALUES(1246,'What He Doesn''t Know',2006,58.29,'COMEDY',1);
/*Create table video*/
CREATE TABLE video
(vid_num INTEGER PRIMARY KEY AUTO_INCREMENT,
vid_indate DATE,
movie_num INTEGER,
FOREIGN KEY(movie_num) REFERENCES movie(movie_num));
/*Insert data into video*/
INSERT INTO video VALUES(54321,'2008-06-18',1234);
INSERT INTO video VALUES(54324,'2008-06-18',1234);
INSERT INTO video VALUES(54325,'2008-06-18',1234);
INSERT INTO video VALUES(34341,'2007-01-22',1235);
INSERT INTO video VALUES(34342,'2007-01-22',1235);
INSERT INTO video VALUES(34366,'2009-03-02',1236);
INSERT INTO video VALUES(34367,'2009-03-02',1236);
INSERT INTO video VALUES(34368,'2009-03-02',1236);
INSERT INTO video VALUES(34369,'2009-03-02',1236);
INSERT INTO video VALUES(44392,'2008-10-21',1237);
INSERT INTO video VALUES(44397,'2008-10-21',1237);
INSERT INTO video VALUES(59237,'2009-02-14',1237);
INSERT INTO video VALUES(61388,'2007-01-25',1239);
INSERT INTO video VALUES(61353,'2006-01-28',1245);
INSERT INTO video VALUES(61354,'2006-01-28',1245);
INSERT INTO video VALUES(61367,'2008-07-30',1246);
INSERT INTO video VALUES(61369,'2008-07-30',1246);
/*Create table membership*/
CREATE TABLE membership
(mem_num INTEGER PRIMARY KEY AUTO_INCREMENT,
mem_fname VARCHAR(30) NOT NULL,
mem_lname VARCHAR(30) NOT NULL,
mem_street VARCHAR(120),
mem_city VARCHAR(50),
mem_state VARCHAR(2),
mem_zip VARCHAR(5),
mem_balance DECIMAL(10,2));
/*Insert data into membership*/
INSERT INTO membership VALUES(102,'Tami','Dawson','2632 Takli Circle','Norene','TN','37136',11);
INSERT INTO membership VALUES(103,'Curt','Knight','4025 Cornell Court','Flatgap','KY','41219',6);
INSERT INTO membership VALUES(104,'Jamal','Melendez','788 East 145th Avenue','Quebeck','TN','38579',0);
INSERT INTO membership VALUES(105,'Iva','Mcclain','6045 Musket Ball Circle','Summit','KY','42783',15);
INSERT INTO membership VALUES(106,'Miranda','Parks','4469 Maxwell Place','Germantown','TN','38183',0);
INSERT INTO membership VALUES(107,'Rosario','Elliott','7578 Danner Avenue','Columbia','TN','38402',5);
INSERT INTO membership VALUES(108,'Mattie','Guy','4390 Evergreen Street','Lily','KY','40740',0);
INSERT INTO membership VALUES(109,'Clint','Ochoa','1711 Elm Street','Greenville','TN','37745',10);
INSERT INTO membership VALUES(110,'Lewis','Rosales','4524 Southwind Circle','Counce','TN','38326',0);
INSERT INTO membership VALUES(111,'Stacy','Mann','2789 East Cook Avenue','Murfreesboro','TN','37132',8);
INSERT INTO membership VALUES(112,'Luis','Trujillo','7267 Melvin Avenue','Heiskell','TN','37754',3);
INSERT INTO membership VALUES(113,'Minnie','Gonzales','6430 Vasili Drive','Williston','TN','38076',0);
/*Create table rental*/
CREATE TABLE rental
(rent_num INTEGER PRIMARY KEY AUTO_INCREMENT,
rent_date DATE,
mem_num INTEGER,
FOREIGN KEY(mem_num) REFERENCES membership(mem_num));
/*Insert data into rental*/
INSERT INTO rental VALUES(1001,'2009-03-01',103);
INSERT INTO rental VALUES(1002,'2009-03-01',105);
INSERT INTO rental VALUES(1003,'2009-03-02',102);
INSERT INTO rental VALUES(1004,'2009-03-02',110);
INSERT INTO rental VALUES(1005,'2009-03-02',111);
INSERT INTO rental VALUES(1006,'2009-03-02',107);
INSERT INTO rental VALUES(1007,'2009-03-02',104);
INSERT INTO rental VALUES(1008,'2009-03-03',105);
INSERT INTO rental VALUES(1009,'2009-03-03',111);
/*Create table detailrental*/
CREATE TABLE detailrental
(rent_num INTEGER,
vid_num INTEGER,
detail_fee DECIMAL(5,2),
detail_duedate DATE,
detail_returndate DATE,
detail_dailylatefee DECIMAL(5,2),
PRIMARY KEY(rent_num, vid_num),
FOREIGN KEY(rent_num) REFERENCES rental(rent_num),
FOREIGN KEY(vid_num) REFERENCES video(vid_num));
/*Insert data into dailyrental*/
INSERT INTO detailrental VALUES(1001,34342,2,'2009-03-04','2009-03-02',1);
INSERT INTO detailrental VALUES(1001,61353,2,'2009-03-04','2009-03-03',1);
INSERT INTO detailrental VALUES(1002,59237,3.5,'2009-03-04','2009-03-04',3);
INSERT INTO detailrental VALUES(1003,54325,3.5,'2009-03-04','2009-03-09',3);
INSERT INTO detailrental VALUES(1003,61369,2,'2009-03-06','2009-03-09',1);
INSERT INTO detailrental VALUES(1003,61388,0,'2009-03-06','2009-03-09',1);
INSERT INTO detailrental VALUES(1004,44392,3.5,'2009-03-05','2009-03-07',3);
INSERT INTO detailrental VALUES(1004,34367,3.5,'2009-03-05','2009-03-07',3);
INSERT INTO detailrental VALUES(1004,34341,2,'2009-03-07','2009-03-07',1);
INSERT INTO detailrental VALUES(1005,34342,2,'2009-03-07','2009-03-05',1);
INSERT INTO detailrental VALUES(1005,44397,3.5,'2009-03-05','2009-03-05',3);
INSERT INTO detailrental VALUES(1006,34366,3.5,'2009-03-05','2009-03-04',3);
INSERT INTO detailrental VALUES(1006,61367,2,'2009-03-07',NULL,1);
INSERT INTO detailrental VALUES(1007,34368,3.5,'2009-03-05',NULL,3);
INSERT INTO detailrental VALUES(1008,34369,3.5,'2009-03-05','2009-03-05',3);
INSERT INTO detailrental VALUES(1009,54324,3.5,'2009-03-05',NULL,3);
INSERT INTO detailrental VALUES(1001,34366,3.5,'2009-03-04','2009-03-02',3);
SELECT rent_num,
vid_num,
detail_returndate,
detail_duedate,
DATEDIFF( detail_duedate, detail_returndate) as days_overdue
FROM detailrental
It should work as shown above. Perhaps add the obligatory `.
SELECT `rent_num`,
`vid_num`,
`detail_returndate`,
`detail_duedate`,
DATEDIFF(`detail_returndate`,`detail_duedate`) as days_overdue
FROM detailrental
Also, DATEDIFF subtracts the second value from the first, so as shown it should give a positive number for days_overdue.