0% found this document useful (0 votes)
10 views25 pages

DR - Osama Lab 3 Alter Table and Keys

The document discusses different ways to define primary keys and composite keys in SQL tables. Primary keys uniquely identify table rows and can be a single column or multiple columns. Composite keys make up the primary key using values from multiple columns. The document also covers altering tables to add or modify columns, keys, and constraints.

Uploaded by

rabee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views25 pages

DR - Osama Lab 3 Alter Table and Keys

The document discusses different ways to define primary keys and composite keys in SQL tables. Primary keys uniquely identify table rows and can be a single column or multiple columns. Composite keys make up the primary key using values from multiple columns. The document also covers altering tables to add or modify columns, keys, and constraints.

Uploaded by

rabee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Lab 3

• Alter Table
• Rename Table
• Data Control Language
• Primary Key
• Composite Key
• Constraints

Prepared By: Dr.Osama Al-Haj Hassan 1


Alter Table
• Sometimes you create a table and you remember
you need to change its definition because of
– Mistake
– Change in design

• To do that, you use command “alter table”.

2
Alter Table
• Things that can be done using alter table:
– Adding a column or multiple columns.

– Modifying an existing column or multiple columns.

– Renaming an existing column or multiple columns.

– Deleting 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;

• Adding multiple columns:


– Syntax:
• alter table $table_name
add ($column_name1 $column1-definition,
$column_name2 $column2-definition
);

• You can add as many columns as you want.


4
Examples
• This is a table to work with.

• create table employee


(
emp_id number,
emp_name varchar2(30)
);

5
Examples
• alter table employee
add salary number;

• alter table employee


add ( address varchar2(30),
gender number,
birth_date date
);

6
Modify an existing column or
multiple columns
• Modifying one column:
– Syntax:
• alter table $table_name
modify $column_name $description;

• Modifying multiple columns:


– Syntax:
• alter table $table_name
modify($column_name1 $column1-definition,
$column_name2 $column2-definition
);

• You can modify as many columns as you want.


7
Examples
• alter table employee
modify emp_name varchar2(40);

• alter table employee


modify( gender varchar2(1),
emp_name varchar2(45)
);

8
Deleting a column or multiple
columns
• Deleting one column:
– Syntax:
• alter table $table_name
drop column $column_name;

• Deleting multiple columns:


– Syntax:
• alter table $table_name
drop ($column_name1,
$column_name2
);

• You can delete as many columns as you want


9
Examples
• alter table employee
drop column gender;

• alter table employee


drop (address, salary);

10
Renaming an existing column
• Renaming an existing column is performed by:
– First: Deleting the existing column.

– Second: Adding it again with the new name.

11
Example
• In this example, I want to rename the “salary” column to
“emp_salary”

– alter table employee


drop column salary;

– alter table employee


add emp_salary number;

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.

– Example: grant and revoke privileges.

– Creating keys is also considered part of DCL.

– Creating constraints is also considered part of DCL.

14
Primary Key
• It is a key attribute(s) in an entity.

• It uniquely identifies an entity.

• It is called primary because one entity can have multiple


candidate keys.
– One of them is called “Primary Key”
– The others are called “Alternative Keys” or “Secondary Keys”

• A primary key value are not allowed to be null.

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.

• For example, in the case of “sections” table, the primary key


of the table cannot be a single attribute. The key in this case is
a combination of:
– sec_id, crs_id, semester, year

• That is why it is called composite key.

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.

• Use the command desc $table_name

• Example: desc sections;

• 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”?

• In this case, we can use “alter table” to define a primary key.

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);

– You can have as many attributes as you want.

24
Example
• create table employee
(
emp_id number,
emp_name varchar2(30)
);

• alter table employee


add constraint c2 primary key (emp_id);

25

You might also like