MySQL: Remove All Columns from a Table Using ALTER TABLE

Lakshmi Srinivas
Updated on 29-Jan-2020 05:37:18

250 Views

Eventually, we cannot remove all the columns from a table by using ALTER TABLE command with DROP keyword. In this case, MySQL will return an error message. It is demonstrated with the help of the following exampleSuppose in table ‘Employee’ we have two columns ‘name’ and ‘id’, now if we will use ALTER TABLE to remove both the columns then MySQL returns an error as followsmysql> ALTER TABLE employee drop column name, drop column id; ERROR 1090 (42000): You can't delete all columns with ALTER TABLE; use DROP TABLE instead

Get More Details About Columns in MySQL Beyond SHOW COLUMNS

Ankith Reddy
Updated on 29-Jan-2020 05:29:08

185 Views

If we want to get more details about the column of an existing table then we need to use SHOW FULL COLUMNS statement. Consider the example below in which SHOW FULL COLUMNS statement has been applied on ‘Employee’ table and MySQL returns result set with some extra details like Collation, Privileges, and Comment, about the columns of the table −mysql> SHOW FULL COLUMNS FROM EMPLOYEE\G *************************** 1. row ***************************      Field: Id       Type: int(11)  Collation: NULL       Null: YES        Key:    Default: NULL      Extra: Privileges: select, insert, update, references ... Read More

Why MySQL Uses INTERVAL Like 7 DAY And 2 HOUR

Abhinaya
Updated on 29-Jan-2020 05:23:20

149 Views

The reason behind this concept is that MySQL requires the unit keywords to be singular, regardless of the English grammar rules. If we will try to supply intervals like 7 days, 2 hours etc then MySQL will produce syntax error as follows −mysql> Select '2017-02-25 05:04:30' + INTERVAL 2 days; 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 'days' at line 1

Insert Date and Time Automatically in MySQL

Krantik Chavan
Updated on 29-Jan-2020 05:20:45

952 Views

In MySQL, we can insert current date and time automatically to a column on inserting the NULL values in other columns by declaring that column as DEFAULT CURRENT_TIMESTAMP. In this case, we cannot declare the column NOT NULL in which we want to insert NULL values.mysql> Create Table Testing1(Name Varchar(20), RegStudent TIMESTAMP DEFAULT CURRENT_TIMESTAMP); Query OK, 0 rows affected (0.15 sec)Above query will create a table ‘Testing1’ with a column named ‘Name’(not declared ‘NOT NULL’) and other column named ‘RegDate’ declared as DEFAULT CURRENT_TIMESTAMP. Now, on inserting the NULL values Name column, the current date and time will be inserted ... Read More

Get First Day of Previous Month in MySQL

Sreemaha
Updated on 29-Jan-2020 05:19:15

3K+ Views

With the help of following MySQL query, we can get the first day of previous month −mysql> SELECT DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH,'%Y-%m-01') AS 'FIRST DAY OF PREVOIUS MONTH'; +-----------------------------+ | FIRST DAY OF PREVOIUS MONTH | +-----------------------------+ | 2017-09-01                  | +-----------------------------+ 1 row in set (0.00 sec)

Get First Day of Next Month in MySQL

V Jyothi
Updated on 29-Jan-2020 05:18:35

1K+ Views

With the help of following MySQL query, we can get the first day of next month −mysql> SELECT DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH,'%Y-%m-01') AS 'FIRST DAY OF NEXT MONTH'; +-------------------------+ | FIRST DAY OF NEXT MONTH | +-------------------------+ | 2017-11-01              | +-------------------------+ 1 row in set (0.00 sec)

Get Last Day of Current Month in MySQL

Sravani S
Updated on 29-Jan-2020 05:17:54

485 Views

With the help of following MySQL query, we can get the last day of the current month −mysql> SELECT LAST_DAY(now()) AS 'LAST DAY OF CURRENT MONTH'; +---------------------------+ | LAST DAY OF CURRENT MONTH | +---------------------------+ | 2017-10-31                | +---------------------------+ 1 row in set (0.00 sec)

Create New MySQL Table by Selecting Specific Columns from Another Table

karthikeya Boyini
Updated on 29-Jan-2020 05:16:32

278 Views

As we know that we can copy the data and structure from an existing table by CTAS script. If we want to select some specific column/s from another table then we need to mention them after SELECT. Consider the following example in which we have created a table named EMP_BACKUP1 by selecting a specific column ‘name’ from already existing table ‘Employee’ −mysql> Create table EMP_BACKUP1 AS Select name from employee; Query OK, 3 rows affected (0.25 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> Select * from EMP_BACKUP1; +--------+ | name   | +--------+ | Ram    | | ... Read More

Copy Data with Conditions from Existing MySQL Table

Sai Nath
Updated on 29-Jan-2020 05:15:52

288 Views

As we know that we can copy the data and structure from an existing table by CTAS script. If we want to copy data with some condition/s then we need to use WHERE clause with CTAS script. Consider the example below −mysql> Create table EMP_BACKUP2 AS SELECT * from EMPLOYEE WHERE id = 300 AND Name = 'Mohan'; Query OK, 1 row affected (0.14 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> Select * from EMP_BACKUP2; +------+-------+ | Id   | Name  | +------+-------+ | 300  | Mohan | +------+-------+ 1 row in set (0.00 sec)In the example ... Read More

Get List of Columns in an Existing MySQL Table

Arjun Thakur
Updated on 29-Jan-2020 05:14:17

337 Views

Suppose if we forgot the names of the columns in an existing table then we can use SHOW COLUMNS statement as follows to get the list of columns −mysql> SHOW COLUMNS from Employee\G *************************** 1. row ***************************   Field: Id    Type: int(11)    Null: YES     Key: Default: NULL   Extra: *************************** 2. row ***************************   Field: Name    Type: varchar(20)    Null: YES     Key: Default: NULL   Extra: 2 rows in set (0.07 sec)In the example above, we got the list of columns of ‘Employee’ table with the help of SHOW COLUMNS statement.

Advertisements