Error Code: 1264. Out of range value for column - mysql

I'm trying to get my code to work and just from the first line I'm inserting I get an error. I've already changed the value of the phone number's to varchar, but this error still remains.
This is my code:
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Customer` (
`Customer_ID` INT NOT NULL,
`Customer_F_Name` VARCHAR(45) NOT NULL,
`Customer_L_Name` VARCHAR(45) NOT NULL,
`Customer_Address` VARCHAR(45) NOT NULL,
`Customer_DOB` DATE NOT NULL,
`Customer_H_Phone` VARCHAR(20) NOT NULL,
`Customer_W_Phone` VARCHAR(20) NOT NULL,
`Customer_Type` VARCHAR(45) NOT NULL,
`Customer_Sponsor_ID` INT NULL,
PRIMARY KEY (`Customer_ID`),
INDEX `fk_Customer_Customer1_idx` (`Customer_Sponsor_ID` ASC),
CONSTRAINT `fk_Customer_Customer1`
FOREIGN KEY (`Customer_Sponsor_ID`)
REFERENCES `mydb`.`Customer` (`Customer_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
INSERT INTO `Customer` (`Customer_ID`, `Customer_F_Name`, `Customer_L_Name`,
`Customer_Address`, `Customer_DOB`, `Customer_H_Phone`, `Customer_W_Phone`,
`Customer_Type`, `Customer_Sponsor_ID`) VALUES
(1, 'Nicole', 'Gilbert', '8295 Golden Leaf Alley',
'1992-03-28', '5744813109', '1646648129', 'Student', NULL);
And then when I just insert the first line of data this is the error I get:
INSERT INTO `Customer` (`Customer_ID`, `Customer_F_Name`, `Customer_L_Name`,
`Customer_Address`, `Customer_DOB`, `Customer_H_Phone`, `Customer_W_Phone`,
`Customer_Type`, `Customer_Sponsor_ID`) VALUES
(1, 'Nicole', 'Gilbert', '8295 Golden Leaf Alley',
'1992-03-28', '5744813109', '1646648129', 'Student', NULL)
Error Code: 1264. Out of range value for column
'Customer_H_Phone' at row 1
Thank you!

Related

Insert GENERATED ALWAYS as in SQL

I created a table like this:
CREATE TABLE `group` (
`g_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`owner_id` int(10) unsigned NOT NULL,
`g_name` varchar(45) NOT NULL,
`refer_code` varchar(45) GENERATED ALWAYS AS (concat(`g_name`)) VIRTUAL,
`created_on` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`row_hash` varchar(256) NOT NULL,
PRIMARY KEY (`g_id`),
UNIQUE KEY `g_hash_UNIQUE` (`row_hash`),
UNIQUE KEY `refer_UNIQUE` (`refer_code`),
KEY `owner_idx` (`owner_id`),
CONSTRAINT `owner_id` FOREIGN KEY (`owner_id`) REFERENCES `user` (`u_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The update statement gives me error:
INSERT INTO `server`.`group` (`owner_id`, `g_name`, `refer_code`, `created_on`, `row_hash`)
VALUES ('4', 'cool', null, '2018-02-13 10:34:11', '452345324')
Error Code: 3105. The value specified for generated column 'refer_code' in table 'group' is not allowed.
How to specify the refer_code while inserting?
Values of the generated column are computed from the expression in the column definition of your CREATE TABLE, so don't include it in INSERT or UPDATE statements:
INSERT INTO `server`.`group` (`owner_id`, `g_name`, `created_on`, `row_hash`)
VALUES ('4', 'cool', '2018-02-13 10:34:11', '452345324')

Mysql autoincrement error when insert a new register

I have this table
CREATE TABLE IF NOT EXISTS `test`.`Challenge` (
`idChallenge` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`description` VARCHAR(45) NULL DEFAULT NULL,
`start` DATETIME NOT NULL,
`ending` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`idChallenge`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC))
ENGINE = InnoDB;
When I try insert a new register:
insert into challenge (name, start) values ("test", now());
Gives me this error:
Error Code: 1364. Field 'id_challenge' doesn't have a default value
But first I don't have this column in my table and if I put it is create.
Us must use backticks ` for reserved words.
insert into Challenge (`name`, `start`) values ("test", now());
The problem are with idChallenge, if I change it to idchallenge works.

Unknown column 'dob' in 'field list'

I am getting error Unknown column 'dob' in 'field list'
I know that dob is in my table and have called it many times and in my code the insert is ordered correctly. I am just learning MySQL so I'm completely lost
insert into users (userid, firstname, username,dob)
values ('gg', 'greg', 'greg2', '1980-01-01');
here is the table I am trying to insert it into.
CREATE TABLE `users` (
`userid` varchar(50) NOT NULL DEFAULT '',
`firstname` varchar(100) DEFAULT NULL,
`lastname` varchar(100) DEFAULT NULL,
`middleName` varchar(100) DEFAULT NULL,
`username` varchar(20) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
`dob` date DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`occupationId` int(11) DEFAULT NULL,
`userStatusId` int(11) DEFAULT NULL,
`userTypeId` int(11) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`userid`),
UNIQUE KEY `username` (`username`),
KEY `userStatusId` (`userStatusId`),
KEY `userTypeId` (`userTypeId`),
KEY `occupationId` (`occupationId`),
KEY `lastname` (`lastname`),
CONSTRAINT `users_ibfk_1` FOREIGN KEY (`userStatusId`) REFERENCES `userStatus` (`userStatusId`),
CONSTRAINT `users_ibfk_2` FOREIGN KEY (`userTypeId`) REFERENCES `userType` (`userTypeId`),
CONSTRAINT `users_ibfk_3` FOREIGN KEY (`occupationId`) REFERENCES `occupation` (`occupationId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I am doing all my work in Sequel Pro
insert into users (userid, firstname, username, dob)
values ('gg', 'greg', 'greg2', '1980-01-01');
i tried it this was as well and get the same error, then i tried it like this
insert into users (userid,firstname,username,dob)
values ('gg','greg','greg2','1980-01-01');
here is the code I ran before my insert
delimiter $$
create trigger usersInsert
before insert on users
for each row begin
set new.created = now();
set new.age = floor(datediff(now(),dob)/365);
end $$
delimiter ;
I was trying to see if my trigger was working so I inserted a new user into the user table to make sure it was working properly
i found where my error was. it was in my trigger
delimiter $$
create trigger usersInsert
before insert on users
for each row begin
set new.created = now();
set new.age = floor(datediff(now(),new.dob)/365);
end $$
delimiter ;
I wasn't using 'new.dob' when setting a value to dob
Thank You for all of your help!

Adding Foreign Key Error

I want to add a foreign key from Table Customers, row= "Customer ID" to Table Pet, row= "Customer ID".
-- Table structure for table `Customers`
CREATE TABLE IF NOT EXISTS `Customers` (
`CustomerID` varchar(50) NOT NULL,
`Fname` varchar(50) DEFAULT NULL,
`LName` varchar(20) DEFAULT NULL,
`Tel` varchar(20) DEFAULT NULL,
`Fax` varchar(20) DEFAULT NULL,
`CustType` varchar(20) DEFAULT NULL,
`AdState` varchar(50) DEFAULT NULL,
`City` varchar(20) DEFAULT NULL,
`Zip` varchar(20) DEFAULT NULL,
`Street` varchar(20) DEFAULT NULL,
PRIMARY KEY (`CustomerID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- Dumping data for table `Customers`
INSERT INTO `Customers` (`CustomerID`, `Fname`, `LName`, `Tel`, `Fax`, `CustType`, `AdState`, `City`, `Zip`, `Street`) VALUES
('AC001', 'All', 'Creatures', '206 555-6622', '206 555-7854', '2', 'WA', 'Tall Pines', '98746', '21 Grace St.'),
('AD001', 'Johnathan', 'Adams', '206 555 7623', '206 555 8855', '1', 'WA', 'Mountain View', '984101012', '66 10th St'),
('AD002', 'William', 'Adams', '503 555 7623', '503 555 7319', '1', 'OR', 'Lakewille', '9740110011', '1122 10th_St'),
('AK001', 'Animal', 'Kingdom', '208 555 7108', '', '2', 'ID', 'Borderville', '834835646', '15 Marlin Lane');
CREATE TABLE IF NOT EXISTS `Pet` (
`ID` varchar(50) NOT NULL,
`CustomerID` varchar(50) NOT NULL,
`Gender` varchar(20) DEFAULT NULL,
`Race` varchar(20) DEFAULT NULL,
`Name` varchar(20) DEFAULT NULL,
`Kind` varchar(20) DEFAULT NULL,
`Birthday` varchar(20) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- Dumping data for table `Pet`
INSERT INTO `Pet` (`ID`, `CustomerID`, `Gender`, `Race`, `Name`, `Kind`, `Birthday`) VALUES
('AC001-01', '0', 'M', 'Long Ear', 'Bobo', 'Rabbit', '4/8/92'),
('AC001-02', '0', 'F', 'Chameleon', 'Presto Chango', 'Lizard', '5/1/92'),
('AC001-03', '0', 'M', '', 'Stinky', 'Skunk', '8/1/91'),
('AC001-04', '0', 'M', 'German Shepherd', 'Fido', 'Dog', '6/1/90'),
('AD001-01', '0', 'F', 'Potbelly', 'Patty', 'Pig', '2/15/91'),
('AD001-02', '0', 'M', 'Palomino', 'Rising Sun', 'Horse', '4/10/90'),
('AD002-01', '0', 'F', 'Mixed', 'Dee Dee', 'Dog', '2/15/91'),
('AK001-03', '0', 'M', '', 'Jerry', 'Rat', '2/1/88'),
('AK001-07', '0', 'M', 'Beagle', 'Luigi', 'Dog', '8/1/92');
This is the code that I have been using to add the foreign key:
ALTER TABLE Pet ADD CONSTRAINT Pet_FK
FOREIGN KEY (CustomerID) REFERENCES Customers (CustomerID);
And the error message from this is:
#1452 - Cannot add or update a child row: a foreign key constraint fails
(`hospital`.`#sql-523_76e`, CONSTRAINT `Pet_FK` FOREIGN KEY (`CustomerID`)
REFERENCES `Customers` (`CustomerID`))
I am quite a beginner with database and I have no idea what I should try next.
I think that's all. Im still new to this stackoverflow so if I missed any necessary information please tell me and I will add it.
UPDATE***
ALTER TABLE Customers ADD CONSTRAINT Customers_FK
FOREIGN KEY (CustomerID) REFERENCES Pet (CustomerID);
I swapped some positions and the error code I recieve is:
#1215 - Cannot add foreign key constraint
Simple one.
There is an row that contains the CustomerID that can't be matched. So first you need to remove/edit/handle the entry and than add a foreign key.
The CustomerID you're trying to enter in PETS table, does not exist in CUSTOMERS table, and that is why your Foreign Key constraint fails and throws error.
You need to ensure that the CustomerIDs you're entering in your Pets table, exist in Customers table OR simply insert NULL in the PETS.CUSTOMERID field
Enterx is right.
For being able to detect not matching row :
SELECT * FROM Pet p WHERE (SELECT COUNT(*) FROM Customers c WHERE c.CustomerID=p.CustomerID)=0
Just change SELECT * by DELETE for deleting missmatching Pet entry.
You can UPDATE Pet.CustomerID to NULL too. But you have to define CustomerID, from Pet table, with NULL option (and not NOT NULL)
It looks like to me that you inserted values in table Pet's column ID when they should have been inserted in CustomerID and vice-versa.
By the way, it's not really good to have IDs as VARCHARs, specially when they are foreign keys. This makes queries processing slower, although your tables don't look like they'll have a huge number of rows for this to make a difference. Anyway, it's just an observation. I would consider have artificial int primary keys in my tables.
EDIT
I had misread table Pet's values. The other answers here are right. You need to update those 0 values in CustomerID column to match existing CustomerIDs in Customer table or delete them, otherwise you'll get an error when trying to create the FK.
try this
CREATE TABLE IF NOT EXISTS `Pet` (
`ID` varchar(50) NOT NULL,
FOREIGN KEY (`CustomerID`) REFERENCES Customers(CustomerID) varchar(50) NOT NULL,
`Gender` varchar(20) DEFAULT NULL,
`Race` varchar(20) DEFAULT NULL,
`Name` varchar(20) DEFAULT NULL,
`Kind` varchar(20) DEFAULT NULL,
`Birthday` varchar(20) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Visual MySQL Workbench Error: 1452 foreign key constraint fails?

Been looking at this for two days and am at a loss as to what this means. I know it means "exactly what it says" but I don't know how to fix it. Can someone explain it to me in layman's terms and help me out?
ERROR: Error 1452: Cannot add or update a child row: a foreign key constraint fails
(`sls11n`.`dependent`, CONSTRAINT `fk_dependent_employee1` FOREIGN KEY (`emp_id`)
REFERENCES `employee` (`emp_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
INSERT INTO `sls11n`.`dependent` (`dep_id`, `emp_id`, `dep_ssn`, `dep_fname`, `dep_lname`,
`dep_street`, `dep_city`, `dep_state`, `dep_zip`, `dep_phone`, `dep_email`, `dep_notes`)
VALUES (NULL, 13, 123456789, 'Gary', 'Hart', 'West St', 'San Diego', 'CA', '23424',
'1234567890', 'garyhart#me.com', NULL)
Here's the script that I believe is relevant:
-- -----------------------------------------------------
-- Table `sls11n`.`employee`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `sls11n`.`employee` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `sls11n`.`employee` (
`emp_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`emp_ssn` INT UNSIGNED NOT NULL,
`emp_fname` VARCHAR(15) NOT NULL,
`emp_lname` VARCHAR(20) NOT NULL,
`emp_street` VARCHAR(45) NOT NULL,
`emp_city` VARCHAR(45) NOT NULL,
`emp_state` CHAR(2) NOT NULL,
`emp_zip` CHAR(9) NOT NULL,
`emp_phone` CHAR(15) NOT NULL,
`emp_email` VARCHAR(100) NOT NULL,
`emp_doh` DATE NOT NULL,
`emp_is_inspect` ENUM('y', 'n') NOT NULL,
`emp_notes` VARCHAR(250) NULL,
PRIMARY KEY (`emp_id`))
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `sls11n`.`dependent`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `sls11n`.`dependent` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `sls11n`.`dependent` (
`dep_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`emp_id` TINYINT UNSIGNED NOT NULL,
`dep_ssn` INT UNSIGNED NOT NULL,
`dep_fname` VARCHAR(45) NOT NULL,
`dep_lname` VARCHAR(45) NOT NULL,
`dep_street` VARCHAR(45) NOT NULL,
`dep_city` VARCHAR(45) NOT NULL,
`dep_state` CHAR(2) NOT NULL,
`dep_zip` CHAR(9) NOT NULL,
`dep_phone` CHAR(10) NOT NULL,
`dep_email` VARCHAR(100) NOT NULL,
`dep_notes` VARCHAR(250) NULL,
PRIMARY KEY (`dep_id`),
INDEX `fk_dependent_employee1_idx` (`emp_id` ASC),
CONSTRAINT `fk_dependent_employee1`
FOREIGN KEY (`emp_id`)
REFERENCES `sls11n`.`employee` (`emp_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Any one have any ideas? Please use simple terms, I'm not kidding when I say I don't understand.
The foreign key constraint says that each dependent's emp_id must match an existing emp_id in the employee table. So before you add this row to the dependent table, you have to add a row to employee with emp_id = 13.