Professional Documents
Culture Documents
A storage area
You specify
o
Table name
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
Table-Level constraint:
A foreign key value must match an existing value in the parent table or be
NULL.
Foreign keys are based on data values and are purely logical, rather than
physical, pointers.
Example: table-level
CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR(25) NOT
NULL, hire_date DATE NOT NULL, .., department_id NUMBER(4), CONSTRAINT
Foreign key: defines the column in the child table at the table-constraint level
On DELETE CASCADE: Delete the dependent rows in the child table when a
row in the parent table is deleted
CHECK constraint
Defines a condition that each row must satisfy
The following expressions are not allowed:
o
Example:
CREATE TABLE employees (emp_id NUMBER(6) CONSTRAINT emp_emp_id PRIMARY
KEY, ..,salary NUMBER(8.2) CONSTRAINT emp_salary_ck CHECK (salary>0),)
Violating constraints
You cannot delete a row that contains a primary key that is used as a foreign
key in another table.
DROPPING a table
All data and structure in the table are deleted.
Any pending transaction are committed
Manipulating DATA 8
INSERT statement:
Match the number of columns in the insert clause to those in the sub query.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX
UPDATE statement
Specific row or rows are modified if you specify the WHERE clause
All rows in the table are modified if you omit the WHERE clause
Example
UPDATE employees SET department id = 70 WHERE employee_id = 113;
UPDATE copy_emp SET department_id = 110;
Two columns with subquery
UPDATE employee 114s job and salary to match that of employee
UPDATE employees SET job_id = (SELECT job_id FROM employees WHERE
employee_id = 205)
Salary = (SELECT salary FROM employees WHERE employee_id = 205) WHERE
employee_id = 114;
DELETE Statement
You can remove exiting rows from a table by using the DELETE statement:
All rows in the table are deleted if you omit the WHERE clause
Example:
DELETE FROM departments WHERE department_name = Finance;
DELETE FROM copy_emp;