IS220 : Database Fundamentals College of Computer and Information Sciences - Information Systems Dept. Lecture7: SQL Overview, Oracle Data Type, DDL and Constraints Part #2 Ref. Chapter6 Prepared by L. Nouf Almujally & Aisha AlArfaj& L.Fatima Alhayan 1
The Process of Database Design Real World Domain Conceptual model (ERD) Relational Data Model Create schema (DDL) Load Data (DML) 2
ALTER TABLE The ALTER TABLE statement is used to: Add columns Delete columns Modify columns Add a new constraint Remove an existing constraint 3Lecture7
ALTER TABLE - Add Column 1. To add a column in a table, use the following syntax: ALTER TABLE table_name ADD column_name datatype ; EXAMPLE: ALTER TABLE Student ADD Degree VARCHAR2(50) ; Table altered. 4Lecture7
ALTER TABLE - Delete Column 2. To delete a column in a table, use the following syntax: ALTER TABLE table_name DROP COLUMN column_name [CASCADE CONSTRAINTS] ; - Use [CASCADE CONSTRAINTS] to drop PRIMARY KEY column with dependent integrity constraints. EXAMPLE: ALTER TABLE Student Table altered. DROP COLUMN Degree; 5Lecture7
SQL> create table parent Table created. (id number(2) primary key); SQL> create table child (id number(2), Constraint id_fk foreign key (id) references parent(id)); Table created. SQL> alter table parent drop column id; alter table parent drop column id * ERROR at line 1: ORA-12992: cannot drop parent key column 6Lecture7
Use Cascade Constraint oracle Drops ID in Parent table and removes all reference constraints to this key. So the foreign key in Child table is removed. SQL> alter table parent drop column id CASCADE CONSTRAINTS; Table altered. 7Lecture7
ALTER TABLE Modify Column 3. To modify a column data type or constraint in a table, use the following syntax: ALTER TABLE table_name MODIFY (Column_name column_specification) ; EXAMPLE: ALTER TABLE orders Table altered. MODIFY (quantity number(5)); 8Lecture7
ALTER TABLE - Add Constraint 4. To add a new constraint in a table, use the following syntax: ALTER TABLE table_name ADD CONSTRAINT Constraint_name Constraint_type (column_name) ; EXAMPLE: ALTER TABLE Module ADD CONSTRAINT ck_unique UNIQUE (title); Table altered. 9Lecture7
ALTER TABLE - Remove Constraint 5. To remove constraint in a table, use the following syntax: ALTER TABLE table_name DROP CONSTRAINT Constraint_name [CASCADE] ; - Use [CASCADE] to delete PRIMARY KEY constraint with dependent integrity constraints. EXAMPLE1: ALTER TABLE Module DROP CONSTRAINT ck_unique; Table altered. 10
SQL> create table parent Table created. (id number(2) primary key); SQL> create table child (id number(2), Constraint id_fk foreign key (id) references parent(id)); Table created. SQL> ALTER TABLE Parent DROP PRIMARY KEY; ALTER TABLE Parent * ERROR at line 1:ORA-02273: this unique/primary key is referenced by some foreign keys 11
ALTER TABLE - Disable Constraint 6. disable constraint is used to deactivate (turn off) an integrity constraint temporarily, use the following syntax: ALTER TABLE table_name DISABLE CONSTRAINT constraint_name [CASCADE] ; Use [CASCADE] to disable PRIMARY KEY with dependent integrity constraints. Example : ALTER TABLE Parent DISABLE CONSTRAINT emp_empno_pk CASCADE; Table altered. ** disables a PRIMARY KEY constraint and any FOREIGN KEY constraints that depend on it (refer to it) 12
ALTER TABLE - Enable Constraint 7. Activate an integrity constraint currently disabled in the table definition by using the ENABLE clause. Use the following syntax: ALTER TABLE table_name ENABLE CONSTRAINT constraint_name ;; 13
DROP TABLES Syntax: Drop table table_name; OR Drop table table_name [Cascade Constraints]; Pay attention to referential integrity constraints when dropping tables. If Cascade Constraints is used, the constraints will be dropped first. 14
Lecture6 Example FK PK DeptNo DeptName EmpId EmpName DeptNo 101 AAA 102 BBB 103 CCC Department 1 Ali 101 2 Sally 101 3 John 103 Employee SQL> drop table Department; drop table Department * ERROR at line 1:ORA02449: unique/primary keys in table referenced by foreign keys SQL> drop table Department Cascade Constraints ; Table dropped. 15
Viewing Table Structures To see the structure of a table, type or DESCRIBE table_name; DESC table_name ; SQL> describe airlines; 16
Constraints Name Code Describes all constraint definitions on tables owned by the current user. SQL> select constraint_name, constraint_type from user_constraints where table_name = table name; Example: SQL> select constraint_name, constraint_type from user_constraints where table_name = PARENT ; CONSTRAINT_NAME C ------------------------------------------- SYS_C00657541 P system generated constraint name. 17
Alter Table Summary ALTER TABLE table_name ADD column_name data type; DROP COLUMN column_name [CASCADE CONSTRAINTS] ; MODIFY (Column_name column_specification) ; ADD CONSTRAINT constraint_name cons_type (col_name); DROP CONSTRAINT constraint_name [CASCADE] ; DISABLE CONSTRAINT constraint_name [CASCADE] ; ENABLE CONSTRAINT constraint_name; 18
19
References Database Systems: A Practical Approach to Design, Implementation and Management. Thomas Connolly, Carolyn Begg. 5 th Edition, Addison-Wesley, 2009. 20