merge two tables from different schemas along with updated foreign keys - mysql

I want to merge two tables with same structure from two different schemas. Right now I am doing this through following query:
INSERT INTO schema1.table1(col1,col2)
SELECT col1,col2
FROM schema2.table1;
This query merges two tables fine into one but foreign keys are not updated. They are the same as in original table. So is there any way to do it.
CREATE TABLE `research_delta`.`source` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`url` varchar(500) COLLATE utf8_bin NOT NULL,
`createdOn` datetime NOT NULL,
`modifiedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`isDeleted` tinyint(4) NOT NULL DEFAULT '0',
`structure` mediumblob,
`firstRunStatus` varchar(50) COLLATE utf8_bin NOT NULL DEFAULT '0',
`isMaster` tinyint(4) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='All the sources supported by the system';
CREATE TABLE `research_delta`.`sourcetagitem` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`source` bigint(20) DEFAULT NULL,
`tagItem` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_source_sourcetagitem` (`source`),
KEY `fk_tagItem_sourcetagitem` (`tagItem`),
CONSTRAINT `fk_source_sourcetagitem` FOREIGN KEY (`source`) REFERENCES `source` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_tagItem_sourcetagitem` FOREIGN KEY (`tagItem`) REFERENCES `tagitem` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=287 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `research_delta`.`tagitem` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_bin NOT NULL,
`description` varchar(1000) COLLATE utf8_bin DEFAULT NULL COMMENT 'this field will contain any description details about the type of category or tag..',
`parentId` bigint(20) DEFAULT NULL COMMENT 'if the category or tag in subject to be under any other catefory or tag then this field will contain the id of the category that it is under.',
`createdOn` datetime DEFAULT NULL,
`modifiedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`isDeleted` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=286 DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='this table represents the tags and categories supported by a';
When i merge two tagitem table from different schema and then merge sourcetagitem table the foreign key i.e tagitem should be updated with updated tagitem id after merging.
Thanks,

Do actually need to merge them, or can you use a union when you want to query the data?
(SELECT * FROM schema1.table1) UNION (SELECT * FROM schema2.table1)
Or create a view in the same way...
CREATE VIEW view1 AS (SELECT * FROM schema1.table1) UNION (SELECT * FROM schema2.table1);
Then select whatever you're interested in from that
SELECT col1 FROM vv;

Related

Laravel 7 leftJoin, Distinct and Sorted by related created_at

I'm developing a listing ads website dedicated to used video games with Laravel 7. I've got a 'GAMES' table and a 'LISTINGS' table as such.
GAMES
CREATE TABLE `games` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`title` text COLLATE utf8mb4_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `fulltext_index` (`title`)
) ENGINE=InnoDB AUTO_INCREMENT=10230 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
LISTINGS
CREATE TABLE `listings` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL,
`game_id` bigint(20) unsigned NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `listings_user_id_foreign` (`user_id`),
KEY `listings_game_id_foreign` (`game_id`),
CONSTRAINT `listings_game_id_foreign` FOREIGN KEY (`game_id`) REFERENCES `games` (`id`) ON DELETE CASCADE,
CONSTRAINT `listings_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=412 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
I need to have a paginated list of all the 'GAMES' (not listings) that are being sorted by the 'created_at' column of the latest 'LISTING' attached.
Thanks to #mayankmodi who helped sloving one of the problem, I've updated the below's part to focus on the sorting issue.
If I do this in my gameController:
$games = Game::leftJoin('listings', function($leftJoin)
{
$leftJoin->on('listings.game_id', 'games.id')
->whereNull('listings.deleted_at');
})
->select('games.*', 'listings.created_at')
->orderByDesc('listings.created_at')
->groupBy('games.id')
->with(['listings' => function ($query) {
$query->latest();
}])
->simplePaginate(36);
My games are distincts, but not ordered by last attached listing.created_at.
Do you have any idea how to solve this ?
Ok, I got it to work as expected.
Should anyone can benefit from 4+ hours of trying things out, I'll paste my solution down here.
$games = Game::leftJoin('listings', function($leftJoin){
$leftJoin->whereNull('listings.deleted_at')
->on('listings.game_id', 'games.id');
})
->select('games.*', DB::raw('MAX(listings.id) AS latest_listing'))
->groupBy('games.id')
->orderByDesc('latest_listing')
->with('listings')
->simplePaginate(36);
Thanks for your help !

slow join on table (10k rows)

i have table actions (30 rows) and passed_actions(10k rows)
actions table:
CREATE TABLE `actions` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`category_id` int(10) unsigned NOT NULL,
`author_id` int(10) unsigned NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'need for url',
`about` longtext COLLATE utf8_unicode_ci,
`image` text COLLATE utf8_unicode_ci,
`page_title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`page_description` varchar(512) COLLATE utf8_unicode_ci DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '0',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `actions_slug_unique` (`slug`),
KEY `actions_author_id_foreign` (`author_id`),
KEY `actions_category_id_foreign` (`category_id`),
CONSTRAINT `actions_author_id_foreign` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `actions_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
and passed_actions (~9500 rows)
CREATE TABLE `passed_actions` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned NOT NULL,
`action_id` int(10) unsigned NOT NULL,
`hash` varchar(6) COLLATE utf8_unicode_ci NOT NULL,
`public` tinyint(1) NOT NULL DEFAULT '1',
`successfully_passed` tinyint(1) NOT NULL DEFAULT '0',
`started_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `passed_actions_hash_unique` (`hash`),
KEY `passed_actions_user_id_foreign` (`user_id`),
KEY `passed_actions_action_id_foreign` (`action_id`),
CONSTRAINT `passed_actions_action_id_foreign` FOREIGN KEY (`action_id`) REFERENCES `actions` (`id`) ON DELETE CASCADE,
CONSTRAINT `passed_actions_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=25733 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
this query takes 0.3-0.5s:
select
actions.*
from actions
left join passed_actions.action_id = actions.id
group by actions.id
order by count(passed_actions.id) DESC
this affects to response time of my api...
why is this happening? i think that 10k rows is not a large table...
i use default mysql config. My server is 1gb ram and 1 cpu (digital ocean droplet)
Your query is actually reasonable fast. Sometimes a correlated subquery can help:
select a.*
from actions a
order by (select count(*) from passed_actions pa where pa.action_id = a.id) desc;
This can use the index you have defined on passed_actions(action_id).
If all you want off the second table is the count for sorting, as appears to be the case, try (untested, sorry):
select
actions.*
from actions
left join (select action_id, count(*) as passed_count from passed_actions group by action_id) p on actions.action_id = p.action_id
order by passed_count DESC
(I can't see where tests.id is coming from, I'm afraid.)
1- Rebuild the index and update statistics
2- Select Only the column you want to use
3- run this query in a new query and hit "Right click" and Click on "Display Estimated Execution Plan" and view the Missing Index Details and build the required index and run the query again

MySQL select from many tables with multiple keys and values

I’m projecting an e-commerce web-app. I have to give the user a set of filters by many goods properties.
In my case, a item in shop may have a many properties, and properties united in collections. For example:
Collection «Bikes» have a «Bryce type» and «Height» properties;
collection «TV» have a «Diagonal» and «Technology».
A items have a property_collection_id key reference to collections table.
The sql query should select a bike with disk braces and height more than 17'', or tv with diagonal more than 100'' and plasma technology (for example).
The table structure is
#
# Sale item
#
CREATE TABLE `object` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`property_collection_id` int(11) DEFAULT NULL,
`price` double DEFAULT NULL,
`description` text COLLATE utf8_unicode_ci,
`state_id` int(11) DEFAULT NULL COMMENT 'State of object, reference to other table',
`created_at` int(11) DEFAULT NULL,
`updated_at` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `object_to_collection` (`property_collection_id`),
KEY `object_state_fk` (`state_id`),
CONSTRAINT `object_state_fk` FOREIGN KEY (`state_id`) REFERENCES `object_state` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `object_to_collection` FOREIGN KEY (`property_collection_id`) REFERENCES `property_collection` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
#
# Collection of possible item property
#
CREATE TABLE `property_collection` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`slug_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Name without spaces and punctuation',
`description` text COLLATE utf8_unicode_ci,
`sort` int(11) NOT NULL DEFAULT '100',
`title_field` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
#
# Description of one property
#
CREATE TABLE `property` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`slug_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`type` enum('boolean','string','integer','list') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'string',
`require` enum('1','0') COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`property_collection_id` int(11) DEFAULT NULL,
`sort` int(11) NOT NULL DEFAULT '100',
`units` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `property_to_collection` (`property_collection_id`),
CONSTRAINT `property_to_collection` FOREIGN KEY (`property_collection_id`) REFERENCES `property_collection` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
#
# Item property values
#
CREATE TABLE `property_values` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`object_id` int(11) DEFAULT NULL,
`property_id` int(11) DEFAULT NULL,
`value` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `value_to_object` (`object_id`),
KEY `value_to_property` (`property_id`),
CONSTRAINT `value_to_property` FOREIGN KEY (`property_id`) REFERENCES `property` (`id`),
CONSTRAINT `value_to_object` FOREIGN KEY (`object_id`) REFERENCES `real_estate_object` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=161 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Code in http://pastebin.com/zJNApskJ
The query that I create is not (and will not) work:
SELECT `object`.* FROM `object`
LEFT JOIN `property_values` ON `object`.`id` = `property_values`.`object_id`
LEFT JOIN `property` ON `property_values`.`property_id` = `property`.`id`
WHERE
(
((`property_values`.`value`='7') AND (`property_values`.`value`='1'))
AND
(CAST(`property_values`.`value` AS DECIMAL) BETWEEN '0' AND '100')
)
AND
(`object`.`property_collection_id`='2') GROUP BY `object`.`id`
How should I create this query?
Your query must reflect the dependency of the tables. Something like
SELECT ...
FROM object o
INNER JOIN property_collections pc ON pc.id = o.property_collection_id
INNER JOIN property p ON p.property_collection_id = pc.id
INNER JOIN property_value pv ON pv.property_id = p.id
WHERE ...
And, as Amdixon already pointed out, you probably meant to say
WHERE pv.value IN ('1','7')
As the original AND condition would always be false. However, I suspect, that you would have to check on the property_collection_id first, in order to make sure, which product type you are actually looking at. Whithout furher data it is impossible to come up with a likely solution.

Fail to create relationship between two tables

I have two tables :
CREATE TABLE `Users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL DEFAULT '',
`last_name` varchar(50) NOT NULL DEFAULT '',
`login` varchar(50) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
AND
CREATE TABLE `Books` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL DEFAULT '',
`author` varchar(50) NOT NULL DEFAULT '',
`year` int(4) NOT NULL,
`available` int(3) NOT NULL DEFAULT '0',
`availabledate` date NOT NULL,
`user_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
I am trying to create a relationship between those two, so that one user may have multiple books (user_id) but whatever I'm doing I'm getting errors. Either
Cannot add or update a child row: a foreign key constraint fails
(bookstore.,
CONSTRAINT books_fk FOREIGN KEY (user_id) REFERENCES users
(user_id) ON DELETE CASCADE ON UPDATE CASCADE)
or before I didn't use unsigned int in the Books table and I said that default value is 0 (which I would prefere but I don't think I can do that?) In that case I got error 150.
I'd recommend, to change your database schema. Why?
Can a book exist without having a user? If yes, you shouldn't have a foreign key from books referencing users. Can a user exist without having a book? If yes, you shouldn't have a foreign key from users referencing books.
Can a user have multiple books? And a book multiple users? If yes, you have a m:n relationship. This means you need a bridge table.
In your tables you don't need foreign keys:
CREATE TABLE `Users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL DEFAULT '',
`last_name` varchar(50) NOT NULL DEFAULT '',
`login` varchar(50) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
CREATE TABLE `Books` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL DEFAULT '',
`author` varchar(50) NOT NULL DEFAULT '',
`year` int(4) NOT NULL,
`available` int(3) NOT NULL DEFAULT '0',
`availabledate` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
And a bridge table would look like this:
CREATE TABLE books_users (
book_id int(11) unsigned NOT NULL,
user_id int(11) unsigned NOT NULL,
PRIMARY KEY (book_id, user_id),
KEY idx_user_id (user_id),
FOREIGN KEY fk_books (book_id) REFERENCES Books(id),
FOREIGN KEY fk_users (user_id) REFERENCES Users(user_id)
) ENGINE=InnoDB;
This solves both problems and is common practice.
To query both users and books in one query, you join them like this:
SELECT
whatever
FROM
Books b
INNER JOIN books_users bu ON b.id = bu.book_id
INNER JOIN users u ON bu.user_id = u.user_id
WHERE user_id = 1 /*for example*/
;
If you want to insert something in the tables, just do the insert and get the id which was generated for the row with SELECT LAST_INSERT_ID();, then insert this id in the books_users bridge table.
Updates don't affect anything, you can simply perform them on users or books. If you really really have to update the auto_increment column (which usually isn't needed and not recommended) you can add ON UPDATE CASCADE after the foreign keys in the books_users table.
Change these lines and try again
change user_id int(11) unsigned NOT NULL AUTO_INCREMENT to user_id
int(11) NOT NULL AUTO_INCREMENT
and
id int(11) unsigned NOT NULL AUTO_INCREMENT, to id int(11) NOT
NULL AUTO_INCREMENT,
and
user_id int(11) unsigned NOT NULL, to user_id int(11) NOT NULL,
Finally try
CREATE TABLE `Users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL DEFAULT '',
`last_name` varchar(50) NOT NULL DEFAULT '',
`login` varchar(50) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
and
CREATE TABLE `Books` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL DEFAULT '',
`author` varchar(50) NOT NULL DEFAULT '',
`year` int(4) NOT NULL,
`available` int(3) NOT NULL DEFAULT '0',
`availabledate` date NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
If you are using Mysql then you must use InnoDB database engine to enable relationship between two tables
MyISAM will not allow this relationship
alter table `users` add constraint `FK_users` FOREIGN KEY (`user_id`) REFERENCES `books` (`user_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
or you can use this query for cascade edit delete
alter table `users` add constraint `FK_users` FOREIGN KEY (`user_id`) REFERENCES `books` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
This is your new DDL for two tables try this
CREATE TABLE IF NOT EXISTS `books` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL DEFAULT '',
`author` varchar(50) NOT NULL DEFAULT '',
`year` int(4) NOT NULL,
`available` int(3) NOT NULL DEFAULT '0',
`availabledate` date NOT NULL,
`user_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL DEFAULT '',
`last_name` varchar(50) NOT NULL DEFAULT '',
`login` varchar(50) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `users`
--
ALTER TABLE `users`
ADD CONSTRAINT `FK_users` FOREIGN KEY (`user_id`) REFERENCES `books` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

Put unique index across more than one table

I have run into a problem where the easiest solution would be to add a unique index that goes across multiple tables in mysql. Is this possible?
For the purpose of this question I will have three tables; status, tracks and events. The status and tracks tables both have an auto incrementing ID (T_ID or S_ID) in. Information from them is added to the events table with a trigger. The problem is that there could be the same auto incrementing ID in tracks and status, this means that their could be the same ID more than once in events.
tracks;
CREATE TABLE `tracks` (
`ID` int(11) NOT NULL,
`url` varchar(200) COLLATE latin1_general_ci NOT NULL,
`name` varchar(100) COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`T_ID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`T_ID`),
UNIQUE KEY `url` (`url`),
UNIQUE KEY `T_ID` (`T_ID`),
KEY `ID` (`ID`),
CONSTRAINT `tracks_ibfk_1` FOREIGN KEY (`ID`) REFERENCES `members` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
status
CREATE TABLE `status` (
`ID` int(11) NOT NULL,
`status` varchar(300) COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`S_ID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`S_ID`),
UNIQUE KEY `S_ID` (`S_ID`),
KEY `ID` (`ID`),
CONSTRAINT `status_ibfk_1` FOREIGN KEY (`ID`) REFERENCES `artists` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
events
CREATE TABLE `events` (
`ID` int(11) NOT NULL,
`action` varchar(100) COLLATE latin1_general_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`E_ID` int(11) NOT NULL,
KEY `ID` (`ID`),
CONSTRAINT `events_ibfk_1` FOREIGN KEY (`ID`) REFERENCES `members` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='This table shows what the artist has done and is used feed'
Agreed, you cannot have an index across multiple tables.
However, consider changing your schema by adding a "source" column to your event table and making a unique constraint on the combination of the source and the foreign key. This would prevent duplicate keys being inserted from the same source.