I need to create 3 tables which look like this
student(sid: CHAR(12), sname: VARCHAR(50), bdate: DATE, address: VARCHAR(50), scity: VARCHAR(20), year: CHAR(20), gpa: FLOAT)
company(cid: CHAR(8), cname: VARCHAR(20))
apply(sid: CHAR(12), cid: CHAR(8))
(Bolded attributes are primary keys)
But I’m not sure how to set the foreign keys since for instance cid of apply table is both primary key in apply table and company table ( which has the same situation for sid between apply table and student table). Thanks for any help.
These are the codes for creating tables:
myQuery = "CREATE TABLE student "
+ "(sid CHAR(12), sname VARCHAR(50), "
+ "bdate DATE, address VARCHAR(50), "
+ "scity VARCHAR(20), year CHAR(20), "
+ "gpa FLOAT) ENGINE=InnoDB;";
myQuery = "CREATE TABLE company "
+ "(cid CHAR(8), cname VARCHAR(20), quota CHAR(8))ENGINE=InnoDB;";
myQuery = "CREATE TABLE apply "
+ "(sid CHAR(12), cid CHAR(8)) ENGINE=InnoDB;";
It looks like the
applytable is a many-to-many join betweenstudentandcompany.In that case, you’d want to set
studentandcompanylike you have (though posting the output ofSHOW CREATE TABLE studentmay get you more helpful answers). So for the apply table, you want two foreign keys: one onsidwhich referencesstudent.sid, and one oncidwhich referencescompany.cid. Maybe something like:Edit: Based on your table creation, you’re not setting the primary keys either. Add a
PRIMARY KEYidentifier to any column you wish to be a primary key.