0% found this document useful (0 votes)
12 views

2 MySQL - Command - Foreign - Key

to take knowledge
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

2 MySQL - Command - Foreign - Key

to take knowledge
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Enter password: ************

Welcome to the MySQL monitor. Commands end with ; or \g.


Your MySQL connection id is 1
Server version: 5.6.50-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle
Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| store |
| test |
+--------------------+
5 rows in set (2.39 sec)
mysql> create database ajay;
Query OK, 1 row affected (0.45 sec)

mysql> use ajay;


Database changed

mysql> create table State(S_Id integer primary key, S_Name varchar(20));


Query OK, 0 rows affected (6.20 sec)

mysql> show tables;


+----------------+
| Tables_in_ajay |
+----------------+
| state |
+----------------+
1 row in set (0.11 sec)

mysql> desc state;


+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| S_Id | int(11) | NO | PRI | NULL | |
| S_Name | varchar(20) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (1.62 sec)

mysql> create table District(D_Id integer primary key, D_Name varchar(20), Ref_S_Id integer, foreign key(Ref_S_Id)
references state(S_Id));
Query OK, 0 rows affected (0.39 sec)

mysql> create table Taluka(T_Id integer primary key, T_Name varchar(20), Ref_D_Id integer, foreign key(Ref_D_Id)
references district(D_Id));
Query OK, 0 rows affected (0.72 sec)
mysql> create table Village(V_Id integer primary key, V_Name varchar(20), Ref_T_Id integer, foreign key(Ref_T_Id)
references Taluka(T_Id));
Query OK, 0 rows affected (0.31 sec)

mysql> show tables;


+----------------+
| Tables_in_ajay |
+----------------+
| district |
| state |
| taluka |
| village |
+----------------+
4 rows in set (0.00 sec)

mysql> insert into state(s_id, s_name) values(1,'Gujarat');


Query OK, 1 row affected (0.52 sec)

mysql> select * from state;


+------+---------+
| S_Id | S_Name |
+------+---------+
| 1 | Gujarat |
+------+---------+
1 row in set (0.11 sec)

mysql> insert into district(d_id, d_name, ref_s_id) values(101,'Ahmedabad',1);


Query OK, 1 row affected (0.13 sec)

mysql> insert into district(d_id, d_name, ref_s_id) values(102,'Surat',1);


Query OK, 1 row affected (0.17 sec)

mysql> select * from district;


+------+-----------+----------+
| D_Id | D_Name | Ref_S_Id |
+------+-----------+----------+
| 101 | Ahmedabad | 1|
| 102 | Surat | 1|
+------+-----------+----------+
2 rows in set (0.00 sec)

mysql> insert into taluka(t_id, t_name, ref_d_id) values(1001,'Choryasi',101);


Query OK, 1 row affected (0.18 sec)

mysql> insert into taluka(t_id, t_name, ref_d_id) values(1002,'Olpad',101);


Query OK, 1 row affected (0.09 sec)

mysql> select * from taluka;


+------+----------+----------+
| T_Id | T_Name | Ref_D_Id |
+------+----------+----------+
| 1001 | Choryasi | 102 |
| 1002 | Olpad | 102 |
+------+----------+----------+
2 rows in set (0.00 sec)

mysql> insert into village(v_id, v_name, ref_t_id) values(10001,'Kamrej',1001);


Query OK, 1 row affected (0.14 sec)

mysql> insert into village(v_id, v_name, ref_t_id) values(10002,'Udhna',1001);


Query OK, 1 row affected (0.13 sec)

mysql> select * from village;


+-------+--------+----------+
| V_Id | V_Name | Ref_T_Id |
+-------+--------+----------+
| 10001 | Kamrej | 1001 |
| 10002 | Udhna | 1001 |
+-------+--------+----------+
2 rows in set (0.00 sec)

mysql> create table student(SID integer primary key, name varchar(50), father_name varchar(50), mother_name
varchar(50), ref_v_id integer, foreign key(ref_v_id) references village(v_id));
Query OK, 0 rows affected (0.72 sec)

mysql> desc student;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| SID | int(11) | NO | PRI | NULL | |
| name | varchar(50) | YES | | NULL | |
| father_name | varchar(50) | YES | | NULL | |
| mother_name | varchar(50) | YES | | NULL | |
| ref_v_id | int(11) | YES | MUL | NULL | |
+-------------+-------------+------+-----+---------+-------+
5 rows in set (0.02 sec)

mysql> insert into student(sid, name, father_name, mother_name, ref_v_id) values(100001, 'abc', 'xyz', 'mno',
10001);
Query OK, 1 row affected (0.17 sec)

mysql> select s.*, v.v_name, t.t_name, d.d_name, st.s_name from student s


-> left join village v on v.v_id = s.ref_v_id
-> left join taluka t on t.t_id = v.ref_t_id
-> left join district d on d.d_id = t.ref_d_id
-> left join state st on st.s_id = d.ref_s_id;
+--------+------+-------------+-------------+----------+--------+----------+-----------+---------+
| SID | name | father_name | mother_name | ref_v_id | v_name | t_name | d_name | s_name |
+--------+------+-------------+-------------+----------+--------+----------+-----------+---------+
| 100001 | abc | xyz | mno | 10001 | Kamrej | Choryasi | Surat | Gujarat |
+--------+------+-------------+-------------+----------+--------+----------+-----------+---------+
1 row in set (1.35 sec)

You might also like