Lec -9 DBMS
Lec -9 DBMS
Database Management
Systems
Lecture 9
Hamza Shaukat
[email protected]
“…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
Single Column
Multiple Columns
Use the DROP COLUMN clause to drop columns you no longer need from the
table.
articles=# \d student;
Table "public.student"
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
Example:
DROP TABLE IF EXISTS vendors;
CREATE TABLE vendors (
id serial PRIMARY KEY,
name VARCHAR NOT NULL
);
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