Add Values into Columns of a MySQL Table

Samual Sam
Updated on 20-Jun-2020 06:13:55

337 Views

INSERT command is used to add values to the columns of a MySQL table. We need to specify the values in INSERT command for all the columns as follows −SyntaxINSERT INTO table_name values(value1, value2, …)ExampleSuppose we have a table named ‘Stock’ with three columns ‘Item_id’, ‘Item_name’ and ‘Item_rate’ then with the help of following query we can add values in these columns.mysql> INSERT INTO Stock values(1, 'HistoryBook', 250); Query OK, 1 row affected (0.07 sec) mysql> INSERT INTO Stock values(2, 'DBMSBook', 280); Query OK, 1 row affected (0.10 sec) mysql> Select * from Stock; +---------+-------------+-----------+ | item_id | ... Read More

Find Current Quarter in MySQL

Sravani S
Updated on 20-Jun-2020 06:13:24

185 Views

We can do it by providing unit value ‘quarter’ to EXTARCT() function. Examples are as follows −mysql> Select EXTRACT(Quarter from '2017-07-29'); +------------------------------------+ | EXTRACT(Quarter from '2017-07-29') | +------------------------------------+ |                                  3 | +------------------------------------+ 1 row in set (0.00 sec)Above query will give the value of quarter from a particular given date.mysql> Select EXTRACT(Quarter from now()); +-----------------------------+ | EXTRACT(Quarter from now()) | +-----------------------------+ |                           4 | +-----------------------------+ 1 row in set (0.00 sec)Above query will give the value of quarter from the current date.

Automatically Define MySQL Table Structure from Another Table

Rishi Raj
Updated on 20-Jun-2020 06:07:11

234 Views

CREATE TABLE command with LIKE keyword will be able to define the structure of a MySQL table same as the structure of another table.SyntaxCREATE TABLE new_table LIKE old_table;Examplemysql> Create table employee(ID INT PRIMARY KEY NOT NULL AUTO_INCREMENT, NAME VARCHAR(20)); Query OK, 0 rows affected (0.21 sec) mysql> Describe employee; +-------+-------------+------+-----+---------+----------------+ | Field | Type        | Null | Key | Default | Extra          | +-------+-------------+------+-----+---------+----------------+ | ID    | int(11)     | NO   | PRI | NULL    | auto_increment | | NAME  | varchar(20) | YES  |     ... Read More

Clone Table with Data, Triggers, and Indexes

Sharon Christine
Updated on 20-Jun-2020 06:06:40

368 Views

For creating a new table just like old one along with its data, trigger, and indexes, we need to run following two queriesCREATE TABLE new_table LIKE old_table; INSERT new_table SELECT * from old_table;Examplemysql> Create table employee(ID INT PRIMARY KEY NOT NULL AUTO_INCREMENT, NAME VARCHAR(20)); Query OK, 0 rows affected (0.21 sec) mysql> Describe employee; +-------+-------------+------+-----+---------+----------------+ | Field | Type        | Null | Key | Default | Extra          | +-------+-------------+------+-----+---------+----------------+ | ID    | int(11)     | NO   | PRI | NULL    | auto_increment | | NAME  | varchar(20) | ... Read More

Obtain Part of a Date in MySQL

Priya Pallavi
Updated on 20-Jun-2020 06:06:06

454 Views

By using EXTRACT() function we can obtain the part from current date or from given date. The parts of the date can be obtained in the form of year, month, days, hours, minutes, seconds and microseconds.Examplesmysql> Select EXTRACT(Year from NOW()) AS YEAR; +-------+ | YEAR  | +-------+ |   2017| +-------+ 1 row in set (0.00 sec)Above MySQL query will obtain the year from current date.mysql> Select EXTRACT(Month from '2017-09-21')AS MONTH; +-------+ | MONTH | +-------+ |     9 | +-------+ 1 row in set (0.00 sec)Above MySQL query will obtain the month from given date.

Generate Days from Date Range in MySQL

Nikitha N
Updated on 20-Jun-2020 06:05:33

1K+ Views

It can be done with the help of following query which uses adddate() function and we are generating the days between ‘2016-12-15’ and ‘2016-12-31’ −mysql> select * from     -> (select adddate('1970-01-01', t4*10000 + t3*1000 + t2*100 + t1*10 + t0) gen_date from     -> (select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,     -> (select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union ... Read More

Change the Name of a MySQL Table

Jai Janardhan
Updated on 20-Jun-2020 06:04:15

183 Views

RENAME command is used to change the name of a MySQL table. Its syntax is as follows −RENAME table old_tablename to new_tablename2;ExampleIn the example below, we rename the table ‘testing’ to ‘test’.mysql> RENAME table testing to test; Query OK, 0 rows affected (0.17 sec) mysql> Select * from testing; ERROR 1146 (42S02): Table 'query.testing' doesn't exist mysql> Select * from test; +-----+---------+ | id1 | Name    | +-----+---------+ | 1   | Harshit | | 2   | Lovkesh | | 3   | MOHIT   | | 4   | MOHIT   | +-----+---------+ 4 rows in set (0.02 sec)

Different Time Format Characters in MySQL DATE_FORMAT Function

Srinivas Gorla
Updated on 20-Jun-2020 06:03:01

406 Views

Different time format characters used by MySQL DATE_FORMAT() function are as follows −Time Format CharacterMeaning %HIt is used to abbreviate Hour on a 24-hour clock in two digits format like 00, 01, 02 up to 23. %hIt is used to abbreviate Hour on 12-hour clock in two digits format like 01, 02 up to 12. %iIt is used to abbreviate minutes in two digits format like 01, 02 up to 59. %lIt is used to abbreviate Hour on 12-hour clock in two digits format without zero-like 01, 02 up to 12. %pIt is used for A.M or P.M.%rIt is used to display time on 12-hour ... Read More

Difference Between MySQL TRUNCATE and DELETE Command

Fendadis John
Updated on 20-Jun-2020 06:02:15

479 Views

As we know that TRUNCATE will remove all the rows without removing table’s structure from the database. Same work can be done with the help of DELETE command on removing all the rows from the table. But there is a significant difference of re-initialization of PRIMARY KEY AUTO_INCREMENT between both the commands.Suppose a column is defined AUTO_INCREMENT having PRIMARY KEY CONSTRAINT, then on deleting all the rows with DELETE command would not re-initialize the table i.e. on entering the new rows, the AUTO_INCREMENT number will start after the last inserted row. In contrast, on using TRUNCATE, the table will be ... Read More

MySQL DELETE Command Overview

Akshaya Akki
Updated on 20-Jun-2020 06:01:49

287 Views

MySQL DELETE command is used to delete the row/s from a table. It is used with WHERE clause.SyntaxDELETE From Table_name WHERE Condition;ExampleIn the example below, we have deleted the rows from table ‘employee’ where id >=100.mysql> select * from employee; +------+--------+ | Id   | name   | +------+--------+ | 100  | Ram    | | 200  | Gaurav | | 300  | MOHAN  | +------+--------+ 3 rows in set (0.00 sec) mysql> delete from employee where id >=100; Query OK, 3 rows affected (0.06 sec) mysql> select * from employee; Empty set (0.00 sec)

Advertisements