PHP / MySQL - Foreign Key Issue - php

Im following a tutorial at the moment and learning how to build something into my existing site.
The tutorial tells me that i need to run this against my database
ALTER TABLE posts ADD FOREIGN KEY(post_by) REFERENCES users(user_id) ON DELETE RESTRICT ON UPDATE CASCADE;
and im receiving an error saying it cant create table.
the 'users' table is from my existing database and everything else is new.
The tutorial gives me a few alter commands to run and they all go perfect, except when im trying to use the 'users' table.
Im totally stuck on this, any help would be greatly appreciated.
Cheers

Check your table type. It should be InnoDB.

Take a look to http://dev.mysql.com/doc/refman/5.1/en/alter-table.html, there is very clear what options are available to each engine.
Take a look to the section that describes:
The FOREIGN KEY and REFERENCES clauses are supported by the InnoDB
storage engine
And:
For other storage engines, the clauses are parsed but ignored.
And as h2ooooooo said you can change your engine with:
ALTER TABLE table ENGINE = INNODB

Related

Laravel PHPUnit failing on ALTER TABLE using SQLite

I have a migration which I made at the beginning of my project, basically adding a TEXT column called 'description' which is set to NOT NULL.
Now several months down the track I need to change that to allow null.
I can't use Laravel 5.5 change() function as I have a enum in my column list and it bugs out, so i need to add it as a raw query in a migration like so;
DB::statement('ALTER TABLE `galleries` MODIFY `description` TEXT NULL;');
When i do a php artisan migrate against my local mysql database it all works great, BUT when i try to run my test suite, it all breaks.
Im using SQLite for my test suite, and the error im getting is as follows;
PDOException: SQLSTATE[HY000]: General error: 1 near "MODIFY": syntax error
If anyone else has come up against this issue and fixed it, i would love to hear how you did it.
Thanks
SQLite only allows you to rename the table or add a column. The ALTER TABLE statement cannot change or remove columns.
In order to change or remove a column in SQLite, you need to create a new table with the desired schema, copy the data from the original table to the new table, delete the original table, and then rename the new table to the original name.
This is all abstracted out for you by Laravel and DBAL, so your best bet may be to get help with figuring out the issue with your enum column (though that would be a separate question).
You can read more about altering tables in the SQLite docs here.

Take MySQL Database dump contain Constraints tables

To take the backup I follow the below procedure.
First I take the list of table in my DB using SHOW TABLES LIKE
Then take table structure using SHOW CREATE TABLE
Then Saving all table structure and its value in to the file.
Now Backup works fine.
While restoring I am facing the problem.
If some of tables contain foreign key constraint with reference of other table.
I am not able to create the table.
I found the problem because I am taking backup table by table .
For example there are 4 table
A,B,C,D
A - Contain Constraint with C
C - Contain constraint with D.
I Take the backup above table and store them into file like this order
A,B,C,D. while Restoring causing error.
My question is How to handle while backup the database if table contain constraints?
I searched lot but I was not able to get good solution. So, Please share how to do this or share if I any thing done wrong.
Thank you.
As for the update to this question, I temporarily disabled the key check now it's worked. this solution posted in this question
http://stackoverflow.com/questions/15501673/how-to-temporarily-disable-a-foreign-key-constraint-in-mysql
SET FOREIGN_KEY_CHECKS=1;

MySQL error when copying/importing database

the premise to my problem is this:
I know very little about MySQL, and I have a MySQL database of a wordpress blog that I'm trying to copy between two instances of XAMPP, from one under OS X, to the other under Windows 7. I assume they both are InnoDB, not sure, though. Both XAMPP versions are the latest to date.
I export the database to an .sql file with all the default settings and then try to import it into an empty database of the same name, when it gets to this table
Indexes for table wp_posts
ALTER TABLE `wp_posts`
ADD PRIMARY KEY (`ID`), ADD KEY `post_name` (`post_name`),
ADD KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
ADD KEY `post_parent` (`post_parent`), ADD KEY `post_author` (`post_author`),
ADD FULLTEXT KEY `crp_related` (`post_title`,`post_content`),
ADD FULLTEXT KEY `crp_related_title` (`post_title`),
ADD FULLTEXT KEY `crp_related_content` (`post_content`);
I get this error:
#1795 - InnoDB presently supports one FULLTEXT index creation at a time
What should I do to succesfully copy the database over?
I understand it's an InnoDB bug of some kind, Googling the exact error phrase I found this thread
MySQL Error When Copying or Importing Database
but I don't know enough to understand what the OP did to his db so it worked...
I also found this
http://sourceforge.net/p/phpmyadmin/feature-requests/1553/
I understand this is directly related to the error i'm getting, and that it probably tells a solution ("That is why PhpMyAdmin at export should separate creation of FULLTEXT indexes into few SQL commands") but I don't know enough to understand that either, and Googling further about FULLTEXT only finds pages about "fulltext search" function.
But what should I DO, exactly? Please advice.

mysql alter table order by not working

I have searched over net a lot. What I could understand is that this thing has been faced by many people before me and it has also been filed as mysql bugs. But I couldn't find any solution to this. The problem is just that I can't get this command working-
alter table areas order by area_name;
I get this warning-
ORDER BY ignored as there is a user-defined clustered index in the table 'areas'
I just want to sort the table on the basis of 'area_name', that is, names of areas. Just to add, I am trying to do this in the database of my laravel app.
If the db engine is InnoDB, then you can't do this.
From the doc:
ORDER BY does not make sense for InnoDB tables because InnoDB always
orders table rows according to the clustered index.

PHP My Admin Not Creating Relations

I am using xamp.
I created a DB using SQL Yog,
I opened my localhost/phpmyadmin/
then selected the newly created database.
I wanted to make relations among tables, for instance there are two tables,USER and USERSTATS, I want to create relation depending on USER_ID, which exists in both table.
I selected create relation option, selected reference key from USERS table, then click on STATS table and selected foreign key, i got a prompt "Create relation", I clicked OK.
Now it was to supposed to be creating relation, but it's not, just a small blank popup window opens in firefox, with link
localhost/phpmyadmin/pmd_general.php?db=MYDBNAME&server=1&token=d9d3ed2661d4cc1d0db47eca1ebee996
But it is not creating the relation.
Please assist me in resolving this issue
Have you created your tables with InnoDB ? InnoDB accepts creation of foreign keys
Did you go through the complete phpmyadmin installation steps? You have to create the phpmyadmin specific tables. Without them you will not be able to see relations or create them.
http://docs.phpmyadmin.net/en/latest/setup.html#phpmyadmin-configuration-storage
mysql doesn't support the foreign key relations, though it accepts that keyword. That's probably the reason phpMyAdmin doesn't allow this.
If you can use InnoDB engine for your table, foreign keys are supported.
From an answer from stackoverflow for why-my-table-doesnt-support-foreign-keys
ALTER TABLE tableName ENGINE = InnoDB;
you need to add an index to the field you want as foreign key. You can do it by going to the table and clicking "Index" or you can do it manually: "ALTER TABLE YourTable ADD INDEX ( YourField )"

Categories