Using MYSQL Triggers and Foreign Keys - mysql

I do not know how to handle my issue here and I am looking for the most practical solution. I have to track a user-id across multiple tables and I am looking at triggers and Foreign Keys to solve the initial inserts into the databases for each user. On registration, I would like to create a row in each database with the user id. That way its always an update statement when I reference it in the code.
Here is my Table Structure
CREATE TABLE `authentication` (
`userid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`fname` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL,
`lname` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL,
`email` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`password` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL,
`online` int(11) DEFAULT NULL,
`created_at` varchar(11) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`email`),
KEY `userid` (`userid`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `user_progress` (
`userid` int(10) NOT NULL,
`progress_event` varchar(25) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Here is an example trigger I tried to make
CREATE TRIGGER user_progress_add
AFTER INSERT ON authentication
FOR EACH ROW
BEGIN
INSERT INTO user_progress (userid, progress_status)
VALUES (NEW.userid, 'signup');
END
This trigger did not work. It gives the error "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 'END' at line 1".
I guess my question is, whats wrong with my trigger? is a trigger a better idea in this case than a foreign key?

Related

MySql - Error Code: 1932. Table doesn't exist in engine

I am getting the error below on a mysql restart after adding an enum field to my existing table. Locks table and only a complete database restore fixes issue as I can't drop the table either.
Error Code: 1932. Table 'users' doesn't exist in engine
My table and data is as follows:
CREATE TABLE `users` (
`id` char(36) NOT NULL,
`name` varchar(191) NOT NULL,
`email` varchar(191) NOT NULL,
`password` varchar(191) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`)
);
INSERT INTO `users` VALUES
(
'11111111-1111-1111-1111-111111111111',
'John Doe',
'user#email.com',
'password',
NOW(),
NOW()
);
Table above works until line below is executed and mysql is restarted.
ALTER TABLE users ADD `role` enum('test1', 'test2') AFTER `password`
I have also done a diff on the table structure of a before and after. Only line with comment gets added and locks the table.
CREATE TABLE `users2` (
`id` char(36) COLLATE utf8mb4_unicode_ci NOT NULL,
`name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`role` enum('test1','test2') COLLATE utf8mb4_unicode_ci DEFAULT NULL, -- only diff
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users1_email_unique` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
This is where it gets weird. Line below adds role to the end of the table and doesn't lock the table.
ALTER TABLE users ADD `role` enum('test1', 'test2'); -- AFTER `password`
Seems the AFTER is the issue. Doesn't matter if I try a different position.
Any suggestions would be appreciated.
You can also assign the default value while adding the column.
ALTER TABLE users ADD `role` enum('test1', 'test2') DEFAULT 'test1' AFTER `password`;
So, NULL values will not allow and all the rows will be assigned with 'test1'.

Why is sql not letting me create this table?

I'm trying to migrate a db
from: MySQL Distrib 5.5.60-MariaDB, for Linux (x86_64)
to: MySQL 5.5.4, UNIX
I tried importing the db as a zip package and it started throwing errors so now I'm trying to re-create each table one at a time on phpMyAdmin.
The query below is throwing a #1064 Syntax error, and I'm having trouble figuring out the issue.
MySQL Said:
#1064 - 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 '(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP(2),
`st' at line 6
I'm looking at line 6, trying to find any reserved words, missing data, typos, and or obsolete commands but no luck.
CREATE TABLE `tblmoto_auth_policies` (
`policy_id` int(11) NOT NULL AUTO_INCREMENT,
`policy_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`policy_desc` text COLLATE utf8_unicode_ci NOT NULL,
`policy_url` text COLLATE utf8_unicode_ci NOT NULL,
`date_added` timestamp(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP,
`status` smallint(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`policy_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I could use some help.
Thanks in advance.
CURRENT_TIMESTAMP is not good
Try this:
CREATE TABLE `tblmoto_auth_policies` (
`policy_id` int(11) NOT NULL AUTO_INCREMENT,
`policy_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`policy_desc` text COLLATE utf8_unicode_ci NOT NULL,
`policy_url` text COLLATE utf8_unicode_ci NOT NULL,
`date_added` timestamp(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP(2),
`status` smallint(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`policy_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
This works in SQL Fiddle:
CREATE TABLE `tblmoto_auth_policies` (
`policy_id` int(11) NOT NULL AUTO_INCREMENT,
`policy_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`policy_desc` text COLLATE utf8_unicode_ci NOT NULL,
`policy_url` text COLLATE utf8_unicode_ci NOT NULL,
`date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` smallint(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`policy_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
i.e., remove the precision (the (2)) from the definition of the date_added column.
TIMESTAMP(2) is valid syntax, but not in combination with the DEFAULT CURRENT_TIMESTAMP nor ON UPDATE CURRENT_TIMESTAMP auto-initializers.

What should be the Schema for Donation table?

I am creating a web portal for an organization and I am a bit confused on this part.
They will be receiving donations from their registered members as well as guests. I was thinking of creating a users table that is solely used for registered members and no guests etc. because users table will contain unique "email" column and I don't want it to be null.
For donations, I can add user_id foreign key for users table.
What I am thinking of doing is that I should add "name" and "mobile" columns in donations table, so that if it's a guest, we should only get his name and phone number and put in donations table. Do you think this is the right way?
For just demo purpose I am showing you the table:
users table
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`mobile` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`status` tinyint(1) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`),
UNIQUE KEY `users_mobile_unique` (`mobile`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
donations table
CREATE TABLE IF NOT EXISTS `donations` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`mobile` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`status` tinyint(1) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `users_user_id_foreign` (`user_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
Any help would be much appreciated.
Thanks
There are many ways to solve data problems - some better than others. I would not have a separate user table if you are already getting some details. Rather have a REGISTERED_USER column or along those lines to denote a "full user" versus a partial. Then everything stays relatively simple AND the user has an option to become a full user later, which I assume you want for donations... :)

incomprehensible error on this simple create table query

I've just exported the CREATE sql of a singular table using phpmyadmin. this is the result:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`register_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`account_type` int(11) NOT NULL,
`active` int(11) NOT NULL,
`email` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`login` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(200) COLLATE utf8_unicode_ci NOT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
But when i run this code on the very phpmyadmin i receive this error:
#1064 - 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 '(`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREME' at line 14
I have tried many ways, but my skills aren't enough.
You forgot to add comma before PRIMARY KEY (id)
`name` varchar(200) COLLATE utf8_unicode_ci NOT NULL, -- <== this
PRIMARY KEY (`id`)
add comma and it will work, see this fiddle (click link)

Mysql Create Table error on second CREATE statement [duplicate]

This question already has answers here:
node-mysql multiple statements in one query
(3 answers)
Closed 5 years ago.
I'm trying to write a script to import a Mysql DB structure. I've exported the database SQL via PhpMyAdmin, and node npm model sqldump. Both produce the same error when trying to create the second table. This doesn't seem to be table-specific - I can mix the tables around (I have 20 tables in this DB) and always hit the same error on the second CREATE TABLE statement.
Can anyone point me towards something stupid I'm missing, please?
The error is:
{ Error: 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 'CREATE TABLE IF NOT EXISTS `autoresponses` (
`id` bigint(20) NOT NULL AUTO_INC' at line 14
at PromiseConnection.query (D:\dev-mysql-update\node_modules\mysql2\promise.js:75:20)
at D:\dev-mysql-update\index.js:157:7
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
message: '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 \'CREATE TABLE IF NOT EXISTS `autoresponses` (\n `id` bigint(20) NOT NULL AUTO_INC\' at line 14',
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '#42000' }
The first two tables are:
CREATE TABLE IF NOT EXISTS `autoresponders` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`uuid` char(36) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
`archived` tinyint(1) NOT NULL DEFAULT '0',
`title` varchar(255) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
`client_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `client_id` (`client_id`),
CONSTRAINT `autoresponders_ibfk_1` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `autoresponses` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`uuid` char(36) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
`archived` tinyint(1) NOT NULL DEFAULT '0',
`hours_delay` int(11) DEFAULT NULL,
`status` varchar(255) DEFAULT 'active',
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
`autoresponder_id` bigint(20) DEFAULT NULL,
`notification_id` bigint(20) DEFAULT NULL,
`client_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `autoresponses_autoresponder_id_notification_id_unique` (`autoresponder_id`,`notification_id`),
KEY `notification_id` (`notification_id`),
CONSTRAINT `autoresponses_ibfk_1` FOREIGN KEY (`autoresponder_id`) REFERENCES `autoresponders` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `autoresponses_ibfk_2` FOREIGN KEY (`notification_id`) REFERENCES `notifications` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Thanks in advance,
Andy
Your table name should be inside your parenthesis.