PL/SQL equivalent to MYSQL COLLATE - mysql

I am trying to convert some MYSQL code over to ORACLE PL/SQL. I am looking for a equivalent to the COLLATE command.
Here is a code snippet:
CREATE TABLE IF NOT EXISTS `service_types` (
`service_type_id` int(11) NOT NULL AUTO_INCREMENT,
`service_type` varchar(50) COLLATE latin1_bin NOT NULL,
`service_type_code` varchar(5) COLLATE latin1_bin NOT NULL,
`last_update_date` datetime NOT NULL,
`last_update_user` varchar(16) COLLATE latin1_bin NOT NULL,
PRIMARY KEY (`service_type_id`),
UNIQUE KEY `service_type_ix1` (`service_type_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin AUTO_INCREMENT=11 ;

I believe you'd want the linguistic sort parameters NLS_SORT and NLS_COMP. Note that these are session-level settings in Oracle, not table-level settings.

Related

I need to optimize a large data table in a mysql database

I have a table in a mysql DB with about 6 mil rows of data. Structure below. Most of my queries are searching for specific "customer" fields and display a value for each customer according to the value in column "value". The query searches the whole Table to match those customers specified in the query. This table started rather small but now it's gotten too big and my queries are taking quite some time to retrieve results. My questions is the following: If i create a separate table with just the customer field, along with an index, will that make my customer queries faster?
TABLE `data` (
`id` bigint(20) UNSIGNED NOT NULL,
`entry_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`date` date DEFAULT NULL,
`media_name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`media_type` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`rate` decimal(8,2) DEFAULT NUCREATELL,
`value` decimal(8,2) DEFAULT NULL,
`page` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`sector` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`category` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`customer` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`product` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`image_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`city` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`address` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`supplier` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`time` time DEFAULT NULL,
`duration` time DEFAULT NULL,
`promoted_on` datetime NOT NULL,
`hasimage` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
You want an index.
If you are searching for customers using in or = (the most common methods), then you can use a standard index on customer.
If you have more complex searches -- say using like with a leading wildcard -- then this does not work. A full text index might help. Or it might not, depending on the nature of the query and the data.
The "separate table" you're thinking about should be an index on your main table.
CREATE INDEX index_name
ON data(customer,value);
This will speed up the queries, and even prevent access to the table itself, at the cost of slightly slower INSERT and UPDATE operations.

Yii2 encrypt and Mysql

I hope someone can help me with this.
I am saving a personal data into MYSQL by encrypting using Yii2 encrypt/decrypt function, testing without saving the content into the DB works.
However saving the content into the DB and retrieving it back, seems something goes wrong because I am not able to decrypt it.
My MYSQL scheme is this
CREATE TABLE IF NOT EXISTS `customer` (
`customer_id` int(11) NOT NULL,
`customerHash` varchar(32) COLLATE utf8_bin DEFAULT NULL,
`customerDate` date DEFAULT NULL,
`customerTime` time DEFAULT NULL,
`customerUpdated` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`customerEmail` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerPassword` varchar(45) COLLATE utf8_bin DEFAULT NULL,
`customerToken` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerAddress` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerPhone` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerPostcode` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerName` blob,
`customerSurname` varchar(255) COLLATE utf8_bin DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Yii code
Encrypt
$request=Yii::$app->request;
$hash=Yii::$app->getSecurity()->generateRandomString();
$model->customerHash=$hash;
$model->customerName=Yii::$app->getSecurity()->encryptByKey($request->post('customerName'), $hash);
Decrypt
<? echo"Decode : ".Yii::$app->getSecurity()->decryptByKey($model->customerName, $model->customerHash);?>
Any help welcome.
Thanks

What could be wrong here

DROP TABLE IF EXISTS 'ci_sessions';
CREATE TABLE 'ci_sessions' (
'session_id' varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0',
'ip_address' varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0',
'user_agent' varchar(120) COLLATE utf8_bin DEFAULT NULL,
'last_activity' int(10) unsigned NOT NULL DEFAULT '0',
'user_data' text COLLATE utf8_bin NOT NULL, PRIMARY KEY ('session_id'),
KEY 'last_activity_idx' ('last_activity') )
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
I've tried to run this code on phpMyAdmin and it says
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near ''ci_sessions'' at line 1
It's the same error in all your table and column names. You use ' while you should use backticks:
`
So this should work:
DROP TABLE IF EXISTS `ci_sessions`;
CREATE TABLE `ci_sessions` (
`session_id` varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0',
`ip_address` varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0',
`user_agent` varchar(120) COLLATE utf8_bin DEFAULT NULL,
`last_activity` int(10) unsigned NOT NULL DEFAULT '0',
`user_data` text COLLATE utf8_bin NOT NULL, PRIMARY KEY (`session_id`),
KEY `last_activity_idx` (`last_activity`) )
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
As a sidenote - you use NOT NULL and then set the default value to the string 0. quite often. That indicates bad practice, or at least more work for later queries. A column named last_activity for example indicates that null values should be allowed...
Second sidenote - last_activity would typically be a DATETIME column, not an int(10)

XAMPP's PHPMyAdmin Ignores AUTO_INCREMENT Attribute When Exporting the Table

The problem that I am going to tell is for all the tables in the DB, When I dump my DB it ignores AUTO_INCREMENT attribute. My actual table is as the following:
CREATE TABLE IF NOT EXISTS `departments` (
`departmentid` int(11) NOT NULL AUTO_INCREMENT,
`chairid` int(11) NOT NULL,
`department_name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`image` varchar(128) NOT NULL DEFAULT 'default_department.png',
`url` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1'
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
But if I dump the table using PHPMyAdmin it does not add auto_increment which I mentioned before.
The output of the exported .sql file's content is here:
CREATE TABLE IF NOT EXISTS `departments` (
`departmentid` int(11) NOT NULL, -- AUTO_INCREMENT is missing
`chairid` int(11) NOT NULL,
`department_name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`image` varchar(128) NOT NULL DEFAULT 'default_department.png',
`url` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1'
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
I especially checked that if auto_increment is disabled in CREATE TABLE options but no, it is not.
This was fixed in phpMyAdmin version 4.5.0.1 (Sep 2015):
https://github.com/phpmyadmin/phpmyadmin/issues/11492
I supposed it ignores auto increment but I explore file and at the bottom of sql I found that it altered to auto increment somewhere else

ActiveRecord in Rails 3.0.3 turns the 8th field of MySQL into a BigDecimal. How to fix it?

I have the member table which has 9 fields: id,email,... so on.
member_type is the 8th field
The 8th field is always converted to decimal, no matter what name it is or what type it is.
Here is some experimenting I have done:
irb(main):010:0> Member.all()[0].attributes
=> {"created_date"=>nil, "email"=>"tanixxx#yahoo.com", "id"=>1, "is_admin"=>0, "
member_type"=>#<BigDecimal:4f87ce0,'0.0',4(8)>, "name"=>"tanin", "password"=>"3c
f622832f10a313cb74a59e6032f115", "profile_picture_path"=>"aaaaa", "status"=>"APP
ROVED"}
Please notice :member_type, which is the 8th field.
Now if I query only some fields, the result is correct:
irb(main):007:0> Member.all(:select=>"member_type,email")[0].attributes
=> {"email"=>"tanixxx#yahoo.com", "member_type"=>"GENERAL"}
I think there must be a bug in ActiveRecord.
Here is some more experiment. I have added "test_8th_field" to be the 8th field and I got this:
irb(main):016:0> Member.all[0].attributes
=> {"created_date"=>nil, "email"=>"tanixxx#yahoo.com", "id"=>1, "is_admin"=>0, "
member_type"=>"GENERAL", "name"=>"tanin", "password"=>"3cf622832f10a313cb74a59e6
032f115", "profile_picture_path"=>"aaaaa", "status"=>"APPROVED", "test_8th_field
"=>#<BigDecimal:30c87f0,'0.0',4(8)>}
The 8th field is a BigDecimal (it is a text field in MySQL, though). But the member_type field is amazingly correct this time.
I don't know what is wrong with the number 8...
Please help me.
Here is my schema dump, including test_8th_field:
CREATE TABLE IF NOT EXISTS `members` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`profile_picture_path` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`status` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`is_admin` int(11) NOT NULL,
`test_8th_field` text COLLATE utf8_unicode_ci NOT NULL,
`member_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'GENERAL',
`created_date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2 ;
I have solved it. It turns out that the MySql binary library does not match the version for the MySql database itself.