Alter Table Command
The SQL syntax for ALTER TABLE Add
Column is,
• ALTER TABLE "table_name"
ADD "column_name" "Data Type";
• ALTER TABLE Customer ADD Gender char(1);
Add multiple columns to a table
• ALTER TABLE Customer ADD (Email char(30),
Telephone char(20) );
Modify Column
• Sometimes we need to change the data type of a
column.
• ALTER TABLE "table_name"
MODIFY "column_name" "New Data Type";
• ALTER TABLE Customer MODIFY Address
char(100);
Modify Columns Names
• Sometimes we want to change the name of a
column.
• ALTER TABLE Customer CHANGE Address
Addr char(50);
Delete a column
• Sometimes we will wish to delete a column
from an existing table in SQL.
• ALTER TABLE "table_name"
DROP "column_name";
• ALTER TABLE Customer DROP Birth_Date;
ADD Constraint
• Sometimes we may decide to add a new
constraint to an existing table
• ALTER TABLE "table_name"
ADD [CONSTRAINT_NAME]
[CONSTRAINT_TYPE]
[CONSTRAINT_CONDITION];
• ALTER TABLE Customer ADD CONSTRAINT
Con_First UNIQUE (Address);
Drop Constraint
• ALTER TABLE "table_name"
DROP [CONSTRAINT|INDEX]
"CONSTRAINT_NAME";
• ALTER TABLE Customer DROP INDEX
Con_First;
Arithmetic Operation
• The AVG function is used to find the average
value in an expression.
• SELECT AVG (<expression>)
FROM "table_name";
• To find the average sales amount, we type in,
• SELECT AVG(Sales) FROM Store_Information;
Arithmetic Cont…
• AVG function on an arithmetic operation
• Assume that sales tax is 10% of the sales
amount, we use the following SQL statement
to get the average sales tax amount:
• SELECT AVG(Sales*0.1) FROM
Store_Information;
Count
• The COUNT function in SQL is used to
calculate the number of rows returned from
the SQL statement.
• SELECT COUNT (<expression>)
FROM "table_name“
• SELECT COUNT(Store_Name)
FROM Store_Information;
Count Cont…
• COUNT and DISTINCT can be used together in
a statement to retrieve the number of distinct
entries in a table.
• SELECT COUNT (DISTINCT Store_Name)
FROM Store_Information;
MAX
• he MAX function is used to find the maximum
value in an expression.
• SELECT MAX (<expression>)
FROM "table_name";
• SELECT MAX(Sales) FROM Store_Information;
Max cont…
• MAX function on an arithmetic operation
• Assume that sales tax is 10% of the sales
amount, we use the following SQL statement
to get the maximum sales tax amount:
• SELECT MAX(Sales*0.1) FROM
Store_Information;
Min
• The MIN function is used to find the minimum
value in an expression.
• SELECT MIN (<expression>)
FROM "table_name";
• SELECT MIN(Sales) FROM Store_Information;
MIN Cont…
• MIN function on an arithmetic operation
• SELECT MIN(Sales*0.1) FROM
Store_Information;
SUM
• The SUM function is used to calculate the total for
an expression.
• SELECT SUM(<expression>)
FROM "table_name";
• SELECT SUM(Sales) FROM Store_Information;
• SELECT SUM(Sales*0.1) FROM Store_Information;
ROUND Function
• The ROUND function in SQL is used to round a
number to a specified precision.
• ROUND (expression, [decimal place])
• SELECT First_Name, ROUND (Rating, 1)
Rounded_Score FROM Student_Rating;
ROUND Function
• Decimal place is negative
• To round to the nearest tens place, we use the
following SQL
• SELECT First_Name, ROUND (Rating, -1)
Rounded_Score FROM Student_Rating;