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
Related
First query:
SELECT
u.id,
u.first_name,
u.last_name,
u.tazkera_id,
cu.relation_type,
CASE csp.`shift`
WHEN '1' THEN 'Morning'
WHEN '2' THEN 'Afternoon'
else 'Neither'
END AS shift
FROM `course_user` as cu
LEFT JOIN `courses` as c ON c.id = cu.`course_id`
LEFT JOIN (
select users.id,
users.first_name,
users.last_name,
users.tazkera_id
FROM `users`
) as u ON u.id = cu.`user_id`
left JOIN `course_schedule_prefs` as csp ON csp.`user_id` = cu.`user_id`
Where cu.relation_type = 1 group by cu.`user_id`;
Second Query:
SELECT
u.id,
u.first_name,
u.last_name,
u.tazkera_id,
cu.relation_type,
CASE csp.`shift`
WHEN '1' THEN 'Morning'
WHEN '2' THEN 'Afternoon'
else 'Neither'
END AS shift
FROM `course_user` as cu
LEFT JOIN `courses` as c ON c.id = cu.`course_id`
LEFT JOIN users as u ON u.id = cu.`user_id`
left JOIN `course_schedule_prefs` as csp ON csp.`user_id` = cu.`user_id`
Where cu.relation_type = 1 group by cu.`user_id`;
Tables :
users:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`last_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 DEFAULT NULL,
`is_password_reset` int(11) NOT NULL DEFAULT '0',
`login_ip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`login_date` datetime DEFAULT NULL,
`last_login_ip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`last_login_date` datetime DEFAULT NULL,
`is_email_address_verified` int(11) NOT NULL DEFAULT '0',
`failed_login_attempts` int(11) DEFAULT NULL,
`last_failed_login_date` datetime DEFAULT NULL,
`tazkera_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`province_code` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`district_code` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`village` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`home_address` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`promote_unique_user_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`forgot_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`email_confirm_token` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`district_other` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`father_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`training_provider_id` int(10) unsigned DEFAULT NULL,
`is_profile_completed` int(11) DEFAULT NULL,
`active` int(11) DEFAULT '1',
`picture` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`small_picture` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`last_poll` bigint(20) DEFAULT NULL,
`last_password_reset` date DEFAULT NULL,
`presence_status` int(11) DEFAULT NULL,
`auto_presence` int(11) NOT NULL DEFAULT '0',
`last_activity` timestamp NULL DEFAULT NULL,
`trash` tinyint(4) NOT NULL DEFAULT '0',
`login_approval` tinyint(4) NOT NULL DEFAULT '0',
`lang` varchar(2) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'en',
`cover` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`small_cover` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`forum_reputation` int(11) NOT NULL DEFAULT '0',
`forum_suspension_date` date DEFAULT NULL,
`forum_status` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`),
KEY `users_training_provider_id_foreign` (`training_provider_id`),
CONSTRAINT `users_training_provider_id_foreign` FOREIGN KEY (`training_provider_id`) REFERENCES `training_providers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1000038 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
course_users:
CREATE TABLE `course_user` (
`course_id` int(10) unsigned DEFAULT NULL,
`user_id` int(10) unsigned DEFAULT NULL,
`relation_type` int(11) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
KEY `course_user_course_id_foreign` (`course_id`),
KEY `course_user_user_id_foreign` (`user_id`),
CONSTRAINT `course_user_course_id_foreign` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `course_user_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
courses;
CREATE TABLE `courses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`curriculum_id` int(10) unsigned DEFAULT NULL,
`training_center_id` int(10) unsigned DEFAULT NULL,
`training_coordinator_id` int(10) unsigned DEFAULT NULL,
`focal_point_id` int(10) unsigned DEFAULT NULL,
`start_date` date DEFAULT NULL,
`end_date` date DEFAULT NULL,
`start_time` time DEFAULT NULL,
`end_time` time DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`shift` int(11) DEFAULT NULL,
`stage_id` int(10) unsigned DEFAULT NULL,
`stream_id` int(10) unsigned DEFAULT NULL,
`unit_id` int(10) unsigned DEFAULT NULL,
`master_trainer_id` int(10) unsigned DEFAULT NULL,
`status` int(11) DEFAULT NULL,
`pre_test_generated` int(11) NOT NULL DEFAULT '0',
`post_test_generated` int(11) NOT NULL DEFAULT '0',
`conduct_days` int(11) DEFAULT NULL,
`no` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`evaluation_available` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `courses_curriculum_id_foreign` (`curriculum_id`),
KEY `courses_training_coordinator_id_foreign` (`training_coordinator_id`),
KEY `courses_focal_point_id_foreign` (`focal_point_id`),
KEY `courses_stage_id_foreign` (`stage_id`),
KEY `courses_stream_id_foreign` (`stream_id`),
KEY `courses_unit_id_foreign` (`unit_id`),
KEY `courses_master_trainer_id_foreign` (`master_trainer_id`),
KEY `courses_training_center_id_foreign` (`training_center_id`),
CONSTRAINT `courses_curriculum_id_foreign` FOREIGN KEY (`curriculum_id`) REFERENCES `curriculums` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `courses_focal_point_id_foreign` FOREIGN KEY (`focal_point_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `courses_master_trainer_id_foreign` FOREIGN KEY (`master_trainer_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `courses_stage_id_foreign` FOREIGN KEY (`stage_id`) REFERENCES `stages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `courses_stream_id_foreign` FOREIGN KEY (`stream_id`) REFERENCES `streams` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `courses_training_center_id_foreign` FOREIGN KEY (`training_center_id`) REFERENCES `training_centers` (`id`),
CONSTRAINT `courses_training_coordinator_id_foreign` FOREIGN KEY (`training_coordinator_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `courses_unit_id_foreign` FOREIGN KEY (`unit_id`) REFERENCES `units` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=139 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
course_schedule_prefs;
CREATE TABLE `course_schedule_prefs` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`cunduct_days` int(11) DEFAULT NULL,
`training_centers` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`shift` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `course_schedule_prefs_user_id_foreign` (`user_id`),
CONSTRAINT `course_schedule_prefs_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
My question is , which one of this query is good in term of performance and accuracy. Why ?
first query: I join directly with table; select all columns of table.
Second query: I used subquery inside of join; select only those element which I need it.
Note : My main question is about users table which is in join
There are too many possibilities here. There is no simple, 'straight', answer.
With LEFT JOIN (instead of JOIN), the Optimizer cannot start with the subquery, which it would usually like to do.
If there is a WHERE clause and it is useful for filtering rows, the Optimizer would like to start with the table that is filtered the most. Your WHERE clause smells like a flag, which is usually not very useful.
If a subquery has a GROUP BY or LIMIT, then it may significantly shrink the number of rows to work with. (Not in your case.) This might make the subquery approach better.
If cu has INDEX(relation_type, user_id), the Optimizer may decide that handling all of the WHERE and GROUP BY leads to the best bet, thereby starting with cu as the 'first' table.
In old versions of MySQL, subqueries in JOIN ( SELECT ... ) had no way of getting an index, thereby leading to inefficient table scans of that tmp table. In new versions (starting with 5.6, I think), the Optimizer takes the extra step (which costs something) to generate the optimal index ((id) in this case) after creating the tmp table and before JOINing it.
More
2 more table definitions needed.
Do you have the indexes I suggested?
InnoDB really needs a PRIMARY KEY - See many:many for advice on course_user.
I'm trying to add a foreign key constraint on my schema devair from field user_id of table bluePrints to the PK id of table users but I get an error:
ERROR 1215: Cannot add foreign key constraint
Here are my table definitions:
users:
CREATE TABLE `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(60) COLLATE utf8_unicode_ci NOT NULL,
`remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
bluePrints:
CREATE TABLE `bluePrints` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`bluePrintName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`user_id` int(10) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `uid` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
And the offending alter table statement:
ALTER TABLE `devair`.`bluePrints`
ADD CONSTRAINT `bp_u`
FOREIGN KEY (user_id)
REFERENCES `devair`.`users` (id)
ON DELETE CASCADE
ON UPDATE CASCADE;
The simple answer would be that you need to do this:
ALTER TABLE table1 ADD CONSTRAINT fk_bp_id FOREIGN KEY (columnFromTable1) REFERENCES table2(columnReferencedFromTable2);
The problem is the 'unsigned' in the bluePrints table. Once it's unsigned, then it works.
Now i have to figure out how to get laravel to use unsigned.
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.
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?
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.