Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove every column in a table in MySQL?

Tags:

mysql

In MySQL, is there a way to drop every field in my table, short of using:

ALTER TABLE `table`
    DROP COLUMN d1,
    DROP COLUMN d1,
    etc....

Almost like TRUNCATE for fields maybe?

like image 293
Chris Laplante Avatar asked Oct 08 '10 22:10

Chris Laplante


People also ask

How do I drop all columns in SQL?

In Object Explorer, locate the table from which you want to delete columns, and expand to expose the column names. Right-click the column that you want to delete, and choose Delete. In Delete Object dialog box, click OK.

How do you remove a column from a database in MySQL?

Syntax. The syntax to drop a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name DROP COLUMN column_name; table_name.

How do I empty a table in MySQL?

To permanently remove a table, enter the following statement within the MySQL shell: DROP TABLE table1; Replace table1 with the name of the table you want to delete. The output confirms that the table has been removed.


2 Answers

You'll get an error when you try to drop the last column:

ERROR 1090 (42000): You can't delete all columns with ALTER TABLE; use DROP TABLE instead

Says it all! There's no way to have a table with zero columns.

like image 191
martin clayton Avatar answered Oct 31 '22 18:10

martin clayton


DROP TABLE table

like image 45
Svisstack Avatar answered Oct 31 '22 16:10

Svisstack