Can not create table in phpmyadmin - mysql

I have removed users table in my database and imported another users table.
It show this error:
Error
SQL query:
CREATE TABLE `users` (
`id` int(11) UNSIGNED NOT NULL,
`user_group_id` int(11) UNSIGNED NOT NULL,
`gender` tinyint(1) DEFAULT NULL,
`first_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`last_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`national_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`birthday` date DEFAULT NULL,
`company` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`eco_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`national_id` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`address` text COLLATE utf8mb4_unicode_ci,
`post_code` varchar(255) COLLATE utf8mb4_unicode_ci[...]
MySQL said: Documentation
#1215 - Cannot add foreign key constraint
My framwork is Laravel 5.6, PHP 7.2
How to solve this issue?

When you are working with migrations, you have to make every change in database through migrations. You can make changes in the existing users_table migration file. You can drop/add column/table using artisan command. Migrations in Laravel can do every interaction with the database. Take your time to go thorugh all the basics of migrations if you don't know:
Laravel Migrations

Use migration in laravel. It is better that way and check the migration field in your table. May be the previous migrations will still be there

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

Zero timestamp value not accepted when using MySQL .NET connector

We have a C# tool to create a (MySQL) schema on a developer machine based on the (MySQL) schema used in production. This tool runs SHOW CREATE TABLE queries on the source connection, grab the resulting table creation scripts and run them locally.
I'm using MySQL 5.7.7 Community Server locally.
One of the creation script looks like this:
CREATE TABLE `agency` (
`id` int(11) unsigned NOT NULL,
`name` varchar(512) COLLATE utf8_unicode_ci NOT NULL,
`url` varchar(512) CHARACTER SET ascii NOT NULL,
`location_path` varchar(10) CHARACTER SET ascii NOT NULL,
`email_pattern` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`generic_email` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`address` text COLLATE utf8_unicode_ci,
`zip_code` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`city` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`region` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`country` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`phone` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`inserted_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
When I run this query in MySQL Workbench, the table is successfully created.
However, when this query is executed on the destination connection by our C# tool, the following error occurs: Failure: Invalid default value for 'inserted_at'.
My MySQL Workbench is configured with the following SQL_MODE: TRADITIONAL,ALLOW_INVALID_DATES.
My MySQL Server instance is configured with the following sql-mode: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Here are the connection strings used by our C# tool:
Local connection string:
Server=localhost;Database=XXX;Uid=XXX;Pwd=XXX;AllowZeroDateTime=true
Source connection string:
Server=X.X.X.X;Database=XXX;Uid=XXX;Pwd=XXX;Use Compression=true;AllowZeroDateTime=true
Any idea appreciated!
It looks like removing STRICT_TRANS_TABLES from sql-mode in my.ini fixes the problem.
Is this the correct fix?

Duplicate entry for key 'PRIMARY'

I'm trying to import a csv (that is a data extract from a SQL Server db) into MySQL.
I'm using this command to load the file:
LOAD DATA INFILE '/Users/Tyler/Desktop/playersToTeams.txt' INTO TABLE players_to_teams FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';
And I get this error:
Error Code: 1062. Duplicate entry '124547' for key 'PRIMARY'
When I run: grep 124547 playersToTeams.txt:
119683,True,True,124547,1493,2011-08-31 02:22:56.630000000,,,,,http://bucknellbison.cstv.com///sports/m-wrestl/mtt/stolfi_joe00.html,,,,,,
124547,True,True,129411,14726,2011-08-31 02:22:56.630000000,Free/Breast,,,,http://usctrojans.collegesports.com/sports/m-swim/mtt/walling_emmett00.html,,,,,,
I can see that the 4th column of the first entry has the same number as the first column (pk, id), but the 4th column doesn't have any sort of index on it.
Here's the create schema created by sql workbench:
CREATE TABLE `players_to_teams` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`IsActive` tinyint(1) DEFAULT NULL,
`IsVisible` tinyint(1) DEFAULT NULL,
`PlayerId` int(11) DEFAULT NULL,
`TeamId` int(11) DEFAULT NULL,
`CreationDate` datetime DEFAULT NULL,
`Position` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Number` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`Club` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`BT` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`BioLink` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`OtherBioLink` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`StartYear` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`EndYear` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`NeulionPlayerID` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`SeasonYear` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`GamesPlayed` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=124549 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci$$
If I rerun the LOAD DATA command, without changing anything, I get a different error, this time the number is 2 more than the previous time (124549 vs. 124547). Running it again skips to 124552.
Any ideas what is causing this duplicate error?
Thanks
The file was ill-formatted. Props to #Vapire for thinking of that.

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.