MySQL Drop table doesn't work for prefix - mysql

I am trying to drop tables with wp_ prefix but its giving error below
Error 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 'WHERE TABLE LIKE 'wp_%'' at line 1
Here is my query
"DROP TABLE WHERE TABLE LIKE '{$wp}%'"
What is wrong in this query? Please help

As far as I know, you can't selectively delete tables. You have to specifically delete each table, since deletion can't use a filter. You could probably use the metadata to get the names of all of your tables, and then find out in your code which ones start with wp_. Then, you would just loop through your list of tables to delete and then delete them with drop table [table-name];.
To get the list of table names from metadata, use select table_name from information_schema.tables;.

Related

Getting an MySQL syntax error and don't know why

Im trying to copy the data from the title and content columns in my articles table from a database into the wp_title and wp_content columns from the wp_posts column from a different database by using this command.
INSERT INTO wp_seetheuniverse.dbo.wp_posts ('wp_title', 'wp_content')
SELECT 'title', 'content' FROM seetheuniverse.dbo.articles;
and this is the error I am getting and do not know why.
ERROR 1064 (42000): 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 '.wp_posts ('wp_title', 'wp_content') SELECT
'title', 'content' FROM seetheuniver' at line 1
INSERT INTO wp_seetheuniverse.dbo.wp_posts ...
You're using a three-part qualified table name like Microsoft SQL Server.
In Microsoft SQL Server, there's a hierarchy of [database].[schema].[table].
MySQL has one fewer levels to the hierarchy. In MySQL, database and schema are the same thing. The terms are synonyms in MySQL.
I would guess that your database (aka schema) is wp_seetheuniverse. That looks like a wordpress database (schema). The dbo schema is a customary schema name in Microsoft SQL Server, not in MySQL.
To confirm, try this statement in the MySQL client:
SHOW DATABASES;
I expect you can get your code to work if you
INSERT INTO wp_seetheuniverse.wp_posts ...
And similarly change other table references so they are [database].[table].
Use below query:
INSERT INTO dbo.wp_posts (wp_title,wp_content)
SELECT title,content FROM dbo.articles;
Where dbo is database name and wp_posts and articles are tables.

update query inside view in mysql

mysql> create view incremented_salary as
-> update employee set salary=salary*1.1;
ERROR 1064 (42000): 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
'update employee set salary=salary*1.1' at line 2
can we put update query inside view? If yes then please tell me why I'm getting above error.I want to increment the salary and display it using view.
Why dont you
create view incremented_salary as
select salary*1.1 from wahtevertable
Views do not modify the values of tables.
They give you a new View on them - f.e. joining multiple tables, aggregating and maybe recalculate some values based on tablevalues.
If what you tried would work, everytime you`d viewed the data, it would increase in value.

How to rename table and change storage type in MySQL?

I first create a table which is set to store data in MyISAM engine. Later this table would be replaced with new table, but I still whant to keep this table and just rename it and change her engine type to "archive". I have try this:
ALTER TABLE myTable RENAME TO myTable-[DATE] ENGINE=archive;
Error which I get is: "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 'ENGINE=archive' at line 1 (1064) (SQLExecDirectW)")"
So how to in mysql rename and convert table from MyISAM to ARCHIVE engine?
Tnx for help
Kindly try this:
ALTER TABLE `test1` RENAME TO `myTable-[18-Aug-2015]`, ENGINE=archive;
Pass the new name as a string as well. Make sure to also include a comma ( , ) between the alterations that you require.

MySQL Truncate Table Before Insert

I am creating a table in MySQL for which I only ever want it to contain one tuple at a time. To enforce this, I am trying to create a trigger which will truncate the table each time an INSERT occurs. However, I am running into problems.
SQL:
CREATE TRIGGER `tbl_hire_truncate`
BEFORE INSERT ON `tbl_hire`
FOR EACH ROW
BEGIN
TRUNCATE TABLE `tbl_hire`;
END
Error:
#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 '' at line 5
Every example of a trigger that I've seen uses a FOR EACH loop, but it certainly doesn't make much sense with what I am trying to achieve.
How can I rewrite my SQL to achieve my goal?

How can I drop a table whose name is "logs/#sql-ib203" that appeared after a MySQL crash?

DROP TABLE logs/#sql-ib203 does not work due to /:
Error Code: 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 '/#sql-ib203' at line 1
The table logs/#sql-ib203 appeared after a database crash (not enough disk space while re-indexing and deleting some attributes in a table in the logs database). SHOW TABLES does not list the table logs/#sql-ib203, but when trying to ALTER the table that was being changed during the crash MySQL complains about the existence of the table logs/#sql-ib203:
ERROR 1050: Table 'logs/#sql-ib203' already exists
SQL Statement:
ALTER TABLE logs.srv_logs DROP COLUMN filenum , DROP COLUMN
agent , DROP COLUMN ip , DROP COLUMN event_source
I use MySQL 5.6.12-winx64 and InnoDB.
Try to execute:
DROP TABLE `logs/#sql-ib203`
Need to wrap the name with ``, that should drop it.
Regards.
You can do a dump of your database, the orphaned innodb temporary table is not referred in the dump file, then you can drop the database and restore again.
Also you can try ; drop table #mysql50##sql-ib203;
Reference : http://dev.mysql.com/doc/refman/5.6/en/identifier-mapping.html