FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How to specify foreign key





bukaida
I am using mysql4.0.24 along with php 4.4.0. I have created the following two tables--





Code:
 
$query1="CREATE TABLE IF NOT EXISTS student(
   s_id VARCHAR(10) NOT NULL,
   s_fname VARCHAR(20)NOT NULL,
   s_mname VARCHAR(20)NOT NULL,
   s_lname VARCHAR(20)NOT NULL,
   s_gname VARCHAR(30)NOT NULL,
    s_address VARCHAR(50)NOT NULL,
    s_dob DATE,
   s_gender VARCHAR(6)NOT NULL,
   s_class VARCHAR(3)NOT NULL,
    s_section VARCHAR(1)NOT NULL,
   s_height INT(4),
    s_weight INT(4),
   s_g_income VARCHAR(20)NOT NULL,
   s_photo VARCHAR(50)NOT NULL,
   s_marks INT(4),
    s_rank VARCHAR(5)NOT NULL,
   s_hobbies VARCHAR(50)NOT NULL,
    PRIMARY KEY(s_id))TYPE = INNODB";
   
   
   $query2="CREATE TABLE IF NOT EXISTS image(
   im_id int(10) NOT NULL auto_increment,
  type varchar(16) NOT NULL default '',
  stream longblob NOT NULL,
  PRIMARY KEY (im_id))TYPE = INNODB";
   
$result1=mysql_query($query1) or die(mysql_error());
$result2=mysql_query($query2) or die(mysql_error());
 


Now I have the following questions--

How to connect the im_id to s_id so that a particular image is fetched for a particular s_id (preferably through a single s_id only)?

Is it mandetary to have the foreign key as integer and auto-increment?
Please help.
kv
Here you go
Code:

SELECT * student, image where student.s_id=image.im_id
Related topics
sql schema primary/foreign key question
How hard is it to make a user database.
some help scripting cgi
Trying to move SQL database
What exactly does indexing mysql do?
foreign key!
tag system?
Multiple Many-to-Many relationship
Avoid Accidental Caps Lock Key Hitting
database design of room assignment schedules of nurses
help me create a query
Call a value through Foreign id from another table(DW+PHP)
Add Foreign Constraint doubt
what does constraint foreign key means when we create table
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.