Greetings,
I have table CUSTOMER:
mysql> describe CUSTOMER;
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| CUSTOMER_NUM | char(3) | NO | PRI | NULL | |
| CUSTOMER_NAME | char(35) | NO | | NULL | |
| STREET | char(15) | YES | | NULL | |
| CITY | char(15) | YES | | NULL | |
| STATE | char(2) | YES | | NULL | |
| ZIP | char(5) | YES | | NULL | |
| BALANCE | decimal(8,2) | YES | MUL | NULL | |
| CREDIT_LIMIT | decimal(8,2) | YES | MUL | NULL | |
| REP_NUM | char(2) | YES | | NULL | |
+---------------+--------------+------+-----+---------+-------+
9 rows in set (0.00 sec)
and table ORDERS;
mysql> describe ORDERS;
+--------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| ORDER_NUM | char(5) | NO | PRI | NULL | |
| ORDER_DATE | date | YES | | NULL | |
| CUSTOMER_NUM | char(3) | YES | MUL | NULL | |
+--------------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
In Table CUSTOMER, column CUSTOMER_NUM is the Primary Key
In Table ORDERS there's also a CUSTOMER_NUM column I attempted to add an integrity constraint (A Foreign Key) to the CUSTOMER_NUM column in the ORDERS table as follows:
mysql> alter table ORDERS
-> add foreign key (CUSTOMER_NUM) references CUSTOMER;
Query OK, 7 rows affected (0.11 sec)
Records: 7 Duplicates: 0 Warnings: 0
The command looks like it worked fine but when I intentionally try to violate the integrity constraint of the foreign key as follows (by adding a row that contains a CUSTOMER_NUM '850' that is not contained in the CUSTOMER_NUM column in the CUSTOMER table):
mysql> insert into ORDERS
-> values
-> ('21625', '2007-10-23', '850');
Query OK, 1 row affected (0.00 sec)
The DBMS does not complain and it let me add that row even though it violates the foreign key constraint, WHY?
There is no CUSTOMER_NUM = '850' in the CUSTOMER table so this should have gave an error similar to what I received in Oracle: ORA-02291:integrity constarint ... violated - parent key not found.