Undergraduate
Database Management
Systems
Lecture 9
Hamza Shaukat
[Link]@[Link]
Information Technology University (ITU)
Faculty of Engineering
View Constraints
Information Technology University (ITU)
Faculty of Engineering
The ALTER TABLE Statement
Use the ALTER TABLE statement to:
▪ Add a new column
▪ Modify an existing column
▪ Drop a column.
▪ Add or Drop a Constraint.
▪ Disable or Enable a Constraint
ALTER TABLE table
ADD (column datatype [DEFAULT expr]
[, column datatype]...);
ALTER TABLE table
MODIFY (column datatype [DEFAULT expr]
[, column datatype]...);
Information Technology University (ITU)
Faculty of Engineering
Adding a Column
“…add a new
DEPT30 New column column into
DEPT30 table…”
EMPNO ENAME ANNSAL JOB
HIREDATE
------ ---------- --------
7698 BLAKE 34200 01-MAY-81
7654 MARTIN 15000 28-SEP-81
7499 ALLEN 19200 20-FEB-81
7844 TURNER 18000 08-SEP-81
...
DEPT30
EMPNO ENAME ANNSAL JOB
HIREDATE
------ ---------- --------
7698 BLAKE 34200 01-MAY-81
7654 MARTIN 15000 28-SEP-81
7499 ALLEN 19200 20-FEB-81
7844 TURNER 18000 08-SEP-81
...
Information Technology University (ITU)
Faculty of Engineering
Adding a Column
▪ You use the ADD clause to add columns.
SQL> ALTER TABLE dept30
2 ADD (job VARCHAR(9));
Table altered.
• The new column becomes the last column.
EMPNO ENAME ANNSAL HIREDATE JOB
--------- ---------- --------- --------- ----
7698 BLAKE 34200 01-MAY-81
7654 MARTIN 15000 28-SEP-81
7499 ALLEN 19200 20-FEB-81
7844 TURNER 18000 08-SEP-81
...
6 rows selected.
Information Technology University (ITU)
Faculty of Engineering
Modifying a Column
▪ You can change a column’s datatype, size, and
default value.
ALTER TABLE cars
ALTER COLUMN color TYPE VARCHAR(30);
Table altered.
▪ A change to the default value affects only
subsequent insertions to the table.
ALTER TABLE employee
ALTER COLUMN salary
SET DEFAULT 0;
Information Technology University (ITU)
Faculty of Engineering
Modify Example
CREATE TABLE customer_groups (
id serial PRIMARY KEY,
name VARCHAR NOT NULL
);
CREATE TABLE customers (
id serial PRIMARY KEY,
name VARCHAR NOT NULL,
phone VARCHAR NOT NULL,
email VARCHAR,
group_id INT,
FOREIGN KEY (group_id) REFERENCES customer_groups (id)
);
Information Technology University (ITU)
Faculty of Engineering
Modify Example
Single Column
ALTER TABLE customers
RENAME COLUMN email TO contact_email;
Multiple Columns
ALTER TABLE customers
RENAME COLUMN name TO customer_name;
ALTER TABLE customers
RENAME COLUMN phone TO contact_phone;
Information Technology University (ITU)
Faculty of Engineering
Dropping and Renaming a Column
Use the DROP COLUMN clause to drop columns you no longer need from the
table.
ALTER TABLE dept30
DROP COLUMN Job;
Table altered.
• Use the RENAME COLUMN clause to rename
a col.
ALTER TABLE dept30
RENAME COLUMN Job TO Designation;
Table altered.
Information Technology University (ITU)
Faculty of Engineering
Adding a Constraint
ALTER TABLE table
ADD [CONSTRAINT constraint] type (column);
Add a FOREIGN KEY constraint to the EMP table
indicating that a manager must already exist as a
valid employee in the EMP table.
SQL> ALTER TABLE emp
2 ADD CONSTRAINT emp_mgr_fk
3 FOREIGN KEY(mgr) REFERENCES emp(empno);
Table altered.
Information Technology University (ITU)
Faculty of Engineering
Example of Adding Constraint
ALTER TABLE student
ADD CONSTRAINT age_constraint CHECK (age >= 10);
articles=# \d student;
Table "[Link]"
Column | Type | Collation | Nullable | Default
--------+-----------------------+-----------+----------+-------------------------------------
id | integer | | not null | nextval('student_id_seq'::regclass)
name | character varying(50) | | not null |
age | integer | | |
gender | character varying(50) | | not null |
marks | double precision | | |
Indexes:
"student_pkey" PRIMARY KEY, btree (id)
Check constraints:
"age_constraint" CHECK (age >= 10)
11
Information Technology University (ITU)
Faculty of Engineering
Dropping a Constraint
Remove foreign key constraint from the EMP table.
SQL> ALTER TABLE emp
2 DROP CONSTRAINT emp_mgr_fk;
Table altered.
• Remove the PRIMARY KEY constraint on the DEPT table and drop the associated
FOREIGN KEY constraint on the [Link] column.
ALTER TABLE customers
DROP Constraint st_id_pk(primary_key_name) cascade;
• To drop a DEFAULT constraint
SQL> ALTER TABLE student
2 ALTER COLUMN program DROP DEFAULT;
Table altered.
Information Technology University (ITU)
Faculty of Engineering
Disabling Constraints
▪ Execute the DISABLE clause of the ALTER
TABLE statement to deactivate an integrity
constraint.
▪ Apply the CASCADE option to disable
dependent integrity constraints.
SQL> ALTER TABLE emp
2 DISABLE CONSTRAINT emp_empno_pk CASCADE;
Table altered.
Information Technology University (ITU)
Faculty of Engineering
Changing the Name of an Object
▪ To change the name of a table, view, sequence, or
synonym, you execute the RENAME statement.
ALTER TABLE IF EXISTS table_name
RENAME TO new_table_name;
Example:
DROP TABLE IF EXISTS vendors;
CREATE TABLE vendors (
id serial PRIMARY KEY,
name VARCHAR NOT NULL
);
ALTER TABLE vendors RENAME TO suppliers;
Information Technology University (ITU)
Faculty of Engineering
Dropping a Table
▪ All data and structure in the table is deleted.
▪ Any pending transactions are committed.
▪ All indexes are dropped.
▪ You cannot roll back this statement.
DROP TABLE [IF EXISTS] table_name
[CASCADE | RESTRICT];
▪ NOTE: if the table has a reference in another table,
the above command may not work. To drop all the
references as well, use the following:
DROP TABLE authors CASCADE;
Information Technology University (ITU)
Faculty of Engineering
Question
Write a SQL statement to add a primary key to the columns location_id in the
locations table. And the existing primary key is country id.
Here is the structure of the table locations.
postgres=# \d locations
Column | Type | Modifiers
-----------------------+-------------------------------+-----------
location_id | numeric(4,0) |
street_address | character varying(40) |
postal_code | character varying(12) |
city | character varying(30) |
state_province | character varying(25) |
country_id | character varying(2) |
Solution
16
Information Technology University (ITU)
Faculty of Engineering