Truncate questions in SQL - sql-server-2008

How can I remove a row by using Truncate in SQL instead of delete by using WHERE condition?
Truncate only use to remove the table or row? if can remove the row by truncate, let me know anyone

You can't. Only DELETE statements can have a WHERE condition on them, TRUNCATE removes all rows.
From MSDN :
Removes all rows from a table or specified partitions of a table,
without logging the individual row deletions. TRUNCATE TABLE is
similar to the DELETE statement with no WHERE clause; however,
TRUNCATE TABLE is faster and uses fewer system and transaction log
resources.

You can only remove a single row with truncate if that row is the only one in the table.

Truncate is not like delete.
You can't use truncate to delete specific rows.
In fact, the statement is truncate table - you can't truncate anything other then a full table.
Truncate will remove all rows from the table, and is only allowed if the table is not referenced by foreign keys, is not used as the basis of an indexed view, and is not published by transactional replication or merge replication.
Also, truncate table can't be executed inside a transaction.
Truncate table will also reset the identity column of the table (if one exists).

Related

MySql - How to insert and on duplicate key update without explicitly specifying all non key columns

I have a table which was created as a select * from a view (and then added a PK).
I want to periodically update the table with all the data from the view.
I thought the best option is to do this using: INSERT INTO table_a SELECT * FROM view_a ON DUPLICATE KEY UPDATE VALUES(non_key_col_1), VALUES(non_key_col_1), .... ;
Since there are quite a lot of columns, and they might change in the future (then I can re-create the table, but I wish I won't have to edit the periodic insert, I was wondering if there is a way to avoid the explicit specification of all columns?
There no such syntax in mysql unfortunately. You'll have to update all the columns one by one.
You can go with a trigger on insert operation, that is if the primary key exists update the row otherwise insert it. But definitely it is going to impact the performance in case of large data
One thing i can think of is get the column names from INFORMATION_SCHEMA.COLUMNS and use those to dynamically compose your query in your app.
SELECT * FROM information_schema.columns WHERE table_name = 'view_a';
Now you have the columns no matter if the view changes.
Do the same for the table and you have the column differences.
Use those differences to run ALTER TABLE statements or drop it and recreate it all together.
Of course this is probably even more laborious then dropping and recreating the table manually.

MySQL: Does `DROP TABLE` completely remove the table or just the structure?

I am under the impression that the MySQL command DROP TABLE User will just remove the data, columns, and relevant constraints. However, the table shell still exists. Is this correct?
Using DROP TABLE will remove the entire table and all records contained inside it. If you want to retain the table structure but remove all data, then consider using TRUNCATE TABLE. Truncating a table is implemented by dropping the entire table and then recreating it. This is faster than doing DELETE FROM yourTable, which removes records one-by-one.
After Drop Table, the table will not exist anymore, so no data and no table definition(which you called 'table shell'); TRUNCATE TABLE keep your table definition and delete all the data ,and reset table auto-increment as well, but be careful about TRUNCATE if the table size is huge, it will expand your tablespace and not easy to shrink.
As mysql manual on drop table says:
Be careful with this statement! It removes the table definition and all table data.
So, no shell (whatever that should mean) remains after dropping a table.
What do you mean by table shell?
From MqSQL dev site:
DROP TABLE removes one or more tables. You must have the DROP privilege for each table.
Be careful with this statement! It removes the table definition and all table data. For a partitioned table, it permanently removes the table definition, all its partitions, and all data stored in those partitions. It also removes partition definitions associated with the dropped table.

Resetting data in MySql database

I am faced with a scenario where I would like to remove all existing data from one of my tables and then reuse the table again and fill it with new data.
My Question: Should I destroy/recreate the exact same table to do this? Or is there an easier way? -Side Note: I am storing user id's and would like for the id's to be set back again as opposed to continuing at the number in which last data was stored.
I'm using PhpMyAdmin.
If you use TRUNCATE TABLE.
TRUNCATE TABLE empties a table completely. Logically, this is equivalent to a DELETE statement that deletes all rows, but there are practical differences under some circumstances.
...
it resets any AUTO_INCREMENT counter to zero
So if you want to keep the counter use DELETE FROM
If you delete all rows in the table with DELETE FROM tbl_name (without a WHERE clause) in autocommit mode, the sequence starts over for all storage engines except InnoDB and MyISAM.
You can also look here: How to prevent mySQL from resetting auto increment value?
You have to truncate your table, see documentation here: https://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
In phpmyadmin there's a button in the table list called: Empty

DELETE using TRUNCATE command and Delete VS. Truncate

Delete a single row of a table in MYSQL using truncate command difference between truncate and delete we can delete a single row but how to delete a single row using truncate?
You can't truncate a single row. Read up on the mysql reference:
TRUNCATE [TABLE] tbl_name TRUNCATE TABLE empties a table completely.
It requires the DROP privilege as of MySQL 5.1.16. (Before 5.1.16, it
requires the DELETE privilege).
Logically, TRUNCATE TABLE is equivalent to a DELETE statement that
deletes all rows, but there are practical differences under some
circumstances.
Reference:
13.1.34 TRUNCATE TABLE Syntax
Just for your knowledge DELETE is a DML Command. and TRUNCATE is a DDL command
No truncate can't be used for one single row. It has to be used for the entire table.
proper working/practical differences are :
With Delete command structure of the table is not purged from database.
you can delete all the data of the database but not the format of the table. that remains stable.
Delete is a DML command.
DELETE handles rows chosen with a WHERE statement. Its use is part of the discipline of running production applications. For example, you might DELETE all rows that have been marked "complete" more than a month ago.
Truncate removes/purge the structure of the table itself. It also purges sets the auto_increment to its initial value (that is zero by default).
Truncate is a DDL command
TRUNCATE rapidly removes all rows from a table while maintaining the table definition. (Row by row DELETE can take time, especially for tables with lots of keys.)
It comes in handy for such things as log tables that start out empty for each week's production.
It has the convenient side effect of resetting the indexes and releasing unused disk storage. I have used TRUNCATE to wipe out the contents of a log table that used to contain millions of rows, to switch to an operational discipline where it only contains a weeks' worth of rows, for example.
TRUNCATE removes all the rows from the Table.
DELETE statement deletes table rows and returns number of rows deleted.we can delete selected no of records from table using DELETE command.
TRUNCATE drops the table and re-create it. It is much faster than deleting rows one by one.
Once u use TRUNCATE command then u cannot retrieve the data again from the table.TRUNCATE removes all the rows from the Table.
Drop - deletes the data as well as structure.
The difference between DROP and DELETE table is that, after executing DELETE statement the contents of table are removed but the structure remains same, but in case of DROP statement both the contents and structure are removed.
When you want to delete a single row you use Delete command. When you want to clear the entire table data you can use Truncate Table command.

Alter table with multiple modify column VS Alter table query with single column

I have huge table with 1 million records, i would like to modify some of the text columns to varchar column. Which is the best way to modify the columns.
Alter table query with all modify columns
Alter table queries with single modify column.
Definitely variant 1, because, as stated in the MySQL documentation,
In most cases, ALTER TABLE makes a temporary copy of the original table. MySQL waits for other operations that are modifying the table, then proceeds. It incorporates the alteration into the copy, deletes the original table, and renames the new one.
So, it's better to have a single copy/wait for operations/delete old table/rename new one operation than multiple ones.