I was trying to use this statement SELECT * INTO new_table FROM old_table but it's giving me the error of undeclared value. I wanted to create a back table from one existing table to another new table.
Do I have first to create another table? or am I missing something.
the link below shows that the same statement can be used, so I don't why it's not working for me
https://www.w3schools.com/sql/sql_select_into.asp
You should use the INSERT ... SELECT statement for MySQL instead.
INSERT INTO new_table
SELECT *
FROM old_table
But new_table must exist. If you have MySQL version >= 8.0.19, then you can use instead the following syntax:
INSERT INTO new_table TABLE old_table;
You may check 13.2.6.1 INSERT ... SELECT Statement.
Background information
The SELECT...INTO is not exactly standard, it has selective support by vendors.
The statement does exist in MySQL, but is used for "query result to be stored in variables or written to a file".
Related
I wanted to check whether a table exists before deleting the values inside the table. In SQL Server we can do as simple as so :
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'table_to_be_edited')
BEGIN
DELETE FROM table_to_be_edited;
END;
but how do we do it in MySQL ?
I am using MySQL Workbench V8.0.
When delete an option is to ignore the table not found error. This eliminates race conditions where a table is created between the test and the truncate. Always consider this when doing SQL operations.
I have been easily using
SELECT * INTO newtable IN 'another_database'
FROM original_table_in_separate_database;
to backup/copy data from one table to another table easily in MSSQL.
Now i am moving to MYSQL and cannot accomplish this task as this feature is not available in MYSQL.
Though CREATE TABLE ... SELECT can somehow accomplish the task in same database, but not with two different database.
Please help me if there is any idea :)
Thanks in advance .
You can use INSERT INTO .. SELECT FROM construct like
INSERT INTO db1.dbo.newtable
SELECT * FROM db2.dbo.original_table_in_separate_database;
Point to note: For INSERT INTO .. SELECT to work both the target table and inserting table must exist. Otherwise, use CREATE TABLE AS ... SELECT like
CREATE TABLE newtable
AS
SELECT * FROM db2.dbo.original_table_in_separate_database;
Is it necessary to define the new table definition before using SELECT INTO query in MYSQL.
I am getting problem to execute the query when I writ e like:
SELECT *
INTO newtable
FROM oldtable
WHERE 1=0;
the error showing is:
Undeclared variabie: newtable
if you have newtable
try :
INSERT INTO newtable SELECT ...
if you don't have newtable
try :
CREATE TABLE newtable AS SELECT ...
The MySQL manual search engine is terrible but googling for something like mysql 5.5 select into will normally take you to the right page:
MySQL Server doesn't support the SELECT ... INTO TABLE Sybase SQL
extension. Instead, MySQL Server supports the INSERT INTO ... SELECT
standard SQL syntax, which is basically the same thing.
If you read the documentation here it says:
With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables
So, yes, you need to create the new table first.
You can use CREATE TABLE new LIKE old to create a new, empty table, which is a copy of the original table structure.
I want to write a trigger.
The trigger works in the following manner :
When table R_published gets a new entry, based on a column value in the entry(R_published.whichPublishable) it needs to copy a row from either the project_task_goodread_master table or the project_document_master table into the R_publishedGoodReads OR R_publishedDocuments tables respectively.
I have written the following trigger and I'm getting the error : "#1327 - Undeclared variable: R_publishedGoodReads"
CREATE TRIGGER trigger_after_published
AFTER INSERT ON R_published
FOR EACH ROW
BEGIN
IF (NEW.whichPublishable=1) THEN
SELECT * INTO R_publishedGoodReads FROM project_task_goodread_master
WHERE
goodReadID= new.publishedItemId;
ELSEIF (NEW.whichPublishable=2) THEN
SELECT * INTO R_publishedDocuments FROM project_document_master where
documentID=new.publishedItemId;
END IF
END
Is there anything wrong with the syntax ? Do I need to declare the table name that I am using for insert ? Thanks.
MySQL doesn't support SELECT...INTO TABLE. See MySQL Documentation
Try instead:
IF (NEW.whichPublishable=1) THEN
INSERT INTO R_publishedGoodReads (col1, col2...)
SELECT col1, col2... FROM project_task_goodread_master
WHERE goodReadID= new.publishedItemId;
I want to confirm whether there is a certain table.
When create a table, there is an SQL sentence such as DROP TABLE IF EXISTS xxx_tb.
Will there be the method that can identify the existence of the table by SQL likewise?
Use INFORMATION_SCHEMA:
select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'MyTable';
Should be portable across most databases.
You want the SHOW TABLES command of MySQL:
SHOW TABLES LIKE 'xxx_tb';
Or indeed, you can just do a query like
SELECT COUNT(*) FROM tbl WHERE 1=0
Which will give an error (see documentation for exact error code, or try it) if the table doesn't exist, but succeed with no results if it does.