Working With Constraints MySQL
Working With Constraints MySQL
One of the objectives of this activity is to set a database and create tables. Below is the entity
relationship diagram (ERD) of the tables we will create on a database. The name of the
company that we will set up with a database is Hope, Incorporated.
Let us drop existing tables (if there is any from our schema):
Download EMPFILE.CSV.
Open the file with Notepad. The content of the file is separated by a comma(comma-separated
values). Do not alter nor save the file.
Select your download file EMPFILE.CSV. Click Next button from the right corner of LOAD DATA
interface.
Select the your schema set to your database. In the example, it is XKLO6953:
Select EMPLOYEE table that you have just created:
Since we have created EMPLOYEE table before appending the file, column names were already
set. The EMPFILE has no header row (column names) --- it is just pure comma-separated data.
Click Header in first row. This must be UNCHECKED. Make sure that the first row is SMITH,
JOHN and not SMITH, JANE.
You should have the following screen in loading 32 rows to your table:
Go back on your RUNSQL interface editor and check if you have the loaded date by executing:
Open an EXCEL spreadsheet and type the following. Note: MAKE SURE YOU TYPE CORRECTLY.
iF IT IS IN UPPER CASE, TYPE IN UPPERCASE. NO EXTRA SPACES in deptCode values. You will get
a problem retrieving a file later if you will not follow:
Save it as DEPFILE.CSV. Check if commas were inserted in the file. Open the file with Notepad.
You should have the following data inside the DEPFILE.CSV:
Click browse files and select DEPFILE.CSV in your My Documents folder:
Click Next button.
Go to RUNSQL interface.
You should have the following error message because ACT is already existing at DEPARTMENT
table:
Check if DEPARTMENT column accepts more than 20 characters
At the end of the loading stage you should have the following data from JOB table:
Create composite and foreign keys
empNo, jobCode, effDate are set as primary key. More than one column set as primary key is
called composite key. Primary key should not contain blank value that is why we indicate the
NOT NULL expression.
All values that entered at empNo and jobCode are cross REFERENCEd with employee and job
tables. The set up increases the referential integrity of the table which means no value will be
entered unless it exists on the two tables.
Alter again the table. Type the following scripts:
Salary has DECIMAL data type which has a size of 10 digits with 2 decimal point numbers based
on the CREATE TABLE definition. It has a CONSTRAINT with a name of salary_ck. Only salary
with values greater or equal to 0.0 are accepted.
effDate has DATE data type. This monitors the effectivity date of the position on the job history
table.
deptCode is set as FOREIGN KEY. We limit the entry on deptCode column which has
REFERENCEd with department table. Foreign key is non-key or not primary key on one table
but primary key in another table. On our case, deptCode is foreign key but referenced with
department table which has its own deptCode set as primary key.
Check the structure of the table. You should have the following result:
Validate input row with constraints set
Let us check if our set constraints are working. Insert the following row:
Notice on the above insert 00096, PR3, and ITC were non-existent at employee, job, and
department tables. jobHistory columns are cross-REFERENCEd with these tables. Any value
not included on these referenced tables will be not accepted as an entry at jobHistory.
Since the jobHistory entry is intended for John Smith who works as Programmer 2 at IT
department, let us correct our insert table with the following script:
We have the following error:
Salary_ck is the constraint name we have set before to check if value correspond with the
range (negative values are not accepted). Remove the negative sign for the salary and re-enter
the script.
DELETE employee
WHERE empNo = '00003';
You can not just delete empNo 00003 from employee table because it is existing in jobHistory
table. You have the following error message:
This is the effect of ON DELETE RESTRICT that you have set in jobHistory when you altered the
table and specify a constraint.
But how do I delete employee 00003 from employee table if I want it to be removed totally
from employee table?
You need to delete first in jobHistory before you delete in employee table.
DELETE jobHistory
WHERE empNo = '00003';
DELETE employee
WHERE empNo = '00003';
DELETE job
WHERE jobCode = 'SA1';
Deleting a row in the parent table job would mean automatic deletion in dependent table
jobHistory. The deletion were cascaded to the dependant table. This is the effect of ON
DELETE CASCADE on jobHistory table when you altered the table and specify a constraint.