30 likes | 113 Views
STUDENTS. More SQL – Specifying Foreign Keys. Consider the following tables, STUDENTS & GRADES. GRADES. Specifying Foreign Keys….
E N D
STUDENTS More SQL – Specifying Foreign Keys Consider the following tables, STUDENTS & GRADES GRADES CSE470 Software Engineering Fall 2000
Specifying Foreign Keys… • The table GRADES has a foreign-key relationship with table STUDENTS, i.e., for every value of STU_ID in GRADES there must be a record with a matching ID in STUDENTS • The SQL statements to create the two tables will be as follows: • SQL to create table STUDENTS: CREATE TABLE STUDENTS ( ID INTEGER, NAME CHAR(30), DOJ DATETIME, EMAIL CHAR(30), CONSTRAINT PK_STUDENTS PRIMARY KEY (ID)) CSE470 Software Engineering Fall 2000
Specifying Foreign Keys… • SQL to create table GRADES, with foreign key CREATE TABLE GRADES ( STU_ID INTEGER, COURSE CHAR(6), GRADE DECIMAL(2,1), CONSTRAINT PK_GRADES PRIMARY KEY (STU_ID, COURSE), CONSTRAINT FK_GRADES FOREIGN KEY (STU_ID) REFERENCES STUDENTS) CSE470 Software Engineering Fall 2000