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..
Related
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
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
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`)
);
I have the following table:
CREATE TABLE IF NOT EXISTS PDF_STORAGE (
ID_PDF_STORAGE bigint(20) NOT NULL AUTO_INCREMENT,
DESC_FILE varchar(255) DEFAULT NULL,
PDF_FILE longblob,
LINK_FILE varchar(255) DEFAULT NULL,
VERSION int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (ID_PDF_STORAGE)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Where PDF_FILE is a file of 10 MB on average. Today it has about 50.000 rows. I need to add a new column to this table but it is taking a long time, more than 10 min, some times giving a 401 error in PhpMyAdmin, so I'd like to know what is the proper way to achieve this...
I already tried:
ALTER TABLE PDF_STORAGE ADD VERSION INT NOT NULL DEFAULT '0' AFTER LINK_FILE ;
and
SET FOREIGN_KEY_CHECKS=0;
CREATE TABLE PDF_STORAGE_new LIKE PDF_STORAGE;
ALTER TABLE PDF_STORAGE_new ADD VERSION INT NOT NULL DEFAULT '0' AFTER LINK_FILE ;
INSERT INTO PDF_STORAGE_new (PDF_STORAGE, DESC_FILE, ID_PDF_STORAGE, LINK_FILE) SELECT * FROM PDF_STORAGE;
RENAME TABLE PDF_STORAGE TO PDF_STORAGE_old, PDF_STORAGE_new TO PDF_STORAGE;
DROP TABLE PDF_STORAGE_old;
SET FOREIGN_KEY_CHECKS = 1;
but they are also slow.. is there a better way?
Thanks
What you are doing now ALTER TABLE is the best approach to my knowledge. You can try making this change when there is not much transaction (or) DB operation going on. I mean say, do the changes in idle time.
ALTER TABLE PDF_STORAGE ADD VERSION INT NOT NULL DEFAULT '0' AFTER LINK_FILE ;
You can as well create a new table same as this table schema along with the new column.
Insert all the records from this table to the newly created table.
Rename the new table to the old table name.
delete the old table.
Hi I am trying to create a trigger in Joomla component installation, However it seems doesn't work
error msg
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 '' at line 4 SQL=CREATE TRIGGER tg_jws_worksheet_worksheet_insert BEFORE INSERT ON jws_worksheet_worksheet FOR EACH ROW BEGIN INSERT INTO jws_worksheet_worksheet_seq (id) VALUES (NULL);
here is the code
--
-- Table structure for table `client`
--
DROP TABLE IF EXISTS `#__worksheet_client`;
CREATE TABLE IF NOT EXISTS `#__worksheet_client` (
`ClientID` int(11) NOT NULL,
`CompanyId` int(11) NOT NULL,
`FullName` text NOT NULL,
`Email` text,
`ContactNo` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `company`
--
DROP TABLE IF EXISTS `#__worksheet_company`;
CREATE TABLE IF NOT EXISTS `#__worksheet_company` (
`CompanyId` int(5) NOT NULL,
`ABN` int(12) NOT NULL,
`CompanyName` text NOT NULL,
`Street` text,
`City` text,
`State` text,
`PostCode` int(5) DEFAULT NULL,
`Country` text,
`Phone` text,
`HourRate` int(11) NOT NULL,
`Notes` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `worksheet_seq`
--
DROP TABLE IF EXISTS `#__worksheet_worksheet_seq`;
CREATE TABLE IF NOT EXISTS `#__worksheet_worksheet_seq` (
`id` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `worksheet`
--
DROP TABLE IF EXISTS `#__worksheet_worksheet`;
CREATE TABLE IF NOT EXISTS `#__worksheet_worksheet` (
`worksheetNo` varchar(9) NOT NULL,
`CompanyId` int(5) NOT NULL,
`UserId` int(5) NOT NULL,
`CompanyName` text NOT NULL,
`Category` varchar(20) NOT NULL,
`JobTitle` text NOT NULL,
`JobDesc` text NOT NULL,
`StartDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`EndDate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`TimeSpd` int(11) NOT NULL,
`Tech` text NOT NULL,
`Status` varchar(8) NOT NULL,
`Hardware` text,
`Price` int(11) NOT NULL,
`file` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
SET FOREIGN_KEY_CHECKS=1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `client`
--
ALTER TABLE `#__worksheet_client`
ADD PRIMARY KEY (`ClientID`), ADD KEY `CompanyId` (`CompanyId`);
--
-- Indexes for table `company`
--
ALTER TABLE `#__worksheet_company`
ADD PRIMARY KEY (`CompanyId`);
--
-- Indexes for table `worksheet`
--
ALTER TABLE `#__worksheet_worksheet`
ADD PRIMARY KEY (`worksheetNo`), ADD KEY `CompanyId` (`CompanyId`);
--
-- Indexes for table `worksheet_seq`
--
ALTER TABLE `#__worksheet_worksheet_seq`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `client`
--
ALTER TABLE `#__worksheet_client`
MODIFY `ClientID` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `company`
--
ALTER TABLE `#__worksheet_company`
MODIFY `CompanyId` int(5) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;
--
-- AUTO_INCREMENT for table `worksheet_seq`
--
ALTER TABLE `#__worksheet_worksheet_seq`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=4;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `client`
--
ALTER TABLE `#__worksheet_client`
ADD CONSTRAINT `client_ibfk_1` FOREIGN KEY (`CompanyId`) REFERENCES `#__worksheet_company` (`CompanyId`);
--
-- Constraints for table `worksheet`
--
ALTER TABLE `#__worksheet_worksheet`
ADD CONSTRAINT `worksheet_ibfk_1` FOREIGN KEY (`CompanyId`) REFERENCES `#__worksheet_company` (`CompanyId`);
--
-- Triggers `worksheet`
--
DROP TRIGGER IF EXISTS `tg_#__worksheet_worksheet_insert`;
CREATE TRIGGER `tg_#__worksheet_worksheet_insert` BEFORE INSERT ON `#__worksheet_worksheet`
FOR EACH ROW
BEGIN
INSERT INTO `#__worksheet_worksheet_seq` (`id`) VALUES (NULL);
SET NEW.worksheetNo = CONCAT('WS-', LPAD(LAST_INSERT_ID(), 5, '0'));
END
create trigger usually requires redefining the delimiter:
DELIMITER $$
DROP TRIGGER IF EXISTS `tg_#__worksheet_worksheet_insert`$$
CREATE TRIGGER `tg_#__worksheet_worksheet_insert` BEFORE INSERT ON `#__worksheet_worksheet`
FOR EACH ROW
BEGIN
INSERT INTO `#__worksheet_worksheet_seq` (`id`) VALUES (NULL);
SET NEW.worksheetNo = CONCAT('WS-', LPAD(LAST_INSERT_ID(), 5, '0'));
END$$
DELIMITER ;
I had the same issue with Joomla 2.5, unable to create a trigger during the install or update procedure while the sql statements work fine.
You will not be able to create a trigger, function, procedure or any sql statement containing more than 1 semi-colon (;). That's why all other statements seem to work in your script.
I tried many appoaches which did not work including these:
In the manifest file use sql-file:
<install>
<sql>
<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
</sql>
</install>
In the manifest file use install-queries:
<install>
<queries>
<query id="1">
CREATE TRIGGER `...` AFTER UPDATE ON `#__...` FOR EACH ROW
BEGIN
...codeline...;
...codeline...;
END
</query>
<query id="2">
...
</query>
</queries>
</install>
(sorry for using blockquotes in stead of code sample, otherwise post will not be shown correctly)
The only way it did work was via the installscript option available since Joomla 2.5, see https://docs.joomla.org/J2.5:Developing_a_MVC_Component/Adding_an_install-uninstall-update_script_file.
This did not work out-of-the-box. I had to remove the access check (the first line with define) and put the following code in the install($parent) and update($parent) functions to add the trigger:
$db = JFactory::getDbo();
$db->setQuery("DROP TRIGGER IF EXISTS `...`");
$db->query();
$query="CREATE TRIGGER `...` AFTER UPDATE ON `#__...` FOR EACH ROW
BEGIN
...codeline...;
...codeline...;
END";
$db->setQuery($query);
$db->query();
if ($error = $db->getErrorMsg()) {
$this->setError($error);
return false;
}
...next sqlstatement or code...
return true;
Make sure the classname in this installer php file matches your component name including init caps, something like:
class com_YourComponentNameInstallerScript
And also, don't forget to open the file with <?php tag but do not put the ?> end tag at the end.
Save the file as php file in the root of your install package at the same level as the manifest file, and add this filename to the manifest file at somewhere between the component description and administration or site tags:
<scriptfile>yourcomponentname_install.php</scriptfile>
This method will also work with creating Stored Procedures and Functions.