lecture7: sql overview, oracle data type , ddl and ... · lecture7: sql overview, oracle data type...

20
Prepared by L. Nouf Almujally & Aisha AlArfaj& L.Fatima Alhayan 1 Ref. Chapter6 IS220 : Database Fundamentals Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 Colleg Comp Informa Scien Informa Syst D

Upload: buidieu

Post on 15-Jul-2018

252 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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.

Page 2: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

The Process of Database Design

Lecture7

Real World Domain Conceptual model (ERD) Relational Data Model Create schema(DDL)

Load Data(DML)

2

Page 3: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

ALTER TABLE

The ALTER TABLE statement is used to:• Add columns• Delete columns• Modify columns• Add a new constraint• Remove an existing constraint

3

Page 4: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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 ;

Page 5: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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.

Page 6: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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

Page 7: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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

Page 8: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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) ;

Page 9: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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) ;

Page 10: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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.

Page 11: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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

Page 12: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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.

Page 13: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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 ;;

Page 14: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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:

Page 15: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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

Page 16: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

Viewing Table Structures

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

Lecture7

16

DESCRIBE table_name; or DESC table_name ;

Page 17: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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.

Page 18: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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

Page 19: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

19

Page 20: Lecture7: SQL Overview, Oracle Data Type , DDL and ... · Lecture7: SQL Overview, Oracle Data Type , DDL and Constraints Part #2 College of Computer Information Sciences - Information

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

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

Lecture7

20