SQL insert Invalid Month - mysql

I am trying to do a simple insert on the table below. When ever I try run the insert I get an error saying invalid month. I have tried changing the date format in SQL developer using tools>preferences>database>NLS>dateFormat. I have changed it to all the different variations and I still getinvalid month. The current format ismm/dd/yyyy`
The Table
create table SALES_TABLE
(
outlet_number number(3),
emp_number number(4),
customer_ID number(4),
product_code number(4),
sale_date date not null,
sale_time timestamp not null,
quantity number(5) not null,
foreign key (outlet_number) references OUTLET_TABLE(outlet_number),
foreign key (emp_number) references EMPLOYEE_TABLE(emp_number),
foreign key (customer_ID) references CUSTOMER_TABLE(customer_ID),
foreign key (product_code) references PRODUCT_TABLE(product_code)
);
The Insert
insert into SALES_TABLE (outlet_number, emp_number, customer_ID, product_code, sale_date, sale_time, quantity) values (10, 41, 7, 2, '01/20/2015', '4:20:00', 3);

I think it is the value for the timestamp, and not the date, that's causing the error. The format that can be used as a valid timestamp is 1970-01-01 00:00:01, for example.
You can find more details about MySql date and timestamp types here

Use str_to_date
insert into SALES_TABLE
(outlet_number, emp_number, customer_ID, product_code,
sale_date, sale_time, quantity)
values (10, 41, 7, 2, str_to_date('01/20/2015','%m/%d/%Y'), '4:20:00', 3);

Have you tried the default SQL date format for the insert, i.e., '2015-01-20' (or '20150120') instead of '01/20/2015'?

Related

Is there a way for me to specify the date function in MySQLWorkBench so that I won’t get an error message?

I tried to create a table, this is how I set it up:
CREATE TABLE emp_tab
(
empno NUMeric(10),
name VARCHAR(50) NOT NULL,
job VARCHAR(50),
manager NUMeric(10),
hiredate DATE,
salary NUMeric(10,2),
commission NUMeric(10,2),
deptno NUMeric(5),
CONSTRAINT pk_emp_tab PRIMARY KEY (empno),
CONSTRAINT fk_emp_tab_deptno FOREIGN KEY (deptno)
REFERENCES dept_tab(deptno)
);
this is how I insert values:
INSERT INTO emp_tab
VALUES(7004, 'SCOTT', 'ANALYST', 7002,
date('87-7-13') - 85,
3000, null, 70
);
INSERT INTO emp_tab
VALUES(7007, 'ADAMS', 'CLERK', 7003,
date('87-7-13') - 51,
1100, null, 40
);
Oddly enough, I did not get an error message for the first inserted value but I got the error message for the second inserted value that says "Incorrect date value: '19870662' for column 'hiredate' at row 1) but after I removed - 51, it worked. However, it will give me incorrect date so I am wondering if there is any chance I can keep - 51 without getting errors?
You should use a well formatted date and DATE_SUB()
DATE_SUB(DATE('1987-07-13'), INTERVAL 85 DAY)

How can I make a constraint so that the appointment cannot be repeated?

I want to make a constraint so that the appointment with a certain doctor cannot be repeated more than once. For example, if a person chooses an appointment on 11/09/2021 from 15:00 until 15:30, the same appointment cannot be booked again at the same doctor.
phpMyAdmin virgin is: Server version: 10.4.10-MariaDB
CREATE OR REPLACE TABLE appointments (
AppId int(11) PRIMARY,
docID int(11),
patientID int(11),
AppStart datetime,
AppEnd datetime
)
You can do:
alter table appointments
add constraint unique_dr_app_start unique (docID, AppStart);
Example:
insert into appointments (AppId, docID, AppStart)
values (1, 123, '2021-11-25 12:30:00'); -- OK
insert into appointments (AppId, docID, AppStart)
values (2, 123, '2021-11-25 10:30:00'); -- OK
insert into appointments (AppId, docID, AppStart)
values (3, 123, '2021-11-25 12:30:00'); -- error
Duplicate entry '123-2021-11-25 12:30:00' for key 'unique_dr_app_start'
EDIT -- Include end appointment validation
You can additionally set up the constraint:
alter table appointments
add constraint unique_dr_app_end unique (docID, AppEnd);
You can see it at work by trying:
insert into appointments (AppId, docID, AppStart)
values (4, 123, '2021-11-25 10:30:00'); -- error
Duplicate entry '123-2021-11-25 10:30:00' for key 'unique_dr_app_start'

Operand should contain 1 column(s) Or Error in sql syntax

I checked almost all related topics, not working for me.
I am a beginner
Either I get "operand should contain 1 columns" or "you have an error in your sql syntax check the manual that corresponds to your mysql server version for the right sytax to use near ' ' at line 1 "
Here is my query :
create database makla;
use makla;
create table orders(
order_id int auto_increment primary key,
order_date DATE
);
create table productionitem(
order_id int not null,
item_name varchar (20),
item_description varchar (100),
constraint order_fk foreign key (order_id) references orders (order_id)
);
insert into orders(order_date) values ('2014/11/4');
insert into orders(order_date) values ('2017/9/30');
insert into orders(order_date) values ('2019/4/13');
insert into productionitem(order_id, item_name, item_description)
values (1, 'tv', 'samsung X');
insert into productionitem(order_id, item_name, item_description)
values (1, 'watch', 'swatch X');
insert into productionitem(order_id, item_name, item_description)
values (2, 'pan', 'metal X');
insert into productionitem(order_id, item_name, item_description)
values (3, 'cup', 'world X');
insert into productionitem(order_id, item_name, item_description)
values (3, 'chair', 'plastic X');
select *
from productionitem
where order_id in (select order_id
from orders
where order_date between '2015/11/4' and '2020/11/4')
please help,
You may need to put the date in proper format yyyy-mm-dd
insert into orders(order_date) values ('2014-11-04');
insert into orders(order_date) values ('2017-09-30'); -- notice 09 not just 9
insert into orders(order_date) values ('2019-04-13');
Same date format will be used for SELECT queries.

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

SQL Conditional Insert or update statement depending on a pair of columns. Using unique index?

OK so I have a table (in my MySQL database) as follows:
CREATE TABLE IF NOT EXISTS `funddata` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ticker` varchar(128) NOT NULL,
`price_date` date NOT NULL,
`price` double NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
And some sample data:
INSERT INTO funddata (ticker, price_date, price) VALUES ("tick1", '2013-06-01', 36.2);
INSERT INTO funddata (ticker, price_date, price) VALUES ("tick2", '2013-06-01', 14.7);
INSERT INTO funddata (ticker, price_date, price) VALUES ("tick3", '2013-06-01', 102.5);
INSERT INTO funddata (ticker, price_date, price) VALUES ("tick1", '2013-07-01', 38.7);
INSERT INTO funddata (ticker, price_date, price) VALUES ("tick2", '2013-07-01', 16.2);
Now let's say I want to add some more prices for tick1. If the price I want to add already exists in my table for that date then I want to update what's there with the new price, else I just want to insert it as a new record.
Does it make sense to make a unique index out of (ticker, price_date) given that no 2 records should share the same ticker and date? If so how would I do this and how would I make use of such an index.
CREATE UNIQUE INDEX index_name ON funddata(priceDate, price);
Then use INSERT... ON DUPLICATE KEY UPDATE
But still,this break the first normal form.http://en.wikipedia.org/wiki/First_normal_form