Header menu

_________________________________________________________________________________
Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

Thursday, 6 February 2014

Explain difference between delete, truncate and drop command in Mysql.

 DELETE

DELETE command in mysql is used to remove rows from a table. We can use where clause  to only remove some rows. If we do not  specify any where clause , all rows will be removed.DELETE operations can be rolled back.
 Syntax : DELETE FROM table_name WHERE column_name=value;

TRUNCATE

TRUNCATE command in mysql removes all rows from the table. The operation cannot be rolled back and no triggers will be fired.  TRUNCATE is faster than DELETE.
Syntax:  TRUNCATE TABLE table_name;

DROP

The DROP command in  mysql removes the table from the database. All the table rows, indexes and privileges will  be removed. No triggers will be fired and the operation cannot be rolled back.
Syntax:  Drop TABLE table_name;

Point to note :
 DELETE operations can be rolled back on the other hand  DROP and TRUNCATE operations cannot be rolled back.

Monday, 3 February 2014

Inner join between more than two tables


Inner Join between more than two tables

To explain this lets take an example.
We have three table table1,table2,table3 .
table1 has primary key pk on which we want to take join of three table.

Query :  

SELECT * FROM table1
INNER JOIN table2 ON table1.pk=table2.table1Id
INNER JOIN table3 ON table1.pk=table3.table1Id

Friday, 17 January 2014

Difference between various types of join in SQL


Assuming you're joining on columns with no duplicates, this the most common case:
  • An inner join of table One and Two gives the result of One intersect Two, i.e. the inner part of a venn diagram intersection.
  • An outer join of One and Two gives the results of One union Two, i.e. the outer parts of a venn diagram union.

Examples
Suppose you have two Tables, with a single column each, and data as follows:
One     Two
-     -
1     3
2     4
3     5
4     6

Note that (1,2) are unique to One, (3,4) are common, and (5,6) are unique to Two.


Inner join
An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common.
select * from One INNER JOIN Two on One.One = Two.Two;


One  | Two

3  | 3
4  | 4

Thursday, 12 September 2013

Write sql queries in cakephp controller

How to write sql queries in cakephp controller 

public function index(){
    $result= $this->User->query("SELECT * FROM users ;");  
}

User :- It is the name of my Model
users:- Table name in database

$result:- contain array of result of sql query