MySql queries:

Inserting data into a existing table:

Syntax without column names specified:

INSERT INTO table_name VALUES ('value1','value2');

Example:

In the example we have inserted the data in a customer_table where we have 4 columns with name cust_id, first_name, last_name, age and we have inserted the data in the columns with this command.

Syntax without column names specified:

INSERT INTO table_name ('column1','column2') VALUES ('value1','value2');

Example:

In this example we have inserted the data in three columns excluding the last_name column, by doing this it will store the information in all the columns and will show NULL in the last_name.

Inserting data in multiple rows:

  • To ‘SELECT’ a statement in a table:
Select column_name from table_name;

Following command will show the data stored in the column_name column in the database.

  • To use ‘WHERE’ command in a table:
Select column_name from table_name Where (column_name = condition);

For example:

We have a table with Customers name , age and email given and we need to see only the data of customer whose age is greater than 25.

Select * from customer_table where age>25;

This command will show the data of the customers whose age is greater than 25.

  • To use logical operators in the query:
Select column_name from table_name Where (column_name = condition) AND (column_name = condition) OR (column_name = condition);
  • To update data in the table:
UPDATE table_name SET column_name='abc' WHERE condition(column);

For example to update the last name of a customer:

UPDATE customer_table SET Last_name='John' WHERE cust_id=5

Tagged : / / /