Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type...

Post on 15-Jul-2018

230 views 0 download

Transcript of Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type...

Prepared by L. Nouf Almujally & AishaAlArfaj& L.Fatima Alhayan

1

Ref. Chapter6

I S 2 2 0 : D a t a b a s e F u n d a m e n t a l s

Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints

Part #2 College ofComputer

InformationSciences -

InformationSystems

Dept.

The Process of Database Design

Lecture7

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

3

ALTER TABLE - Add Column

1. To add a column in a table, use the following syntax: EXAMPLE:ALTER TABLE Student ADD Degree VARCHAR2(50) ; Table altered.

Lecture7

4

ALTER TABLE table_name ADD column_name datatype ;

ALTER TABLE - Delete Column1. To delete a column in a table, use the following syntax:

EXAMPLE:ALTER TABLE Student DROP COLUMN Degree; Table altered.

5

ALTER TABLE table_name DROP COLUMN column_name [CASCADE CONSTRAINTS] ; - Use [CASCADE CONSTRAINTS] to drop PRIMARY KEY columnwith dependent integrity constraints.

Lecture7

6

SQL> create table parent (id number(2) primary key);Table created. 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

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.

7

ALTER TABLE – Modify Column

1. To modify a column data type or constraint in a table, usethe following syntax:

EXAMPLE:ALTER TABLE orders MODIFY (quantity number(5)); Table altered.

Lecture7

8

ALTER TABLE table_name MODIFY(Column_name column_specification) ;

ALTER TABLE - Add Constraint1. To add a new constraint in a table, use the following

syntax: EXAMPLE:ALTER TABLE Module ADD CONSTRAINT ck_unique UNIQUE (title); Table altered.

9

ALTER TABLE table_nameADD CONSTRAINT Constraint_name Constraint_type(column_name) ;

ALTER TABLE - Remove Constraint

1. To remove constraint in a table, use the followingsyntax:

EXAMPLE1:ALTER TABLE Module DROP CONSTRAINT ck_unique;Table altered.

Lecture7

10

ALTER TABLE table_nameDROP CONSTRAINT Constraint_name [CASCADE] ; - Use [CASCADE] to delete PRIMARY KEY constraint withdependent integrity constraints.

11

SQL> create table parent (id number(2) primary key);Table created. 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 foreignkeys

ALTER TABLE - Disable Constraint1. disable constraint is used to deactivate (turn off) an

integrity constraint temporarily, use the following syntax: Example :ALTER TABLE Parent DISABLE CONSTRAINT emp_empno_pk CASCADE;Table altered.** disables a PRIMARY KEY constraint and any FOREIGNKEY constraints that depend on it (refer to it)

Lecture7

12

ALTER TABLE table_nameDISABLE CONSTRAINT constraint_name [CASCADE]; Use [CASCADE] to disable PRIMARY KEY withdependent integrity constraints.

ALTER TABLE - Enable Constraint1. Activate an integrity constraint currently disabled in the

table definition by using the ENABLE clause. Use thefollowing syntax:

13

ALTER TABLE table_nameENABLE CONSTRAINT constraint_name ;;

DROP TABLES

• Pay attention to referential integrity constraints whendropping tables.

• If Cascade Constraints is used, the constraints will be

dropped first.

Lecture7

14

Drop table table_name;ORDrop table table_name [Cascade Constraints];

Syntax:

Example

• 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

EmpId

EmpName DeptNo

1 Ali 101

2 Sally 101

3 John 103

DeptNo DeptName

101 AAA

102 BBB

103 CCC

PKFK

EmployeeDepartment

Viewing Table Structures

• To see the structure of a table, type SQL> describe airlines;

Lecture7

16

DESCRIBE table_name; or DESC table_name ;

Constraints Name CodeDescribes all constraint definitions on tables owned by thecurrent 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

17system generated constraintname.

Alter Table SummaryALTER 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;

Lecture7

18

19

References• “Database Systems: A Practical Approach to Design,

Implementation and Management.” Thomas Connolly,Carolyn Begg. 5th Edition, Addison-Wesley, 2009.

Lecture7

20