Wednesday, 1 October 2014

Delete records from table

By using DELETE query, you can delete records from a table.

Syntax
DELETE FROM table_name WHERE [condition];

mysql> select * from employee;
+----+-----------+-----------+--------+---------------------+
| id | firstName | lastName  | salary | mailId              |
+----+-----------+-----------+--------+---------------------+
|  1 | Madhav    | Ananda    | 100000 | krishna@krishna.com |
|  2 | Arjun     | Dhanunjay |  50000 | arjun@arjun.com     |
|  3 | jaideep   | Geera     |  25000 | ptr@ptr.com         |
+----+-----------+-----------+--------+---------------------+
3 rows in set (0.00 sec)


1. Delete record, where id is 2.
mysql> DELETE FROM employee where id=2;
Query OK, 1 row affected (0.07 sec)


mysql> select * from employee;
+----+-----------+----------+--------+---------------------+
| id | firstName | lastName | salary | mailId              |
+----+-----------+----------+--------+---------------------+
|  1 | Madhav    | Ananda   | 100000 | krishna@krishna.com |
|  3 | jaideep   | Geera    |  25000 | ptr@ptr.com         |
+----+-----------+----------+--------+---------------------+
2 rows in set (0.00 sec)


Prevoius                                                 Next                                                 Home

Update records in a table

UPDATE query is used to update records in a table.

Syntax
UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition];

id firstName lastName salary mailId
1 Krishna Ananda 100000 krishna@krishna.com
2 Arjun Dhanunjay 50000 arjun@arjun.com
3 Ptr Ptr 25000 ptr@ptr.com

1. Update the firstName as 'Madhav', where firstName is 'Krishna'.
mysql> UPDATE employee SET firstName="Madhav" WHERE firstName="Krishna";
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from employee;
+----+-----------+-----------+--------+---------------------+
| id | firstName | lastName  | salary | mailId              |
+----+-----------+-----------+--------+---------------------+
|  1 | Madhav    | Ananda    | 100000 | krishna@krishna.com |
|  2 | Arjun     | Dhanunjay |  50000 | arjun@arjun.com     |
|  3 | Ptr       | Ptr       |  25000 | ptr@ptr.com         |
+----+-----------+-----------+--------+---------------------+
3 rows in set (0.00 sec)

2. Update firstName as jaideep, lastName as Geera, where firstName is ptr.
mysql> UPDATE employee SET firstName="jaideep", lastName="Geera" WHERE firstName="Ptr";
Query OK, 1 row affected (0.09 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from employee;
+----+-----------+-----------+--------+---------------------+
| id | firstName | lastName  | salary | mailId              |
+----+-----------+-----------+--------+---------------------+
|  1 | Madhav    | Ananda    | 100000 | krishna@krishna.com |
|  2 | Arjun     | Dhanunjay |  50000 | arjun@arjun.com     |
|  3 | jaideep   | Geera     |  25000 | ptr@ptr.com         |
+----+-----------+-----------+--------+---------------------+
3 rows in set (0.00 sec)

Prevoius                                                 Next                                                 Home

WHERE clause

Using WHERE clause you can get the records based on a condition.

Syntax
SELECT column1, column2, … columnN FROM table_name WHERE [condition]

1. Select employee details, where name is krishna.
mysql> select * from employee WHERE firstName="Krishna";
+----+-----------+----------+--------+---------------------+
| id | firstName | lastName | salary | mailId              |
+----+-----------+----------+--------+---------------------+
|  1 | Krishna   | Ananda   | 100000 | krishna@krishna.com |
+----+-----------+----------+--------+---------------------+
1 row in set (0.07 sec)

2. Select employee details, where salary is greater than 10000.
mysql> select * from employee WHERE salary > 10000;
+----+-----------+-----------+--------+---------------------+
| id | firstName | lastName  | salary | mailId              |
+----+-----------+-----------+--------+---------------------+
|  1 | Krishna   | Ananda    | 100000 | krishna@krishna.com |
|  2 | Arjun     | Dhanunjay |  50000 | arjun@arjun.com     |
|  3 | Ptr       | Ptr       |  25000 | ptr@ptr.com         |
+----+-----------+-----------+--------+---------------------+
3 rows in set (0.00 sec)

3. Select employee details, where salary is 25000 and firstName is Ptr.
mysql> select * from employee WHERE (salary = 25000 AND firstName="Ptr");
+----+-----------+----------+--------+-------------+
| id | firstName | lastName | salary | mailId      |
+----+-----------+----------+--------+-------------+
|  3 | Ptr       | Ptr      |  25000 | ptr@ptr.com |
+----+-----------+----------+--------+-------------+
1 row in set (0.00 sec)

Prevoius                                                 Next                                                 Home

View data in table

Use SELECT statement, select and display the records from table.

Syntax
SELECT column1, column2, columnN FROM table_name;

1. To select ids from employee table
mysql> select id from employee;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+
3 rows in set (0.00 sec)

2. To select id, salary from employee table
mysql> select id,salary from employee;
+----+--------+
| id | salary |
+----+--------+
|  1 | 100000 |
|  2 |  50000 |
|  3 |  25000 |
+----+--------+
3 rows in set (0.00 sec)

3. To select all the records from employee table.
mysql> select * from employee;
+----+-----------+-----------+--------+---------------------+
| id | firstName | lastName  | salary | mailId              |
+----+-----------+-----------+--------+---------------------+
|  1 | Krishna   | Ananda    | 100000 | krishna@krishna.com |
|  2 | Arjun     | Dhanunjay |  50000 | arjun@arjun.com     |
|  3 | Ptr       | Ptr       |  25000 | ptr@ptr.com         |
+----+-----------+-----------+--------+---------------------+
3 rows in set (0.00 sec)



Prevoius                                                 Next                                                 Home

Insert data into table

Synatx
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
                 (OR)
INSERT INTO TABLE_NAME VALUES (value1, value2, value3,...valueN);

id firstName lastName salary mailId
1 Krishna Ananda 100000 krishna@krishna.com
2 Arjun Dhanunjay 50000 arjun@arjun.com
3 Ptr Ptr 25000 ptr@ptr.com

INSERT INTO employee values(1, "Krishna", "Ananda", 100000.00, "krishna@krishna.com");
INSERT INTO employee values(2, "Arjun", "Dhanunjay", 50000.00, "arjun@arjun.com");
INSERT INTO employee values(3, "Ptr", "Ptr", 25000.00, "ptr@ptr.com");

Above statements add the records to employee table.




Prevoius                                                 Next                                                 Home

Create table

Lets say I want to create an employee table with below fields
id : Integer
firstName : String
lastName : Stirng
salary : double
mailId :String

Before creating a table, create a database and tell MySQL use this database.

CREATE DATABASE sample;
USE sample;

Syntax
CREATE TABLE table_name(
   column1 datatype,
   column2 datatype,
   column3 datatype,
   .....
   columnN datatype,
   PRIMARY KEY( one or more columns )
);

Example
CREATE TABLE employee
(
 id int,
 firstName varchar(30),
 lastName varchar(30),
 salary decimal,
 mailId varchar(30),
 PRIMARY KEY(id)
);

Above statement creates employee table and makes id as a primary key for employee table.



Prevoius                                                 Next                                                 Home

Operators in SQL

An operator is a symbol which perform an operation. Below are the operators in SQL.

1. Arithmetic operators
Operator
Description
Example
+
Addition
20+30
-
Subtraction
20-30
*
Multiplication
20*30
/
Division
20/30
%
Modulus : Get the remainder
20%30

2. Comparison Operators
Used to compare one expression with another.
Operator
Description
Example
=
Checks for equality
SELECT name FROM emp WHERE sal = 1000;
!=, ^=, <>
Checks for inequality
SELECT name FROM emp WHERE sal != 1000;
>
Greater than test
SELECT name FROM emp WHERE sal > 1000;
<
Less than test
SELECT name FROM emp WHERE sal < 1000;
>=
Greater than or equal to test.
SELECT name FROM emp WHERE sal >= 1000;
<=
Less than or equal to test.
SELECT name FROM emp WHERE sal <= 1000;
IN
"Equivalent to any member of" test.
SELECT name FROM emp WHERE name IN ('Krishna', 'Arjun');
ANY/ SOME
Compares a value to each value in a list or returned by a query.
SELECT * FROM emp WHERE name = SOME ('Krishna','Arjun');
NOT IN
Equivalent to "!= ANY".
SELECT name FROM emp WHERE name NOT IN ('Krishna', 'Arjun');
ALL
Compares a value with every value in a list or returned by a query.
SELECT * FROM emp WHERE sal >= ALL (10000, 30000);
BETWEEN x and y
greater than or equal to x and less than or equal to y.
SELECT name FROM emp WHERE sal BETWEEN 5000 AND 25000;
EXISTS
TRUE if a sub-query returns at least one row.
SELECT * FROM emp WHERE EXISTS (SELECT name FROM EMP WHERE salary=50000);
LIKE
x LIKE y : Returns true, if x matches the pattern y.
SELECT * FROM emp WHERE name LIKE '%K%';
IS NULL
Tests for null
SELECT name FROM emp WHERE lastName IS NULL;
NOT LIKE
x NOT LIKE y : Returns true, if x doesn't matches the pattern y.
SELECT * FROM emp WHERE name NOT LIKE '%K%';
NOT NULL
Tests for not null
SELECT name FROM emp WHERE lastName IS NOT NULL;

3. Logical Operators
Operator
Description
Example
NOT
Returns TRUE if the following condition is FALSE. Returns FALSE if it is TRUE.
SELECT * FROM EMP WHERE NOT (lastName IS NULL);
AND
Returns TRUE if both component conditions are TRUE, else FALSE.
SELECT * FROM EMP WHERE name='Krishna' AND salary=50000
OR
Returns TRUE if either component condition is TRUE, else FALSE
SELECT * FROM EMP WHERE name='Krishna' OR salary=50000





Prevoius                                                 Next                                                 Home