adding relation between mysql tables in Codeigniter - mysql

I'm using Codeigniter and want to add relation between the posts and comments tabels.
posts:
CREATE TABLE IF NOT EXISTS `posts` (
`post_id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`slug` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`content` text COLLATE utf8_persian_ci NOT NULL,
`time` varchar(10) COLLATE utf8_persian_ci NOT NULL,
`num_of_comments` int(3) NOT NULL,
`cat` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`tags` varchar(100) COLLATE utf8_persian_ci NOT NULL,
PRIMARY KEY (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=15 ;
comments:
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`content` text COLLATE utf8_persian_ci NOT NULL,
`date` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`time` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`name` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`email` varchar(30) COLLATE utf8_persian_ci NOT NULL,
`image` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`ip` varchar(16) COLLATE utf8_persian_ci NOT NULL,
`submit` int(1) NOT NULL,
`reply` text COLLATE utf8_persian_ci NOT NULL,
`reply_date` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`reply_time` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`post_id` int(5) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `post_id` (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=26 ;
but it prints this error:
SQL query:
ALTER TABLE `comments` ADD FOREIGN KEY ( `post_id` ) REFERENCES `codeig`.`posts` (
`post_id`
) ON DELETE CASCADE ON UPDATE CASCADE ;
MySQL said:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`codeig`. <result 2 when explaining filename '#sql-fc8_dd'>, CONSTRAINT `#sql-fc8_dd_ibfk_1` FOREIGN KEY (`post_id`) REFERENCES `posts` (`post_id`) ON DELETE CASCADE ON UPDATE CASCADE)
and i don't know what is talking about at all.
please help...

Where did you write this query? Or this was generated by codeigniter?
Try this in any sql administration tool:
ALTER TABLE comments ADD FOREIGN KEY (post_id) REFERENCES posts(post_id) ON DELETE CASCADE ON UPDATE CASCADE ;
You don't need the codeig part, I'm assume that is the database name.

Related

recovery and join

For a back-end web project, we use PHP 7.4 and MySQL 8.X, here is the structure of a part of the DB
CREATE TABLE `availability ` (
`id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL
`student_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`start` date DEFAULT NULL,
`end` date DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_2CBACE2FDDEAB1A3` (`student_id`),
CONSTRAINT `FK_2CBACE2FDDEAB1A3` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
CREATE TABLE `student` (
`id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`sex_id` int DEFAULT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`first_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`phone` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`picture` longtext COLLATE utf8mb4_unicode_ci,
`cv` longtext COLLATE utf8mb4_unicode_ci,
PRIMARY KEY (`id`),
KEY `IDX_717E22E3448F3B3C` (`sex_id`),
CONSTRAINT `FK_717E22E3448F3B3C` FOREIGN KEY (`sex_id`) REFERENCES `sex` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
CREATE TABLE `request` (
`id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`student_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`date` datetime NOT NULL,
`title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`content` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `IDX_2694D7A5DDEAB1A3` (`student_id`),
CONSTRAINT `FK_2694D7A5DDEAB1A3` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
Here are the cardinalities:
a STUDENT can have several availabilities (no matter what the
category of the availability is) an AVAILABILITY can belong to one or
more STUDENTS
a student can have one or more APPLICATIONS
a REQUEST belongs to only one STUDENT
but when I try to retrieve all the REQUESTS according to a period, it retrieves the data twice:
SELECT DE.*, ET.picture AS picture_editor, CONCAT(ET.first_name, ' ', ET.name) AS name_editeur, DI.start, DI.end
FROM request DE
INNER JOIN student ET ON ET.id = DE.student_id
INNER JOIN availability DI ON DI.student_id = DE.student_id
WHERE DI.start< CAST('2022-12-31' AS DATETIME) AND DI.end> CAST('2022-09-11' AS DATETIME)
Can you help me as I want to retrieve the data according to the student's availability

PhpMyAdmin export : reference without uppercase

First, I'm sorry for my poor English, I'm french.
I use PhpMyAdmin with Xampp in localhost and an other PhpMyAdmin on server
My tables are like Personne, Adresse, Titre.
When I use the export option with server my sql is perfect :
CREATE TABLE IF NOT EXISTS `Personne` (
`idPersonne` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`gsm` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`nom` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`prenom` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`telephone` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`idAdresse` int(11) DEFAULT NULL,
`idTitre` int(11) DEFAULT NULL,
PRIMARY KEY (`idPersonne`),
UNIQUE KEY `uniquePersonne` (`nom`,`prenom`),
KEY `IDX_F6B8ABB9D3E663E4` (`idAdresse`),
KEY `IDX_F6B8ABB9E8B304A9` (`idTitre`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=685 ;
ALTER TABLE `Personne`
ADD CONSTRAINT `FK_F6B8ABB9D3E663E4` FOREIGN KEY (`idAdresse`) REFERENCES `Adresse` (`idAdresse`) ON DELETE SET NULL,
ADD CONSTRAINT `FK_F6B8ABB9E8B304A9` FOREIGN KEY (`idTitre`) REFERENCES `Titre` (`idTitre`) ON DELETE SET NULL;
By when I use the local export
CREATE TABLE IF NOT EXISTS `Personne` (
`idPersonne` int(11) NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`gsm` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`nom` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`prenom` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`telephone` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`idAdresse` int(11) DEFAULT NULL,
`idTitre` int(11) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=685 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
it's makes lines for primary, unique, normal key and auto_increment :
ALTER TABLE `Personne`
ADD PRIMARY KEY (`idPersonne`), ADD UNIQUE KEY `uniquePersonne` (`nom`,`prenom`), ADD KEY `IDX_F6B8ABB9D3E663E4` (`idAdresse`), ADD KEY `IDX_F6B8ABB9E8B304A9` (`idTitre`);
ALTER TABLE `Personne`
MODIFY `idPersonne` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=685;
And reference don't contain uppercase
ALTER TABLE `Personne`
ADD CONSTRAINT `FK_F6B8ABB9D3E663E4` FOREIGN KEY (`idAdresse`) REFERENCES `adresse` (`idAdresse`) ON DELETE SET NULL,
ADD CONSTRAINT `FK_F6B8ABB9E8B304A9` FOREIGN KEY (`idTitre`) REFERENCES `titre` (`idTitre`) ON DELETE SET NULL;
How have the same result of server?
Thank you for your help
Adjust your lower_case_table_names setting for MySQL to achieve your goal. See How to force case sensitive table names?

mysql refference key issue, phpmyadmin error code 150 [duplicate]

This question already has answers here:
MySQL Creating tables with Foreign Keys giving errno: 150
(20 answers)
Closed 7 years ago.
I am trying to create two table as below,
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` ( `Id` int(11) NOT NULL
AUTO_INCREMENT, `UserType` varchar(32) DEFAULT NULL, `FirstName`
varchar(64) DEFAULT NULL, `LastName` varchar(64) DEFAULT NULL,
`Email` varchar(64) DEFAULT NULL, `CompanyName` varchar(64) DEFAULT
NULL, `Telephone` varchar(64) DEFAULT NULL, `Country` varchar(64)
DEFAULT NULL, `Website` varchar(64) DEFAULT NULL, `JobTitle`
varchar(64) DEFAULT NULL, `Active` int(1) DEFAULT NULL, `Notes`
text, `DateOfRegistration` datetime DEFAULT NULL, PRIMARY KEY
(`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Table structure for table `login`
--
CREATE TABLE IF NOT EXISTS `login` ( `Id` int(11) NOT NULL,
`Username` varchar(64) NOT NULL, `Email` varchar(64) NOT NULL,
`Password` varchar(64) NOT NULL, `FailedAttemptCount` int(2) NOT
NULL, `LastLogin` datetime NOT NULL, `UserLevel` int(1) NOT NULL,
`IsVerified` int(1) NOT NULL DEFAULT '0', `VerificationKey`
varchar(256) NOT NULL, `VerifiKeyCreated` datetime NOT NULL,
PRIMARY KEY (`Id`), FOREIGN KEY (`Id`) REFERENCES users.Id, UNIQUE
KEY `username` (`Username`), UNIQUE KEY `Email` (`Email`) )
ENGINE=InnoDB DEFAULT CHARSET=latin1;
I want to add a foreign key i.e. login.Id references users.Id, but I am getting error(code 150) in phpmyadmin.
Thanks in advance for your help!
Best Regards,
Rajib
It should be
REFERENCES `users`(`Id`)
instead of
REFERENCES users.Id
cf. http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

mysql INSERT abysmal performance

I'm iterating over about 14,000 records which I get from csv file.
For each user I do this:
1) I create a user record in users table
2) I insert ~21 attributes for that user into users_attributes table
Initially each user takes ~2 seconds to be inserted, but after awhile insertion time grows to ~50 seconds... and so script takes forever....
Why is it so slow?
Example insert statement:
Executed once:
INSERT INTO users (uname, email, pass, passreminder, activated, approved_date, approved_by, user_regdate, lastlogin, theme, ublockon, ublock, tz, locale) VALUES ('someid_example.com', 'someid#example.com', 'test', 'User-generated', -32768, '2014-09-03 14:41:50', 2, '2014-09-03 14:41:50', '1970-01-01 00:00:00', '', 0, '', '', '');
Repeated 21 times:
INSERT INTO users_attributes (name, value, user_id) VALUES ('user_information_address', 'some address', 9862);
Schema:
CREATE TABLE `users` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`uname` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`pass` varchar(138) COLLATE utf8_unicode_ci NOT NULL,
`passreminder` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`activated` smallint(6) NOT NULL,
`approved_date` datetime NOT NULL,
`approved_by` int(11) NOT NULL,
`user_regdate` datetime NOT NULL,
`lastlogin` datetime NOT NULL,
`theme` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`ublockon` smallint(6) NOT NULL,
`ublock` longtext COLLATE utf8_unicode_ci NOT NULL,
`tz` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`locale` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`uid`),user
KEY `uname` (`uname`),
KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `users_attributes` (
`user_id` INT(11) NOT NULL,
`name` VARCHAR(80) COLLATE utf8_unicode_ci NOT NULL,
`value` longtext COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`user_id`,`name`),
KEY `IDX_SOMENUM` (`user_id`),
CONSTRAINT `IDX_SOMENUM` FOREIGN KEY (`user_id`) REFERENCES `users` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

MySQL: ERROR 1072 (42000) at line.. Key column UserID doesn't exist in the table

I am trying to make simple database with 2 tables, first for user information, and second for their uploads, because it's project for faculty, I have some assignments... And one is to use foreign key.
But I can't figure out how to do that, when I do it with phpMyAdmin everything is working just fine, but when I export it and put it on my server I have some errors; please help me.
CREATE TABLE IF NOT EXISTS `korisnici` (
`UserID` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(12) COLLATE utf8_bin NOT NULL,
`password` varchar(32) COLLATE utf8_bin NOT NULL,
`email` varchar(32) COLLATE utf8_bin NOT NULL,
`telefon` varchar(16) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`UserID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin ROW_FORMAT=COMPACT AUTO_INCREMENT=4;
CREATE TABLE IF NOT EXISTS `slike` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) COLLATE utf8_bin NOT NULL,
`size` int(11) NOT NULL,
`type` varchar(200) COLLATE utf8_bin NOT NULL,
`file_path` varchar(200) COLLATE utf8_bin NOT NULL,
`username` varchar(12) COLLATE utf8_bin NOT NULL,
`naslov` varchar(32) COLLATE utf8_bin NOT NULL,
`adresa` varchar(80) COLLATE utf8_bin NOT NULL,
`opis` varchar(1200) COLLATE utf8_bin NOT NULL,
`datum` date NOT NULL,
`UserID` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `UserID` (`UserID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=4 ;
ALTER TABLE `slike`
ADD CONSTRAINT `slike_ibfk_1` FOREIGN KEY (`UserID`) REFERENCES `korisnici` (`UserID`);
ERRORS:
ERROR 1072 (42000) at line 77: Key column 'UserID' doesn't exist in table
I would appreciate if someone could fix my code and explain.
EDIT new error:
ERROR 1054 (42S22) at line 42: Unknown column 'UserID' in 'field list'
Again when I insert something:
SET FOREIGN_KEY_CHECKS=0;
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE IF NOT EXISTS `korisnici` (
`username` varchar(12) COLLATE utf8_bin NOT NULL,
`password` varchar(32) COLLATE utf8_bin NOT NULL,
`email` varchar(32) COLLATE utf8_bin NOT NULL,
`telefon` varchar(16) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
INSERT INTO `korisnici` (`username`, `password`, `email`, `telefon`) VALUES('Aleksa2', 'c4b4d0dea25b6f2f38fef63330ea15c8', 'sss#gmail.com', '0649999999');
INSERT INTO `korisnici` (`username`, `password`, `email`, `telefon`) VALUES('Aleksa123', 'a4b4d0dea25b6f2f38fef63330ea15c8', 'aaaa#gmail.com', '0649999999');
CREATE TABLE IF NOT EXISTS `slike` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) COLLATE utf8_bin NOT NULL,
`size` int(11) NOT NULL,
`type` varchar(200) COLLATE utf8_bin NOT NULL,
`file_path` varchar(200) COLLATE utf8_bin NOT NULL,
`username` varchar(12) COLLATE utf8_bin NOT NULL,
`naslov` varchar(32) COLLATE utf8_bin NOT NULL,
`adresa` varchar(80) COLLATE utf8_bin NOT NULL,
`opis` varchar(1200) COLLATE utf8_bin NOT NULL,
`datum` date NOT NULL,
PRIMARY KEY (`id`),
KEY `USERNAME_ID` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=3 ;
INSERT INTO `slike` (`id`, `name`, `size`, `type`, `file_path`, `username`, `naslov`, `adresa`, `opis`, `datum`) VALUES(15, '4eb40ffebd3fbec4b1acf34a4fe8cb2d.png', 43395, 'image/png', 'C:/Program Files (x86)/EasyPHP-DevServer-14.1VC11/data/localweb/images/4eb40ffebd3fbec4b1acf34a4fe8cb2d.png', 'Aleksa123', 'asdasdasd', 'dadasdas', 'Opisite problem u najvise 1200 karaktera.', '2014-09-05');
INSERT INTO `slike` (`id`, `name`, `size`, `type`, `file_path`, `username`, `naslov`, `adresa`, `opis`, `datum`) VALUES(16, '92bc0821619c053c503694666f2717ee.png', 32461, 'image/png', 'C:/Program Files (x86)/EasyPHP-DevServer-14.1VC11/data/localweb/images/92bc0821619c053c503694666f2717ee.png', 'Aleksa123', 'dadasdasdas', 'dadasdasdasdasdas', 'Opisite problem u najvise 12dasdasdasdas00 karaktera.', '2014-09-05');
INSERT INTO `slike` (`id`, `name`, `size`, `type`, `file_path`, `username`, `naslov`, `adresa`, `opis`, `datum`) VALUES(17, '89d0507bf3ed3f492647b7fd2f39047a.png', 162203, 'image/png', 'C:/Program Files (x86)/EasyPHP-DevServer-14.1VC11/data/localweb/images/89d0507bf3ed3f492647b7fd2f39047a.png', 'Aleksa123', '1111111111111a', 'dasdasdasda', 'Opisite problsdasdasdasdasaem u najvise 1200 karaktera.', '2014-09-05');
ALTER TABLE `slike`
ADD CONSTRAINT `slike_ibfk_1` FOREIGN KEY (`username`) REFERENCES `korisnici` (`username`);
SET FOREIGN_KEY_CHECKS=1;