Can't create table with foreign key Maria DB - mysql

I'm having hard time creating a table in maria db with foreign key and I can't figure it out.
I got that message : "Can't create table evanightdb.events_liked (errno: 150 "Foreign key constraint is incorrectly formed")".
The table I try to create :
CREATE TABLE events_liked (
id INT NOT NULL AUTO_INCREMENT,
id_events int(11),
title_event text,
event_liked int NOT NULL,
PRIMARY KEY ID,
FOREIGN KEY (id_events) REFERENCES event_details(`id`),
FOREIGN KEY (title_event) REFERENCES event_details(`title`)
)
And this is the table "event_details".
CREATE TABLE `event_details` (
`id` int(11) NOT NULL,
`event_url` varchar(300) NOT NULL,
`url` varchar(300) NOT NULL,
`title` text NOT NULL,
`description` text DEFAULT NULL,
`event_image_url` text DEFAULT NULL,
`image_path` text DEFAULT NULL,
`address_1` text DEFAULT NULL,
`address_2` text DEFAULT NULL,
`latitude` decimal(10,8) DEFAULT NULL,
`longitude` decimal(10,8) DEFAULT NULL,
`event_by` text DEFAULT NULL,
`genre` varchar(255) DEFAULT NULL,
`start_time` timestamp NULL DEFAULT NULL,
`start_time_not_parsed` datetime DEFAULT NULL,
`end_time` timestamp NULL DEFAULT NULL,
`duration` time DEFAULT NULL,
`ticket` text DEFAULT NULL,
`check_address` varchar(255) DEFAULT NULL,
`going` text DEFAULT NULL,
`interested` text DEFAULT NULL,
`createdAt` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
UNIQUE KEY `url` (`url`);
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

In MariaDB
TEXT and BLOB columns cannot be used as foreign keys
Knowledge Base » MariaDB Server Documentation » High Availability & Performance Tuning » Optimization and Tuning » Optimization and Indexes » Foreign Keys
Possible solution 1: add generated column of VARCHAR(xxx) datatype which is assigned with the prefix of TEXT column then use it for FK.
Possible solution 2: add generated column of proper datatype which is assigned with some hash of TEXT column then use it for FK.
Both solutions assumes that generated column is created in both tables.

Related

phpMyAdmin Error - Says my table does not have a unique column when it has two

phpMyAdmin is displaying:
Current selection does not contain a unique column. Grid edit, checkbox, Edit, Copy and
Delete features are not available.
It displays this even when I have a unique column. In fact, I have two:
CREATE TABLE `Rest_Details` (
`Resturant_ID` bigint(255) NOT NULL AUTO_INCREMENT,
`Resturant_name` varchar(100) NOT NULL,
`Resturant_des` varchar(200) NOT NULL,
`Res_Address_Line_1` varchar(200) NOT NULL,
`Res_Address_Line_2` varchar(200) DEFAULT NULL,
`City_name` varchar(100) NOT NULL,
`Resturant_Postcode` varchar(8) DEFAULT NULL,
`Cat_ID` tinyint(11) NOT NULL,
`Avg_Del` tinyint(11) NOT NULL,
`Est_Del` tinyint(11) NOT NULL,
`Email1` varchar(200) NOT NULL,
`Email2` varchar(200) DEFAULT NULL,
`Min_ord` tinyint(11) NOT NULL,
PRIMARY KEY (`Resturant_ID`),
UNIQUE KEY `Resturant_name` (`Resturant_name`),
UNIQUE KEY `Resturant_ID` (`Resturant_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
I believe this is causing countless problems with inserting data and selecting data from the table in question. How can I resolve this?

Regex to get names of all SQL tables

I have a CREATE TABLE document and I need to get a listing of all the table names. This is what I have:
CREATE TABLE `mturk_reviewqueue` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`catalog_id` int(11) DEFAULT NULL,
`tv_series_id` int(11) DEFAULT NULL,
`added_on` datetime NOT NULL,
`correct_url` varchar(100) DEFAULT NULL,
`notes` varchar(400) DEFAULT NULL,
`completed_on` datetime DEFAULT NULL,
`completed_by_id` int(11) DEFAULT NULL,
`top_url` varchar(100) DEFAULT NULL,
`diff_score` decimal(5,2) DEFAULT NULL,
`ip_checkout` varchar(24) DEFAULT NULL,
`incorrect_original_url` varchar(100) DEFAULT NULL,
`fix_notes` varchar(1000) DEFAULT NULL,
`is_promotional_content` tinyint(1) DEFAULT NULL,
`contains_multiple_titles` tinyint(1) DEFAULT NULL,
`is_garbage_series` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `completed_by` (`completed_by_id`),
KEY `catalog_id` (`catalog_id`),
KEY `tv_series_id` (`tv_series_id`),
CONSTRAINT `mturk_reviewqueue_ibfk_1` FOREIGN KEY (`completed_by_id`) REFERENCES `auth_user` (`id`),
CONSTRAINT `mturk_reviewqueue_ibfk_2` FOREIGN KEY (`catalog_id`) REFERENCES `main_catalog` (`id`),
CONSTRAINT `mturk_reviewqueue_ibfk_3` FOREIGN KEY (`tv_series_id`) REFERENCES `main_tvseriesinstance` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=196089 DEFAULT CHARSET=utf8;
-- Create syntax for TABLE 'mturk_worker'
CREATE TABLE `mturk_worker` (
`worker_id` varchar(22) NOT NULL DEFAULT '',
`notes` varchar(100) NOT NULL,
...etc...
And this is what I need:
mturk_reviewqueue
mturk_worker
etc...
So far I have:
r'CREATE\sTABLE\s`(.+)`\s\('
This is getting me the table name, but I can't seem to get rid of all the junk after the table name but before the next one. What would be the best regex to do this?
r'CREATE\sTABLE\s`(.+?)`'
This should do it for you.
If you'd like a command line option. Say your sql file is named "foo":
cat foo | grep 'CREATE TABLE' | cut -d\` -f2
This should work on MacOS.
This should also work (even when there should be more than one blank between words) ...
/CREATE\s+TABLE\s+`([^`]+)`/g
If you're using ack you can do this:
ack 'CREATE TABLE `(.+)`' --output='$1'

Foreign key constraint fails during `drop table`

I have a strange problem in which I'm not able to delete a table as a foreign key constraint fails. The scenario is as follows.
I'm trying to drop the table departments from my DB, the structure for which is as follows:
show create table `departments`
CREATE TABLE `departments` (
`dept_id` int(11) NOT NULL AUTO_INCREMENT,
`dept_name` varchar(50) NOT NULL,
PRIMARY KEY (`dept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Now, the only other table in the database that has department_id is the employee table:
show create table employee
CREATE TABLE `employee` (
`emp_id` varchar(20) NOT NULL,
`role` varchar(10) DEFAULT NULL,
`password` varchar(500) DEFAULT NULL,
`division_id` int(20) DEFAULT NULL,
`email_bb` varchar(100) DEFAULT NULL,
`is_active` tinyint(1) NOT NULL,
`date_joining` date DEFAULT NULL,
`date_confirmation` date DEFAULT NULL,
`date_appraisal` date DEFAULT NULL,
`date_leaving` date DEFAULT NULL,
`first_name` varchar(100) DEFAULT NULL,
`middle_name` varchar(100) DEFAULT NULL,
`last_name` varchar(100) DEFAULT NULL,
`sex` varchar(1) DEFAULT NULL,
`dob` date DEFAULT NULL,
`email_other` varchar(100) DEFAULT NULL,
`contact` varchar(100) DEFAULT NULL,
`present_addr` varchar(1000) DEFAULT NULL,
`perma_addr` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`emp_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
As you can see, none of these tables are related via foreign keys. So why do I get this error when trying to drop the department table:
#1217 - Cannot delete or update a parent row: a foreign key constraint fails
Is there a better way (and hopefully, simpler) way to see the foreign keys defined? What might be going wrong?
show create table doesn't show incoming FK restraints (e.g. FK is specified in child table, not parent)
So there is a possibility that you have another table with a FK constraint to that table. I usually dump the schema of the database, which shows all FK constraints.

MySQL Error Number 150 when creating Table with Foreign Key

I am having an issue creating a new table in my database. I've seen that the error code it is returning is to do with Foreign Key constraints.
I checked to ensure that the data type of the foreign key in the new table matched the data type of the primary key in the other table. They are both int(11).
However I am still getting an error. Am I missing something? This is my SQL script for creating the new table:
CREATE TABLE `regular_features` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(200) DEFAULT NULL,
`day` VARCHAR(200) DEFAULT NULL,
`description` TEXT DEFAULT NULL,
`programme_id` INT(11) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`programme_id`) REFERENCES directoryprogramme(id)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
This is the original table containing the primary key:
CREATE TABLE `directoryprogramme` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(250) NOT NULL,
`broadcast_time` VARCHAR(100) NOT NULL,
`description` TEXT NOT NULL,
`days` VARCHAR(150) NOT NULL,
`contributors` VARCHAR(250) NOT NULL,
`directorycompany_id` INT(11) NOT NULL,
`directorycontact_id` VARCHAR(250) NOT NULL,
`facebook_link` VARCHAR(250) DEFAULT NULL,
`twitter_link` VARCHAR(250) DEFAULT NULL,
`wikipedia_link` VARCHAR(250) DEFAULT NULL,
`web` VARCHAR(250) DEFAULT NULL,
`imageextension` VARCHAR(10) DEFAULT NULL,
`type` VARCHAR(20) NOT NULL DEFAULT 'other',
PRIMARY KEY (`id`)
) ENGINE=MYISAM AUTO_INCREMENT=1161 DEFAULT CHARSET=utf8;
The Foreign Key will be the id of directoryprogramme
The problem is the last line of your create statement:
ENGINE=INNODB DEFAULT CHARSET=utf8;
You mix MYISAM in ald table with INNODB in your new table.
That doesn't work.
Chnage the engine in your new table to MYISAM and it works.

django fulltext search mysql

I have the default User model in django as per below:
delimiter $$
CREATE TABLE `auth_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(30) NOT NULL,
`email` varchar(75) NOT NULL,
`password` varchar(128) NOT NULL,
`is_staff` tinyint(1) NOT NULL,
`is_active` tinyint(1) NOT NULL,
`is_superuser` tinyint(1) NOT NULL,
`last_login` datetime NOT NULL,
`date_joined` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=latin1$$
I tried to add full text searching by alter table 'auth_user' add fulltext(first_name), last_name, email), and I keep getting the error Error Code: 1214. The used table type doesn't support FULLTEXT indexes. Is there a reason why this doesn't support full text searching? I'm thinking it may be because I extended the model and added my own table?
Fulltext indices only work on MyISAM tables, and yours is InnoDB. source
You may want to use Django-Sphinx for full text search or look at their implementation. https://github.com/dcramer/django-sphinx