Page 83 - SQL
P. 83

(The data type of the Foreign Key must match the datatype of the referenced key.)


        The Foreign Key constraint on the column Dept_Code allows values only if they already exist in the
        referenced table, Department. This means that if you try to insert the following values:


         INSERT INTO Programming_Courses Values ('CS300', 'FDB-DB001', 'Database Systems');


        the database will raise a Foreign Key violation error, because CS300 does not exist in the Department
        table. But when you try a key value that exists:


         INSERT INTO Programming_Courses VALUES ('CS205', 'FDB-DB001', 'Database Systems');
         INSERT INTO Programming_Courses VALUES ('CS205', 'DB2-DB002', 'Database Systems II');


        then the database allows these values.



        A few tips for using Foreign Keys




            •  A Foreign Key must reference a UNIQUE (or PRIMARY) key in the parent table.
            •  Entering a NULL value in a Foreign Key column does not raise an error.
            •  Foreign Key constraints can reference tables within the same database.
            •  Foreign Key constraints can refer to another column in the same table (self-reference).

        Read Foreign Keys online: https://riptutorial.com/sql/topic/1533/foreign-keys

















































        https://riptutorial.com/                                                                               65
   78   79   80   81   82   83   84   85   86   87   88