MySQL alter two column for same foreign key - mysql

I have a table called user and the primary key is user_id.
I have another table called follows. This table is for storing which user follow which user(it is something like twitter follow function).
This is my follow table.
CREATE TABLE `follows` (
`id` int(11) NOT NULL,
`orginal_user_id` int(11) NOT NULL,
`follow_user_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `follows`
ADD PRIMARY KEY (`id`);
So, how can I alter this table to set both orginal_user_id and follow_user_id as a foreign key of user_id of user table...
If a user is deleted from the user table, I want to automatically delete rows in follows table either that user id appears on an orginal_user_id column or follow_user_id column.

You may use cascading delete constraints in your table:
CREATE TABLE follows (
id int(11) NOT NULL PRIMARY KEY,
orginal_user_id int(11) NOT NULL,
follow_user_id int(11) NOT NULL,
CONSTRAINT fk_original_user FOREIGN KEY (orginal_user_id)
REFERENCES user(id) ON DELETE CASCADE,
CONSTRAINT fk_follow_user FOREIGN KEY (follow_user_id)
REFERENCES user(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Related

foreign key shall automatically create a new entry

I have 2 tables users and iom.
Primary key of users is iomid and of course in iom the foreign key is iomid that references users.iomid.
If i create a new user in users, I want it automatically inserted into iom aswell without making a second statement.
How can I do it?
iom:
CREATE TABLE IF NOT EXISTS `iom` (
`id` int(11) NOT NULL,
`iomid` varchar(50) NOT NULL,
`subscribed` int(11) DEFAULT NULL,
`workoutcounter` int(11) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
ALTER TABLE `iom`
ADD UNIQUE KEY `id` (`id`), ADD KEY `iom_ibfk_1` (`iomid`);
ALTER TABLE `iom`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `iom`
ADD CONSTRAINT `iom_ibfk_1` FOREIGN KEY (`iomid`) REFERENCES `users` (`iomid`) ON DELETE CASCADE ON UPDATE CASCADE;
user:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL,
`iomid` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
ALTER TABLE `users`
ADD PRIMARY KEY (`iomid`), ADD UNIQUE KEY `id` (`id`);
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
Thanks and greetings!
Now that I look at your question again, the issues with your foreign key conventions are fairly irrelevant. Even with more typical relations, foreign key constraints cannot create rows; at most, they can "cascade" updates and deletes of referenced values to referencing tables.
What you need is an "AFTER INSERT ON users" trigger. (Documentation here.)

How to restrict inserting duplicate pairs of values in a table?

I have the following table in MySQL DB (MariaDB):
CREATE TABLE `restrictions_projects` (
`ID` int(11) NOT NULL,
`project_id` int(11) NOT NULL,
`restriction_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `restrictions_projects`
ADD PRIMARY KEY (`ID`),
ADD KEY `project_id` (`project_id`),
ADD KEY `restriction_id` (`restriction_id`);
ALTER TABLE `restrictions_projects`
MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `restrictions_projects`
ADD CONSTRAINT `restrictions_prfk_1` FOREIGN KEY (`project_id`) REFERENCES `projects` (`ID`),
ADD CONSTRAINT `restrictions_prfk_2` FOREIGN KEY (`restriction_id`) REFERENCES `restrictions` (`ID`);
COMMIT;
How can I restrict inserting duplicate pairs of project_id and restriction_id?
INSERT INTO restrictions_projects (project_id,restriction_id)
VALUES (1,2) // ??? ON DUPLICATE KEY UPDATE project_id=1 AND
restriction_id=2
Add a unique constraint:
ALTER TABLE restrictions_projects
ADD CONSTRAINT unq_restrictions_2 UNIQUE(project_id, restriction_id);
You can also do this using a unique index, which is effectively the same thing.
You need composite unique key on both column
UNIQUE (project_id,restriction_id)
You should alter your table to add unique key
ALTER TABLE restrictions_projects ADD CONSTRAINT restrictions_projects_uniq UNIQUE (project_id,restriction_id)

Creating foreign key between 2 tables

Ok, so it's like this. I have 2 tables in phpmyadmin. One is for personal details and the other is for login information. Both tables have AccountID, so I tried using foreign key constraints to connect the tables. After I did that it seems like I cannot update the table with new data. Before the constraint, updating the tables worked fine.
What I'm trying to do is store user login info and personal info in these table. Then whenever the user wants to delete their current account, the personal details and login details of are deleted from both tables or when they wanted to search for their login and personal info the search engine can search from both tables with AccountID.
so far.i have make 2 new tables.1 table which is personal information have 'AccountID'[A_I][PRIMARY] and 'loginID'.another table is login info.it has 'loginID'[A_i][PRIMARY]
i already make the 'loginID' at personal info and index but i cannot assign foreign key constraint for it bcause it did not detect 'loginID' in personal info.
Your AccountId should be a primary key in one table say Personal Info table.
This AccountId should be the foreign key in another table (Login) and make sure you set on Delete Cascade and on Update Cascade.
So in this structure, when personal info is deleted, its corresponding record in the login table will be automatically deleted.
DROP TABLE IF EXISTS `stacktest`.`personal_info`;
CREATE TABLE `stacktest`.`personal_info` (
`account_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`age` varchar(45) DEFAULT NULL,
PRIMARY KEY (`account_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `stacktest`.`login_info`;
CREATE TABLE `stacktest`.`login_info` (
`loginId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`loginId`),
KEY `FK_login_info_1` (`account_id`),
CONSTRAINT `FK_login_info_1` FOREIGN KEY (`account_id`) REFERENCES `personal_info` (`account_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
Above is the way how the 2 tables should be created.
Then insert some values in both table,
Note that account_id value has to be same in both the tables.
After that you can fire a delete query like:
delete from personal_info where accound_id=2;
This will delete rows from parent table personal_info and also from child table login_info where account_id is 2
Keeping the account_id as NOT NULL in child table:
DROP TABLE IF EXISTS `stacktest`.`login_info`;
CREATE TABLE `stacktest`.`login_info` (
`loginId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`loginId`),
KEY `FK_login_info_1` (`account_id`),
CONSTRAINT `FK_login_info_1` FOREIGN KEY (`account_id`) REFERENCES `personal_info` (`account_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

how to use foreign key for auto-incremented records?

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`student_id` int(11) NOT NULL AUTO_INCREMENT,
`student_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`student_id`)
) ENGINE=InnoDB AUTO_INCREMENT=901840505 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `grades`;
CREATE TABLE `grades` (
`grade_id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) DEFAULT NULL,
`grade` varchar(45) DEFAULT NULL,
PRIMARY KEY (`grade_id`),
KEY `stdgrd_idx` (`student_id`),
CONSTRAINT `stdgrd` FOREIGN KEY (`student_id`) REFERENCES `student` (`student_id`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1641 DEFAULT CHARSET=utf8;
I have created two tables student and grade. In order to send the auto-incremented values ofstudent_id to grade(student_id) table I established a foreign key between those two records as follows:
CONSTRAINT `stdgrd` FOREIGN KEY (`student_id`) REFERENCES `student`
(`student_id`) ON DELETE CASCADE ON UPDATE CASCADE
My question is when I insert values into student_name record in the student table the student(student_id) is auto-incremented so I want the auto-incremented value to be sent to grade(student_id)
but the grade table is not taking the value in. I already tried with the help of questions posted in stackoverflow but I couldn't make it. Please can anyone help out with this issue??
The Foreign Key relationship is only intended to validate the content of the child column. There is no automatic transfer of data. Inserting into student and thus generating the student_id auto_inc value will have no immediate impact on the grades table. That merely creates a student_id value that is now an acceptable content for the grades.student_id column.

MySQL foreign keys with non-identifying relationships

All I need is to create 2 tabeles with next structure:
The SQL:
CREATE TABLE IF NOT EXISTS `ds_cats` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `module_news_cats` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent` int(11) NOT NULL,
`cat_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_module_news_cats_module_news_cats` (`parent`),
KEY `fk_module_news_cats_ds_cats1` (`cat_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
ALTER TABLE `module_news_cats`
ADD CONSTRAINT `fk_module_news_cats_ds_cats1` FOREIGN KEY (`cat_id`) REFERENCES `ds_cats` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_module_news_cats_module_news_cats` FOREIGN KEY (`parent`) REFERENCES `module_news_cats` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
But when I try to insert first row to my table "module_news_cats", I recive next error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`empty`.`module_news_cats`, CONSTRAINT `fk_module_news_cats_module_news_cats` FOREIGN KEY (`parent`) REFERENCES `module_news_cats` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Question:
How I can create table which will have an index with non-identifying relationship to the anther index in the same table? Some rows will have parents, and some not.
I think you just need to allow NULLs in module_news_cats.parent:
CREATE TABLE IF NOT EXISTS `module_news_cats` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent` int(11) NULL, -- Change this
`cat_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_module_news_cats_module_news_cats` (`parent`),
KEY `fk_module_news_cats_ds_cats1` (`cat_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
and then if there isn't a parent, create the row with a NULL in parent.
Your 'parent' field cannot be empty (NULL) if you insert a record, which means that every record you insert should refer to a parent ID (which is impossible if there are no entries in your table yet).
If you make the 'parent' field in the module_news_cats table nullable:
ALTER TABLE `module_news_cats` CHANGE `parent` `parent` INT( 11 ) NULL DEFAULT NULL
you should be able to insert records that have no parent ID associated (just supply NULL instead of a value).
You could make the parent column in the module_news_cats table nullable.
Then for rows that have no parents populate the parent column with null.