0% found this document useful (0 votes)
10 views9 pages

Join, Constraints

The document discusses various SQL queries used to create tables, insert data, perform inner joins, left joins and right joins between two tables. It also explains different data integrity constraints like primary key, foreign key, unique, not null, check and default constraints.

Uploaded by

Snehal Wakchaure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Join, Constraints

The document discusses various SQL queries used to create tables, insert data, perform inner joins, left joins and right joins between two tables. It also explains different data integrity constraints like primary key, foreign key, unique, not null, check and default constraints.

Uploaded by

Snehal Wakchaure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

create table stud1(rno int , name varchar(20));

insert into stud1 values(1,'rakesh');

insert into stud1 values(2,'mahesh');

insert into stud1 values(3,'suresh');

create table stud2(rno int , marks int);


insert into stud2 values(1,90);

insert into stud2 values(2,70);

insert into stud2 values(4,60);

MariaDB [test]> select stud1.rno , name , marks from stud1,stud2 where


stud1.rno=stud2.rno;

+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
| 2 | mahesh | 70 |
+------+--------+-------+

MariaDB [test]> select * from stud1;


+------+--------+
| rno| name |
+------+--------+
| 1 | rakesh |
| 2 | mahesh |
| 3 | suresh |
+------+--------+

MariaDB [test]> select * from stud2;


+------+-------+
| rollno | marks |
+------+-------+
| 1 | 90 |
| 2 | 70 |
| 4 | 60

+------+-------+

+------+--------+-------+
| 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.

// Below is the query of inner join which is default join type.

// We must write table name along with common column name . e.g. stud1.rno . rno
column is common column .

Different ways of writing inner join query :-

1)

MariaDB [test]> select stud1.rno , name , marks from stud1,stud2 where


stud1.rno=stud2.rollno;

+------+--------+-------+
| rno | name | marks |
+------+--------+-------+
| 1 | rakesh | 90 |
| 2 | mahesh | 70 |
+------+--------+-------+
2 rows in set (0.107 sec)

2)

// Alias means alternative name . e.g. In below query we are giving s1 as a


alternative name to stud1 table , s2 as a alternative name to stud2 table

select s1.rno , s1.name , s2.marks from stud1 s1 join stud2 s2 on s1.rno=s2.rno;

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)

//In natural join we do not require to specify join condition .


// But we need to have common column name same in both the table

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)

//writing above query using inner join

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)

MariaDB [test]> select * from stud1;


+------+--------+
| rno | name |
+------+--------+
| 1 | rakesh |
| 2 | mahesh |
| 3 | suresh |
+------+--------+
3 rows in set (0.000 sec)

MariaDB [test]> select * from stud2;


+------+-------+
| rno | marks |
+------+-------+
| 1 | 90 |
| 2 | 70 |
| 4 | 60 |
+------+-------+
3 rows in set (0.000 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)

MariaDB [test]> select * from stud1;


+------+--------+
| rno | name |
+------+--------+
| 1 | rakesh |
| 2 | mahesh |
| 3 | suresh |
+------+--------+
3 rows in set (0.001 sec)

MariaDB [test]> select * from stud2;


+------+-------+
| rno | marks |
+------+-------+
| 1 | 90 |
| 2 | 70 |
| 4 | 60 |
+------+-------+
3 rows in set (0.002 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]> select * from stud1;


+------+--------+
| rno | name |
+------+--------+
| 1 | rakesh |
| 2 | mahesh |
| 3 | suresh |
+------+--------+
3 rows in set (0.001 sec)

// Not Null Constraint

MariaDB [test]> create table stud6(rno int not null, marks int);
Query OK, 0 rows affected (0.283 sec)

MariaDB [test]> insert into stud6 values(1,null);


Query OK, 1 row affected (0.026 sec)

MariaDB [test]> insert into stud6 values(null,80);


ERROR 1048 (23000): Column 'rno' cannot be null

// unique Constraint

// unique column can have null value . But can not have duplicate values

MariaDB [test]> create table stud7(rno int unique, marks int);


Query OK, 0 rows affected (0.368 sec)

MariaDB [test]> insert into stud7 values(1,80);


Query OK, 1 row affected (0.047 sec)

MariaDB [test]> insert into stud7 values(1,80);


ERROR 1062 (23000): Duplicate entry '1' for key 'rno'

MariaDB [test]> insert into stud7 values(null,80);


Query OK, 1 row affected (0.018 sec)
// Primary Key Constraint is a combination of unique and not null constraint
// primary key column is used to identify records

MariaDB [test]> create table stud8(rno int primary key, marks int);
Query OK, 0 rows affected (0.322 sec)

MariaDB [test]> insert into stud8 values(null,80);


ERROR 1048 (23000): Column 'rno' cannot be null

MariaDB [test]> insert into stud8 values(1,80);


Query OK, 1 row affected (0.061 sec)

MariaDB [test]> insert into stud8 values(1,80);


ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

MariaDB [test]> create table stud9(rno int primary key auto_increment, marks int);
Query OK, 0 rows affected (0.212 sec)

insert into stud9 values(80)

MariaDB [test]> insert into stud9(marks) values(80);


Query OK, 1 row affected (0.113 sec)

MariaDB [test]> select * from stud9;


+-----+-------+
| rno | marks |
+-----+-------+
| 1 | 80 |
+-----+-------+
1 row in set (0.000 sec)

MariaDB [test]> insert into stud9(marks) values(70);


Query OK, 1 row affected (0.151 sec)

MariaDB [test]> select * from stud9;


+-----+-------+
| rno | marks |
+-----+-------+
| 1 | 80 |
| 2 | 70 |
+-----+-------+
2 rows in set (0.001 sec)

MariaDB [test]> delete from stud9 where rno=2;


Query OK, 1 row affected (0.139 sec)

MariaDB [test]> insert into stud9(marks) values(70);


Query OK, 1 row affected (0.120 sec)
MariaDB [test]> select * from stud9;
+-----+-------+
| rno | marks |
+-----+-------+
| 1 | 80 |
| 3 | 70 |
+-----+-------+
2 rows in set (0.000 sec)

// Check Constraint

MariaDB [test]> create table stud10(rno int , marks int,check (marks<=100));


Query OK, 0 rows affected (0.292 sec)

MariaDB [test]> insert into stud10(marks) values(70);


Query OK, 1 row affected (0.040 sec)

MariaDB [test]> insert into stud10(marks) values(101);


ERROR 4025 (23000): CONSTRAINT `CONSTRAINT_1` failed for `test`.`stud10`

//Default Constraint

MariaDB [test]> create table stud12(rno int , city varchar(20) default 'pune');
Query OK, 0 rows affected (0.259 sec)

MariaDB [test]> insert into stud12(rno) values(1);


Query OK, 1 row affected (0.024 sec)

MariaDB [test]> insert into stud12(rno,city) values(2,'dhule');


Query OK, 1 row affected (0.024 sec)

MariaDB [test]> select * from stud12;


+------+------+
| rno | city |
+------+------+
| 1 | pune |
+---2---+dhule------+
1 row in set (0.000 sec)

MariaDB [test]> drop table stud1;


Query OK, 0 rows affected (0.125 sec)

MariaDB [test]> drop table stud2;


Query OK, 0 rows affected (0.066 sec)
Primary Key -- Foreign Key Constraint

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)

MariaDB [test]> insert into stud2 values(1,90);


Query OK, 1 row affected (0.047 sec)

MariaDB [test]> insert into stud2 values(4,90);


ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint
fails (`test`.`stud2`, CONSTRAINT `stud2_ibfk_1` FOREIGN KEY (`rno`) REFERENCES
`stud1` (`rno`))

MariaDB [test]> insert into stud2 values(null,90);


Query OK, 1 row affected (0.043 sec)

MariaDB [test]> insert into stud2 values(1,80);


Query OK, 1 row affected (0.133 sec)

You might also like