Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

UNIT-3 CHAPTER-2

SQL-3

13) ALTER TABLE COMMAND:

The ALTER TABLE command is used to modify the definition (structure) of a table by
modifying the definition of its columns. The ALTER TABLE command is used to perform
the following operations:
a) To add a column to an existing table.
b) To rename any existing column.
c) To change the datatype of any column or to modify its size.
d) To remove or physically delete a column.

(a) Adding a column to an existing table


Once a table has been created, new columns can be added later on, if required. The new
column is added with NULL values for all the records/rows in the table. It is possible to
add, a new column using ADD clause with ALTER TABLE command.
Syntax:
ALTER TABLE <table_name> ADD(<column_name><datatype> [size]);

For example,
To add a new column Mobile of type integer in the table student:
ALTER TABLE STUDENT ADD (MOBILE INTEGER);
Thus, the above statement shall add a new column Mobile_no into the table student
with NULL value in it

(b) Modifying an existing column definition


The MODIFY clause can be used with ALTER TABLE command to change the
datatype, size to any column of the table.
Syntax:
ALTER TABLE <table_name>MODIFY([column_name1] <datatype1>);

For example,
ALTER TABLE STUDENT MODIFY (NAME VARCHAR(25));

17
2024-25/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P
The above command will modify the datatype size for the Name field from 20 to 25
characters.

(c) Renaming a column name


The existing column in a relation can be renamed using ALTER TABLE command.
Syntax :
ALTER TABLE <table_name>CHANGE <old-column-name><new-coloumn-name>
column_definition;
For example,
ALTER TABLE STUDENT CHANGE STREAMSUBJECTVARCHAR(10);
The above command will rename the column stream as subject.

d) Removing a column
To remove or drop a column in a table, ALTER TABLE command is used.
Syntax :
ALTER TABLE <TABLE-NAME> DROP <COLUMN-NAME>;

For example,
ALTER TABLE STUDENT DROP (GENDER);
The above command will drop Gender column from the student table

Case study question

1) Ms. Shalini has just created a table named “Employee” containing


columns Ename, Department and Salary.

After creating the table, she realized that she has forgotten to add a
primary key column in the table. Help her in writing an SQL command to
add a primary key column EmpId of integer type to the table
Employee.
Thereafter, write the command to insert the following record in the table:
EmpId- 999
Ename- Shweta
Department: Production
Salary: 26900

Answer
SQL Command to add primary key:
ALTER TABLE Employee ADD EmpId INTEGER PRIMARY KEY;
18
2024-25/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P
As the primary key is added as the last field, the command for
inserting data will be:
INSERT INTO Employee VALUES("Shweta","Production",26900,999);
OR
INSERT INTO Employee(EmpId,Ename,Department,Salary) VALUES (999, "Shweta",
"Production", 26900);
2) Ms. Shalini has just created a table named “Employee” containing columns Eid, Ename,
Department and Salary. Help her in writing an SQL command to remove a primary key
column EmpId
ALTER TABLE Employee DROP PRIMARY KEY;
Since a table can have only one primary key, you don’t need to specify the primary key
column(s) name

14) SQL JOINS:


A join is a query that combines rows from two of more tables. In JOIN query more
than one table are listed in FROM clause. MySQL provides various type of Joining :
a) EQUI-JOIN
b) NATURAL JOIN
EQUI-JOIN
The join in which columns are compared for equality is called Equi-Join. In Equi-
join we put(*) in the select list therefore the common column will appear twice in
the output.
Syntax :
a) SELECT * FROM TABLE1,TABLE2 WHERE COL1=COL2;
b) SELECT * FROM TABLE1 A, TABLE2 B WHERE A.COL1=B.COL2;
c) SELECT TABLE1.COL, TABLE2.COL FROM TABLE1 A, TABLE2 B
WHERE A.COL1=B.COL2;

For example,
Let’s take 2 table one for EMPLOYEE (contains employee detail with deptno) and
another for DEPARTMENTcontains deptno and other department details.

TABLE EMPLOYEE

19
2024-25/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P
TABLE DEPARTMENT

Select * from emp ,dept where emp.deptno=dept.deptno


OR
Select * from emp e,dept d where e.deptno=d.deptno
OR
Selectemp.deptno, emp.code, emp,name. emp.salary, dept.deptno, dept.dname,
dept.dhead from emp e,dept d where e.deptno=d.deptno

The above will produce the following result.

From the above query, we can observe that while doing equi-join we have to
give equality condition on common column of both table so that it picks
related records

20
2024-25/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P
NATURAL JOIN:
A natural join is a type of equi-join where the join condition compares all the same
names columns in both tables. The pre condition of the natural join is, at least one
attribute(column) should be same name in both tables. The rows of the resultant table
contains common values of the common attribute.

Syntax :

SELECT * FROM TABLE1 NATURAL JOIN TABLE2;

For example,
SELECT * FROM EMPLOYEE NATURAL JOIN DEPARTMENT;

Equi-join and Natural join are equivalent except that the duplicate columns are
eliminated in the natural join that would otherwise appear in the equi-join.

15) SQL ALIAS:

SQL aliases are used to give an alternate name, i.e., a temporary name, to a database
table or a column in a table.
Aliases can be used when
• more than one table is involved in a query.
• functions are used in the query.
• column names are big or not very readable.
• two or more columns are combined together.
Syntax for table alias:

21
2024-25/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P
SELECT <column(s)>FROM <table_name> AS <alias_name>WHERE [<condition>];

Syntax for column alias:

SELECT <column-name> AS <“alias_name”>FROM <table_name>WHERE


[<condition>];

15) CARTESIAN PRODUCT:-


The Cartesian product of 2 relations yiels a new relation with all possible
combination of tup[les of 2 relations operated upon.
 The new relation will have a degree equal to sum of degree of 2 relations
operated .
 The new relation will have a cardinality equal to product of cardinalities of
2 relations operated .
 For example .consider the 2 tables student and detail given below
STUDENT DETAIL

Adno Name Age


101 Arun 17 Subject Result
102 Fida 15 Eng Pass
103 Sofi 18 Maths Fail

SELECT * FROM STUDENT, DETAIL ; will result in the following table

Adno Name Age Subject Result


101 Arun 17 Eng Pass
Resultant table
101 Arun 17 Maths Fail
Cardinalty= 6 (3 * 2)
102 Fida 15 Eng Pass
102 Fida 15 Maths Fail Degrree = 5 (3 + 2)
103 Sofi 18 Eng Pass
103 Sofi 18 Maths Fail

*****************************
22
2024-25/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P

You might also like