Join, Constraints
Join, Constraints
+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
| 2 | mahesh | 70 |
+------+--------+-------+
+------+-------+
+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
| 2 | mahesh | 70 |
+------+--------+-------+
2 rows in set (0.107 sec)
// We should think about join whenever we require columns from different tables.
// We must write table name along with common column name . e.g. stud1.rno . rno
column is common column .
1)
+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
| 2 | mahesh | 70 |
+------+--------+-------+
2 rows in set (0.107 sec)
2)
MariaDB [test]> select s1.rno , s1.name , s2.marks from stud1 s1 ,stud2 s2 where
s1.rno=s2.rno;
+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
| 2 | mahesh | 70 |
+------+--------+-------+
2 rows in set (0.001 sec)
3)
MariaDB [test]> select stud1.rno , name , marks from stud1 join stud2 on
stud1.rno=stud2.rno;
+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
| 2 | mahesh | 70 |
+------+--------+-------+
2 rows in set (0.001 sec)
MariaDB [test]> select stud2.rno , name , marks from stud1 join stud2 on
stud1.rno=stud2.rno;
+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
| 2 | mahesh | 70 |
+------+--------+-------+
2 rows in set (0.001 sec)
// Inner Join is default join type . In inner join we get matching records only
from 2 tables .
MariaDB [test]> select stud1.rno , name , marks from stud1 join stud2 on
stud1.rno=stud2.rno;
+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
| 2 | mahesh | 70 |
+------+--------+-------+
2 rows in set (0.001 sec)
MariaDB [test]> select stud2.rno , name , marks from stud1 , stud2 where
stud1.rno=stud2.rno;
+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
| 2 | mahesh | 70 |
+------+--------+-------+
2 rows in set (0.001 sec)
MariaDB [test]> select rno , name , marks from stud1 natural join stud2;
+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
| 2 | mahesh | 70 |
+------+--------+-------+
2 rows in set (0.001 sec)
MariaDB [test]> select rno , name , marks from stud1 natural join stud2 where
marks>80;
+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
+------+--------+-------+
1 row in set (0.101 sec)
MariaDB [test]> select stud2.rno , name , marks from stud1 , stud2 where
stud1.rno=stud2.rno and marks>80;
+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
+------+--------+-------+
1 row in set (0.001 sec)
MariaDB [test]> select stud2.rno , name , marks from stud1 join stud2 on
stud1.rno=stud2.rno;
+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
| 2 | mahesh | 70 |
+------+--------+-------+
2 rows in set (0.001 sec)
// In left join we get all , matching and unmatching both records from the table
which is at left hand side of left join keyword
select stud1.rno , name , marks from stud1 left join stud2 on stud1.rno=stud2.rno;
+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
| 2 | mahesh | 70 |
| 3 | suresh | NULL |
+------+--------+-------+
3 rows in set (0.000 sec)
// In right join we get all matching and unmatching both records from the table
which is at right hand side of right join keyword
MariaDB [test]> select stud2.rno , name , marks from stud1 right join stud2 on
stud1.rno=stud2.rno;
+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
| 2 | mahesh | 70 |
| 4 | NULL | 60 |
+------+--------+-------+
3 rows in set (0.001 sec)
MariaDB [test]> select * from stud1;
+------+--------+
| rno | name |
+------+--------+
| 1 | rakesh |
| 2 | mahesh |
| 3 | suresh |
+------+--------+
3 rows in set (0.000 sec)
MariaDB [test]> create table stud6(rno int not null, marks int);
Query OK, 0 rows affected (0.283 sec)
// unique Constraint
// unique column can have null value . But can not have duplicate values
MariaDB [test]> create table stud8(rno int primary key, marks int);
Query OK, 0 rows affected (0.322 sec)
MariaDB [test]> create table stud9(rno int primary key auto_increment, marks int);
Query OK, 0 rows affected (0.212 sec)
// Check Constraint
//Default Constraint
MariaDB [test]> create table stud12(rno int , city varchar(20) default 'pune');
Query OK, 0 rows affected (0.259 sec)
MariaDB [test]> create table stud1 (rno int primary key,name varchar(20));
MariaDB [test]> insert into stud1 values(1,'d');
MariaDB [test]> insert into stud1 values(2,'e');
MariaDB [test]> insert into stud1 values(3,'f');
//Foreign Key Constraint : - Foreign Key can not have value which is not present in
primary key . e.g. roll number 4 is not present
// in primary key column rno from stud1 table . Hence we can not have 4 value for
rno foreign key column in stud2 table .
// Primary Key can not have null value whereas Foreign key can have null value
// Primary Key duplicate and null both are not allowed . In Foreign key , it is
allowed
MariaDB [test]> create table stud2 (rno int,marks int,foreign key(rno) references
stud1(rno));
Query OK, 0 rows affected (0.199 sec)