For creating a relation between the tables you can make use of PRIMARY KEY and FOREIGN KEY.
Depeding on which engine is being run you you might have to make a very minute adjustment to your query.
CREATE TABLE temp1(
slNumber INT(4) NOT NULL AUTO_INCREMENT,
.....
PRIMARY KEY(slNumber)
)
CREATE TABLE temp2(
slNumber INT(4) NOT NULL AUTO_INCREMENT,
idNumber INT(4) NOT NULL AUTO_INCREMENT,
.....
PRIMARY KEY(idNumber),
FOREIGN KEY(slNumber) REFERENCES temp1(slNumber)
)ENGINE=INNODB
Specifying the
ENGINE=INNODB
is necessary as MYISAM engine does not support the FOREIGN KEY.
For getting the relationship details like One-One, One-Many or Many-Many, i would suggest you make use of
MySql Workbench[
^] As it provides you with a GUI that can be used to get the details of relationship.
There are few more things that you need to be careful about before you make use of the FOREIGN KEY.
Its always advisable to make use of
ON DELETE CASCADE
and
ON UPDATE CASCADE
after the table with the FOREIGN KEY is created.
And make sure that the size of the FOREIGN KEY matches the size of the PRIMATY KEY.
BR//
Harsha Narayana