database error invalid values - mysql

i am getting error in my database. i am encountering invalid default value for timestamp. how can i fix this.
here's my database:
CREATE TABLE IF NOT EXISTS `thread` (
`id` int(11) NOT NULL,
`title` varchar(100) NOT NULL,
`dateCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`userId` int(11) NOT NULL,
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`categoryId` int(11) NOT NULL,
`view` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=45 DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `user`
--
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL,
`fullname` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`username` varchar(60) NOT NULL,
`password` varchar(60) NOT NULL,
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`profileUrl` varchar(200) NOT NULL DEFAULT 'https://mymonas.com/forum/img/nophoto.png',
`profileBg` varchar(200) NOT NULL,
`dateCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`aboutMe` varchar(500) NOT NULL,
`isModerator` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=latin1;

My guess is you're running MySQL 5.5. If that's the case, you can't use CURRENT_TIMESTAMP with DATETIME. DATETIME has to have a default of null or no default value. You can, however, change them to TIMESTAMP, but then you can only have one default and one ON UPDATE per table. You can also use a post-insert trigger to fill in a NOW() value on new records. Here's the docs to help.

use TIMESTAMP instead of datetime
This may be helpful.

Related

Invalid Default value (Time Stamp) issue in Database(SQL)

I'm try to create table as below, but get error:
Invalid default value for 'updated_at'
I don't know what to do...what's wrong in query.
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`user_id` int(11) DEFAULT NULL,
`title` varchar(255) NOT NULL,
`slug` varchar(255) NOT NULL UNIQUE,
`views` int(11) NOT NULL DEFAULT '0',
`image` varchar(255) NOT NULL,
`body` text NOT NULL,
`published` tinyint(1) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
According SQL standard '0000-00-00 00:00:00' is not valid value for timestamp field. You should to use NULL as default value fro updated_at column.
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`user_id` int(11) DEFAULT NULL,
`title` varchar(255) NOT NULL,
`slug` varchar(255) NOT NULL UNIQUE,
`views` int(11) NOT NULL DEFAULT '0',
`image` varchar(255) NOT NULL,
`body` text NOT NULL,
`published` tinyint(1) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Also ON UPDATE CURRENT_TIMESTAMP part is not relevant for created_at field, but relevant for updated_at

Mysql - Cannot add foreign key constraint, there is no forign key in SQL query

This question is completely different from similar ones. There is no foreign key in the SQL query. This is a silly error I see when I import the SQL file on remote server. This is the SQL code
CREATE TABLE `locations` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
As you see there is no foreign key, But when I run the following code, it is ok
CREATE TABLE `locations` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(191) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ;
If I rename it to something else it is OK too.
CREATE TABLE `locationssss` (
`id` int(10) UNSIGNED NOT NULL,
`title` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
what is wrong?
Just for future references:
Do you have more tables within your database? If so, is there a table that does contain a foreign key connected with the locations table?

MySQL issue with altering column to add default

I have a table named Users with a column call created. Whenever a record is created I want to add the datetime.
Users Table:
CREATE TABLE `Users` (
`userId` int(11) unsigned NOT NULL AUTO_INCREMENT,
`fullName` varchar(50) DEFAULT NULL,
`firstName` varchar(25) NOT NULL DEFAULT '',
`lastName` varchar(25) NOT NULL DEFAULT '',
`address` varchar(50) NOT NULL DEFAULT '',
`city` varchar(25) DEFAULT NULL,
`state` char(2) DEFAULT NULL,
`zipCode` varchar(25) DEFAULT NULL,
`email` varchar(50) NOT NULL DEFAULT '',
`cellPhone` varchar(15) DEFAULT NULL,
`birthDate` date NOT NULL,
`creditCard` varchar(250) NOT NULL DEFAULT '',
`subscriptionStarted` date NOT NULL,
`subscriptionEnded` date NOT NULL,
`basicPlan` tinyint(1) DEFAULT NULL,
`standardPlan` tinyint(1) DEFAULT NULL,
`premiumPlan` tinyint(1) DEFAULT NULL,
`staff` tinyint(1) DEFAULT NULL,
`admin` tinyint(1) DEFAULT NULL,
`systemAdmin` tinyint(1) DEFAULT NULL,
`edited` datetime DEFAULT NULL,
`created` datetime DEFAULT NULL,
PRIMARY KEY (`userId`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=latin1;
now i added this extra query to make my created field get the current datetime when a new record is created.
ALTER TABLE Users
ALTER COLUMN created SET DEFAULT CURRENT_TIMESTAMP
The problem is that I get the following error when running the alter table query
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 'CURRENT_TIMESTAMP' at line 2
Your syntax is slightly off, I think you have to specify the column to change:
ALTER TABLE Users CHANGE created created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
See this sample SQL Fiddle for an example.

Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

I always get the error:
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
DROP TABLE IF EXISTS `characters`;
CREATE TABLE `characters`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`accId` int(11) NOT NULL,
`charId` int(11) NOT NULL,
`charType` int(11) NOT NULL,
`level` int(11) NOT NULL,
`exp` int(11) NOT NULL,
`fame` int(11) NOT NULL,
`items` varchar(128) NOT NULL,
`hp` int(11) NOT NULL,
`mp` int(11) NOT NULL,
`stats` varchar(64) NOT NULL DEFAULT '"1,2,3,4,5,6,7,8"',
`dead` tinyint(1) NOT NULL DEFAULT '0',
`tex1` int(11) NOT NULL DEFAULT '0',
`tex2` int(11) NOT NULL DEFAULT '0',
`pet` int(11) NOT NULL DEFAULT '0',
`fameStats` varchar(128) NOT NULL DEFAULT 'eNoVytkRgCAMRdGH4IIbgmsPdmNVNmZf5n7kzM0kksLjJN2V4b30vcHK1YYam9hCxxqh5zpQI0wwQ4IFMhRYYeNjpw444YIfA3kDIA',
`createTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deathTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`totalFame` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
This is my first time using MySQL stuff. The reason I am using it because I downloaded a source for a game with server and this is needed.
The problem is isolated to these two lines
`createTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deathTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
To get rid of the error, you need to modify one of those lines to remove the DEFAULT CURRENT_TIMESTAMP. MySQL only allows one timestamp column to be defined that way. For example:
`createTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`deathTime` timestamp NOT NULL,
Because the deathTime column is defined as NOT NULL and doesn't have a default, you'll need to supply a value for deathTime whenever you insert a row. You could get the current date and time assigned to that column for an INSERT using a BEFORE INSERT trigger.
You are trying to, well, define more than "one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause". Specifically, createTime and deathTime.
You Can't Do That.™

Getting a duplicate key error in MYSQL. No duplicate found

I have a table. (Code taken from table generation code, I did not write this)
DROP TABLE IF EXISTS `CatalogueBasket`;
CREATE TABLE `CatalogueBasket` (
`ID` int(11) NOT NULL auto_increment,
`Shopper` char(35) NOT NULL default '',
`ItemLink` int(11) NOT NULL default '0',
`Quantity` int(11) NOT NULL default '0',
`Created` datetime NOT NULL default '0000-00-00 00:00:00',
`ExpectedDelivery1` datetime default NULL,
`ExpectedDelivery2` datetime default NULL,
`Comments` char(255) default NULL,
`Status` int(10) unsigned default NULL,
`QuantityShipped` int(10) unsigned default NULL,
`HarmonyNumber` int(10) unsigned default NULL,
`StartDate` datetime default NULL,
KEY `ID` (`ID`),
KEY `Shopper` (`Shopper`),
KEY `ItemLink` (`ItemLink`),
KEY `Quantity` (`Quantity`),
KEY `Created` (`Created`)
) TYPE=MyISAM;
When trying to insert a new Row at the end of this table I am getting the following message.
Duplicate entry '116604' for key 1
The insert statement is:
INSERT INTO CatalogueBasket (Shopper,ItemLink,Quantity,Created, Status, StartDate)
VALUES ('0.80916300 1338507348',58825,1,'2012-06-01 09:58:23', 0, '0-0-0')
I'm assuming it is talking about the ID column.
If I run the following query I get 116603 as the last key
SELECT * FROM `CatalogueBasket` order by ID desc limit 1
Any insight / help into this is appreciated.