I can't find what's wrong with this SQL script - mysql

I have a big script that I'm trying to execute. It's located here: https://github.com/diamondo25/mcdb-files/blob/master/mcdb-4.3-global-83/mcdb-4.3-global-83.sql.gz
However, I'm receviing the following errors:
I really tried everything but failed. I know it's a big script but unfortunately I have no other place than StackOverflow to ask for help. BTW I'm using MariaDB and MySQL query browser to execute it.
Here's the queries before line 28:
/*Data for the table `block_chat_reason_data` */
insert into `block_chat_reason_data`(`id`,`reason`,`message`) values (1,'Foul Language','Foul language/Harrassment'),(2,'Advertising','Advertising websites'),(3,'Hack','Fake GM'),(4,'Account Trading','Trading or selling account'),(5,'Trading','Do not report fame scams and trade scams. '),(6,'Penalty Alert','The reporter\'s conversation is also subject to penalties.');
/*Table structure for table `block_reason_data` */
DROP TABLE IF EXISTS `block_reason_data`;
CREATE TABLE `block_reason_data` (
`id` int(11) NOT NULL,
`block_type` varchar(15) NOT NULL DEFAULT '',
`message` varchar(120) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
);

You are not escaping comma, try
insert into `block_chat_reason_data`(`id`,`reason`,`message`) values (1,'Foul Language','Foul language/Harrassment'),(2,'Advertising','Advertising websites'),(3,'Hack','Fake GM'),(4,'Account Trading','Trading or selling account'),(5,'Trading','Do not report fame scams and trade scams. '),(6,'Penalty Alert','The reporter\''s conversation is also subject to penalties.');

Last comma is the problem on line 5.
So use this:
CREATE TABLE `block_reason_data`(
`id` int(11) NOT NULL,
`block_type` varchar(15) NOT NULL DEFAULT '',
`message` varchar(120) NOT NULL DEFAULT ''
PRIMARY KEY (`id`)
);

Related

Use the value of an attribute from another table as a default value of an attribute from another table in MYSQL

I wanted to use the value on student_lastname in table tbl_student as a default value of sis_password in
table tbl_sis_account but I am out of idea on how to do it. I tried putting "Select query" after the "Default" but it doesn'nt work, anyway here's the sql:
DROP TABLE IF EXISTS tbl_sis_account;
CREATE TABLE `tbl_sis_account`(
sis_account_id INT(15) NOT NULL AUTO_INCREMENT,
sis_username INT(15) NOT NULL,
sis_password VARCHAR(8) DEFAULT '====>Value of attribute student_lastname<====',
PRIMARY KEY(`sis_account_id`),
CONSTRAINT `sis_username_student_fk` FOREIGN KEY (`sis_username`) REFERENCES `tbl_student`
(`student_id`) ON UPDATE CASCADE
)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
SELECT * FROM tbl_sis_account;
DROP TABLE IF EXISTS tbl_student;
CREATE TABLE `tbl_student` (
`student_id` INTEGER(15) NOT NULL AUTO_INCREMENT,
`student_firstname` VARCHAR(50) NOT NULL,
`student_midname` VARCHAR(50) NOT NULL,
`student_lastname` VARCHAR(50) NOT NULL,
PRIMARY KEY(`student_id`)
)ENGINE=INNODB AUTO_INCREMENT=20201 DEFAULT CHARSET=utf8mb4;
SELECT * FROM tbl_student;
No you can't do that, but you can query the tbl_student table at the time of insertion in the tbl_sis_account table to retrieve student_lastname from `student_id' via a nested sql query or a trigger.
I've figured the solution for this problem, for a while now. Forgot to post the answer though, coz I am no longer using this method. But here's what I did.
On the tbl_student I created a "After Insert trigger"
BEGIN
INSERT INTO tbl_sis_account (student_id,sis_password) values (new.student_id, concat(new.student,new.student_lastname));
END
so the inserted result on tbl_sis_account is
student_id | sis_password
20200001 | 202000001Doe

How to use CHAR_LENGTH() in CREATE TABLE query - MYSQL

My question may sound vague but am going to try as much as possible to make it clear enough. Prior to this, I have made some research on the internet and other SO pages but to no avail.
Is it possible to use CHAR_LENGTH() function in a CREATE TABLE clause like we usually do in SELECT Clause :
SELECT CHAR_LENGTH("SQL Tutorial") AS LengthOfString;
What I want to do is similar to this:
CREATE TABLE `tbl_content` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` varchar(32) NOT NULL,
`no_of_chars` int(4) NULL DEFAULT CHAR_LENGTH(`content`) ,
PRIMARY KEY (`id`)
)
But the above CREATE statement gives an error near CHAR_LENGTH.
Precisely, During insertion of a record into such a table, I want the server to be able to read the length of the content field and store in no_of_chars field as default value.
Is this POSSIBLE?
Yes, you could use generated column:
CREATE TABLE `tbl_content` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` varchar(32) NOT NULL,
`no_of_chars` int(4) AS ( CHAR_LENGTH(`content`)) ,
PRIMARY KEY (`id`)
);
DBFiddle Demo

SQL query Failed

please give me recommendation my query does not working
SQL query:
CREATE TABLE `amenities` (
`amenities_id` int(11) NOT NULL auto_increment,
`pic` varchar(100) NOT NULL,
`des` text NOT NULL,
PRIMARY KEY (`amenities_id`)
) TYPE=MariaDB AUTO_INCREMENT=13
MySQL said: Documentation
1064 - 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 'TYPE=MariaDB AUTO_INCREMENT=13' at line 6
There is no type table option, you possibly want to define the table engine
and there is no mariadb engine try
CREATE TABLE amenities ( amenities_id int(11) NOT NULL auto_increment,
pic varchar(100) NOT NULL, des text NOT NULL, PRIMARY KEY (amenities_id) )
AUTO_INCREMENT=13,
engine=innodb
Or leave out the engine option if you want to default the table to the database engine/
Hope this works.
CREATE TABLE amenities (
amenities_id int(11) NOT NULL auto_increment,
pic varchar(100) NOT NULL,
des text NOT NULL,
PRIMARY KEY (amenities_id)
) AUTO_INCREMENT=13
The TYPE keyword was replaced by ENGINE long ago.
The ENGINEs are InnoDB, MyISAM, MEMORY, ARIA and possibly others. Not MySQL, nor MariaDB.
The error message ... near 'TYPE ... points exactly at or after the offending syntax: TYPE in this case. (Not AUTO_INCREMENT, which was later)
AUTO_INCREMENT=13 is produced by SHOW CREATE TABLE for possible reloading. However, it is rarely useful otherwise. It is also mostly harmless.
DROP TABLE IF EXISTS `amenities`;
CREATE TABLE `amenities` (
`amenities_id` int(11) NOT NULL AUTO_INCREMENT,
`pic` varchar(100) NOT NULL,
`des` text,
PRIMARY KEY (`amenities_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Create new Table amenities where amenities_id is a PRIMARY KEY that will be auto increment. another table field pic is a varchar data type and des is a text data type that is used for rich text.

mysql drop statement with --

What is the difference between drop table and --drop table in mysql
For example: I'm getting error if I use -- but in all other places of Magento they are using -- before drop.
--DROP TABLE IF EXISTS {$this->getTable('faq/dinkchika')};
CREATE TABLE IF NOT EXISTS {$this->getTable('faq/dinkchika')} (
`faq_id` int(11) NOT NULL AUTO_INCREMENT,
`faq_question` varchar(255) DEFAULT NULL,
`faq_answer` varchar(255) DEFAULT NULL,
PRIMARY KEY (`faq_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
A line that starts with -- and has a space after that, is treated as a comment until the end of line. It won't be executed.
You can read more about Mysql comment syntax here: http://dev.mysql.com/doc/refman/5.1/en/comments.html
use a white space after -- ,if you are not using whitespace after -- then it will not
count as comment.after whitespace your query will look like this.
-- DROP TABLE IF EXISTS {$this->getTable('faq/dinkchika')};
CREATE TABLE IF NOT EXISTS {$this->getTable('faq/dinkchika')} (
`faq_id` int(11) NOT NULL AUTO_INCREMENT,
`faq_question` varchar(255) DEFAULT NULL,
`faq_answer` varchar(255) DEFAULT NULL,
PRIMARY KEY (`faq_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
Or you may use #(Hash) as well and
Try this: Drop table IF EXISTS table_name;
And then continue with creating the table, as it will be guaranteed to no longer exist.
I hope it will help for you..

Mysql - duplicate entry error for key with auto increment

Why do I get an error of the form:
Error in query: Duplicate entry '10' for key 1
...when doing an INSERT statement like:
INSERT INTO wp_abk_period (pricing_id, apartment_id) VALUES (13, 27)
...with 13 and 27 being valid id-s for existing pricing and apartment rows, and the table is defined as:
CREATE TABLE `wp_abk_period` (
`id` int(11) NOT NULL auto_increment,
`apartment_id` int(11) NOT NULL,
`pricing_id` int(11) NOT NULL,
`type` enum('available','booked','unavailable') collate utf8_unicode_ci default NULL,
`starts` datetime default NULL,
`ends` datetime default NULL,
`recur_type` enum('daily','weekly','monthly','yearly') collate utf8_unicode_ci default NULL,
`recur_every` char(3) collate utf8_unicode_ci default NULL,
`timedate_significance` char(4) collate utf8_unicode_ci default NULL,
`check_in_times` varchar(255) collate utf8_unicode_ci default NULL,
`check_out_times` varchar(255) collate utf8_unicode_ci default NULL,
PRIMARY KEY (`id`),
KEY `fk_period_apartment1_idx` (`apartment_id`),
KEY `fk_period_pricing1_idx` (`pricing_id`),
CONSTRAINT `fk_period_apartment1` FOREIGN KEY (`apartment_id`) REFERENCES `wp_abk_apartment` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_period_pricing1` FOREIGN KEY (`pricing_id`) REFERENCES `wp_abk_pricing` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Isn't key 1 id in this case and having it on auto_increment sufficient for being able to not specify it?
Note: If I just provide an unused value for id, like INSERT INTO wp_abk_period (id, pricing_id, apartment_id) VALUES (3333333, 13, 27) it works fine, but then again, it is set as auto_increment so I shouldn't need to do this!
Note 2: OK, this is a complete "twilight zone" moment: so after running the query above with the huge number for id, things started working normally, no more duplicate entry errors. Can someone explain me WTF was MySQL doing to produce this weird behavior?
It could be that your AUTO_INCREMENT value for the table and the actual values in id column have got out of whack.
This might help:
Step 1 - Get Max id from table
select max(id) from wp_abk_period
Step 2 - Align the AUTO_INCREMENT counter on table
ALTER TABLE wp_abk_period AUTO_INCREMENT = <value from step 1 + 100>;
Step 3 - Retry the insert
As for why the AUTO_INCREMENT has got out of whack I don't know. Added auto_increment after data was in the table? Altered the auto_increment value after data was inserted into the table?
Hope it helps.
I had the same problem and here is my solution :
My ID column had a bad parameter. It was Tinyint, and MySql want to write a 128th line.
Sometimes, your problem you think the bigger you have is only a tiny parameter...
Late to the party, but I just ran into this tonight - duplicate key '472817' and the provided answers didn't help.
On a whim I ran:
repair table wp_abk_period
which output
Number of rows changed from 472816 to 472817
Seems like mysql had the row count wrong, and the issue went away.
My environment:
mysql Ver 14.14 Distrib 5.1.73, for Win64 (unknown)
Create table syntax:
CREATE TABLE `env_events` (
`tableId` int(11) NOT NULL AUTO_INCREMENT,
`deviceId` varchar(50) DEFAULT NULL,
`timestamp` int(11) DEFAULT NULL,
`temperature` float DEFAULT NULL,
`humidity` float DEFAULT NULL,
`pressure` float DEFAULT NULL,
`motion` int(11) DEFAULT NULL,
PRIMARY KEY (`tableId`)
) ENGINE=MyISAM AUTO_INCREMENT=528521 DEFAULT CHARSET=latin1
You can check the current value of the auto_increment with the following command:
show table status
Then check the max value of the id and see if it looks right. If not change the auto_increment value of your table.
When debugging this problem check the table name case sensitivity (especially if you run MySql not on Windows).
E.g. if one script uses upper case to 'CREATE TABLE my_table' and another script tries to 'INSERT INTO MY_TABLE'. These 2 tables might have different contents and different file system locations which might lead to the described problem.