Presentation is loading. Please wait.

Presentation is loading. Please wait.

Alter an existing Table Note: (a) Primary key: SID + Course_code.

Similar presentations


Presentation on theme: "Alter an existing Table Note: (a) Primary key: SID + Course_code."— Presentation transcript:

1 Alter an existing Table Note: (a) Primary key: SID + Course_code

2 Add a Primary Key Add a Alternate Key/Unique Key Add a Foreign Key Add a New Column Drop an existing Column Alter an existing Column Make an existing Column NOT NULL Add a Constraint (Rule) Drop a Constraint (Rule) Disable a Constraint Enable a Constraint

3 Add a Primary Key E.g. (1) Alter Table Student_Info add Primary Key(Sid); (2) Alter Table Student_Scores add Primary Key (Sid, Course_code);

4 Add an Alternate Key E.g. (1) Alter Table Student_Info add unique (HKID);

5 Add an Foreign Key E.g. (1) Alter Table Student_Scores add foreign key (Sid) references Student_info(Sid); That is, the Sid of Student_Scores must either find a matching value in Student_Info.

6 Add a new Column E.g. (1) Alter Table Student_Info add (Date_of_birth Date); (2) Alter Table Student_Scores add (Test_Scores Number(3));

7 Alter an existing Column E.g. (1) Alter Table Student_Scores modify (Test_Scores Number(5,2)); (2) Alter Table Student_Scores modify (Name Char(10)); If Name contains data, the (2) does not work.

8 Drop an existing Column E.g. (1) Alter Table Student_Info drop (Date_of_birth); (2) Alter Table Student_Scores drop (Test_Scores) ;

9 Add a new Constraint (Rule) E.g. (1) Alter Table Student_Info add constraint name_01 check (Gender in (‘M’,’F’)); (2) Alter Table Student_Info add constraint name_02 check (Name = Upper(Name));

10 Add a new Constraint without a name E.g. (1) Alter Table Student_Info add check (Gender in (‘M’,’F’)); (2) Alter Table Student_Scores add check (Course_Work_Scores> Exam_Scores);

11 Drop an existing Constraint (Rule) E.g. (1) Alter Table Student_Info drop constraint name_01; To find out the contraint name, use SQL: Select * from user_constraints where table_name = 'STUDENT_INFO';

12 Drop an existing Constraint (Rule) To find out the contraint name, use SQL: Select * from user_constraints where table_name = 'STUDENT'; (noted: table name must be all upper case letters) Alter table Student_Info Drop Constraint Sys_C ;

13 Disable an existing Constraint (Rule) (1)Alter table Student_Info disable Constraint Sys_C ; Q: Why do we need to disable an constraint? Table_01’s Col_01 references Table_02’s Pri Key And Table_02’s Col_02 references Table_01’s Pri Key

14 Enable an existing Constraint (Rule) (1)Alter table Student_Info enable Constraint Sys_C ;

15 Special Character You must type the command below first: Set Escape ‘\’; (1) & E.g. (a) Insert into staff values ('\&xx'); (b) Select * from staf where name = '\&xx'; (2) ‘ select * from book where book_description = ' John'\'s cat ' ;


Download ppt "Alter an existing Table Note: (a) Primary key: SID + Course_code."

Similar presentations


Ads by Google