Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor: Samia arshad

Similar presentations


Presentation on theme: "Instructor: Samia arshad"— Presentation transcript:

1 Instructor: Samia arshad
Alter Table Instructor: Samia arshad

2 More SQL Data Definition
Changing Tables Sometimes you want to change the structure of an existing table One way is to DROP it then rebuild it This is dangerous, so there is the ALTER TABLE command instead ALTER TABLE can Add a new column Remove an existing column Add a new constraint Remove an existing constraint More SQL Data Definition

3 More SQL Data Definition
ALTERing Columns Examples: Suppose we have a table student having id and degree column ALTER TABLE Student ADD Degree VARCHAR(20) DROP COLUMN Degree To add or remove columns use ALTER TABLE <table> ADD <col> DROP COLUMN <name> More SQL Data Definition

4 More SQL Data Definition
ALTERing Columns Examples Changing attribute type: ALTER TABLE Student Modify Degree CHAR(5) Renaming attribute: CHANGE Degree Degree1 varchar(25) To change existing attribute type and renaming a column ALTER TABLE <table> Modify <col> Alter TABLE <table> CHANGE <oldname> <newname> type(size) More SQL Data Definition

5 More SQL Data Definition
Adding Constraints Examples Adding constraint: Unique Constraint ALTER TABLE STUDENT ADD CONSTRAINT ck UNIQUE (DEGREE) Not Null constraint alter table STUDENT modify Degree varchar(25) Not Null; To add or remove columns use ALTER TABLE <table> ADD CONSTRAINT <definition> ALTER TABLE <table name> modify <column name> type(size) not null; More SQL Data Definition

6 More SQL Data Definition
Adding Constraints Primary Key ALTER TABLE STUDENT ADD CONSTRAINT pk Primary key (id) Foreign key ALTER TABLE STUDENT ADD CONSTRAINT fk Foreign key (column name) references primary table(column name) More SQL Data Definition

7 More SQL Data Definition
Adding constraints Check ALTER TABLE STUDENT ADD CONSTRAINT chk check (id>10) More SQL Data Definition

8 More SQL Data Definition
Dropping Constraint Dropping Constraint: ALTER TABLE STUDENT DROP foreign key constraintname; Dropping Not Null constraint ALTER TABLE student CHANGE column name column name int(25) null; ALTER TABLE <table> DROP CONSTRAINT <name> More SQL Data Definition

9 More SQL Data Definition
Dropping Constraint To drop primary key, check, unique constraint, the following syntax will be used Alter table STUDENT drop index constraintname; More SQL Data Definition

10 More SQL Data Definition
Drop table Examples Drop TABLE student Drop table Drop TABLE <table name> More SQL Data Definition


Download ppt "Instructor: Samia arshad"

Similar presentations


Ads by Google