Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
To rename any table in SQL we use following command:
RENAME OldTableName TO NewTableName
How does this work internally?
My assumption is it probably first creates a table with NewTableName using AS SELECT, then deletes the old table with DROP, just like we did when renaming files when handling files in C. Am I right, or does it work differently?
It works differently. The data isn't copied. The name of the table is simply updated in the metadata tables -- along with references to things like triggers, and so on.
You may get some help from here:-
The rename operation is done atomically, which means that no other
session can access any of the tables while the rename is running. For
example, if you have an existing table old_table, you can create
another table new_table that has the same structure but is empty, and
then replace the existing table with the empty one as follows
(assuming that backup_table does not already exist).
...............
When you execute RENAME, you cannot have any locked tables or active
transactions. You must also have the ALTER and DROP privileges on the
original table, and the CREATE and INSERT privileges on the new table.
If MySQL encounters any errors in a multiple-table rename, it does a
reverse rename for all renamed tables to return everything to its
original state.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 days ago.
Improve this question
i saved a table in my SSMS database but its not showing even after i refresh it. any idea what should be done??
Try reconnect and asure yourself that you are creating table rather than temporary table which disapear after closing or interupting session.
When reconnect will help. It means that table was created but only view of it wasn't available yet.
Data from temporary table begin name with # sign. To check if you using temporary table you can perform code shown below:
if object_id('tempdb..#yourtablename') is null
PRINT N'Temporary table is not created';
if object_id('tempdb..#yourtablename') is not null
PRINT N'Temporary table is created';
if object_id('dbo.yourtablename') is null
PRINT N'Normal table is not created';
if object_id('dbo.yourtablename') is not null
PRINT N'Normal table is created';
Make sure that you are in the database where you previously created the table
A couple of basics you can try for this:
Try running:
SELECT * FROM [<YOUR_DATABASE>..<YOUR_TABLE>];
If an exception is returned, the table did not create.
In the left sidebar, expand:
Databases > your db > Tables
Right-click tables and press 'refresh'. See if it appears in the list.
If not, the table did not create.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have single use keys stored in a table column. I have a table which contains only unique keys which, once read and sent, must be deleted from table using an atomic operation.
How can I accomplish this?
I need to use Node.js in asynchronous way; it is cumbersome to run two queries(select, and delete) for each request using transactions.
I wish that Delete returned the key as well... that'd have been easiest.
MySQL version: 5.5.54
Write a Stored Procedure to do the SELECT .. FOR UPDATE and DELETE in a single transaction and returning the SELECT results. Then perform a single CALL from your client.
This is a possible duplicate of MySQL update output deleted which has no answer either.
The thing is that MySQL has not implemented such syntax yet. It is available in other DBs, but in MySQL you are most likely stuck with SELECT .. FOR UPDATE; DELETE ... inside of a transaction. I am afraid that in MySQL you might not have any better option yet.
Some other links:
In MariaDB 10.0.5 it is possible to return values from
DELETE
OUTPUT
Clause in MySQL
In
mysql can I return deleted rows after a
deletion?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Does the order matter, when calling DROP TABLE IF EXISTS <some table> at the top of a SQL script?
If you are concerning about foreign keys, then you should first drop child tables. You can not drop a table if it is referenced in another table. But it's easier to temporarily disable key checks.
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE ...;
...
DROP TABLE ...;
SET FOREIGN_KEY_CHECKS = 1;
Then you won't need to care about the order of table drops.
Note that when you enable the key checks, the schema should be "clean" - There should not be any foreign key reference to a not existing table. Otherwise SET FOREIGN_KEY_CHECKS = 1 will fail.
Yes, if you call DROP TABLE IF EXISTS.... after script it will delete already created new table, what most probably you don't want to.
If you call DROP TABLE IF EXISTS.... before, you make sure that your script won't fail, if the table already exists
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How can we create a table though if exists in MySql.
I want to create a table if exists then drop it, and create it.
In other words, i want to do these two queries atomically.
drop table if exists `tbl_Name`;
create table `tbl_Name` (c1 int, c2 text);
**we can not sure that the sequence of those two queries will up to down.
thanks
You can use MySQL DROP TABLE IF EXISTS:
DROP TABLE IF EXISTS tbl_Name;
CREATE TABLE tbl_Name (
c1 INT,
c2 TEXT
);
IF OBJECT_ID('[dbo].[YourTable]') IS NOT NULL
DROP TABLE [dbo].[YourTable]
In MySQL all data definition statements (such as create or drop table) are run in their own, specific transaction:
The intent is to handle each such statement in its own special transaction because it cannot be rolled back anyway.
Your issue is not that these statements are not executed in an atomic way, your problem is that a create table statement may be executed before the corresponding drop table is executed because of parallel execution of instructions. In this case you either
Need to enforce serialization of drop table - create table command pairs
Or you use create table if not exists tablename version of the create table command:
The keywords IF NOT EXISTS prevent an error from occurring if the table exists. However, there is no verification that the existing table has a structure identical to that indicated by the CREATE TABLE statement.
However, even if you used the latter one, you still have to make sure, that there is no subsequent drop table statement that would remove your nicely created table. Pls also note that if not exists purely checks the table name, but does not check the table structure!
So, to be honest, I would go with option 1: you must serialise the execution of DDL statements.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
How to truncate a view table from a stored procedure in MySql.First tried executing the following query which failed what can be the error "TRUNCATE TABLE tester ". Tester is a view table which I created from create view in mysql. I want to truncate the view table and enter new rows via a stored procedure.
TRUNCATE TABLE does not work for views.
You can DELETE FROM tester and if the view is an updatable view it will apply the DELETE to the underlying table. But DELETE is slower than TRUNCATE, because DELETE has to do some bookkeeping to make sure it can be rolled back, whereas TRUNCATE cannot be rolled back.
You can instead do TRUNCATE TABLE table_tester_is_based_on, but as #fancyPants mentions, be sure this is what you want to do, because it will dispose of all the data in the base table. MySQL views don't store a copy of the base table, they are more like an alias for a query against the base table.