insert trigger not working - mysql

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.

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 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

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`)

Recursive Table nullable parent_id

CREATE TABLE IF NOT EXISTS `db_teamup`.`programming_languages` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
`name` VARCHAR(255) NOT NULL COMMENT '',
`count` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '',
`parent_id` INT UNSIGNED NULL COMMENT '',
`icon_path` VARCHAR(255) NOT NULL DEFAULT 'default_icon.svg' COMMENT '',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`, `parent_id`) COMMENT '',
INDEX `fk_programming_languages_programming_language_parent_idx` (`parent_id` ASC) COMMENT '',
CONSTRAINT `fk_programming_languages_programming_language_parent_id`
FOREIGN KEY (`parent_id`)
REFERENCES `db_teamup`.`programming_languages` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I am trying to make a recursive mysql table, but when i execute the script, on workbench, it sets the parent_id to not null, is there a setting or a command that i am not executing before running my script?
The worst part is that it makes the default value 0.
thank you
PRIMARY KEY ('id')
I don't think 'parent_id' should be part of your primary key. It wouldn't be able to be NULL if it was.

MySQL: referencing foreign keys

I am trying to reference a foreign key to its parent key in mysql and i get an awkward error.
I have tried the following.
ALTER TABLE `website`
ADD CONSTRAINT `website_cms_fk1` FOREIGN KEY (`cms_id`) REFERENCES `cms_technology` (`ID`);
also
ALTER TABLE website ADD FOREIGN KEY (cms_id) REFERENCES cms_technology (ID)
And I get the following error.
*#1005 - Can't create table 'script.#sql-5203_110b8ba' (errno: 150)*
The following is my tables
CREATE TABLE IF NOT EXISTS `cms_technology` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`cms_name` varchar(250) NOT NULL DEFAULT '',
`cms_description` varchar(250) NOT NULL DEFAULT '',
PRIMARY KEY (`ID`)
);
INSERT INTO `cms_technology` (`ID`, `cms_name`, `cms_description`) VALUES
(1, 'Wordpress', 'WordPress › Blog Tool, Publishing Platform, and CMS');
CREATE TABLE IF NOT EXISTS `website` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`website_url` varchar(60) NOT NULL DEFAULT '',
`website_ip` varchar(20) NOT NULL DEFAULT '',
`website_title` varchar(250) NOT NULL DEFAULT '',
`website_status` varchar(10) NOT NULL DEFAULT '',
`website_scanned` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`website_response` int(3) NOT NULL DEFAULT '0',
`cms_id` int(5) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`)
);
INSERT INTO `website` (`ID`, `website_url`, `website_ip`, `website_title`, `website_status`, `website_scanned`, `website_response`, `website_cms`) VALUES
(1, 'http://www.wpbeginner.com/', '', '', '', '0000-00-00 00:00:00', 0, 0);
What im I doing wrong?
you need to change the data type of the of column cms_ID from table website in order to reference table cms_technology. The properties of the to columns must be the same.
CREATE TABLE IF NOT EXISTS `website` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`website_url` varchar(60) NOT NULL DEFAULT '',
`website_ip` varchar(20) NOT NULL DEFAULT '',
`website_title` varchar(250) NOT NULL DEFAULT '',
`website_status` varchar(10) NOT NULL DEFAULT '',
`website_scanned` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`website_response` int(3) NOT NULL DEFAULT '0',
`cms_id` bigint(20) unsigned NOT NULL, -- <<== HERE
PRIMARY KEY (`ID`)
);
SQLFiddle Demo
I was missing the KEY cms_id (cms_id)
CREATE TABLE IF NOT EXISTS `website` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`website_url` varchar(60) NOT NULL DEFAULT '',
`website_ip` varchar(20) NOT NULL DEFAULT '',
`website_title` varchar(250) NOT NULL DEFAULT '',
`website_status` varchar(10) NOT NULL DEFAULT '',
`website_scanned` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`website_response` int(3) NOT NULL DEFAULT '0',
`cms_id` bigint(20) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
KEY `cms_id` (`cms_id`)
);
ALTER TABLE `website`
ADD CONSTRAINT `website_cms_fk1` FOREIGN KEY (`cms_id`) REFERENCES `cms_technology` (`ID`);