Mysql Primary Foreign for Beginners

Here I am goving to create two tables first will be parent and second will be child table.

Table are Student and Marks

In student table we make rollno as primary key which is act as a foreign in Marks table.

-------------------------------------------------------------------------------------------------------------------

sql>> create table student (roll int primary key,name text)


sql>> create table marks(sub1 int,sub2 int, sub3 int ,roll int, foreign key(roll) references student (roll));

-------------------------------------------------------------------------------------------------------------------

sql>show tables;

student
marks

here we can created two table succesfully with paarent child relatoinship.


---------------------------------------------------------------------------------------------------------------------

Insert data
-----------------

If i try to insert data in child table (example Marks table) without adding data in Parent table (ie, in Student) then we will get Error

----------------------------------------------------------------------------------------------------------------------------

So first we insert data in parent:


Sql> Inert into student values(101,'Raju');
Sql>Insert into marks values(45,45,34,101);

-----------------------------------------------------------------------------------------------------------------------------
display data:

select * form marks where id=101;

Comments

Popular posts from this blog

Filter In Javafx

Kotlin with StandAlone Complier