Module-4
Module-4
CREATE Syntax:
Create table <table name>(Attribute <type><size>, Attribute <type><size>, ……,
Attribute <type><size> );
create table Employee (SSN integer PRIMARY KEY, name varchar (5), bdate DATE,
salary integer, Dname varchar (5), DNo integer FOREIGN KEY(DNo) References
Department);
DROP Syntax:
It is used to remove a relation (base table) and its definition Permanently.
Drop table <table name>;
Drop table Employee;
DELETE Syntax:
It is used to delete a record or tuple from a relation (base table.
Delete from <table name>;
Delete from Employee;
ALTER Syntax:
This command is used to modify any physical information of an existing relation.
Syntax to add new Attribute:
Alter table <table name> add <new-attribute > <type> <size>;
Alter table Employee add course varchar (2);
Syntax to Delete an existing attribute:
Alter table <table name> drop column <attribute name>;
Alter table Employee drop column dname;
Syntax to Rename an existing attribute:
Alter table <table name> Rename column <old name> to <new name>;
Alter table Employee rename column Salary to Esalary;
To add primary key into an existing relation:
Alter table <table name> Add PRIMARY KEY (attribute name);
Alter table Employee ADD PRIMARY KEY (Essn);
SYSDATE Syntax:
This function returns the current date time from the operating system.
Write a subquery to Print the names of all employees who belongs to the department
which is higher or equal than all department number.
Select names from Employee where Dno. >= All (Select Dnumber from
Department);
Write a subquery to print all employees in the database who earn more than each
employee of ‘WIPRO’.
Select names from Employee where salary > (Select MAX (salary) from Employee
where Cname = ‘WIPRO’);
σ . = ‘DBMS’ ∧
× S. Regd No = S. Regd No
C. CNo = E. CNo
∧
S E C
Step-2: Move the select operation downwards of the tree and place it in their appropriate
place.
𝜋 . , . , .
𝝈𝑪.𝑪𝑵𝒐 𝑬.𝑪𝑵𝒐
𝝈 . .
× 𝝈 .
S E C
Step-3: Replace the Cartesian product and select operation with join operation.
𝐂. 𝐂𝐍𝐨 = 𝐄. 𝐂𝐍𝐨
𝝈 .
S E C
Step-4:
To reduce the number of resultant records, interchange the relations and perform the join
operation with their appropriate joining attributes.
𝜋 . , . , .
𝐒. 𝐑𝐞𝐠𝐝 𝐍𝐨 = 𝐄. 𝐑𝐞𝐠𝐝 𝐍𝐨
C E S
Step-5:
𝜋 . , . , .
S. Regd No = E. Regd No
𝜋 . . , . , .
𝝈 .
𝜋 . , .
E S
C
Question:
Write the SQL query and it’s corresponding Relation algebra (RA) to draw an initial query tree
for the query” For every project located in ‘Stafford’, retrieve the project number, the
controlling department number, and the department manager’s last name, address, and
birthdate”. Then apply heuristic query optimization on the above initial query tree to find the
optimal query tree.