Oracle SQL in 10 Minutes - Asim Abbasi
CH1: SQL Basics | CH2: SQL Operators | CH3: SQL Built-in Functions | CH4: SQL Joins | CH5: UPDATE, INSERT & DELETE Statements | CH6: CREATE, ALTER & DROP Statements | CH7: Constraints | CH8: Linking Tables vs Joining Tables | CH9: SQL Statements for Other Database Objects | CH10: SQL Statements for Database Security
Chapter 7: Constraints
Next we will create another table emp_test and link it with the dept_test table using FOREIGN KEY constraint. Once such a table gets created, the table having PRIMARY KEY is called the “Parent Table” and the one having the FOREIGN KEY is called the “Child Table”. A parent table can have as many child tables as you like or in other words a primary key constraint column can be linked to one or more tables using FOREIGN KEY constraint. No value can be inserted in the FOREIGN KEY constraint column having no exactly similar value in the referenced PRIMARY KEY constraint column of parent table.
Example:
CREATE TABLE emp_test
(empno NUMBER(4) CONSTRAINT pk_dept_test PRIMARY KEY,
ename VARCHAR2(10),
job VARCHAR2(10),
deptno NUMBER(3),
CONSTRAINT fk_emp_test FOREIGN KEY (deptno) REFERENCES dept_test(deptno)
);
Once this table gets created you can say that you have developed a
“link” between the two tables and this link will be permanent. The two
beauties of PRIMARY/FOREIGN KEY relationship are as follows.
Chapter 7: Constraints
CH1: SQL Basics | CH2:
SQL Operators |
CH3: SQL Built-in Functions | CH4:
SQL Joins | CH5:
UPDATE, INSERT & DELETE Statements |
CH6: CREATE, ALTER & DROP Statements | CH7:
Constraints | CH8:
Linking Tables vs Joining Tables | CH9:
SQL
Statements for Other Database Objects | CH10:
SQL Statements for
Database Security Share with others: