SQL 4
SQL 4
SQL-3
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.
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
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.
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
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
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
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 :
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.
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>];
*****************************
22
2024-25/ COMPUTER SCIENCE XII/ SQL NOTES JISHA T P