- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL ON vs USING?
In general, we use ON in MySQL. In Joins, we use ON in a set of columns. USING is useful when both the tables share a column of the exact same name on which they join.
Example of On.
Creating our first table.
mysql> CREATE table ForeignTableDemo -> ( -> Id int, -> Name varchar(100), - > FK int - > ); Query OK, 0 rows affected (0.47 sec)
Creating our second table.
mysql> CREATE table PrimaryTableDemo - > ( - > FK int, - > Address varchar(100), - > primary key(FK) - > ); Query OK, 0 rows affected (0.47 sec)
Let us now add constraints.
mysql> ALTER table ForeignTableDemo add constraint FKConst foreign key(FK) references PrimaryTableDemo(FK); Query OK, 0 rows affected (1.54 sec) Records: 0 Duplicates: 0 Warnings: 0
To add records into the second table.
mysql> INSERT into PrimaryTableDemo values(1,'US'); Query OK, 1 row affected (0.10 sec) mysql> INSERT into PrimaryTableDemo values(2,'UK'); Query OK, 1 row affected (0.14 sec) mysql> INSERT into PrimaryTableDemo values(3,'Unknown'); Query OK, 1 row affected (0.08 sec)
Displaying all the records.
mysql> SELECT * from PrimaryTableDemo;
The following is the output.
+----+---------+ | FK | Address | +----+---------+ | 1 | US | | 2 | UK | | 3 | Unknown | +----+---------+ 3 rows in set (0.00 sec)
Now, to add records into the first table.
mysql> INSERT into ForeignTableDemo values (1,'John',1); Query OK, 1 row affected (0.20 sec) mysql> INSERT into ForeignTableDemo values (2,'Bob',2); Query OK, 1 row affected (0.27 sec)
Let us now display all the records from the first table.
mysql> SELECT * from ForeignTableDemo;
Here is the output.
+------+------+------+ | Id | Name | FK | +------+------+------+ | 1 | John | 1 | | 2 | Bob | 2 | +------+------+------+ 2 rows in set (0.00 sec)
The query for straight join that displays only matching rows is shown below. We have used ON here.
mysql> SELECT ForeignTableDemo.Id, ForeignTableDemo.Name, PrimaryTableDemo.Address - > from ForeignTableDemo - > join PrimaryTableDemo - > on ForeignTableDemo.FK = PrimaryTableDemo.FK;
The following is the output.
+------+------+---------+ | Id | Name | Address | +------+------+---------+ | 1 | John | US | | 2 | Bob | UK | +------+------+---------+ 2 rows in set (0.14 sec)
Example of USING.
The following is the syntax of USING in MySQL, that displays the record where FK = 1.
mysql> select *from ForeignTableDemo join PrimaryTableDemo using(FK) where FK=1;
Here is the output.
+------+------+------+---------+ | FK | Id | Name | Address | +------+------+------+---------+ | 1 | 1 | John | US | +------+------+------+---------+ 1 row in set (0.09 sec)
Advertisements