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

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

Related

MySql unexpected query result

I have such a tables offers and offer_operating_systems related by offer_id. I made two queris and from my understanding they should return same result, but they don't
Query 1:
select
count(*)
from
`offers`
where
(
select count(*)
from `offer_operating_systems`
where
`offer_operating_systems`.`offer_id` = `offers`.`id`
and
`operating_system` = 'android'
) = 1
order by `id` asc
Query 2:
select
count(*)
from offer_operating_systems
where
operating_system = 'android'
order by `id` asc
Can someone explain to me why the results are not the same? Thanks!
EDITED
operating_systemcolumn is unique so each offer can have only one record with adnroid
Table structures
CREATE TABLE `offers` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`type` int(11) NOT NULL DEFAULT '0',
`url` text COLLATE utf8_unicode_ci,
`status` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'pending',
`api_created_at` timestamp NULL DEFAULT NULL,
`api_updated_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `offers_status_index` (`status`)
) ENGINE=InnoDB AUTO_INCREMENT=423 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `offer_operating_systems` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`offer_id` int(10) unsigned NOT NULL,
`operating_system` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `offer_operating_systems_offer_id_operating_system_unique` (`offer_id`,`operating_system`),
KEY `offer_operating_systems_offer_id_foreign` (`offer_id`),
CONSTRAINT `offer_operating_systems_offer_id_foreign` FOREIGN KEY (`offer_id`) REFERENCES `offers` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=728 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

"1062 Duplicate entry" on SELECT

I've got a SELECT query that's returning the following error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'user_id-name email#address.com--2018-11-04 01:24:38' for key ''
(I replaced the user_id, name and email for privacy)
SELECT users.user_id, users.fullname, users.email, users.phone_formatted, users.date_created,
MAX(cards.card_id) AS latest_card_id, MAX(cards.date_created) AS latest_date_card, MAX(punches.date_created) AS date_lastvisited
FROM users
INNER JOIN cards ON cards.user_id = users.user_id
INNER JOIN punches ON punches.card_id = cards.card_id
WHERE punches.business_id=1626
GROUP BY users.user_id, users.fullname, users.email, users.phone_formatted, users.date_created
ORDER BY users.fullname;
Here are the table definitions for cards, punches and users:
CREATE TABLE `cards` (
`card_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`business_id` int(11) DEFAULT NULL,
`printjob_id` int(11) DEFAULT NULL,
`temp` varchar(256) DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`whitelabel_id` int(11) NOT NULL DEFAULT '0',
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`card_id`),
KEY `printjob_id` (`printjob_id`),
KEY `active` (`active`),
KEY `user_id` (`user_id`),
KEY `business_id` (`business_id`)
) ENGINE=InnoDB AUTO_INCREMENT=576475 DEFAULT CHARSET=latin1;
CREATE TABLE `punches` (
`punch_id` int(11) NOT NULL AUTO_INCREMENT,
`punch_count` int(11) unsigned NOT NULL DEFAULT '1',
`employeebonus` tinyint(1) NOT NULL DEFAULT '0',
`details` text,
`card_id` int(11) DEFAULT NULL,
`behaviour` set('basicpunch','employeebonus','visitsregularly','broughtfriend','mytreat','firstcustomer','dailyrepeater','visitslocations','facebookshare','mostfrequent','longabsence','registerer','employeepromoter','luckiest','enroll','opportunist') NOT NULL DEFAULT 'basicpunch',
`location_id` int(11) DEFAULT NULL,
`business_id` int(11) DEFAULT NULL,
`employee_id` int(11) DEFAULT NULL,
`v1` float DEFAULT NULL,
`eventcounter` smallint(5) unsigned NOT NULL DEFAULT '0',
`date_local` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`punch_id`),
KEY `business_id` (`business_id`),
KEY `employee_id` (`employee_id`),
KEY `card_id` (`card_id`),
KEY `date_created` (`date_created`),
KEY `behaviour_id` (`behaviour`),
KEY `location_id` (`location_id`),
KEY `employeebonus` (`employeebonus`),
KEY `punch_count` (`punch_count`)
) ENGINE=InnoDB AUTO_INCREMENT=807402 DEFAULT CHARSET=latin1;
CREATE TABLE `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`fullname` varchar(60) DEFAULT NULL,
`email` varchar(50) NOT NULL,
`undeliverable` tinyint(4) DEFAULT '0',
`phone_formatted` varchar(25) DEFAULT NULL,
`phone_unformatted` varchar(15) DEFAULT NULL,
`password_hash` char(64) DEFAULT NULL,
`password_salt` char(6) DEFAULT NULL,
`reset_hash` binary(32) DEFAULT NULL,
`date_reset` datetime DEFAULT NULL,
`stripecustomer_id` int(11) DEFAULT NULL,
`stripe_customer_id` varchar(40) DEFAULT NULL,
`stripe_customer_access_id` varchar(40) DEFAULT NULL,
`problemwithpayment` tinyint(1) NOT NULL DEFAULT '0',
`enterprisemanager_whitelabel_id` int(11) NOT NULL DEFAULT '0',
`god` tinyint(1) NOT NULL DEFAULT '0',
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`),
KEY `email` (`email`),
KEY `phone_unformatted` (`phone_unformatted`),
KEY `date_reset` (`date_reset`),
KEY `undeliverable` (`undeliverable`)
) ENGINE=InnoDB AUTO_INCREMENT=16809 DEFAULT CHARSET=latin1;
I don't understand how I can get this error with a SELECT query. When I remove the MAX() functions, I no longer get the error.

Adding a Foreign Key to MariaDB / MySQL table

I have 3 tables:
CREATE TABLE `channels` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`status` enum('active','inactive') NOT NULL DEFAULT 'active’,
`description` text DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=485 DEFAULT CHARSET=utf8;
CREATE TABLE `categories` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`content` text DEFAULT NULL,
`description` text DEFAULT NULL,
`status` enum('active','inactive') NOT NULL DEFAULT 'active',
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
`updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=128 DEFAULT CHARSET=utf8;
CREATE TABLE `events` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`content` text DEFAULT NULL,
`timezone` varchar(255) DEFAULT NULL,
`recurring` tinyint(1) NOT NULL DEFAULT 0,
`all_day` tinyint(1) NOT NULL DEFAULT 0,
`starts_at` timestamp NULL DEFAULT NULL,
`ends_at` timestamp NULL DEFAULT NULL,
`started_at` timestamp NULL DEFAULT NULL,
`completed_at` timestamp NULL DEFAULT NULL,
`status` enum('active','inactive','in_progress','complete') DEFAULT 'active',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=82 DEFAULT CHARSET=utf8;
I want to alter the events table to add more fields and add foreign keys to channel, event_type and client_event_type.
I've tried this:
alter table `events`
add `channel` int unsigned not null,
add `event_type` int unsigned not null,
add `client_event_type` int unsigned not null,
add constraint `events_channel_foreign` foreign key `channel` references `channels`(`id`),
add constraint `events_event_type_foreign` foreign key `event_type` references `categories`(`id`),
add constraint `events_clientEventType_foreign` foreign key `client_event_type` references `categories`(`id`),
It comes up with :
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 'references channels(id), add constraint
events_event_type_foreign foreig' at line 5
What am I doing wrong?
Thanks
You're missing parantheses:
alter table `events`
add `channel` int unsigned not null,
add `event_type` int unsigned not null,
add `client_event_type` int unsigned not null,
add constraint `events_channel_foreign` foreign key (`channel`) references `channels`(`id`),
add constraint `events_event_type_foreign` foreign key (`event_type`) references `categories`(`id`),
add constraint `events_clientEventType_foreign` foreign key (`client_event_type`) references `categories`(`id`)

database error invalid values

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.

insert trigger not working

I have the following table and trigger but the trigger isn't setting the create_dt value to now() on the insert event:
CREATE TABLE `user` (
`user_id` int(10) NOT NULL auto_increment,
`user_name` varchar(255) NOT NULL default '',
`password` varchar(255) NOT NULL default '',
`first_name` varchar(255) NOT NULL default '',
`last_name` varchar(255) NOT NULL default '',
`email` varchar(255) NOT NULL default '',
`modify_by` int(10) NOT NULL default '1',
`modify_dt` timestamp NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`create_by` int(10) NOT NULL default '1',
`create_dt` datetime NOT NULL default '0000-00-00 00:00:00',
`active` enum('Yes','No') NOT NULL default 'No',
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_name` (`user_name`),
UNIQUE KEY `email` (`email`),
FOREIGN KEY (`modify_by`) REFERENCES `user`(`user_id`),
FOREIGN KEY (`create_by`) REFERENCES `user`(`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
CREATE TRIGGER ins_user BEFORE INSERT ON `user` FOR EACH ROW SET #create_dt = NOW();
I've tried both BEFORE and AFTER trigger action time but no change. Does anyone have any suggestions?
The goal is to have the create_dt value set with a date_time NOW() value on insert.
Setting #create_dt sets a variable. You want to SET NEW.create_dt = NOW(). This will change the value of the incoming record.