I want to use cascading in my project to simplify certain processes. I created these two queries as well as a couple more but all child tables throw the same error on execution. They worked when I used MyISAM as the engine, but on further testing and research I figured that it does not support cascading, so I switched the engine to InnoDB which triggered these errors. I've looked at a couple of forums and threads with the same problem but I just can't seem to figure out where the actual problem lies. Can someone help?
ParentTable:
CREATE TABLE IF NOT EXISTS `branches` ( `branch_id` int(11) NOT NULL AUTO_INCREMENT, `key` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `name` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `short_name` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `city` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `timezone` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, UNIQUE(`key`), PRIMARY KEY (`branch_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8
Child Table:
CREATE TABLE IF NOT EXISTS `files` ( `file_id` int(11) NOT NULL AUTO_INCREMENT, `branch_id` int(11) NOT NULL, `path` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `use_google_analytics` BOOLEAN NOT NULL, FOREIGN KEY(`branch_id`) REFERENCES `branches`(`branch_id`) ON DELETE CASCADE ON UPDATE CASCADE, PRIMARY KEY (`file_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8
Error:
1005: Can't create table 'files' (errno: 150)
You defined your foreign key
ON DELETE CASCADE
But your branch_id is defined as NOT NULL. That won't work.
You want your branch_id never be null but then the foreign key related data gets removed you want it set to NULL with the CASCADE option.
So either change removd the ON DELETE CASCADE or allow NULL in the foreign key. This works
SQLFiddle demo
Related
I am trying to json_encode data from the database but the file does not display more than 255 characters.
I have a table column with 812 characters and I want to return a json.
The column in which that data is saved is of type TEXT and it allows to save the 812 characters. But when I run json_encode from the table, it doesnot show anything and when I reduce the content, it returns the json and yet I need all the content.
This is what i get after i run: SHOW CREATE TABLE
CREATE TABLE news` (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
headline varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
details text COLLATE utf8mb4_unicode_ci NOT NULL,
file varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
created_at timestamp NULL DEFAULT NULL,
updated_at timestamp NULL DEFAULT NULL, PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci`
I am getting the following error message when trying to run some sql code in my 5.5.35-MariaDB. Please can someone help me understand what is wrong?
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS action_recorder ( id int(11) NOT NULL AUTO_I' at line 1
My code is below:
CREATE TABLE IF NOT EXISTS `action_recorder` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`module` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`user_id` int(11) DEFAULT NULL,
`user_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`identifier` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`success` char(1) COLLATE utf8_unicode_ci DEFAULT NULL,
`date_added` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_action_recorder_module` (`module`),
KEY `idx_action_recorder_user_id` (`user_id`),
KEY `idx_action_recorder_identifier` (`identifier`),
KEY `idx_action_recorder_date_added` (`date_added`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=23 ;
Please can someone help me resolve this? Thanks!
You're missing the closing parenthesis. The one at the end is only for the key idx_action_recorder_date_added, but you need another one to close the whole table definition.
Also, I think you shouldn't use those normal single quotes. They are for strings. Use backticks or omit them altogether.
Since the "near..." mentions 'CREATE...', the error is either the CREATE or what immediately precedes it. I vote for the latter -- Look at what have right before it.
I am using the redbeanPHP ORM and mysql. I have the following table:
CREATE TABLE `mast` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`note` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`geolocation` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`location` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`zip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`state` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`app` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UQ_84a93b55f688c94f73092dba1b9590c41a92cbf5` (`app`,`geolocation`)
) ENGINE=InnoDB AUTO_INCREMENT=98 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I want to insert records into the 'mast' table providing they are unique with respect to both of the 2 fields listed above. In other words if either 'geolocation' or 'app' is a duplicate, I don't want to insert the associated record.
I am using following php code to insert the records using rebean:
$resultBean= R::dispense('mast');
$resultBean ->import($resultsarray);
try {
$id = R::store($resultBean); // TRY TO INSERT INTO MAST
} catch (Exception $exc) {
}
The insert is occurring except I notice that duplicate records on at least the 'app' field are being inserted. I am getting a normal looking record, while the duplicate has all zero or null values except for the 'app' field which has a duplicate entry.
I don't want the duplicate entries in the table at all. How can I prevent them from being inserted?
if either 'geolocation' or 'app' is a duplicate, I
don't want to insert the associated record.
UNIQUE KEY `UQ_84a93b55f688c94f73092dba1b9590c41a92cbf5` (`app`,`geolocation`)
Only prevents the pair app and geolocation from being duplicated.
If you want to prevent either one individually being duplicated, add separate indexes for each i.e.
CREATE UNIQUE INDEX UQ_app ON mast (app);
CREATE UNIQUE INDEX UQ_geolocation ON mast (geolocation);
I am a newbie to PHP. I have been given the code snippet below as homework:
CREATE TABLE `admin_log` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`statusdate` DATETIME DEFAULT NULL,
`type` INT(11) DEFAULT NULL
PRIMARY KEY (`id`)
) ENGINE=MYISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin
Why will it not be possible to set up any foreign key constraints using this table?
I have done some research on Google and I cant find a reason why foreign key constraints are not possible. Please help
You can do that like this :
CREATE TABLE `admin_log` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`statusdate` DATETIME DEFAULT NULL,
`type` INT(11) DEFAULT NULL,
INDEX (type),
FOREIGN KEY (type)
REFERENCES type(id)
ON UPDATE CASCADE ON DELETE RESTRICT,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=latin
Notice the engine INNODB insetad of MYISAM which doesn't permit foreign key.
Or using MySQLAdmin in the "structure" tab, click on the "relationals view" link below the table description.
Even it has been mentioned on comment before -- just to make it more prominent: MyISAM is not supporting foreign keys. So you will need to change the engine of your table to e.g. INNODB if possible.
I have integrated Jstree in my application, now i want to understand different column in that table:
CREATE TABLE IF NOT EXISTS `tree` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`parent_id` bigint(20) unsigned NOT NULL,
`position` bigint(20) unsigned NOT NULL,
`left` bigint(20) unsigned NOT NULL,
`right` bigint(20) unsigned NOT NULL,
`level` bigint(20) unsigned NOT NULL,
`title` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`type` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=13 ;
This is the default table provided by the site.
Now if want to add a node, how do i know the value for left, right and level.
This looks like a mix of Adjacency list an nested sets.
Nested sets are a better way of storing trees in a relational database.
It's hard to explain the principle you have to look here and here.
When you use nested sets you don't need parent_id.
I think jstree provided a sample table where you can choose by yourself what technique you use.
Another way of storing trees in a database would be a Closure Table.
It's my personal favourite. It's simple but powerful. But you hardly find anything about it on the net.