Alter Table allows changing the structure of an existing table. It can add or remove columns, modify column attributes, add or drop constraints like primary keys, foreign keys, unique keys and checks. This is safer than dropping and recreating the table. The ALTER TABLE statement is used along with ADD, DROP, MODIFY, CHANGE clauses.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
137 views
Alter Table: Instructor: Samia Arshad
Alter Table allows changing the structure of an existing table. It can add or remove columns, modify column attributes, add or drop constraints like primary keys, foreign keys, unique keys and checks. This is safer than dropping and recreating the table. The ALTER TABLE statement is used along with ADD, DROP, MODIFY, CHANGE clauses.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10
Alter Table
Instructor: Samia arshad
Changing Tables • Sometimes you want • ALTER TABLE can to change the • Add a new column structure of an • Remove an existing existing table column • One way is to DROP it • Add a new constraint then rebuild it • Remove an existing • This is dangerous, so constraint there is the ALTER TABLE command instead
More SQL Data Definition
ALTERing Columns To add or remove Examples: Suppose we have a table columns use student having id and degree column ALTER TABLE <table> ALTER TABLE Student ADD <col> ADD Degree ALTER TABLE <table> VARCHAR(20) DROP COLUMN <name> ALTER TABLE Student DROP COLUMN Degree
More SQL Data Definition
ALTERing Columns To change existing Examples attribute type and Changing attribute renaming a column type: ALTER TABLE Student ALTER TABLE <table> Modify Modify <col> Degree CHAR(5) Alter TABLE <table> Renaming attribute: CHANGE <oldname> ALTER TABLE Student <newname> CHANGE Degree type(size) Degree1 varchar(25) More SQL Data Definition Adding Constraints To add or remove Examples columns use Adding constraint: Unique Constraint ALTER TABLE <table> ALTER TABLE STUDENT ADD CONSTRAINT ADD CONSTRAINT <definition> ck UNIQUE (DEGREE) Not Null constraint ALTER TABLE <table • alter table STUDENT name> modify modify Degree varchar(25) <column name> Not Null; type(size) not null; More SQL Data Definition Adding Constraints Primary Key • Foreign key ALTER TABLE ALTER TABLE STUDENT STUDENT ADD CONSTRAINT ADD CONSTRAINT pk Primary key fk Foreign key (id) (column name) references primary table(column More SQL Data Definition name) Adding constraints • Check ALTER TABLE STUDENT ADD CONSTRAINT chk check (id>10)
More SQL Data Definition
Dropping Constraint Dropping Constraint: ALTER TABLE ALTER TABLE STUDENT <table> DROP foreign key DROP constraintname; CONSTRAINT Dropping Not Null <name> constraint ALTER TABLE student CHANGE column name column name int(25) null;
More SQL Data Definition
Dropping Constraint • To drop primary • Alter table key, check, STUDENT drop unique constraint, index the following constraintname; syntax will be used