DR - Osama Lab 3 Alter Table and Keys
DR - Osama Lab 3 Alter Table and Keys
• Alter Table
• Rename Table
• Data Control Language
• Primary Key
• Composite Key
• Constraints
2
Alter Table
• Things that can be done using alter table:
– Adding a column or multiple columns.
– Renaming a table.
3
Adding a column or multiple
columns
• Adding one column:
– Syntax:
• alter table $table_name
add $column_name $column-definition;
5
Examples
• alter table employee
add salary number;
6
Modify an existing column or
multiple columns
• Modifying one column:
– Syntax:
• alter table $table_name
modify $column_name $description;
8
Deleting a column or multiple
columns
• Deleting one column:
– Syntax:
• alter table $table_name
drop column $column_name;
10
Renaming an existing column
• Renaming an existing column is performed by:
– First: Deleting the existing column.
11
Example
• In this example, I want to rename the “salary” column to
“emp_salary”
12
Renaming a table
• Syntax:
– alter table $table_name
rename to $new_table_name;
• Example:
– alter table employee
rename to company_employee;
13
Data Control Language
(DCL)
• It is used to:
– Control actions on DB objects.
14
Primary Key
• It is a key attribute(s) in an entity.
15
Defining a primary key
1) Using create table statement (Definition 1)
• Syntax:
– Create table $table_name
(
$attribute1 $data_type primary key,
$attribute2 $data_type
);
– Here attribute1 is the primary key of the table.
– Example:
create table employee
(
emp_id number primary key,
emp_name varchar2(30)
); 16
Defining a primary key
2) Using create table statement (Definition 2)
– Syntax:
– Create table $table_name
(
$attribute1 $data_type,
$attribute2 $data_type,
primary key($attribute1)
);
– Example:
create table employee
(
emp_id number,
emp_name varchar2(30),
primary key (emp_id)
); 17
Defining a primary key
3) By adding a constraint on create table (Definition 3)
– Syntax:
– Create table $table_name
(
$attribute1 $data_type,
$attribute2 $data_type,
constraint $const_name primary key($attribute1);
);
– Example:
create table employee
(
emp_id number,
emp_name varchar2(30),
constraint pk_const primary key (emp_id)
); 18
Composite Keys
• Sometimes primary keys might consist of more than one
attribute.
19
Defining Composite Key
• You can use either definition 2 or 3 which you saw in the
previous slides.
• Example:
– create table sections
(
sec_id number,
crs_id number,
semester number,
year number,
primary key(sec_id,crs_id,semester,year)
);
• How many keys table “sections” have? Only “1” key that is
composed of 4 attributes. 20
Defining Composite Key
• You can use either definition 2 or 3 which you saw in the
previous slides.
• Example:
– create table sections
(
sec_id number,
crs_id number,
semester number,
year number,
constraint c1 primary key(sec_id,crs_id,semester,year)
);
21
Note
• Look at the table description after you defined a primary key.
• You can see that a primary key attribute has “Not Null”
written beside it.
22
Defining a primary key
• What if we forgot to define a primary key at the time of using
“create table”?
23
Defining a primary key
4) By adding a constraint using alter table command:
• Syntax:
– alter table $table_name
add constraint $const_name primary key($attribute1, $attribute2);
24
Example
• create table employee
(
emp_id number,
emp_name varchar2(30)
);
25