Database Systems Lecture 8
Database Systems Lecture 8
› NOT NULL
› UNIQUE
› PRIMARY KEY
› FOREIGN KEY
› CHECK
Constraints
– The foreign key is defined in the child table and the table containing the
referenced column is the parent table. The foreign key is defined using a
combination of the following keywords:
✓ FOREIGN KEY is used to define the column in the child table at the
table-constraint level.
✓ REFERENCES identifies the table and the column in the parent table.
✓ ON DELETE CASCADE indicates that when a row in the parent table is
deleted, the dependent rows in the child table are also deleted.
✓ ON DELETE SET NULL indicates that when a row in the parent table is
deleted, the foreign key values are set to null.
FOREIGN KEY Constraint
– The default behavior is called the restrict rule, which disallows the
update or deletion of referenced data.
The new table has the same column definitions. All columns
or specific columns can be selected.
When you create a new table using existing table, new table
would be populated using existing values in the old table.
Create Table Using another
Table (Using a Subquery)
Syntax:
• Example
• Example
• Syntax
Allter table table_name add column_name datatype size
• Example
Alter table emp add phone_num number(10)
ALTER TABLE Statement
• Syntax
Allter table table_name modify column_name datatype size
• Example
Alter table emp modify phone_num number(15)
ALTER TABLE Statement
• Syntax
Allter table table_name rename column old_column_name to
new_column_name
• Example
Alter table emp rename column phone_num to emp_phone_num
ALTER TABLE Statement
• Syntax
Allter table table_name add constraint constraint_name
constraint_type(column_name)
• Example
Alter table emp add constraint uni_ph_num unique(emp_phone_num)
ALTER TABLE Statement
• Syntax
Allter table table_name drop constraint constraint_name
• Example
Alter table emp drop constraint uni_ph_num
ALTER TABLE Statement
• Syntax
Allter table table_name drop column column_name
• Example
Alter table emp drop column emp_phone_num
RENAME Statement
› Renaming a Table
• Syntax
rename old_table_name to new_table_name
• Example
rename emp to employee
DROP TABLE Statement
› Dropping a Table
• Syntax
drop table table_name
• Example
drop table emp
References
SQL TUTORIAL , Simply Easy Learning by tutorialspoint.com.
Oracle Slides.